File size: 1,800 Bytes
89c67a6
 
 
 
647f0c6
 
 
558ce13
 
89c67a6
 
c04da13
89c67a6
 
c04da13
 
 
 
 
 
 
 
89c67a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4143042
89c67a6
 
 
 
c04da13
7e78087
c04da13
 
 
cbde59c
7e78087
cbde59c
89c67a6
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
import gradio as gr
from ultralytics import YOLO
import cv2

examples=[["photo/a.jpg"],["photo/b.jpg"],
          ["photo/c.jpg"],["photo/d.jpg"],
          ["photo/e.jpg"],["photo/f.jpg"],
          ["photo/g.jpg"],["photo/h.jpg"],
          ["photo/multi tomatos.jpg"]]


def detect_objects_on_image(image_path, conf_threshold, iou_threshold):
    image = cv2.imread(image_path)
    model = YOLO("best.pt")
    results = model.predict(
        source=image,
        conf=conf_threshold,
        iou=iou_threshold,
        show_labels=True,
        show_conf=True,
        imgsz=640,
    )
    result = results[0]
    output = []
    for box in result.boxes:
        x1, y1, x2, y2 = [
            round(x) for x in box.xyxy[0].tolist()
            
        ]
        class_id = box.cls[0].item()
        prob = round(box.conf[0].item(), 2)
        output.append([
            x1, y1, x2, y2, result.names[class_id], prob
        ])
        cv2.rectangle(
            image,
            (x1, y1),
            (x2, y2),
            color=(0, 0, 255),
            thickness=2,
            lineType=cv2.LINE_AA
        )
        
        cv2.putText(image,result.names[class_id]+'_'+str(prob),  (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2)
    return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

demo = gr.Interface(
    fn=detect_objects_on_image,
    inputs=[
        gr.Image(type="filepath", label="Input Image"),
        gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
        gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
    ],
    outputs=[
    gr.Image(type="numpy", label="Output Image"),
    ],
    title="Yolov8 Custom Object Detection",
    examples=examples,
    cache_examples=False,
)

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