Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,19 +4,20 @@ import gradio as gr
|
|
| 4 |
import easyocr
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
|
| 11 |
-
#
|
|
|
|
| 12 |
model = YOLO(WEIGHTS_PATH, task="detect")
|
| 13 |
-
model.model = torch.load(WEIGHTS_PATH, map_location="cpu", weights_only=False)
|
| 14 |
|
| 15 |
# Initialize EasyOCR
|
| 16 |
reader = easyocr.Reader(['en'], gpu=False)
|
| 17 |
|
| 18 |
def detect_and_read(image):
|
| 19 |
-
# YOLO detection
|
| 20 |
results = model(image)
|
| 21 |
detections = results[0].boxes.xyxy.cpu().numpy() # x1, y1, x2, y2
|
| 22 |
labels = results[0].names
|
|
@@ -45,7 +46,7 @@ demo = gr.Interface(
|
|
| 45 |
fn=detect_and_read,
|
| 46 |
inputs=gr.Image(type="numpy"),
|
| 47 |
outputs="json",
|
| 48 |
-
title="
|
| 49 |
description="Object detection with YOLOv8 and OCR with EasyOCR"
|
| 50 |
)
|
| 51 |
|
|
|
|
| 4 |
import easyocr
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
+
import ultralytics.nn.tasks as tasks
|
| 8 |
|
| 9 |
+
# ✅ Allowlist the YOLOv8 DetectionModel class so PyTorch 2.6 can unpickle it safely
|
| 10 |
+
torch.serialization.add_safe_globals([tasks.DetectionModel])
|
| 11 |
|
| 12 |
+
# Load YOLO model (trusted official weights from Ultralytics)
|
| 13 |
+
WEIGHTS_PATH = "yolov8n.pt" # Should be in your Space folder or auto-downloaded
|
| 14 |
model = YOLO(WEIGHTS_PATH, task="detect")
|
|
|
|
| 15 |
|
| 16 |
# Initialize EasyOCR
|
| 17 |
reader = easyocr.Reader(['en'], gpu=False)
|
| 18 |
|
| 19 |
def detect_and_read(image):
|
| 20 |
+
# Run YOLO detection
|
| 21 |
results = model(image)
|
| 22 |
detections = results[0].boxes.xyxy.cpu().numpy() # x1, y1, x2, y2
|
| 23 |
labels = results[0].names
|
|
|
|
| 46 |
fn=detect_and_read,
|
| 47 |
inputs=gr.Image(type="numpy"),
|
| 48 |
outputs="json",
|
| 49 |
+
title="YOLOv8 + EasyOCR",
|
| 50 |
description="Object detection with YOLOv8 and OCR with EasyOCR"
|
| 51 |
)
|
| 52 |
|