-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
117 lines (88 loc) · 3.91 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
115
116
117
from flask import Flask, request, jsonify
from deepface import DeepFace
import cv2
import numpy as np
import logging
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
logging.basicConfig(level=logging.DEBUG)
@app.route('/detect_emotion', methods=['POST'])
def detect():
if 'image' not in request.files:
print('Image not provided')
return jsonify({
'status': 'fail', 'message': 'Image not provided'
}), 400
file = request.files['image']
if not file:
print('Image not as file')
return jsonify({'status': 'fail', 'message': 'Invalid image data'}), 400
try:
# Read the image in OpenCV format
file_bytes = np.frombuffer(file.read(), np.uint8)
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
if img is None:
print('No image')
return jsonify({'status': 'fail', 'message': 'Invalid image data'}), 400
new_image_path = f'api/detect_emotion.jpg'
cv2.imwrite(new_image_path, img)
objs = DeepFace.analyze(
img_path = new_image_path,
actions = ['emotion'],
)
print(objs)
return jsonify({'status': 'success', 'message': 'emotion detected'})
except Exception as e:
app.logger.error(f'Error processing image: {e}')
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/save_faceimage', methods=['POST'])
def save():
if 'image' not in request.files or 'name' not in request.form:
return jsonify({'status': 'fail', 'message': 'Image or name not provided'}), 400
file = request.files['image']
user_name = request.form['name']
if not file or not user_name:
return jsonify({'status': 'fail', 'message': 'Invalid image data or name'}), 400
try:
# Read the image in OpenCV format
file_bytes = np.frombuffer(file.read(), np.uint8)
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
if img is None:
return jsonify({'status': 'fail', 'message': 'Invalid image data'}), 400
new_image_path = f'api/{user_name}.jpg'
cv2.imwrite(new_image_path, img)
return jsonify({'status': 'success', 'message': f'Image saved as {new_image_path}'})
except Exception as e:
app.logger.error(f'Error processing image: {e}')
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/face_recognition', methods=['POST'])
def recognize():
if 'image' not in request.files or 'name' not in request.form:
return jsonify({'status': 'fail', 'message': 'Image or name not provided'}), 400
file = request.files['image']
user_name = request.form['name']
if not file or not user_name:
return jsonify({'status': 'fail', 'message': 'Invalid image data or name'}), 400
try:
# Read the image in OpenCV format
file_bytes = np.fromstring(file.read(), np.uint8)
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
if img is None:
return jsonify({'status': 'fail', 'message': 'Invalid image data'}), 400
# Save the captured image temporarily
captured_image_path = 'api/captured_image.jpg'
cv2.imwrite(captured_image_path, img)
# Path to the known image
known_image_path = f'api/{user_name}.jpg' # Change this to the path of your known image
print(known_image_path)
# Perform face verification
result = DeepFace.verify(img1_path=captured_image_path, img2_path=known_image_path)
if result['verified']:
return jsonify({'status': 'success', 'message': 'Face recognized'})
else:
return jsonify({'status': 'fail', 'message': 'Face not recognized'})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500
if __name__ == "__main__":
app.run(debug=True)