forked from misbah2014/imageprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask-app.py
More file actions
194 lines (164 loc) · 6.2 KB
/
Copy pathflask-app.py
File metadata and controls
194 lines (164 loc) · 6.2 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import jsonpickle
from flask import Flask, request, Response, make_response
import requests
import numpy as np
import cv2
import Class_Face_detection
import train_main
import os
import time
import face_recognition
from flask_cors import CORS, cross_origin
import base64
from PIL import Image
from io import BytesIO
from Database import Database
import json
app = Flask(__name__)
# Configure a secret SECRET_KEY
CORS(app)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
db = Database()
def _build_cors_prelight_response():
response = make_response()
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add('Access-Control-Allow-Headers', "*")
response.headers.add('Access-Control-Allow-Methods', "*")
return response
def _corsify_actual_response(response):
response.headers.add("Access-Control-Allow-Origin", "*")
return response
detection = Class_Face_detection.Detection()
train = train_main.Train()
print('Initilize done.... ')
sampleNum = 0
# add students in data base
@app.route("/fetchAllStudents", methods=['POST'])
def fetchAllStudents():
student_ids = []
key = request.json['ID']
link = 'http://192.168.10.28:4001/api/v1/Users/PickListByCustomerID/{}'.format(key)
payload = {}
headers = {
'Content-Type': 'application/json'
}
response = requests.request("GET", link, headers=headers, data=payload)
result = response.json()
print(result["Result"][0])
for x in result["Result"]:
student_ids.append(x["ProfileIdFK"])
for x in student_ids:
bol = db.InsertStudent(x, key, 0)
print(bol)
#["ProfileIdFK"])
return response.text
pass
@app.route("/status/fetchStatus", methods=['POST'])
def fetchStatus():
try:
key = request.json['ID']
data = db.get_all_users(key)
response = {'data': '{}'.format(data),
'message':'Done'
}
response_pickled = json.dumps(response)
return Response(response=response_pickled, status=200, mimetype="application/json")
pass
except Exception as error:
print('Error in Fetching Student status from DB : {}'.format(error))
response = {
'message': 'Wrong'
}
response_pickled = jsonpickle.encode(response)
return Response(response=response_pickled, status=200, mimetype="application/json")
pass
def getListOfFiles(dirName):
listOfFile = os.listdir(dirName)
return listOfFile
@app.route("/qa", methods=['POST'])
def _getImages():
print("QA testing Images now !!")
image = request.json['File']['_imageAsDataUrl']
image = image_spliter(image)
im = np.array(image)
image = detection.detect_picture(im)
# build a response dict to send back to client
if len(image) > 0:
response = {'message': '{}'.format(image[0])
}
else:
respose = {'message' : 'No Image!!'}
# encode response using jsonpickle
response_pickled = jsonpickle.encode(response)
return Response(response=response_pickled, status=200, mimetype="application/json")
@app.route("/check_training_pending", methods=["GET"])
def check_training_peding():
folderPath = 'software_data/'
if not os.path.exists(folderPath):
response = {'message': 'Error in Finding Path!!'
}
else:
listOfFiles = getListOfFiles(folderPath)
print(listOfFiles)
response = {'message': '{}'.format(listOfFiles)
}
# os.makedirs(folderPath)
response_pickled = jsonpickle.encode(response)
return Response(response=response_pickled, status=200, mimetype="application/json")
@app.route("/check_image", methods=['POST'])
def getImages():
print(request.data)
# convert string of image data to uint8
nparr = np.fromstring(request.data, np.uint8)
print(nparr)
# decode image
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
image = detection.detect_picture(img)
# build a response dict to send back to client
response = {'message': '{}'.format(image[0])
}
# encode response using jsonpickle
response_pickled = jsonpickle.encode(response)
return Response(response=response_pickled, status=200, mimetype="application/json")
def image_spliter(string):
if len(string) > 0:
image = string[23:]
image = Image.open(BytesIO(base64.b64decode(image)))
return image
@app.route("/get_train_images", methods=['POST']) # training from client side application
@cross_origin()
def gettrainImages():
print("Getting new images from client side....")
image = request.json['File']['_imageAsDataUrl']
key = request.json['ID']
number = request.json['number']
cus_id = request.json['CID']
folderName = str(cus_id) + "/" + str(key) # creating the person or user folder
folderPath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "software_data/" + folderName)
if not os.path.exists(folderPath):
os.makedirs(folderPath)
image = image_spliter(image)
result = detection.check_faces_in_training_pictures(image)
print('Result getting from face detecting module = {}'.format(result))
if result == 'True':
db.UpdateStudent(key,cus_id,1)
image.save(folderPath + "/ActiOn_" + str(number) + ".jpeg", "JPEG")
time.sleep(0.01)
# image = detection.detect_picture(img)
response = {'message': 'Done'
}
else:
response = {'message': 'Wrong'
}
# encode response using jsonpickle
response_pickled = jsonpickle.encode(response)
return Response(response=response_pickled, status=200, mimetype="application/json")
@app.route("/start_training", methods=["GET"])
def start_training():
response = train.training()
response_pickled = jsonpickle.encode(response)
return Response(response=response_pickled, status=200, mimetype="application/json")
pass
if __name__ == "__main__":
#app.run(debug=True, host="127.0.0.1", port=5001)
app.run(debug=True)