Fix: Import ImageDraw for drawing
Browse files
.gradio/flagged/Image with Predictions/19fe55b15859fae38b71/image.webp
ADDED
![]() |
.gradio/flagged/Upload an Image/4b70d3af99abf0d653ef/egfb.jpg
ADDED
![]() |
.gradio/flagged/dataset1.csv
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
Upload an Image,Image with Predictions,timestamp
|
2 |
+
.gradio\flagged\Upload an Image\4b70d3af99abf0d653ef\egfb.jpg,.gradio\flagged\Image with Predictions\19fe55b15859fae38b71\image.webp,2025-04-01 08:34:58.485687
|
app.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from PIL import Image
|
4 |
import io
|
5 |
from ultralytics import YOLO
|
6 |
|
7 |
# --- Load YOLO Model ---
|
8 |
MODEL_PATH = 'model/char.pt'
|
|
|
9 |
try:
|
10 |
model = YOLO(MODEL_PATH)
|
11 |
print(f"Model loaded successfully from: {MODEL_PATH}")
|
@@ -15,30 +16,29 @@ except Exception as e:
|
|
15 |
|
16 |
# --- Prediction Function for Gradio ---
|
17 |
def predict(image):
|
18 |
-
if model is None
|
19 |
-
return
|
20 |
|
21 |
try:
|
22 |
-
img = Image.fromarray(image).convert('RGB')
|
23 |
-
results = model(img)
|
|
|
|
|
|
|
24 |
|
25 |
-
predictions = []
|
26 |
for result in results:
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
label = f"{pred['label']} ({pred['confidence']:.2f})"
|
38 |
-
draw.rectangle([x1, y1, x2, y2], outline="green", width=2)
|
39 |
-
draw.text((x1, y1 - 10), label, fill="red")
|
40 |
|
41 |
-
return img
|
42 |
|
43 |
except Exception as e:
|
44 |
return f"Error during prediction: {e}"
|
@@ -52,4 +52,4 @@ iface = gr.Interface(
|
|
52 |
description="Upload an image to see object detection predictions using a YOLO model.",
|
53 |
)
|
54 |
|
55 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from PIL import Image, ImageDraw, ImageFont # Import ImageFont for better labels
|
4 |
import io
|
5 |
from ultralytics import YOLO
|
6 |
|
7 |
# --- Load YOLO Model ---
|
8 |
MODEL_PATH = 'model/char.pt'
|
9 |
+
|
10 |
try:
|
11 |
model = YOLO(MODEL_PATH)
|
12 |
print(f"Model loaded successfully from: {MODEL_PATH}")
|
|
|
16 |
|
17 |
# --- Prediction Function for Gradio ---
|
18 |
def predict(image):
|
19 |
+
if model is None:
|
20 |
+
return "Model is not loaded properly."
|
21 |
|
22 |
try:
|
23 |
+
img = Image.fromarray(image).convert('RGB') # Convert to PIL Image
|
24 |
+
results = model(img) # Perform inference
|
25 |
+
|
26 |
+
draw = ImageDraw.Draw(img)
|
27 |
+
font = ImageFont.load_default() # Load a default font for text
|
28 |
|
|
|
29 |
for result in results:
|
30 |
+
if hasattr(result, 'boxes') and result.boxes is not None:
|
31 |
+
for box in result.boxes:
|
32 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Bounding box coordinates
|
33 |
+
label = model.model.names[int(box.cls)] # Get class label
|
34 |
+
confidence = float(box.conf[0]) # Get confidence score
|
35 |
|
36 |
+
# Draw bounding box and text
|
37 |
+
draw.rectangle([x1, y1, x2, y2], outline="green", width=3)
|
38 |
+
text = f"{label} ({confidence:.2f})"
|
39 |
+
draw.text((x1, y1 - 10), text, fill="red", font=font)
|
|
|
|
|
|
|
40 |
|
41 |
+
return img # Return the image with drawn boxes
|
42 |
|
43 |
except Exception as e:
|
44 |
return f"Error during prediction: {e}"
|
|
|
52 |
description="Upload an image to see object detection predictions using a YOLO model.",
|
53 |
)
|
54 |
|
55 |
+
iface.launch()
|