File size: 1,611 Bytes
682f119
 
3701ecb
 
 
9d9ac60
0818c70
951acc7
28acd98
b553b15
951acc7
 
 
 
b553b15
 
0818c70
68d9d6f
951acc7
cb46fe2
68d9d6f
 
682f119
28acd98
 
 
0818c70
951acc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0818c70
 
 
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
from ultralytics import YOLO
from PIL import Image
import gradio as gr
from huggingface_hub import snapshot_download
import os

#load model
def load_model(repo_id):

    download_dir = snapshot_download(repo_id)
    print(download_dir)
    model_path = os.path.join(download_dir,"best_int8_openvino_model")
    print(model_path)
    detection_model = YOLO(model_path, task='detect') 
    return detection_model

#object detection for image 
def predict(pil_img, conf_thresh, iou_thresh):
    source = pil_img
    results = detection_model.predict(source, conf=conf_thresh, iou=iou_thresh)
    annotated_img = results[0].plot()
    return Image.fromarray(annotated_img[..., ::-1])  # Convert BGR to RGB

REPO_ID = "qiqiyuan/glasses_and_mouth" 
detection_model = load_model(REPO_ID)

#gradio interface
def iface():

    interface = gr.Interface(
            fn=predict,  # Function to be called
            inputs=[  # List of input components
                gr.Image(type="pil", label="Input Image"),
                gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.5, label="Confidence Threshold"),
                gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.5, label="IoU Threshold")
            ],
            outputs=gr.Image(type="pil", label="InputImage"), # Output type
            title="Object Detection for Glasses and Mouth (Human)", 
            description="Upload an image to detect glasses and mouth (Human) using a pre-trained YOLO model",
            theme="huggingface",
        )

    return interface

#launch the gradio app
app_iface = iface()
app_iface.launch(share=True)