Commit Β·
943fd64
1
Parent(s): a1dab0b
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,17 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
|
|
|
| 3 |
from sahi.prediction import ObjectPrediction
|
| 4 |
from sahi.utils.cv import visualize_object_predictions, read_image
|
| 5 |
from ultralyticsplus import YOLO
|
| 6 |
|
| 7 |
def yolov8_inference(
|
| 8 |
image: gr.Image = None,
|
| 9 |
-
model_path:
|
| 10 |
-
image_size: int = 640,
|
| 11 |
-
conf_threshold: float = 0.25,
|
| 12 |
-
iou_threshold: float = 0.45,
|
| 13 |
):
|
| 14 |
"""
|
| 15 |
YOLOv8 inference function
|
|
@@ -21,45 +23,51 @@ def yolov8_inference(
|
|
| 21 |
iou_threshold: IOU threshold
|
| 22 |
|
| 23 |
"""
|
|
|
|
| 24 |
model = YOLO(model_path)
|
| 25 |
model.overrides['conf'] = conf_threshold
|
| 26 |
model.overrides['iou'] = iou_threshold
|
| 27 |
model.overrides['agnostic_nms'] = False # NMS class-agnostic
|
| 28 |
-
model.overrides['max_det'] = 1000
|
| 29 |
-
|
| 30 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
top_class_index = torch.argmax(results[0].probs).item()
|
| 32 |
Class1 = model.names[top_class_index]
|
| 33 |
-
globals(Class1)
|
| 34 |
-
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
inputs = [
|
| 37 |
-
gr.Image(type="
|
| 38 |
-
gr.
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
label="
|
| 42 |
-
gr.Slider(minimum=320, maximum=1280, step=32, label="Image Size"),
|
| 43 |
-
gr.Slider(minimum=0.0, maximum=1.0, step=0.05, label="Confidence Threshold"),
|
| 44 |
-
gr.Slider(minimum=0.0, maximum=1.0, step=0.05, label="IOU Threshold"),
|
| 45 |
]
|
| 46 |
|
| 47 |
-
outputs = gr.
|
|
|
|
| 48 |
title = "AI-Powered Tire Quality Inspection: YOLOv8s Enhanced Classification"
|
| 49 |
|
| 50 |
description = """
|
| 51 |
Welcome to our π€ AI-Powered Tire Quality Inspection Space β a cutting-edge solution harnessing the capabilities of YOLOv8s to revolutionize π tire quality control processes.
|
| 52 |
"""
|
| 53 |
|
| 54 |
-
examples = [['Sample/Bald tyre.jpg', 'foduucom/Tyre-Quality-Classification-AI', 640, 0.25, 0.45], ['Sample/Good tyre.png', 'foduucom/Tyre-Quality-Classification-AI', 640, 0.25, 0.45]]
|
| 55 |
demo_app = gr.Interface(
|
| 56 |
fn=yolov8_inference,
|
| 57 |
inputs=inputs,
|
| 58 |
outputs=outputs,
|
| 59 |
title=title,
|
| 60 |
description=description,
|
| 61 |
-
examples=examples,
|
| 62 |
-
cache_examples=True,
|
| 63 |
theme='huggingface',
|
| 64 |
)
|
| 65 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 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 |
def yolov8_inference(
|
| 10 |
image: gr.Image = None,
|
| 11 |
+
model_path: str = "foduucom/Tyre-Quality-Classification-AI",
|
| 12 |
+
image_size: int = 640,
|
| 13 |
+
conf_threshold: float = 0.25,
|
| 14 |
+
iou_threshold: float = 0.45,
|
| 15 |
):
|
| 16 |
"""
|
| 17 |
YOLOv8 inference function
|
|
|
|
| 23 |
iou_threshold: IOU threshold
|
| 24 |
|
| 25 |
"""
|
| 26 |
+
# Load your model using the specified model_path (You should adjust this part based on your model loading logic)
|
| 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 |
+
|
| 33 |
+
# Preprocess your image as needed (You should adjust this part based on your preprocessing logic)
|
| 34 |
+
image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 35 |
+
image_rgb = cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB)
|
| 36 |
+
|
| 37 |
+
# Perform inference with your model (You should adjust this part based on your inference logic)
|
| 38 |
+
results = model(image_cv)
|
| 39 |
+
|
| 40 |
+
# Observe results (You should adjust this part based on your result extraction logic)
|
| 41 |
top_class_index = torch.argmax(results[0].probs).item()
|
| 42 |
Class1 = model.names[top_class_index]
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
return Class1
|
| 45 |
+
|
| 46 |
+
# Define Gradio input and output components
|
| 47 |
inputs = [
|
| 48 |
+
gr.Image(type="file", label="Input Image"),
|
| 49 |
+
gr.Textbox(label="Model Path", default="foduucom/Tyre-Quality-Classification-AI"),
|
| 50 |
+
gr.Number(default=640, label="Image Size"),
|
| 51 |
+
gr.Slider(minimum=0.0, maximum=1.0, default=0.25, label="Confidence Threshold"),
|
| 52 |
+
gr.Slider(minimum=0.0, maximum=1.0, default=0.45, label="IOU Threshold"),
|
|
|
|
|
|
|
|
|
|
| 53 |
]
|
| 54 |
|
| 55 |
+
outputs = gr.Textbox(label="Result")
|
| 56 |
+
|
| 57 |
title = "AI-Powered Tire Quality Inspection: YOLOv8s Enhanced Classification"
|
| 58 |
|
| 59 |
description = """
|
| 60 |
Welcome to our π€ AI-Powered Tire Quality Inspection Space β a cutting-edge solution harnessing the capabilities of YOLOv8s to revolutionize π tire quality control processes.
|
| 61 |
"""
|
| 62 |
|
|
|
|
| 63 |
demo_app = gr.Interface(
|
| 64 |
fn=yolov8_inference,
|
| 65 |
inputs=inputs,
|
| 66 |
outputs=outputs,
|
| 67 |
title=title,
|
| 68 |
description=description,
|
|
|
|
|
|
|
| 69 |
theme='huggingface',
|
| 70 |
)
|
| 71 |
+
|
| 72 |
+
# Launch the Gradio app
|
| 73 |
+
demo_app.launch()
|