Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,87 @@
|
|
| 1 |
import cv2
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
from
|
| 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 |
-
cap.release()
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
|
|
|
|
| 1 |
import cv2
|
| 2 |
+
import dlib
|
| 3 |
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
from flask import Flask, render_template
|
| 7 |
+
import requests
|
| 8 |
+
|
| 9 |
+
# Initialize Flask app
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
# Facial Recognition Setup
|
| 13 |
+
detector = dlib.get_frontal_face_detector()
|
| 14 |
+
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
|
| 15 |
+
|
| 16 |
+
# Load Skin Care Model (TensorFlow)
|
| 17 |
+
skin_care_model = load_model('skin_condition_model.h5')
|
| 18 |
+
|
| 19 |
+
# Makeup Image
|
| 20 |
+
lipstick = cv2.imread("lipstick_image.png", cv2.IMREAD_UNCHANGED)
|
| 21 |
+
|
| 22 |
+
def get_weather(location="your_city"):
|
| 23 |
+
# Fetch weather data from OpenWeatherMap API
|
| 24 |
+
api_key = "your_api_key"
|
| 25 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={location}&appid={api_key}&units=metric"
|
| 26 |
+
response = requests.get(url)
|
| 27 |
+
weather_data = response.json()
|
| 28 |
+
return weather_data['weather'][0]['description']
|
| 29 |
+
|
| 30 |
+
def suggest_outfit(weather):
|
| 31 |
+
if "rain" in weather:
|
| 32 |
+
return "Suggested Outfit: Raincoat and boots."
|
| 33 |
+
elif "clear" in weather:
|
| 34 |
+
return "Suggested Outfit: Light jacket and sunglasses."
|
| 35 |
+
else:
|
| 36 |
+
return "Suggested Outfit: Comfortable casuals."
|
| 37 |
+
|
| 38 |
+
def apply_makeup(face_image, makeup_image):
|
| 39 |
+
y_offset = 50 # Example offset
|
| 40 |
+
x_offset = 100 # Example offset
|
|
|
|
| 41 |
|
| 42 |
+
for c in range(0, 3):
|
| 43 |
+
face_image[y_offset:y_offset+makeup_image.shape[0], x_offset:x_offset+makeup_image.shape[1], c] = \
|
| 44 |
+
makeup_image[:, :, c] * (makeup_image[:, :, 3] / 255.0) + \
|
| 45 |
+
face_image[y_offset:y_offset+makeup_image.shape[0], x_offset:x_offset+makeup_image.shape[1], c] * \
|
| 46 |
+
(1.0 - makeup_image[:, :, 3] / 255.0)
|
| 47 |
+
return face_image
|
| 48 |
+
|
| 49 |
+
def predict_skin_condition(image):
|
| 50 |
+
image = cv2.resize(image, (224, 224)) # Resize to model's input size
|
| 51 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 52 |
+
image = image / 255.0 # Normalize the image
|
| 53 |
|
| 54 |
+
prediction = skin_care_model.predict(image)
|
| 55 |
+
if prediction[0] > 0.5:
|
| 56 |
+
return "Acne detected: Recommended product XYZ"
|
| 57 |
+
else:
|
| 58 |
+
return "Skin is clear: Recommended moisturizer ABC"
|
| 59 |
+
|
| 60 |
+
@app.route('/')
|
| 61 |
+
def home():
|
| 62 |
+
# Capture the video feed from webcam
|
| 63 |
+
cap = cv2.VideoCapture(0)
|
| 64 |
+
ret, frame = cap.read()
|
| 65 |
+
cap.release()
|
| 66 |
+
|
| 67 |
+
# Process the face for skin care condition
|
| 68 |
+
skin_condition = predict_skin_condition(frame)
|
| 69 |
+
|
| 70 |
+
# Detect face and apply makeup
|
| 71 |
+
faces = detector(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY))
|
| 72 |
+
for face in faces:
|
| 73 |
+
landmarks = predictor(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), face)
|
| 74 |
+
frame = apply_makeup(frame, lipstick)
|
| 75 |
+
|
| 76 |
+
# Get weather and outfit suggestion
|
| 77 |
+
weather = get_weather()
|
| 78 |
+
outfit_suggestion = suggest_outfit(weather)
|
| 79 |
+
|
| 80 |
+
# Display results on the web page
|
| 81 |
+
return render_template('index.html', skin_condition=skin_condition, outfit_suggestion=outfit_suggestion)
|
| 82 |
+
|
| 83 |
+
if __name__ == '__main__':
|
| 84 |
+
app.run(debug=True)
|
| 85 |
|
| 86 |
|
| 87 |
|