Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
-
from flask import Flask, render_template, request, jsonify, send_file
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import io
|
| 6 |
import base64
|
|
|
|
|
|
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
|
@@ -12,51 +12,55 @@ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_fronta
|
|
| 12 |
|
| 13 |
def detect_faces(image_data, scale_factor=1.1):
|
| 14 |
"""Detect faces in image and return results"""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# Convert to grayscale for face detection
|
| 22 |
-
gray_image = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
|
| 23 |
-
|
| 24 |
-
# Detect faces
|
| 25 |
-
faces = face_cascade.detectMultiScale(
|
| 26 |
-
gray_image,
|
| 27 |
-
scaleFactor=scale_factor,
|
| 28 |
-
minNeighbors=5,
|
| 29 |
-
minSize=(30, 30)
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
# Draw bounding boxes
|
| 33 |
-
for (x, y, w, h) in faces:
|
| 34 |
-
cv2.rectangle(image_np, (x, y), (x+w, y+h), (0, 255, 0), 2)
|
| 35 |
-
cv2.putText(image_np, f"Face", (x, y-10),
|
| 36 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
|
| 37 |
-
|
| 38 |
-
# Convert back to base64
|
| 39 |
-
result_image = Image.fromarray(image_np)
|
| 40 |
-
buffered = io.BytesIO()
|
| 41 |
-
result_image.save(buffered, format="JPEG")
|
| 42 |
-
result_base64 = base64.b64encode(buffered.getvalue()).decode()
|
| 43 |
-
|
| 44 |
-
# Simple age/gender estimation (placeholder)
|
| 45 |
-
results = []
|
| 46 |
-
for i, (x, y, w, h) in enumerate(faces):
|
| 47 |
-
# Very basic mock data
|
| 48 |
-
import random
|
| 49 |
-
ages = ["20-25", "26-32", "33-40", "41-50", "51-60"]
|
| 50 |
-
genders = ["Male", "Female"]
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
|
|
|
| 60 |
|
| 61 |
@app.route('/')
|
| 62 |
def index():
|
|
@@ -84,4 +88,4 @@ def detect():
|
|
| 84 |
})
|
| 85 |
|
| 86 |
if __name__ == '__main__':
|
| 87 |
-
app.run(
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 4 |
import base64
|
| 5 |
+
import io
|
| 6 |
+
from PIL import Image
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
|
|
|
| 12 |
|
| 13 |
def detect_faces(image_data, scale_factor=1.1):
|
| 14 |
"""Detect faces in image and return results"""
|
| 15 |
+
try:
|
| 16 |
+
# Convert base64 image to numpy array
|
| 17 |
+
image_data = image_data.split(',')[1] # Remove data:image/jpeg;base64,
|
| 18 |
+
image_bytes = base64.b64decode(image_data)
|
| 19 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 20 |
+
image_np = np.array(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Convert to grayscale for face detection
|
| 23 |
+
gray_image = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
|
| 24 |
+
|
| 25 |
+
# Detect faces
|
| 26 |
+
faces = face_cascade.detectMultiScale(
|
| 27 |
+
gray_image,
|
| 28 |
+
scaleFactor=scale_factor,
|
| 29 |
+
minNeighbors=5,
|
| 30 |
+
minSize=(30, 30)
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Draw bounding boxes and labels
|
| 34 |
+
for (x, y, w, h) in faces:
|
| 35 |
+
cv2.rectangle(image_np, (x, y), (x+w, y+h), (0, 255, 0), 2)
|
| 36 |
+
cv2.putText(image_np, f"Face", (x, y-10),
|
| 37 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
|
| 38 |
+
|
| 39 |
+
# Convert back to base64
|
| 40 |
+
result_image = Image.fromarray(image_np)
|
| 41 |
+
buffered = io.BytesIO()
|
| 42 |
+
result_image.save(buffered, format="JPEG")
|
| 43 |
+
result_base64 = base64.b64encode(buffered.getvalue()).decode()
|
| 44 |
+
|
| 45 |
+
# Simple age/gender estimation (placeholder)
|
| 46 |
+
results = []
|
| 47 |
+
for i, (x, y, w, h) in enumerate(faces):
|
| 48 |
+
# Very basic mock data - in production, use proper models
|
| 49 |
+
import random
|
| 50 |
+
ages = ["20-25", "26-32", "33-40", "41-50", "51-60"]
|
| 51 |
+
genders = ["Male", "Female"]
|
| 52 |
+
|
| 53 |
+
results.append({
|
| 54 |
+
'id': i + 1,
|
| 55 |
+
'age': random.choice(ages),
|
| 56 |
+
'gender': random.choice(genders),
|
| 57 |
+
'position': {'x': int(x), 'y': int(y), 'width': int(w), 'height': int(h)}
|
| 58 |
+
})
|
| 59 |
+
|
| 60 |
+
return f"data:image/jpeg;base64,{result_base64}", results
|
| 61 |
|
| 62 |
+
except Exception as e:
|
| 63 |
+
raise e
|
| 64 |
|
| 65 |
@app.route('/')
|
| 66 |
def index():
|
|
|
|
| 88 |
})
|
| 89 |
|
| 90 |
if __name__ == '__main__':
|
| 91 |
+
app.run(host='0.0.0.0', port=5000, debug=False)
|