File size: 989 Bytes
318cb4f
4c33f39
2b5fd75
 
 
318cb4f
cd206de
318cb4f
 
2b5fd75
 
cd206de
2b5fd75
cd206de
2b5fd75
 
 
 
 
 
 
 
 
79cc9f7
 
 
 
2b5fd75
318cb4f
 
2b5fd75
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
import gradio as gr
from ultralytics import YOLO
import cv2
from PIL import Image
import spaces  # <--- 1. Wajib import library spaces bawaan Hugging Face

# Load model kustom YOLOv11
model = YOLO("best.pt")

# 2. Wajib tambahkan decorator ini tepat di atas fungsi yang melakukan pemrosesan gambar
@spaces.GPU  
def predict_image(img):
    # Jalankan inferensi YOLO
    results = model(img)
    # Plot hasil ke gambar (BGR)
    res_bgr = results[0].plot()
    # Konversi ke RGB untuk Gradio
    res_rgb = cv2.cvtColor(res_bgr, cv2.COLOR_BGR2RGB)
    return Image.fromarray(res_rgb)

# Buat Interface Gradio yang simpel dan interaktif
demo = gr.Interface(
    fn=predict_image,
    inputs=gr.Image(type="pil", label="Upload Foto"),
    outputs=gr.Image(type="pil", label="Hasil Deteksi"),
    title="😷 Face Mask Detection",
    description="Unggah foto untuk mendeteksi penggunaan masker (mask / no-mask). Silakan upload foto proyek Anda!"
)

if __name__ == "__main__":
    demo.launch()