File size: 2,259 Bytes
1e54fbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import pandas as pd
import cv2
import gradio as gr
import torch
from ultralyticsplus import YOLO, render_result
import matplotlib.pyplot as plt

# Creating a function to perform predictions
def prediction(image: gr.Image = None, 
                image_size: gr.Slider = 640,
                conf_threshold: gr.Slider = 0.4,
                iou_threshold: gr.Slider = 0.50):

    model = YOLO("best.pt")

    results = model.predict(
        source=image,
        conf=conf_threshold,
        iou=iou_threshold,
        imgsz=image_size
    )
    
    image = cv2.imread(image)
        
    for result in results[0].obb:
        point_1_x = float(result.xyxyxyxy[0][0][0])
        point_1_y = float(result.xyxyxyxy[0][0][1])
        point_2_x = float(result.xyxyxyxy[0][1][0])
        point_2_y = float(result.xyxyxyxy[0][1][1])
        point_3_x = float(result.xyxyxyxy[0][2][0])
        point_3_y = float(result.xyxyxyxy[0][2][1])
        point_4_x = float(result.xyxyxyxy[0][3][0])
        point_4_y = float(result.xyxyxyxy[0][3][1])
        
        cls = int(result.cls)
        
        if cls == 1:
            color = (0, 255, 0)
        else:
            color = (255, 0, 0)
        
        conf = float(result.conf)
        
        text = f"{cls} : {np.round(conf) * 100}%" 
        
        points = np.array([[point_1_x, point_1_y], 
                           [point_2_x, point_2_y], 
                           [point_3_x, point_3_y], 
                           [point_4_x, point_4_y]], np.int32)
         
        points = points.reshape((-1, 1, 2))
        
        cv2.polylines(image, [points], isClosed = True, color = color, thickness = 2)   

    return image

inputs = [
    gr.Image(type="filepath", label="Select an image"),
    gr.Slider(minimum=320, maximum=1280, value=640, step=32, label="Image Size"),
    gr.Slider(minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold"),
    gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold")
]

outputs = gr.Image(type = "filepath", label="Output Image")  

yolo_app = gr.Interface(
    fn = prediction,
    inputs = inputs,
    outputs = outputs,
    title = "VPS Model"
)

yolo_app.launch(debug = True, share = True)