-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.py
114 lines (80 loc) · 3.2 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""
@author: devdatta supnekar
@topic: ocr on documents
@created_on: 29/11/2020
@last_updated:
@updated_by
"""
# imports
from flask import Flask, flash, Response, redirect, url_for, render_template, request
import pandas as pd
from config import *
import numpy as np
import imutils
import time
import cv2
import os
import datetime
from werkzeug.utils import secure_filename
from processing import *
from utils.log import log_setup
# initialize all golbal variables
outputFrame = None
imageFrame = None
logger = log_setup("main", LOGFILE)
app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecretkey'
############################## FILES UPLOAD #################################
# route for home page
@app.route("/")
def index():
return render_template("index.html")
# route to upload files
@app.route("/upload", methods=['GET', 'POST'])
def upload():
# if request.method == "POST":
if "photo" in request.files:
# get image file from request
file = request.files["photo"]
# get document type from request
doc_type = request.form["type"]
logger.debug("[INFO] Request received for `{}`...".format(doc_type))
timeStamp = time.strftime('%d-%m-%Y_%H-%M-%S')
# if the doc_type folder doesn't alredy exist, create it
if not os.path.exists(IMAGE_UPLOADS + doc_type):
os.mkdir(IMAGE_UPLOADS + doc_type)
# directory for saving faces in the id cards
os.mkdir(IMAGE_UPLOADS + doc_type + "/" + "Faces")
# sequre the namee
img_name = secure_filename(file.filename)
# setting filename that is being received to current time stamp with its directory
save_path = IMAGE_UPLOADS + doc_type + "/" + img_name
file.save(save_path)
logger.debug("[INFO] Document image file uploaded successfully: `{}` ".format(save_path))
logger.debug("[INFO] Processing image file...")
# image = cv2.imread(save_path)
# print(image)
# detect faces in the frame and detect the emotion
global imageFrame
# Note: use your function here
imageFrame, details = text_detection(save_path, doc_type)
# savepath = os.path.join(TEMP_IMAGES, "temp_{}_{}".format(timeStamp, ".jpg"))
# cv2.imwrite(savepath, imageFrame)
# logger.debug("[INFO] Saving Document Copy...")
logger.debug("[INFO] Request Completed for `{}` ".format(doc_type))
logger.debug("######################################################################################")
return render_template("image.html", details=details)
########################### IMAGE PROCESSING ####################################
# function to encode image files and send to client
def GetImage():
global imageFrame
# encode the frame in JPEG format
(flag, img) = cv2.imencode(".png", imageFrame)
while True:
yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + bytearray(img) + b'\r\n')
# route to display image output
@app.route("/display_image")
def display_image():
return Response(GetImage(), mimetype="multipart/x-mixed-replace; boundary=frame")
if __name__ == "__main__":
app.run(host=SERVER_URL, port=SERVER_PORT, debug=True)