Spaces:
Sleeping
Sleeping
Update Interface
Browse files
app.py
CHANGED
|
@@ -20,38 +20,35 @@ def process(img):
|
|
| 20 |
img = np.array(img) / 255.0
|
| 21 |
return np.expand_dims(img, axis=0)
|
| 22 |
|
| 23 |
-
def predict(img):
|
| 24 |
img = process(img)
|
| 25 |
pred = model.predict(img)
|
| 26 |
class_idx = np.argmax(pred)
|
| 27 |
confidence = pred[0][class_idx]
|
| 28 |
class_name = class_labels[class_idx]
|
| 29 |
-
|
|
|
|
| 30 |
return {
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
}
|
| 35 |
else:
|
| 36 |
-
return {
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
}
|
| 41 |
|
| 42 |
|
|
|
|
| 43 |
interface = gr.Interface(
|
| 44 |
fn=predict,
|
| 45 |
inputs=[
|
| 46 |
gr.Image(type="pil"),
|
| 47 |
gr.Slider(0, 1, value=0.5, label="Confidence Threshold")
|
| 48 |
],
|
| 49 |
-
outputs=
|
| 50 |
-
gr.Textbox(label="Label"),
|
| 51 |
-
gr.Textbox(label="Confidence"),
|
| 52 |
-
gr.Textbox(label="Status")
|
| 53 |
-
],
|
| 54 |
-
title="CNN Makanan Classifier"
|
| 55 |
)
|
| 56 |
|
| 57 |
interface.launch()
|
|
|
|
| 20 |
img = np.array(img) / 255.0
|
| 21 |
return np.expand_dims(img, axis=0)
|
| 22 |
|
| 23 |
+
def predict(img, threshold):
|
| 24 |
img = process(img)
|
| 25 |
pred = model.predict(img)
|
| 26 |
class_idx = np.argmax(pred)
|
| 27 |
confidence = pred[0][class_idx]
|
| 28 |
class_name = class_labels[class_idx]
|
| 29 |
+
|
| 30 |
+
if confidence < threshold:
|
| 31 |
return {
|
| 32 |
+
"label": "Tidak Dikenali",
|
| 33 |
+
"confidence": float(confidence),
|
| 34 |
+
"status": "Unknown"
|
| 35 |
}
|
| 36 |
else:
|
| 37 |
+
return {
|
| 38 |
+
"label": class_name,
|
| 39 |
+
"confidence": float(confidence),
|
| 40 |
+
"status": "OK"
|
| 41 |
}
|
| 42 |
|
| 43 |
|
| 44 |
+
|
| 45 |
interface = gr.Interface(
|
| 46 |
fn=predict,
|
| 47 |
inputs=[
|
| 48 |
gr.Image(type="pil"),
|
| 49 |
gr.Slider(0, 1, value=0.5, label="Confidence Threshold")
|
| 50 |
],
|
| 51 |
+
outputs=gr.JSON(label="Hasil Prediksi")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
interface.launch()
|