xiang-wuu commited on
Commit
43b4a95
·
1 Parent(s): 07825d4

gradio component for image classification added

Browse files
Files changed (1) hide show
  1. classify_utils.py +42 -0
classify_utils.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from ultralytics import YOLO
4
+ import requests
5
+ import supervision as sv
6
+
7
+ response = requests.get("https://git.io/JJkYN")
8
+ labels = response.text.split("\n")
9
+
10
+
11
+ def clasify(image, radio_choice, slider_val):
12
+ print(radio_choice)
13
+ model = YOLO(radio_choice + '.pt')
14
+ result = model(image, verbose=False)[0]
15
+ classifications = sv.Classifications.from_ultralytics(result)
16
+ out_dic = {}
17
+ cls_out = classifications.get_top_k(4)
18
+ for idx in range(4):
19
+ cls_id = cls_out[0][idx]
20
+ cls_prob = cls_out[1][idx]
21
+ out_dic[labels[int(cls_id)]] = float(cls_prob)
22
+ return out_dic
23
+
24
+
25
+ inputs_thresh = [
26
+ gr.inputs.Image(type="filepath", label="Input Image"),
27
+ gr.inputs.Radio(label="Classification Methods",
28
+ choices=[
29
+ "yolov8n-cls", "yolov8s-cls"
30
+ ]),
31
+ gr.components.Slider(label="Class Probability Value",
32
+ value=10, minimum=1, maximum=100, step=1),
33
+ ]
34
+
35
+ classify_tab = gr.Interface(
36
+ clasify,
37
+ inputs=inputs_thresh,
38
+ outputs=gr.outputs.Label(num_top_classes=4),
39
+ title="supervision",
40
+ examples=[["dog.jpeg", "yolov8s-cls"], ["dog-2.jpeg", "yolov8n-cls"]],
41
+ description="Gradio based demo for <a href='https://github.com/roboflow/supervision' style='text-decoration: underline' target='_blank'>roboflow/supervision</a>, We write your reusable computer vision tools."
42
+ )