-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
56 lines (45 loc) · 1.63 KB
/
app.py
File metadata and controls
56 lines (45 loc) · 1.63 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
from flask import Flask, request, jsonify
import cv2
import numpy as np
import face_recognition as fr
path = 'ImageDataset'
images = []
ClassNames = []
KnownEncodeList=[]
app = Flask(__name__)
@app.route('/', methods=['GET'])
def root():
return jsonify({"message": "Hello World"})
@app.route('/upload_image_and_user', methods=['POST'])
def upload_image_and_user():
user_id = request.form.get('user_id')
img = request.files['image_data'].read()
nparr = np.frombuffer(img, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
encode = fr.face_encodings(img)[0]
KnownEncodeList.append(encode)
ClassNames.append(user_id)
return f"Image data uploaded successfully for user ID: {user_id}"
@app.route('/recognize_user_id', methods=['POST'])
def recognize_user_id():
img = request.files['image_data'].read()
nparr = np.frombuffer(img, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
CurrentFrame = fr.face_locations(img)
EncodeCurrentFrame = fr.face_encodings(img, CurrentFrame)
names = []
resultId=''
for EncodeFace, FaceLoc in zip(EncodeCurrentFrame, CurrentFrame):
matches = fr.compare_faces(KnownEncodeList, EncodeFace)
Facedis = fr.face_distance(KnownEncodeList, EncodeFace)
matchIndex = np.argmin(Facedis)
if matches[matchIndex]:
names.append(ClassNames[matchIndex])
resultId=ClassNames[matchIndex]
else:
names.append("Unknown")
return jsonify({"user_id": resultId})
if __name__ == '__main__':
app.run()