Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -75,6 +75,7 @@ def process_image(image, text):
|
|
| 75 |
font_size = max(10, int(height * 0.2)) # Adjust size relative to height
|
| 76 |
try:
|
| 77 |
font = ImageFont.truetype(FONT_PATH, size=font_size)
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
logging.warning(f"Error loading font. Using default. {e}")
|
| 80 |
font = ImageFont.load_default()
|
|
@@ -84,18 +85,31 @@ def process_image(image, text):
|
|
| 84 |
text_height = bbox[3] - bbox[1]
|
| 85 |
|
| 86 |
text_position = ((width - text_width) // 2, (height - text_height) // 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
draw.text(text_position, text, fill=(0, 0, 0, 255), font=font)
|
| 88 |
|
| 89 |
# Convert back to OpenCV and reapply the perspective transformation
|
| 90 |
warped_with_text = cv2.cvtColor(np.array(pil_warped), cv2.COLOR_RGBA2BGRA)
|
|
|
|
|
|
|
|
|
|
| 91 |
transformed_back = cv2.warpPerspective(
|
| 92 |
-
warped_with_text,
|
| 93 |
(image_cv.shape[1], image_cv.shape[0]),
|
| 94 |
flags=cv2.WARP_INVERSE_MAP,
|
| 95 |
)
|
| 96 |
|
| 97 |
# Overlay the transformed text onto the original image
|
| 98 |
mask = transformed_back[:, :, 3] > 0 # Alpha channel as mask
|
|
|
|
|
|
|
| 99 |
image_cv[mask] = transformed_back[mask]
|
| 100 |
|
| 101 |
# Convert back to PIL format and save
|
|
@@ -136,3 +150,4 @@ interface = gr.Interface(
|
|
| 136 |
if __name__ == "__main__":
|
| 137 |
interface.launch(share=True)
|
| 138 |
|
|
|
|
|
|
| 75 |
font_size = max(10, int(height * 0.2)) # Adjust size relative to height
|
| 76 |
try:
|
| 77 |
font = ImageFont.truetype(FONT_PATH, size=font_size)
|
| 78 |
+
logging.debug(f"Font loaded successfully: {FONT_PATH} with size {font_size}")
|
| 79 |
except Exception as e:
|
| 80 |
logging.warning(f"Error loading font. Using default. {e}")
|
| 81 |
font = ImageFont.load_default()
|
|
|
|
| 85 |
text_height = bbox[3] - bbox[1]
|
| 86 |
|
| 87 |
text_position = ((width - text_width) // 2, (height - text_height) // 2)
|
| 88 |
+
|
| 89 |
+
# Debugging rectangle for text position
|
| 90 |
+
draw.rectangle(
|
| 91 |
+
[text_position, (text_position[0] + text_width, text_position[1] + text_height)],
|
| 92 |
+
outline="red", # Debugging rectangle color
|
| 93 |
+
width=2
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
draw.text(text_position, text, fill=(0, 0, 0, 255), font=font)
|
| 97 |
|
| 98 |
# Convert back to OpenCV and reapply the perspective transformation
|
| 99 |
warped_with_text = cv2.cvtColor(np.array(pil_warped), cv2.COLOR_RGBA2BGRA)
|
| 100 |
+
matrix_back = cv2.getPerspectiveTransform(np.float32(dst_corners), np.float32(corners))
|
| 101 |
+
logging.debug(f"Inverse Transformation Matrix: {matrix_back}")
|
| 102 |
+
|
| 103 |
transformed_back = cv2.warpPerspective(
|
| 104 |
+
warped_with_text, matrix_back,
|
| 105 |
(image_cv.shape[1], image_cv.shape[0]),
|
| 106 |
flags=cv2.WARP_INVERSE_MAP,
|
| 107 |
)
|
| 108 |
|
| 109 |
# Overlay the transformed text onto the original image
|
| 110 |
mask = transformed_back[:, :, 3] > 0 # Alpha channel as mask
|
| 111 |
+
logging.debug(f"Alpha mask shape: {mask.shape}, Non-zero values: {np.count_nonzero(mask)}")
|
| 112 |
+
|
| 113 |
image_cv[mask] = transformed_back[mask]
|
| 114 |
|
| 115 |
# Convert back to PIL format and save
|
|
|
|
| 150 |
if __name__ == "__main__":
|
| 151 |
interface.launch(share=True)
|
| 152 |
|
| 153 |
+
|