Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,6 +25,7 @@ streaming = False
|
|
| 25 |
|
| 26 |
def detect_and_classify(frame):
|
| 27 |
faces = detector.detect_faces(frame)
|
|
|
|
| 28 |
if faces:
|
| 29 |
for face in faces:
|
| 30 |
x, y, w, h = face['box']
|
|
@@ -46,12 +47,26 @@ def detect_and_classify(frame):
|
|
| 46 |
(text_width, text_height), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.8, 2)
|
| 47 |
cv2.rectangle(frame, (x, y - text_height - 10), (x + text_width + 5, y), (0, 255, 0), -1)
|
| 48 |
cv2.putText(frame, text, (x, y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 0), 2)
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
@app.route('/')
|
| 52 |
def index():
|
| 53 |
return render_template('index.html', top_emotions=None, img_base64=None, show_upload=True, show_camera=False, initial_image=True)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
@app.route('/predict_video', methods=['POST'])
|
| 56 |
def predict_video():
|
| 57 |
data = request.json
|
|
@@ -66,11 +81,11 @@ def predict_video():
|
|
| 66 |
frame = np.array(image)
|
| 67 |
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
| 68 |
|
| 69 |
-
processed_frame = detect_and_classify(frame)
|
| 70 |
_, buffer = cv2.imencode(".jpg", processed_frame)
|
| 71 |
processed_image_base64 = base64.b64encode(buffer).decode("utf-8")
|
| 72 |
|
| 73 |
-
return jsonify({"processed_frame": processed_image_base64})
|
| 74 |
|
| 75 |
if __name__ == '__main__':
|
| 76 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 25 |
|
| 26 |
def detect_and_classify(frame):
|
| 27 |
faces = detector.detect_faces(frame)
|
| 28 |
+
detected_faces = []
|
| 29 |
if faces:
|
| 30 |
for face in faces:
|
| 31 |
x, y, w, h = face['box']
|
|
|
|
| 47 |
(text_width, text_height), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.8, 2)
|
| 48 |
cv2.rectangle(frame, (x, y - text_height - 10), (x + text_width + 5, y), (0, 255, 0), -1)
|
| 49 |
cv2.putText(frame, text, (x, y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 0), 2)
|
| 50 |
+
detected_faces.append((top_emotion, top_percentage))
|
| 51 |
+
return frame, detected_faces
|
| 52 |
|
| 53 |
@app.route('/')
|
| 54 |
def index():
|
| 55 |
return render_template('index.html', top_emotions=None, img_base64=None, show_upload=True, show_camera=False, initial_image=True)
|
| 56 |
|
| 57 |
+
@app.route('/classify', methods=['POST'])
|
| 58 |
+
def classify_image():
|
| 59 |
+
image = request.files['image']
|
| 60 |
+
img = Image.open(image)
|
| 61 |
+
img = np.array(img)
|
| 62 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 63 |
+
processed_frame, top_emotions = detect_and_classify(img)
|
| 64 |
+
|
| 65 |
+
_, buffer = cv2.imencode('.png', processed_frame)
|
| 66 |
+
img_base64 = base64.b64encode(buffer).decode('utf-8')
|
| 67 |
+
|
| 68 |
+
return render_template('index.html', top_emotions=top_emotions, img_base64=img_base64, show_upload=True, show_camera=False, initial_image=False)
|
| 69 |
+
|
| 70 |
@app.route('/predict_video', methods=['POST'])
|
| 71 |
def predict_video():
|
| 72 |
data = request.json
|
|
|
|
| 81 |
frame = np.array(image)
|
| 82 |
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
| 83 |
|
| 84 |
+
processed_frame, detected_faces = detect_and_classify(frame)
|
| 85 |
_, buffer = cv2.imencode(".jpg", processed_frame)
|
| 86 |
processed_image_base64 = base64.b64encode(buffer).decode("utf-8")
|
| 87 |
|
| 88 |
+
return jsonify({"processed_frame": processed_image_base64, "emotions": detected_faces})
|
| 89 |
|
| 90 |
if __name__ == '__main__':
|
| 91 |
app.run(host='0.0.0.0', port=7860)
|