Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,11 +18,12 @@ face_shape_descriptions = {
|
|
| 18 |
|
| 19 |
# Frame images path
|
| 20 |
glasses_images = {
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
-
"
|
| 24 |
-
"
|
| 25 |
-
"
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
id2label = {0: 'Heart', 1: 'Oblong', 2: 'Oval', 3: 'Round', 4: 'Square'}
|
|
@@ -105,7 +106,14 @@ def predict(image):
|
|
| 105 |
frame_recommendations = recommend_glasses_tree(pred_label)
|
| 106 |
|
| 107 |
description = face_shape_descriptions[pred_label]
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
# Build explanation text
|
| 111 |
if frame_recommendations:
|
|
@@ -117,16 +125,10 @@ def predict(image):
|
|
| 117 |
explanation = (f"Bentuk wajah kamu adalah {pred_label} ({pred_prob:.2f}%). "
|
| 118 |
f"Tidak ada rekomendasi frame untuk bentuk wajah ini.")
|
| 119 |
|
| 120 |
-
|
| 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
|
| 125 |
-
|
| 126 |
-
return pred_label, explanation, frame_image
|
| 127 |
|
| 128 |
except Exception as e:
|
| 129 |
-
return "Error", str(e),
|
| 130 |
|
| 131 |
# Gradio Interface
|
| 132 |
iface = gr.Interface(
|
|
@@ -135,7 +137,7 @@ iface = gr.Interface(
|
|
| 135 |
outputs=[
|
| 136 |
gr.Textbox(label="Bentuk Wajah Terdeteksi"),
|
| 137 |
gr.Textbox(label="Rekomendasi dan Penjelasan"),
|
| 138 |
-
gr.
|
| 139 |
],
|
| 140 |
title="Rekomendasi Kacamata Berdasarkan Bentuk Wajah",
|
| 141 |
description="Upload foto wajahmu untuk mendapatkan rekomendasi bentuk kacamata yang sesuai!"
|
|
|
|
| 18 |
|
| 19 |
# Frame images path
|
| 20 |
glasses_images = {
|
| 21 |
+
"Oval": "glasses/oval.jpg",
|
| 22 |
+
"Round": "glasses/round.jpg",
|
| 23 |
+
"Square": "glasses/square.jpg",
|
| 24 |
+
"Octagon": "glasses/octagon.jpg",
|
| 25 |
+
"Cat Eye": "glasses/cat eye.jpg",
|
| 26 |
+
"Pilot (Aviator)": "glasses/aviator.jpg"
|
| 27 |
}
|
| 28 |
|
| 29 |
id2label = {0: 'Heart', 1: 'Oblong', 2: 'Oval', 3: 'Round', 4: 'Square'}
|
|
|
|
| 106 |
frame_recommendations = recommend_glasses_tree(pred_label)
|
| 107 |
|
| 108 |
description = face_shape_descriptions[pred_label]
|
| 109 |
+
frame_images = []
|
| 110 |
+
|
| 111 |
+
# Load images for all recommended frames
|
| 112 |
+
for frame in frame_recommendations:
|
| 113 |
+
frame_image_path = glasses_images.get(frame)
|
| 114 |
+
if frame_image_path and os.path.exists(frame_image_path):
|
| 115 |
+
frame_image = Image.open(frame_image_path)
|
| 116 |
+
frame_images.append(frame_image)
|
| 117 |
|
| 118 |
# Build explanation text
|
| 119 |
if frame_recommendations:
|
|
|
|
| 125 |
explanation = (f"Bentuk wajah kamu adalah {pred_label} ({pred_prob:.2f}%). "
|
| 126 |
f"Tidak ada rekomendasi frame untuk bentuk wajah ini.")
|
| 127 |
|
| 128 |
+
return pred_label, explanation, frame_images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
except Exception as e:
|
| 131 |
+
return "Error", str(e), []
|
| 132 |
|
| 133 |
# Gradio Interface
|
| 134 |
iface = gr.Interface(
|
|
|
|
| 137 |
outputs=[
|
| 138 |
gr.Textbox(label="Bentuk Wajah Terdeteksi"),
|
| 139 |
gr.Textbox(label="Rekomendasi dan Penjelasan"),
|
| 140 |
+
gr.Gallery(label="Gambar Frame Rekomendasi", columns=3)
|
| 141 |
],
|
| 142 |
title="Rekomendasi Kacamata Berdasarkan Bentuk Wajah",
|
| 143 |
description="Upload foto wajahmu untuk mendapatkan rekomendasi bentuk kacamata yang sesuai!"
|