Spaces:
Sleeping
Sleeping
File size: 1,599 Bytes
74f2314 6c01461 74f2314 5ada9bd 74f2314 |
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 |
import gradio as gr
from ultralytics import YOLO
import cv2
# Yolov8 nano on custom data with BS=32
# model = YOLO('custom_model.pt')
# Use YoloV8 Medium model trained on Custom data with bs32
model = YOLO('yolov8m_bs32.pt')
def infer(path):
img = cv2.imread(path)
output = model(source=img)
res = output[0].cpu().numpy()
# Extract bbox, cls id and conf
bboxes = res.boxes.xyxy
class_ids = res.boxes.cls
conf_scores = res.boxes.conf
for i in range(len(bboxes)):
xmin, ymin, xmax, ymax = int(bboxes[i][0]), int(bboxes[i][1]), int(bboxes[i][2]), int(bboxes[i][3])
conf = conf_scores[i]
cls_id = int(class_ids[i])
label = model.names[cls_id] # Get the label name
# Draw rectangle for bounding box
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), color=(0, 0, 255), thickness=2, lineType=cv2.LINE_AA)
# Prepare label text with confidence score
label_text = f'{label} {conf:.2f}'
# Put text (label) on the image
cv2.putText(img, label_text, (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1, lineType=cv2.LINE_AA)
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
input_image = [
gr.components.Image(type='filepath', label='Input Image'),
]
output_image = [
gr.components.Image(type='numpy', label='Prediction'),
]
interface = gr.Interface(
fn=infer,
inputs=input_image,
outputs=output_image,
title='Skin Defects Detection',
cache_examples=False,
)
gr.TabbedInterface(
[interface],
tab_names=['Image Inference']
).queue().launch() |