Commit ·
6242e13
1
Parent(s): c702f68
feat: YOLO image segmentation with Gradio UI
Browse files- app.py +59 -3
- requirements.txt +19 -1
app.py
CHANGED
|
@@ -1,7 +1,63 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
|
| 6 |
+
def detect_and_crop_spines(image, model_path):
|
| 7 |
+
# Cache the YOLO model to avoid reloading it for every inference
|
| 8 |
+
if not hasattr(detect_and_crop_spines, 'model'):
|
| 9 |
+
detect_and_crop_spines.model = YOLO(model_path)
|
| 10 |
+
|
| 11 |
+
# Convert gradio image to CV2 format if needed
|
| 12 |
+
if isinstance(image, str):
|
| 13 |
+
image = cv2.imread(image)
|
| 14 |
+
elif isinstance(image, np.ndarray) and len(image.shape) == 3 and image.shape[2] == 3:
|
| 15 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 16 |
+
|
| 17 |
+
# Add error handling for image loading
|
| 18 |
+
if image is None:
|
| 19 |
+
raise ValueError("Failed to load image")
|
| 20 |
|
| 21 |
+
# Use confidence threshold and add error handling for predictions
|
| 22 |
+
try:
|
| 23 |
+
results = detect_and_crop_spines.model.predict(image, show=False, conf=0.25)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return None, f"Error during detection: {str(e)}"
|
| 26 |
+
|
| 27 |
+
annotated_image = image.copy()
|
| 28 |
+
|
| 29 |
+
for det in enumerate(results[0].boxes.xyxy.cpu().numpy()):
|
| 30 |
+
# Get corner points
|
| 31 |
+
points = np.array([[det[0], det[1]], [det[2], det[1]],
|
| 32 |
+
[det[2], det[3]], [det[0], det[3]]], dtype=np.float32)
|
| 33 |
+
|
| 34 |
+
# Get minimum area rectangle
|
| 35 |
+
rect = cv2.minAreaRect(points)
|
| 36 |
+
box = cv2.boxPoints(rect)
|
| 37 |
+
box = np.int0(box)
|
| 38 |
+
|
| 39 |
+
# Draw rotated rectangle
|
| 40 |
+
cv2.drawContours(annotated_image, [box], 0, (0, 255, 0), 2)
|
| 41 |
+
|
| 42 |
+
# Convert back to RGB for Gradio
|
| 43 |
+
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
| 44 |
+
|
| 45 |
+
return annotated_image, f"Detected {len(results[0].boxes)} book spines"
|
| 46 |
+
|
| 47 |
+
# Create Gradio interface
|
| 48 |
+
demo = gr.Interface(
|
| 49 |
+
fn=detect_and_crop_spines,
|
| 50 |
+
inputs=[
|
| 51 |
+
gr.Image(),
|
| 52 |
+
gr.Text(label="YOLO Model Path", value="yolo11x.pt")
|
| 53 |
+
],
|
| 54 |
+
outputs=[
|
| 55 |
+
gr.Image(label="Annotated Image"),
|
| 56 |
+
gr.Text(label="Detection Results")
|
| 57 |
+
],
|
| 58 |
+
title="Book Spine Detector",
|
| 59 |
+
description="Upload an image of a bookshelf to detect book spines using YOLO"
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Launch the app
|
| 63 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -4,9 +4,12 @@ anyio==4.8.0
|
|
| 4 |
certifi==2024.12.14
|
| 5 |
charset-normalizer==3.4.1
|
| 6 |
click==8.1.8
|
|
|
|
|
|
|
| 7 |
fastapi==0.115.6
|
| 8 |
ffmpy==0.5.0
|
| 9 |
filelock==3.16.1
|
|
|
|
| 10 |
fsspec==2024.12.0
|
| 11 |
gradio==5.10.0
|
| 12 |
gradio_client==1.5.3
|
|
@@ -16,18 +19,26 @@ httpx==0.28.1
|
|
| 16 |
huggingface-hub==0.27.1
|
| 17 |
idna==3.10
|
| 18 |
Jinja2==3.1.5
|
|
|
|
| 19 |
markdown-it-py==3.0.0
|
| 20 |
MarkupSafe==2.1.5
|
|
|
|
| 21 |
mdurl==0.1.2
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
orjson==3.10.13
|
| 24 |
packaging==24.2
|
| 25 |
pandas==2.2.3
|
| 26 |
pillow==11.1.0
|
|
|
|
|
|
|
| 27 |
pydantic==2.10.4
|
| 28 |
pydantic_core==2.27.2
|
| 29 |
pydub==0.25.1
|
| 30 |
Pygments==2.19.1
|
|
|
|
| 31 |
python-dateutil==2.9.0.post0
|
| 32 |
python-multipart==0.0.20
|
| 33 |
pytz==2024.2
|
|
@@ -36,16 +47,23 @@ requests==2.32.3
|
|
| 36 |
rich==13.9.4
|
| 37 |
ruff==0.8.6
|
| 38 |
safehttpx==0.1.6
|
|
|
|
|
|
|
| 39 |
semantic-version==2.10.0
|
| 40 |
shellingham==1.5.4
|
| 41 |
six==1.17.0
|
| 42 |
sniffio==1.3.1
|
| 43 |
starlette==0.41.3
|
|
|
|
| 44 |
tomlkit==0.13.2
|
|
|
|
|
|
|
| 45 |
tqdm==4.67.1
|
| 46 |
typer==0.15.1
|
| 47 |
typing_extensions==4.12.2
|
| 48 |
tzdata==2024.2
|
|
|
|
|
|
|
| 49 |
urllib3==2.3.0
|
| 50 |
uvicorn==0.34.0
|
| 51 |
websockets==14.1
|
|
|
|
| 4 |
certifi==2024.12.14
|
| 5 |
charset-normalizer==3.4.1
|
| 6 |
click==8.1.8
|
| 7 |
+
contourpy==1.3.1
|
| 8 |
+
cycler==0.12.1
|
| 9 |
fastapi==0.115.6
|
| 10 |
ffmpy==0.5.0
|
| 11 |
filelock==3.16.1
|
| 12 |
+
fonttools==4.55.3
|
| 13 |
fsspec==2024.12.0
|
| 14 |
gradio==5.10.0
|
| 15 |
gradio_client==1.5.3
|
|
|
|
| 19 |
huggingface-hub==0.27.1
|
| 20 |
idna==3.10
|
| 21 |
Jinja2==3.1.5
|
| 22 |
+
kiwisolver==1.4.8
|
| 23 |
markdown-it-py==3.0.0
|
| 24 |
MarkupSafe==2.1.5
|
| 25 |
+
matplotlib==3.10.0
|
| 26 |
mdurl==0.1.2
|
| 27 |
+
mpmath==1.3.0
|
| 28 |
+
networkx==3.4.2
|
| 29 |
+
numpy==1.26.4
|
| 30 |
+
opencv-python==4.10.0.84
|
| 31 |
orjson==3.10.13
|
| 32 |
packaging==24.2
|
| 33 |
pandas==2.2.3
|
| 34 |
pillow==11.1.0
|
| 35 |
+
psutil==6.1.1
|
| 36 |
+
py-cpuinfo==9.0.0
|
| 37 |
pydantic==2.10.4
|
| 38 |
pydantic_core==2.27.2
|
| 39 |
pydub==0.25.1
|
| 40 |
Pygments==2.19.1
|
| 41 |
+
pyparsing==3.2.1
|
| 42 |
python-dateutil==2.9.0.post0
|
| 43 |
python-multipart==0.0.20
|
| 44 |
pytz==2024.2
|
|
|
|
| 47 |
rich==13.9.4
|
| 48 |
ruff==0.8.6
|
| 49 |
safehttpx==0.1.6
|
| 50 |
+
scipy==1.15.0
|
| 51 |
+
seaborn==0.13.2
|
| 52 |
semantic-version==2.10.0
|
| 53 |
shellingham==1.5.4
|
| 54 |
six==1.17.0
|
| 55 |
sniffio==1.3.1
|
| 56 |
starlette==0.41.3
|
| 57 |
+
sympy==1.13.1
|
| 58 |
tomlkit==0.13.2
|
| 59 |
+
torch==2.5.1
|
| 60 |
+
torchvision==0.20.1
|
| 61 |
tqdm==4.67.1
|
| 62 |
typer==0.15.1
|
| 63 |
typing_extensions==4.12.2
|
| 64 |
tzdata==2024.2
|
| 65 |
+
ultralytics==8.3.58
|
| 66 |
+
ultralytics-thop==2.0.13
|
| 67 |
urllib3==2.3.0
|
| 68 |
uvicorn==0.34.0
|
| 69 |
websockets==14.1
|