File size: 1,037 Bytes
ccfbad9
 
 
 
 
 
 
d3fc9cb
ccfbad9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
import cv2
import numpy as np
from PIL import Image

# Load model YOLOv5 yang telah kamu latih
model = torch.hub.load('ultralytics/yolov8', 'custom', path='my_model.pt', force_reload=True)

# Fungsi deteksi wajah
def detect_face(image):
    image_cv = np.array(image)  # Konversi ke array numpy
    image_cv = cv2.cvtColor(image_cv, cv2.COLOR_RGB2BGR)

    results = model(image_cv)  # Deteksi wajah
    
    # Gambar kotak di sekitar wajah
    for *box, conf, cls in results.xyxy[0]:  
        x1, y1, x2, y2 = map(int, box)
        cv2.rectangle(image_cv, (x1, y1), (x2, y2), (0, 255, 0), 2)

    image_result = Image.fromarray(cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB))  # Konversi kembali ke PIL
    return image_result

# Buat UI dengan Gradio
iface = gr.Interface(
    fn=detect_face, 
    inputs=gr.Image(type="pil"), 
    outputs=gr.Image(type="pil"),
    title="Deteksi Wajah dengan YOLOv5",
    description="Upload gambar dan model akan mendeteksi wajah."
)

# Jalankan aplikasi
iface.launch()