Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,61 @@
|
|
|
|
1 |
import cv2
|
2 |
-
import
|
3 |
import numpy as np
|
4 |
-
from flask import Flask, render_template, request
|
5 |
import tensorflow as tf
|
|
|
|
|
6 |
|
7 |
-
# Initialize
|
8 |
app = Flask(__name__)
|
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 |
-
return
|
35 |
-
|
36 |
-
#
|
37 |
-
def
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
def get_health_recommendation(face_image):
|
46 |
-
# Analyze health metrics (dummy function for example)
|
47 |
-
return "Health alert: Normal blood pressure"
|
48 |
-
|
49 |
-
def get_fashion_recommendation(face_image):
|
50 |
-
# Suggest outfits based on style and weather (dummy function for example)
|
51 |
-
return "Suggested outfit: Casual wear suitable for sunny weather"
|
52 |
-
|
53 |
-
# Route to handle the display of the mirror and recommendations
|
54 |
@app.route('/')
|
55 |
def index():
|
56 |
-
return render_template('index.html')
|
57 |
-
|
58 |
-
@app.route('/capture', methods=['POST'])
|
59 |
-
def capture():
|
60 |
-
# Capture an image from the webcam
|
61 |
-
cap = cv2.VideoCapture(0)
|
62 |
-
ret, frame = cap.read()
|
63 |
-
if not ret:
|
64 |
-
return "Failed to capture image", 400
|
65 |
-
|
66 |
-
# Process the captured frame to detect faces and provide recommendations
|
67 |
-
frame = detect_faces(frame)
|
68 |
-
|
69 |
-
# Extract personalized recommendations (example placeholders)
|
70 |
-
skin_care = get_skin_care_recommendation(frame)
|
71 |
-
makeup = get_makeup_recommendation(frame)
|
72 |
-
health = get_health_recommendation(frame)
|
73 |
-
fashion = get_fashion_recommendation(frame)
|
74 |
-
|
75 |
-
# Return recommendations as response
|
76 |
-
recommendations = {
|
77 |
-
'skin_care': skin_care,
|
78 |
-
'makeup': makeup,
|
79 |
-
'health': health,
|
80 |
-
'fashion': fashion
|
81 |
-
}
|
82 |
|
83 |
-
|
84 |
-
|
|
|
85 |
|
86 |
if __name__ == "__main__":
|
87 |
app.run(debug=True)
|
|
|
1 |
+
import os
|
2 |
import cv2
|
3 |
+
import dlib
|
4 |
import numpy as np
|
|
|
5 |
import tensorflow as tf
|
6 |
+
import mediapipe as mp
|
7 |
+
from flask import Flask, render_template, Response
|
8 |
|
9 |
+
# Initialize Flask App
|
10 |
app = Flask(__name__)
|
11 |
|
12 |
+
# Load Face Detector (Dlib)
|
13 |
+
detector = dlib.get_frontal_face_detector()
|
14 |
+
|
15 |
+
# Load Pretrained Model for Skin Analysis (Placeholder)
|
16 |
+
model_path = "skin_model.h5"
|
17 |
+
if os.path.exists(model_path):
|
18 |
+
skin_model = tf.keras.models.load_model(model_path)
|
19 |
+
else:
|
20 |
+
skin_model = None
|
21 |
+
|
22 |
+
# OpenCV Video Capture
|
23 |
+
cap = cv2.VideoCapture(0)
|
24 |
+
|
25 |
+
# Function to Analyze Skin
|
26 |
+
def analyze_skin(frame):
|
27 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
28 |
+
faces = detector(gray)
|
29 |
+
for face in faces:
|
30 |
+
x, y, w, h = face.left(), face.top(), face.width(), face.height()
|
31 |
+
face_crop = frame[y:y+h, x:x+w]
|
32 |
+
if skin_model:
|
33 |
+
face_crop = cv2.resize(face_crop, (224, 224)) / 255.0
|
34 |
+
prediction = skin_model.predict(np.expand_dims(face_crop, axis=0))
|
35 |
+
return f"Skin Condition Score: {prediction[0][0]:.2f}"
|
36 |
+
return "No face detected"
|
37 |
+
|
38 |
+
# Video Stream Function
|
39 |
+
def generate_frames():
|
40 |
+
while True:
|
41 |
+
success, frame = cap.read()
|
42 |
+
if not success:
|
43 |
+
break
|
44 |
+
else:
|
45 |
+
skin_result = analyze_skin(frame)
|
46 |
+
cv2.putText(frame, skin_result, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
|
47 |
+
ret, buffer = cv2.imencode('.jpg', frame)
|
48 |
+
frame = buffer.tobytes()
|
49 |
+
yield (b'--frame\r\n'
|
50 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
@app.route('/')
|
53 |
def index():
|
54 |
+
return render_template('index.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
@app.route('/video_feed')
|
57 |
+
def video_feed():
|
58 |
+
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
|
59 |
|
60 |
if __name__ == "__main__":
|
61 |
app.run(debug=True)
|