-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
171 lines (135 loc) · 5.18 KB
/
driver.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
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
from flask import (
Flask,
render_template,
jsonify,
send_from_directory,
send_file,
make_response,
)
from flask_restful import Resource, Api
from flask_cors import CORS, cross_origin
from flask_restful.utils import cors
import recognize_faces as rec_image
from flask import request
import encode_faces
from werkzeug.utils import secure_filename
from werkzeug.wrappers import Response
import json
import os
import pickle
app = Flask(__name__, static_folder="poormanrekog")
CORS(
app,
support_credentials=True,
allow_headers=["Content-Type", "Authorization", "Access-Control-Allow-Origin"],
)
api = Api(app)
app.encoding_file = "encodings2.pickle"
if not os.path.exists("static/output"):
os.makedirs("static/output")
if not os.path.exists("static/output/images"):
os.makedirs("static/output/images")
if not os.path.exists("static/output/videos"):
os.makedirs("static/output/videos")
class recData(Resource):
def post(self): # processed the image/video and returns appropriate json
if "image" in request.files:
image = request.files["image"]
coordinates, dict = rec_image.recognise_faces(app.encoding_file, image)
data = {}
for (i, value) in enumerate(dict):
data[str(i)] = value
return jsonify(
{
"success": "image processed successfully.",
"likeliness": dict,
"coordinates": coordinates,
}
)
elif "video" in request.files:
print("else statment")
f = request.files["video"]
f.save(secure_filename(f.filename))
rec_image.recognise_video(
app.encoding_file, f.filename, "static/output/videos/processed.mp4"
)
os.remove(f.filename)
return jsonify(
{
"success": "Video processed and saved successfully. Hit a get request to get it"
}
)
def get(self):
# returns the last saved video
if os.path.isfile("static/output/videos/processed.mp4"):
# g = wrap_file("static/output/videos/processed.mp4")
# return Response(g, direct_passthrough=True)
return make_response(
send_file(
"static/output/videos/processed.mp4",
attachment_filename="processed.mp4",
),
200,
)
else:
return jsonify({"error": "No latest video found. Create one!"})
class feedBack(Resource):
def post(self):
# gets an image and its corresponsing label and adds it to encodings
if "image" not in request.files:
return jsonify({"error": "Supply an 'image' file and a 'name'"})
if "name" not in request.form:
return jsonify(
{
"name_error": "Enter a valid name. A valid name has underscores instead of spaces. Make sure you get the list of all celebrities so there isn't two names for the single person."
}
)
else:
name = request.form["name"]
image = request.files["image"]
encode_faces.feedback(app.encoding_file, image, name)
return jsonify({"success": "name added successfully"})
class listNames(Resource):
# lists all names of known celebrities
def get(self):
data = pickle.loads(open(app.encoding_file, "rb").read())
names = list(set(data["names"]))
print(len(names))
return jsonify({"success": "Names received successfully", "names": names})
class findSimilarity(Resource):
def post(self):
if "image1" not in request.files or "image2" not in request.files:
return jsonify(
{
"error": "Send two images with keys 'image1' and 'image2' respectively"
}
)
else:
img1 = request.files["image1"]
img2 = request.files["image2"]
sim = rec_image.similarity(img1, img2)
return jsonify({"similarity": sim})
class timeFaces(Resource):
def post(self):
# post request will return a json about which celebrity is there from which second to which second. Right now, it will return celebrity faces in every frame
if "video" not in request.files:
return jsonify({"error": "Send a video"})
else:
f = request.files["video"]
f.save(secure_filename(f.filename))
processed_json = rec_image.json_from_faces(app.encoding_file, f.filename)
os.remove(f.filename)
return jsonify(
{"processed": processed_json, "success": "video processed successfully"}
)
class root(Resource):
def get(self):
return make_response(send_file("templates/index.html"))
api.add_resource(recData, "/recogniseFaces")
api.add_resource(feedBack, "/feedback")
api.add_resource(listNames, "/names")
api.add_resource(root, "/")
api.add_resource(findSimilarity, "/similarity")
api.add_resource(timeFaces, "/timeFaces")
if __name__ == "__main__":
app.run(port=8765, debug=True)