Add app.py, model, and requirements
Browse files- app.py +55 -0
- model/char.pt +3 -0
- requirements.txt.txt +4 -0
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}")
|
12 |
+
except Exception as e:
|
13 |
+
print(f"Error loading model: {e}")
|
14 |
+
model = None
|
15 |
+
|
16 |
+
# --- Prediction Function for Gradio ---
|
17 |
+
def predict(image):
|
18 |
+
if model is None or image is None:
|
19 |
+
return None
|
20 |
+
|
21 |
+
try:
|
22 |
+
img = Image.fromarray(image).convert('RGB')
|
23 |
+
results = model(img)
|
24 |
+
|
25 |
+
predictions = []
|
26 |
+
for result in results:
|
27 |
+
for box in result.boxes:
|
28 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
29 |
+
label = model.model.names[int(box.cls)]
|
30 |
+
confidence = float(box.conf[0])
|
31 |
+
predictions.append({'label': label, 'confidence': confidence, 'bbox': (x1, y1, x2, y2)})
|
32 |
+
|
33 |
+
# Draw bounding boxes on the image
|
34 |
+
draw = ImageDraw.Draw(img)
|
35 |
+
for pred in predictions:
|
36 |
+
x1, y1, x2, y2 = pred['bbox']
|
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}"
|
45 |
+
|
46 |
+
# --- Gradio Interface ---
|
47 |
+
iface = gr.Interface(
|
48 |
+
fn=predict,
|
49 |
+
inputs=gr.Image(label="Upload an Image"),
|
50 |
+
outputs=gr.Image(label="Image with Predictions"),
|
51 |
+
title="YOLO Object Detection",
|
52 |
+
description="Upload an image to see object detection predictions using a YOLO model.",
|
53 |
+
)
|
54 |
+
|
55 |
+
iface.launch()
|
model/char.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:be71c61b3e156ac487d62313585facd7cd97123f861f955c168d7cca40ce9d5f
|
3 |
+
size 19150547
|
requirements.txt.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
Pillow
|
3 |
+
ultralytics
|
4 |
+
gradio
|