File size: 1,351 Bytes
f5f8873
 
 
 
 
74261ab
e0c5f86
69b56ed
 
 
 
 
 
53d945d
e0c5f86
f5f8873
5b3c174
f5f8873
 
 
f7b2c15
f5f8873
ed15643
76630e1
 
 
e0c5f86
 
ed15643
 
5b3c174
ed15643
 
 
5b3c174
 
ed15643
 
 
 
5b3c174
f5f8873
e8714f7
ed15643
e8714f7
 
 
 
 
 
ed15643
e8714f7
 
f5f8873
1
2
3
4
5
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image

model = tf.keras.models.load_model("best_model_mobilenetv2_finetune.h5")
class_labels = [
    'Ayam Bakar','Ayam Goreng','Bakso','Bakwan',
    'Bihun','Capcay','Gado-Gado','Ikan Goreng',	
    'Kerupuk',	'Martabak Telur','Mie','Nasi Goreng',	
	'Nasi Putih','Nugget','Opor Ayam','Pempek',
	'Rendang','Roti','Sate','Sosis',
	'Soto','Tahu','Telur','Tempe',
	'Tumis Kangkung','Udang',
]

CONFIDENCE_THRESHOLD = 0.5
def process(img):
    img = img.resize((224,224))
    img = np.array(img) / 255.0
    return np.expand_dims(img, axis=0)

def predict(img, threshold):
    img = process(img)
    pred = model.predict(img)
    class_idx = np.argmax(pred)
    confidence = pred[0][class_idx]
    class_name = class_labels[class_idx]

    if confidence < threshold:
        return {
            "label": "Tidak Dikenali",
            "confidence": float(confidence),
            "status": "Unknown"
        }
    else:
        return {
            "label": class_name,
            "confidence": float(confidence),
            "status": "OK"
        }



interface = gr.Interface(
    fn=predict,
    inputs=[
        gr.Image(type="pil"),
        gr.Slider(0, 1, value=0.5, label="Confidence Threshold")
    ],
    outputs=gr.JSON(label="Hasil Prediksi")
)

interface.launch()