File size: 2,277 Bytes
34b002e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79b1612
34b002e
 
 
 
 
 
 
 
a46b896
34b002e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17d191e
34b002e
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import gradio as gr
import cv2
import requests
import os
import cvzone
import math
import random
import numpy as np

from ultralytics import YOLO




model = YOLO('yolov8x-seg.pt')
path  = []
video_path = []

listClasses = ['person', 'bicycle', 'car']


def show_preds_image(image_path):
    image = cv2.imread(image_path)
    outputs = model.predict(source=image_path)
    results = outputs[0].cpu().numpy()

    yolo_classes = list(model.names.values())
    classes_ids = [yolo_classes.index(clas) for clas in yolo_classes]
    colors = [random.choices(range(256), k=3) for _ in classes_ids]
    
    for result in outputs:
        for mask, box in zip(result.masks.xy, result.boxes):
            
    #for r in results:
        #boxes = r.boxes
        #for box in boxes:
            cls = box.cls[0]
            conf = math.ceil((box.conf[0]*100))/100
            if (int(cls)<3) and (conf > 0.70):
                
                points = np.int32([mask])
                cv2.polylines(img, points, True, (255, 0, 0), 1)
                color_number = classes_ids.index(int(box.cls[0]))
                color = colors[color_number]
                            
                cv2.fillPoly(image, points, color)
                
                x1, y1, x2, y2 = box.xyxy[0]
                x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)

        


                name = yolo_classes[int(cls)]
                # fontScale
                fontScale = 0.5

                color_number = classes_ids.index(int(box.cls[0]))
                color = colors[color_number]

                # Line thickness of 2 px
                thickness = 1
                font = cv2.FONT_HERSHEY_SIMPLEX

                cv2.putText(image, str(name) + " " + str(conf), (max(0,x1), max(35,y1)), font,
                       fontScale, color, thickness, cv2.LINE_AA)


    return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

inputs_image = [
    gr.components.Image(type="filepath", label="Input Image"),
]
outputs_image = [
    gr.components.Image(type="numpy", label="Output Image"),
]
interface_image = gr.Interface(
    fn=show_preds_image,
    inputs=inputs_image,
    outputs=outputs_image,
    title="Object segmentation",
    examples=path,
    cache_examples=False,
).launch()