Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -16,16 +16,7 @@ face_shape_descriptions = {
|
|
16 |
"Square": "dengan rahang tegas dan dahi lebar."
|
17 |
}
|
18 |
|
19 |
-
# Frame
|
20 |
-
glasses_recommendations = {
|
21 |
-
"Heart": "Frame Rimless",
|
22 |
-
"Oblong": "Frame Persegi Panjang",
|
23 |
-
"Oval": "Frame Bulat",
|
24 |
-
"Round": "Frame Kotak",
|
25 |
-
"Square": "Frame Oval"
|
26 |
-
}
|
27 |
-
|
28 |
-
# Frame images path (make sure these files are available!)
|
29 |
glasses_images = {
|
30 |
"Heart": "Glasses/RimlessFrame.jpg",
|
31 |
"Oblong": "Glasses/RectangleFrame.jpg",
|
@@ -58,6 +49,22 @@ model.eval()
|
|
58 |
# Initialize Mediapipe
|
59 |
mp_face_detection = mp.solutions.face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
# Preprocess function
|
62 |
def preprocess_image(image):
|
63 |
img = np.array(image)
|
@@ -94,17 +101,24 @@ def predict(image):
|
|
94 |
pred_label = id2label[pred_idx]
|
95 |
pred_prob = probs[0][pred_idx].item() * 100
|
96 |
|
97 |
-
|
|
|
|
|
98 |
description = face_shape_descriptions[pred_label]
|
99 |
frame_image_path = glasses_images.get(pred_label)
|
100 |
|
101 |
-
# Build
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
# Load frame image if available
|
107 |
-
if os.path.exists(frame_image_path):
|
108 |
frame_image = Image.open(frame_image_path)
|
109 |
else:
|
110 |
frame_image = None
|
|
|
16 |
"Square": "dengan rahang tegas dan dahi lebar."
|
17 |
}
|
18 |
|
19 |
+
# Frame images path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
glasses_images = {
|
21 |
"Heart": "Glasses/RimlessFrame.jpg",
|
22 |
"Oblong": "Glasses/RectangleFrame.jpg",
|
|
|
49 |
# Initialize Mediapipe
|
50 |
mp_face_detection = mp.solutions.face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5)
|
51 |
|
52 |
+
# --- New: Decision tree function
|
53 |
+
def recommend_glasses_tree(face_shape):
|
54 |
+
face_shape = face_shape.lower()
|
55 |
+
if face_shape == "square":
|
56 |
+
return ["Oval", "Round"]
|
57 |
+
elif face_shape == "round":
|
58 |
+
return ["Square", "Octagon", "Cat Eye"]
|
59 |
+
elif face_shape == "oval":
|
60 |
+
return ["Oval", "Pilot (Aviator)", "Cat Eye", "Round"]
|
61 |
+
elif face_shape == "heart":
|
62 |
+
return ["Pilot (Aviator)", "Cat Eye", "Round"]
|
63 |
+
elif face_shape == "oblong":
|
64 |
+
return ["Square", "Oval", "Pilot (Aviator)", "Cat Eye"]
|
65 |
+
else:
|
66 |
+
return []
|
67 |
+
|
68 |
# Preprocess function
|
69 |
def preprocess_image(image):
|
70 |
img = np.array(image)
|
|
|
101 |
pred_label = id2label[pred_idx]
|
102 |
pred_prob = probs[0][pred_idx].item() * 100
|
103 |
|
104 |
+
# --- Use decision tree for recommendations
|
105 |
+
frame_recommendations = recommend_glasses_tree(pred_label)
|
106 |
+
|
107 |
description = face_shape_descriptions[pred_label]
|
108 |
frame_image_path = glasses_images.get(pred_label)
|
109 |
|
110 |
+
# Build explanation text
|
111 |
+
if frame_recommendations:
|
112 |
+
recommended_frames = ', '.join(frame_recommendations)
|
113 |
+
explanation = (f"Bentuk wajah kamu adalah {pred_label} ({pred_prob:.2f}%). "
|
114 |
+
f"Kamu memiliki bentuk wajah {description} "
|
115 |
+
f"Rekomendasi bentuk kacamata yang sesuai dengan wajah kamu adalah: {recommended_frames}.")
|
116 |
+
else:
|
117 |
+
explanation = (f"Bentuk wajah kamu adalah {pred_label} ({pred_prob:.2f}%). "
|
118 |
+
f"Tidak ada rekomendasi frame untuk bentuk wajah ini.")
|
119 |
|
120 |
# Load frame image if available
|
121 |
+
if frame_image_path and os.path.exists(frame_image_path):
|
122 |
frame_image = Image.open(frame_image_path)
|
123 |
else:
|
124 |
frame_image = None
|