Spaces:
Running
Running
add: check for the text being small so it can't go below 0, also file isn't image.png but the current timestamp
Browse files- main.py +14 -4
- utils/write_text_on_image.py +26 -6
main.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
from typing import Dict, Any
|
4 |
|
5 |
import numpy as np
|
@@ -39,14 +39,16 @@ def extract_text_from_regions(
|
|
39 |
|
40 |
|
41 |
def predict(image: np.ndarray, target_lang: str):
|
|
|
|
|
42 |
|
43 |
image = Image.fromarray(image)
|
44 |
-
image.save(
|
45 |
|
46 |
try:
|
47 |
np_image = np.array(image)
|
48 |
|
49 |
-
results = predict_bounding_boxes(object_detection_model,
|
50 |
extract_text_from_regions(np_image, target_lang, results)
|
51 |
|
52 |
return np_image
|
@@ -54,3 +56,11 @@ def predict(image: np.ndarray, target_lang: str):
|
|
54 |
except Exception as e:
|
55 |
print(f"Error: {str(e)}")
|
56 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
from typing import Dict, Any
|
4 |
|
5 |
import numpy as np
|
|
|
39 |
|
40 |
|
41 |
def predict(image: np.ndarray, target_lang: str):
|
42 |
+
timestamp = str(int(time.time() * 1000000)) # Generate a unique timestamp
|
43 |
+
temp_filename = f"image_{timestamp}.png"
|
44 |
|
45 |
image = Image.fromarray(image)
|
46 |
+
image.save(temp_filename)
|
47 |
|
48 |
try:
|
49 |
np_image = np.array(image)
|
50 |
|
51 |
+
results = predict_bounding_boxes(object_detection_model, temp_filename)
|
52 |
extract_text_from_regions(np_image, target_lang, results)
|
53 |
|
54 |
return np_image
|
|
|
56 |
except Exception as e:
|
57 |
print(f"Error: {str(e)}")
|
58 |
return None
|
59 |
+
|
60 |
+
finally:
|
61 |
+
# Clean up the temporary file
|
62 |
+
if os.path.exists(temp_filename):
|
63 |
+
try:
|
64 |
+
os.remove(temp_filename)
|
65 |
+
except OSError as e:
|
66 |
+
print(f"Warning: Could not remove temporary file {temp_filename}: {e}")
|
utils/write_text_on_image.py
CHANGED
@@ -64,6 +64,8 @@ def add_text(image: np.ndarray, text: str, contour: np.ndarray):
|
|
64 |
line_height = 16
|
65 |
font_size = 14
|
66 |
wrapping_ratio = 0.075
|
|
|
|
|
67 |
|
68 |
wrap_width = max(1, int(w * wrapping_ratio))
|
69 |
wrapped_text = textwrap.fill(text, width=wrap_width, break_long_words=True)
|
@@ -73,22 +75,40 @@ def add_text(image: np.ndarray, text: str, contour: np.ndarray):
|
|
73 |
lines = wrapped_text.split("\n")
|
74 |
total_text_height = (len(lines)) * line_height
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
80 |
wrap_width = max(1, int(w * wrapping_ratio))
|
81 |
wrapped_text = textwrap.fill(text, width=wrap_width, break_long_words=True)
|
82 |
font = ImageFont.truetype(font_path, size=font_size)
|
83 |
lines = wrapped_text.split("\n")
|
84 |
total_text_height = (len(lines)) * line_height
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
# Vertical centering
|
87 |
-
|
|
|
88 |
|
89 |
for line in lines:
|
90 |
text_length = draw.textlength(line, font=font)
|
91 |
-
text_x = x + (
|
|
|
|
|
92 |
draw.text((text_x, text_y), line, font=font, fill=(0, 0, 0))
|
93 |
text_y += line_height
|
94 |
|
|
|
64 |
line_height = 16
|
65 |
font_size = 14
|
66 |
wrapping_ratio = 0.075
|
67 |
+
min_font_size = 6 # Minimum font size to prevent going to 0
|
68 |
+
max_iterations = 20 # Prevent infinite loops
|
69 |
|
70 |
wrap_width = max(1, int(w * wrapping_ratio))
|
71 |
wrapped_text = textwrap.fill(text, width=wrap_width, break_long_words=True)
|
|
|
75 |
lines = wrapped_text.split("\n")
|
76 |
total_text_height = (len(lines)) * line_height
|
77 |
|
78 |
+
iterations = 0
|
79 |
+
while (
|
80 |
+
total_text_height > h
|
81 |
+
and font_size > min_font_size
|
82 |
+
and iterations < max_iterations
|
83 |
+
):
|
84 |
+
line_height = max(line_height - 2, min_font_size)
|
85 |
+
font_size = max(font_size - 2, min_font_size)
|
86 |
+
wrapping_ratio = min(wrapping_ratio + 0.025, 0.5)
|
87 |
wrap_width = max(1, int(w * wrapping_ratio))
|
88 |
wrapped_text = textwrap.fill(text, width=wrap_width, break_long_words=True)
|
89 |
font = ImageFont.truetype(font_path, size=font_size)
|
90 |
lines = wrapped_text.split("\n")
|
91 |
total_text_height = (len(lines)) * line_height
|
92 |
+
iterations += 1
|
93 |
+
|
94 |
+
# If text still doesn't fit after all adjustments, truncate it
|
95 |
+
if total_text_height > h:
|
96 |
+
max_lines = max(1, h // line_height)
|
97 |
+
lines = lines[:max_lines]
|
98 |
+
if len(lines) < len(wrapped_text.split("\n")):
|
99 |
+
# Add ellipsis to last line if text was truncated
|
100 |
+
if lines:
|
101 |
+
lines[-1] = lines[-1][: max(0, len(lines[-1]) - 3)] + "..."
|
102 |
|
103 |
# Vertical centering
|
104 |
+
actual_text_height = len(lines) * line_height
|
105 |
+
text_y = y + max(0, (h - actual_text_height) // 2)
|
106 |
|
107 |
for line in lines:
|
108 |
text_length = draw.textlength(line, font=font)
|
109 |
+
text_x = x + max(
|
110 |
+
0, (w - text_length) // 2
|
111 |
+
) # Ensure x coordinate is not negative
|
112 |
draw.text((text_x, text_y), line, font=font, fill=(0, 0, 0))
|
113 |
text_y += line_height
|
114 |
|