Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,41 @@
|
|
1 |
-
import cv2
|
2 |
-
import numpy as np
|
3 |
-
import datetime
|
4 |
import gradio as gr
|
|
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
|
10 |
-
classes = []
|
11 |
-
with open("coco.names", "r") as f:
|
12 |
-
classes = [line.strip() for line in f.readlines()]
|
13 |
|
14 |
-
def
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
confidences = []
|
22 |
-
boxes = []
|
23 |
-
|
24 |
-
for out in outs:
|
25 |
-
for detection in out:
|
26 |
-
scores = detection[5:]
|
27 |
-
class_id = np.argmax(scores)
|
28 |
-
confidence = scores[class_id]
|
29 |
-
if confidence > 0.5:
|
30 |
-
center_x = int(detection[0] * width)
|
31 |
-
center_y = int(detection[1] * height)
|
32 |
-
w = int(detection[2] * width)
|
33 |
-
h = int(detection[3] * height)
|
34 |
-
|
35 |
-
x = int(center_x - w / 2)
|
36 |
-
y = int(center_y - h / 2)
|
37 |
-
|
38 |
-
boxes.append([x, y, w, h])
|
39 |
-
confidences.append(float(confidence))
|
40 |
-
class_ids.append(class_id)
|
41 |
-
|
42 |
-
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
|
43 |
-
|
44 |
-
return [(boxes[i], class_ids[i], confidences[i]) for i in range(len(boxes)) if i in indexes]
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
cv2.putText(image, f'{label} {confidence:.2f}', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
|
55 |
-
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
56 |
|
57 |
-
|
58 |
-
cap = cv2.VideoCapture(0)
|
59 |
-
while cap.isOpened():
|
60 |
-
ret, frame = cap.read()
|
61 |
-
if not ret:
|
62 |
-
break
|
63 |
-
processed_frame = process_image(frame)
|
64 |
-
yield processed_frame
|
65 |
-
cap.release()
|
66 |
|
67 |
# Define Gradio interface
|
68 |
with gr.Blocks() as iface:
|
69 |
-
gr.Markdown("#
|
70 |
-
gr.Markdown("##
|
71 |
|
72 |
with gr.Tab("Upload Image"):
|
73 |
-
gr.Markdown("Upload an image and the YOLO model will detect
|
74 |
image_input = gr.Image(type="numpy", label="Upload an image")
|
75 |
-
image_output = gr.Image(type="
|
76 |
-
image_input.upload(
|
77 |
|
78 |
# Launch Gradio interface
|
79 |
iface.launch()
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from ultralytics import YOLO
|
4 |
+
from PIL import Image, ImageDraw
|
5 |
|
6 |
+
# Load YOLO model (Ensure best.pt is in the same directory)
|
7 |
+
YOLO_MODEL_PATH = "best.pt"
|
8 |
+
model = YOLO(YOLO_MODEL_PATH, task='detect').to("cpu") # Force CPU usage
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
def detect_text(image):
|
11 |
+
""" Runs YOLOv8 detection on the input image and returns bounding box results. """
|
12 |
+
image = Image.fromarray(image) # Convert NumPy array to PIL Image
|
13 |
+
|
14 |
+
# Run YOLO detection
|
15 |
+
results = model.predict(image, conf=0.3, iou=0.4, device="cpu")
|
16 |
+
detected_boxes = results[0].boxes.xyxy.tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
# Draw bounding boxes
|
19 |
+
image_with_boxes = image.copy()
|
20 |
+
draw = ImageDraw.Draw(image_with_boxes)
|
21 |
+
|
22 |
+
for box in detected_boxes:
|
23 |
+
x1, y1, x2, y2 = map(int, box)
|
24 |
+
draw.rectangle([x1, y1, x2, y2], outline="red", width=2) # Draw bounding box
|
25 |
+
draw.text((x1, y1 - 10), "Text", fill="red") # Label each box
|
|
|
|
|
26 |
|
27 |
+
return image_with_boxes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# Define Gradio interface
|
30 |
with gr.Blocks() as iface:
|
31 |
+
gr.Markdown("# Text Detection with YOLOv8")
|
32 |
+
gr.Markdown("## Upload an image and detect text regions using YOLO")
|
33 |
|
34 |
with gr.Tab("Upload Image"):
|
35 |
+
gr.Markdown("Upload an image, and the YOLO model will detect text in the image.")
|
36 |
image_input = gr.Image(type="numpy", label="Upload an image")
|
37 |
+
image_output = gr.Image(type="pil", label="Detected text")
|
38 |
+
image_input.upload(detect_text, inputs=image_input, outputs=image_output)
|
39 |
|
40 |
# Launch Gradio interface
|
41 |
iface.launch()
|