Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from ultralytics import YOLO
|
3 |
-
from PIL import Image, ImageDraw
|
4 |
import random
|
5 |
|
6 |
# Load YOLO model (ensure best.pt exists in the working directory)
|
@@ -16,8 +16,11 @@ def get_class_color(class_id):
|
|
16 |
CLASS_COLORS[class_id] = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
|
17 |
return CLASS_COLORS[class_id]
|
18 |
|
|
|
|
|
|
|
19 |
def detect_text_lines(image):
|
20 |
-
"""Detects text lines and draws bounding boxes with
|
21 |
image = Image.fromarray(image)
|
22 |
original_image = image.copy()
|
23 |
|
@@ -29,15 +32,33 @@ def detect_text_lines(image):
|
|
29 |
|
30 |
# Draw bounding boxes on the image
|
31 |
draw = ImageDraw.Draw(original_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
for idx, (x1, y1, x2, y2) in enumerate(detected_boxes):
|
33 |
class_id = int(class_ids[idx])
|
34 |
color = get_class_color(class_id)
|
|
|
|
|
|
|
35 |
draw.rectangle([x1, y1, x2, y2], outline=color, width=2)
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
total_objects = len(detected_boxes)
|
38 |
total_classes = len(set(class_ids))
|
39 |
|
40 |
-
return original_image, f"Total Objects Detected: {total_objects}", f"Total
|
41 |
|
42 |
# Gradio UI
|
43 |
with gr.Blocks() as iface:
|
@@ -50,13 +71,12 @@ with gr.Blocks() as iface:
|
|
50 |
image_input = gr.Image(type="numpy", label="Upload an image")
|
51 |
|
52 |
with gr.Column(scale=1):
|
53 |
-
gr.Markdown("### 🖼 Annotated Image with Bounding Boxes")
|
54 |
output_annotated = gr.Image(type="pil", label="Detected Text Lines")
|
55 |
|
56 |
-
gr.Markdown("### 🔢
|
57 |
output_objects = gr.Textbox(label="Total Objects Detected", lines=1)
|
58 |
-
gr.
|
59 |
-
output_classes = gr.Textbox(label="Total Class Detected", lines=1)
|
60 |
|
61 |
image_input.upload(
|
62 |
detect_text_lines,
|
@@ -66,4 +86,75 @@ with gr.Blocks() as iface:
|
|
66 |
|
67 |
# 🚀 Ensure the app runs properly in Hugging Face Spaces
|
68 |
if __name__ == "__main__":
|
69 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from ultralytics import YOLO
|
3 |
+
from PIL import Image, ImageDraw, ImageFont
|
4 |
import random
|
5 |
|
6 |
# Load YOLO model (ensure best.pt exists in the working directory)
|
|
|
16 |
CLASS_COLORS[class_id] = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
|
17 |
return CLASS_COLORS[class_id]
|
18 |
|
19 |
+
# Class Names (Modify based on your dataset)
|
20 |
+
CLASS_NAMES = {0: "Text Line", 1: "Heading", 2: "Signature"} # Example labels
|
21 |
+
|
22 |
def detect_text_lines(image):
|
23 |
+
"""Detects text lines and draws bounding boxes with class names."""
|
24 |
image = Image.fromarray(image)
|
25 |
original_image = image.copy()
|
26 |
|
|
|
32 |
|
33 |
# Draw bounding boxes on the image
|
34 |
draw = ImageDraw.Draw(original_image)
|
35 |
+
|
36 |
+
try:
|
37 |
+
font = ImageFont.truetype("arial.ttf", 18) # Load a font (ensure arial.ttf is available)
|
38 |
+
except:
|
39 |
+
font = ImageFont.load_default() # Fallback in case font is missing
|
40 |
+
|
41 |
for idx, (x1, y1, x2, y2) in enumerate(detected_boxes):
|
42 |
class_id = int(class_ids[idx])
|
43 |
color = get_class_color(class_id)
|
44 |
+
class_name = CLASS_NAMES.get(class_id, f"Class {class_id}")
|
45 |
+
|
46 |
+
# Draw bounding box
|
47 |
draw.rectangle([x1, y1, x2, y2], outline=color, width=2)
|
48 |
|
49 |
+
# Draw label with background
|
50 |
+
text_size = draw.textbbox((0, 0), class_name, font=font)
|
51 |
+
text_width = text_size[2] - text_size[0]
|
52 |
+
text_height = text_size[3] - text_size[1]
|
53 |
+
|
54 |
+
# Draw filled rectangle behind text for better visibility
|
55 |
+
draw.rectangle([x1, y1 - text_height - 4, x1 + text_width + 6, y1], fill=color)
|
56 |
+
draw.text((x1 + 3, y1 - text_height - 2), class_name, fill="white", font=font)
|
57 |
+
|
58 |
total_objects = len(detected_boxes)
|
59 |
total_classes = len(set(class_ids))
|
60 |
|
61 |
+
return original_image, f"Total Objects Detected: {total_objects}", f"Total Classes Detected: {total_classes}"
|
62 |
|
63 |
# Gradio UI
|
64 |
with gr.Blocks() as iface:
|
|
|
71 |
image_input = gr.Image(type="numpy", label="Upload an image")
|
72 |
|
73 |
with gr.Column(scale=1):
|
74 |
+
gr.Markdown("### 🖼 Annotated Image with Bounding Boxes and Labels")
|
75 |
output_annotated = gr.Image(type="pil", label="Detected Text Lines")
|
76 |
|
77 |
+
gr.Markdown("### 🔢 Detection Results")
|
78 |
output_objects = gr.Textbox(label="Total Objects Detected", lines=1)
|
79 |
+
output_classes = gr.Textbox(label="Total Classes Detected", lines=1)
|
|
|
80 |
|
81 |
image_input.upload(
|
82 |
detect_text_lines,
|
|
|
86 |
|
87 |
# 🚀 Ensure the app runs properly in Hugging Face Spaces
|
88 |
if __name__ == "__main__":
|
89 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
90 |
+
|
91 |
+
|
92 |
+
# import gradio as gr
|
93 |
+
# from ultralytics import YOLO
|
94 |
+
# from PIL import Image, ImageDraw
|
95 |
+
# import random
|
96 |
+
|
97 |
+
# # Load YOLO model (ensure best.pt exists in the working directory)
|
98 |
+
# YOLO_MODEL_PATH = "Yolov12s-trained.pt"
|
99 |
+
# model = YOLO(YOLO_MODEL_PATH, task='detect').to("cpu")
|
100 |
+
|
101 |
+
# # Define a set of colors for different classes
|
102 |
+
# CLASS_COLORS = {}
|
103 |
+
|
104 |
+
# def get_class_color(class_id):
|
105 |
+
# """Assign a random color to each class."""
|
106 |
+
# if class_id not in CLASS_COLORS:
|
107 |
+
# CLASS_COLORS[class_id] = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
|
108 |
+
# return CLASS_COLORS[class_id]
|
109 |
+
|
110 |
+
# def detect_text_lines(image):
|
111 |
+
# """Detects text lines and draws bounding boxes with different colors for each class."""
|
112 |
+
# image = Image.fromarray(image)
|
113 |
+
# original_image = image.copy()
|
114 |
+
|
115 |
+
# # Run YOLO text detection
|
116 |
+
# results = model.predict(image, conf=0.4, device="cpu")
|
117 |
+
# detected_boxes = results[0].boxes.xyxy.tolist()
|
118 |
+
# class_ids = results[0].boxes.cls.tolist()
|
119 |
+
# detected_boxes = [list(map(int, box)) for box in detected_boxes]
|
120 |
+
|
121 |
+
# # Draw bounding boxes on the image
|
122 |
+
# draw = ImageDraw.Draw(original_image)
|
123 |
+
# for idx, (x1, y1, x2, y2) in enumerate(detected_boxes):
|
124 |
+
# class_id = int(class_ids[idx])
|
125 |
+
# color = get_class_color(class_id)
|
126 |
+
# draw.rectangle([x1, y1, x2, y2], outline=color, width=2)
|
127 |
+
|
128 |
+
# total_objects = len(detected_boxes)
|
129 |
+
# total_classes = len(set(class_ids))
|
130 |
+
|
131 |
+
# return original_image, f"Total Objects Detected: {total_objects}", f"Total Class Detected: {total_classes}"
|
132 |
+
|
133 |
+
# # Gradio UI
|
134 |
+
# with gr.Blocks() as iface:
|
135 |
+
# gr.Markdown("# 📜 Text Line Detection with YOLO")
|
136 |
+
# gr.Markdown("## 📷 Upload an image to detect text lines")
|
137 |
+
|
138 |
+
# with gr.Row():
|
139 |
+
# with gr.Column(scale=1):
|
140 |
+
# gr.Markdown("### 📤 Upload Image")
|
141 |
+
# image_input = gr.Image(type="numpy", label="Upload an image")
|
142 |
+
|
143 |
+
# with gr.Column(scale=1):
|
144 |
+
# gr.Markdown("### 🖼 Annotated Image with Bounding Boxes")
|
145 |
+
# output_annotated = gr.Image(type="pil", label="Detected Text Lines")
|
146 |
+
|
147 |
+
# gr.Markdown("### 🔢 Total Objects Detected")
|
148 |
+
# output_objects = gr.Textbox(label="Total Objects Detected", lines=1)
|
149 |
+
# gr.Markdown("### 🔢 Total Class Detected")
|
150 |
+
# output_classes = gr.Textbox(label="Total Class Detected", lines=1)
|
151 |
+
|
152 |
+
# image_input.upload(
|
153 |
+
# detect_text_lines,
|
154 |
+
# inputs=image_input,
|
155 |
+
# outputs=[output_annotated, output_objects, output_classes]
|
156 |
+
# )
|
157 |
+
|
158 |
+
# # 🚀 Ensure the app runs properly in Hugging Face Spaces
|
159 |
+
# if __name__ == "__main__":
|
160 |
+
# iface.launch(server_name="0.0.0.0", server_port=7860)
|