Commit
Β·
f90b008
1
Parent(s):
3a3705f
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import cv2
|
4 |
-
import numpy as np
|
5 |
-
from sahi.prediction import ObjectPrediction
|
6 |
-
from sahi.utils.cv import visualize_object_predictions, read_image
|
7 |
-
from ultralyticsplus import YOLO
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
model_path: gr.Dropdown = None,
|
12 |
-
image_size: gr.Slider = 640,
|
13 |
-
conf_threshold: gr.Slider = 0.25,
|
14 |
-
iou_threshold: gr.Slider = 0.45,
|
15 |
-
):
|
16 |
-
"""
|
17 |
-
YOLOv8 inference function
|
18 |
-
Args:
|
19 |
-
image: Input image
|
20 |
-
model_path: Path to the model
|
21 |
-
image_size: Image size
|
22 |
-
conf_threshold: Confidence threshold
|
23 |
-
iou_threshold: IOU threshold
|
24 |
-
Returns:
|
25 |
-
Rendered image
|
26 |
-
"""
|
27 |
-
model = YOLO(model_path)
|
28 |
-
model.overrides['conf'] = conf_threshold
|
29 |
-
model.overrides['iou']= iou_threshold
|
30 |
-
model.overrides['agnostic_nms'] = False # NMS class-agnostic
|
31 |
-
model.overrides['max_det'] = 1000
|
32 |
-
image = read_image(image)
|
33 |
-
# Observe results (You should adjust this part based on your result extraction logic)
|
34 |
-
top_class_index = torch.argmax(results[0].probs).item()
|
35 |
-
Class1 = model.names[top_class_index]
|
36 |
|
37 |
-
|
38 |
|
39 |
-
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
gr.Dropdown(["foduucom/Tyre-Quality-Classification-AI"],
|
44 |
-
default="foduucom/Tyre-Quality-Classification-AI", label="Model"),
|
45 |
-
gr.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
|
46 |
-
gr.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
|
47 |
-
gr.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
|
48 |
-
]
|
49 |
-
outputs = gr.Textbox(label="Result")
|
50 |
|
51 |
-
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
|
57 |
-
|
58 |
-
fn=yolov8_inference,
|
59 |
-
inputs=inputs,
|
60 |
-
outputs=outputs,
|
61 |
-
title=title,
|
62 |
-
description=description,
|
63 |
-
theme='huggingface',
|
64 |
-
)
|
65 |
|
66 |
-
#
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# catgories
|
5 |
+
categories =['Good_Tyre','Defective_Tyre']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# returning classifiers output
|
8 |
|
9 |
+
def image_classifier(inp):
|
10 |
+
model = YOLO("best.pt")
|
11 |
|
12 |
+
result = model.predict(source=inp)
|
13 |
+
probs = result[0].probs.data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Combine the two lists and sort based on values in descending order
|
16 |
+
sorted_pairs = sorted(zip(categories, probs), key=lambda x: x[1], reverse=True)
|
17 |
|
18 |
+
resultado = []
|
19 |
+
for name, value in sorted_pairs:
|
20 |
+
resultado.append(f'{name}: {value:.2f}%')
|
21 |
|
22 |
+
return ', '.join(resultado)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
# gradio code block for input and output
|
25 |
+
with gr.Blocks() as app:
|
26 |
+
gr.Markdown("## Classification for tyre Quality measure (Good tyre and defective tyre) on Yolo-v8")
|
27 |
+
with gr.Row():
|
28 |
+
inp_img = gr.Image()
|
29 |
+
out_txt = gr.Textbox()
|
30 |
+
btn = gr.Button(value="Submeter")
|
31 |
+
btn.click(image_classifier, inputs=inp_img, outputs=out_txt)
|
32 |
+
|
33 |
+
gr.Markdown("## Exemplos")
|
34 |
+
gr.Examples(
|
35 |
+
examples=['Sample/Good tyre.png', 'Sample/Bald tyre.png'],
|
36 |
+
inputs=inp_img,
|
37 |
+
outputs=out_txt,
|
38 |
+
fn=image_classifier,
|
39 |
+
cache_examples=True,
|
40 |
+
)
|
41 |
+
|
42 |
+
app.launch(share=True)
|