Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,15 +4,28 @@ import numpy as np
|
|
| 4 |
import base64
|
| 5 |
import io
|
| 6 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
# Initialize face detector
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
@@ -45,7 +58,6 @@ def detect_faces(image_data, scale_factor=1.1):
|
|
| 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"]
|
|
@@ -60,10 +72,12 @@ def detect_faces(image_data, scale_factor=1.1):
|
|
| 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():
|
|
|
|
| 67 |
return render_template('index.html')
|
| 68 |
|
| 69 |
@app.route('/detect', methods=['POST'])
|
|
@@ -82,10 +96,16 @@ def detect():
|
|
| 82 |
'face_data': face_data
|
| 83 |
})
|
| 84 |
except Exception as e:
|
|
|
|
| 85 |
return jsonify({
|
| 86 |
'success': False,
|
| 87 |
'error': str(e)
|
| 88 |
})
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
if __name__ == '__main__':
|
|
|
|
| 91 |
app.run(host='0.0.0.0', port=5000, debug=False)
|
|
|
|
| 4 |
import base64
|
| 5 |
import io
|
| 6 |
from PIL import Image
|
| 7 |
+
import logging
|
| 8 |
+
|
| 9 |
+
# Configure logging
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
app = Flask(__name__)
|
| 14 |
|
| 15 |
# Initialize face detector
|
| 16 |
+
try:
|
| 17 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
| 18 |
+
logger.info("Face cascade classifier loaded successfully")
|
| 19 |
+
except Exception as e:
|
| 20 |
+
logger.error(f"Error loading face cascade: {e}")
|
| 21 |
+
face_cascade = None
|
| 22 |
|
| 23 |
def detect_faces(image_data, scale_factor=1.1):
|
| 24 |
"""Detect faces in image and return results"""
|
| 25 |
try:
|
| 26 |
+
if face_cascade is None:
|
| 27 |
+
raise Exception("Face detector not initialized")
|
| 28 |
+
|
| 29 |
# Convert base64 image to numpy array
|
| 30 |
image_data = image_data.split(',')[1] # Remove data:image/jpeg;base64,
|
| 31 |
image_bytes = base64.b64decode(image_data)
|
|
|
|
| 58 |
# Simple age/gender estimation (placeholder)
|
| 59 |
results = []
|
| 60 |
for i, (x, y, w, h) in enumerate(faces):
|
|
|
|
| 61 |
import random
|
| 62 |
ages = ["20-25", "26-32", "33-40", "41-50", "51-60"]
|
| 63 |
genders = ["Male", "Female"]
|
|
|
|
| 72 |
return f"data:image/jpeg;base64,{result_base64}", results
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
+
logger.error(f"Error in detect_faces: {e}")
|
| 76 |
raise e
|
| 77 |
|
| 78 |
@app.route('/')
|
| 79 |
def index():
|
| 80 |
+
logger.info("Index page accessed")
|
| 81 |
return render_template('index.html')
|
| 82 |
|
| 83 |
@app.route('/detect', methods=['POST'])
|
|
|
|
| 96 |
'face_data': face_data
|
| 97 |
})
|
| 98 |
except Exception as e:
|
| 99 |
+
logger.error(f"Error in detect endpoint: {e}")
|
| 100 |
return jsonify({
|
| 101 |
'success': False,
|
| 102 |
'error': str(e)
|
| 103 |
})
|
| 104 |
|
| 105 |
+
@app.route('/health')
|
| 106 |
+
def health():
|
| 107 |
+
return jsonify({'status': 'healthy', 'face_detector_loaded': face_cascade is not None})
|
| 108 |
+
|
| 109 |
if __name__ == '__main__':
|
| 110 |
+
logger.info("Starting Flask application...")
|
| 111 |
app.run(host='0.0.0.0', port=5000, debug=False)
|