Spaces:
Sleeping
Sleeping
Commit
·
74f2314
1
Parent(s):
dac0a64
Demo Available
Browse files- app.py +54 -0
- custom_model.pt +3 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
|
| 5 |
+
# Yolov8 nano on custom data with BS=32
|
| 6 |
+
model = YOLO('custom_model.pt')
|
| 7 |
+
|
| 8 |
+
def infer(path):
|
| 9 |
+
img = cv2.imread(path)
|
| 10 |
+
output = model(img)
|
| 11 |
+
res = output[0].cpu().numpy()
|
| 12 |
+
|
| 13 |
+
# Extract bbox, cls id and conf
|
| 14 |
+
bboxes = res.boxes.xyxy
|
| 15 |
+
class_ids = res.boxes.cls
|
| 16 |
+
conf_scores = res.boxes.conf
|
| 17 |
+
|
| 18 |
+
for i in range(len(bboxes)):
|
| 19 |
+
xmin, ymin, xmax, ymax = int(bboxes[i][0]), int(bboxes[i][1]), int(bboxes[i][2]), int(bboxes[i][3])
|
| 20 |
+
conf = conf_scores[i]
|
| 21 |
+
cls_id = int(class_ids[i])
|
| 22 |
+
label = model.names[cls_id] # Get the label name
|
| 23 |
+
|
| 24 |
+
# Draw rectangle for bounding box
|
| 25 |
+
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), color=(0, 0, 255), thickness=2, lineType=cv2.LINE_AA)
|
| 26 |
+
|
| 27 |
+
# Prepare label text with confidence score
|
| 28 |
+
label_text = f'{label} {conf:.2f}'
|
| 29 |
+
|
| 30 |
+
# Put text (label) on the image
|
| 31 |
+
cv2.putText(img, label_text, (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1, lineType=cv2.LINE_AA)
|
| 32 |
+
|
| 33 |
+
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 34 |
+
|
| 35 |
+
input_image = [
|
| 36 |
+
gr.components.Image(type='filepath', label='Input Image'),
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
output_image = [
|
| 40 |
+
gr.components.Image(type='numpy', label='Prediction'),
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
interface = gr.Interface(
|
| 44 |
+
fn=infer,
|
| 45 |
+
inputs=input_image,
|
| 46 |
+
outputs=output_image,
|
| 47 |
+
title='Skin Defects Detection',
|
| 48 |
+
cache_examples=False,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
gr.TabbedInterface(
|
| 52 |
+
[interface],
|
| 53 |
+
tab_names=['Image Inference']
|
| 54 |
+
).queue().launch()
|
custom_model.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e7fd8dc09cbe746ea54a9cc716646473cb83b8bdd00f8d25d00df6d1b6f8f54d
|
| 3 |
+
size 6231214
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
opencv-python-headless
|