Spaces:
Sleeping
Sleeping
add quad boxes
Browse files
app.py
CHANGED
@@ -118,13 +118,15 @@ def plot_bbox(image, data, use_quad_boxes=False):
|
|
118 |
|
119 |
# Handle both 'bboxes' and 'quad_boxes'
|
120 |
if use_quad_boxes:
|
121 |
-
for quad_box, label in zip(data
|
122 |
quad_box = np.array(quad_box).reshape(-1, 2)
|
123 |
poly = patches.Polygon(quad_box, linewidth=1, edgecolor='r', facecolor='none')
|
124 |
ax.add_patch(poly)
|
125 |
plt.text(quad_box[0][0], quad_box[0][1], label, color='white', fontsize=8, bbox=dict(facecolor='red', alpha=0.5))
|
126 |
else:
|
127 |
-
|
|
|
|
|
128 |
x1, y1, x2, y2 = bbox
|
129 |
rect = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=1, edgecolor='r', facecolor='none')
|
130 |
ax.add_patch(rect)
|
@@ -183,7 +185,7 @@ def main_process(image, task):
|
|
183 |
|
184 |
if task in IMAGE_TASKS:
|
185 |
if task == "OCR with Region":
|
186 |
-
fig = plot_bbox(image, result
|
187 |
output_image = fig_to_pil(fig)
|
188 |
text_output = result.get('<OCR_WITH_REGION>', {}).get('recognized_text', 'No text found')
|
189 |
|
@@ -192,12 +194,13 @@ def main_process(image, task):
|
|
192 |
|
193 |
return output_image, gr.update(visible=True), text_output, gr.update(visible=True)
|
194 |
else:
|
195 |
-
fig = plot_bbox(image, result
|
196 |
output_image = fig_to_pil(fig)
|
197 |
return output_image, gr.update(visible=True), None, gr.update(visible=False)
|
198 |
else:
|
199 |
return None, gr.update(visible=False), str(result), gr.update(visible=True)
|
200 |
|
|
|
201 |
def reset_outputs():
|
202 |
return None, gr.update(visible=False), None, gr.update(visible=True)
|
203 |
|
@@ -207,7 +210,7 @@ with gr.Blocks(title="PLeIAs/📸📈✍🏻Florence-PDF") as iface:
|
|
207 |
|
208 |
with gr.Column():
|
209 |
image_input = gr.Image(type="pil", label="Input Image")
|
210 |
-
task_dropdown = gr.Dropdown(list(TASK_PROMPTS.keys()), label="Task", value="Caption")
|
211 |
|
212 |
with gr.Row():
|
213 |
submit_button = gr.Button("Process")
|
|
|
118 |
|
119 |
# Handle both 'bboxes' and 'quad_boxes'
|
120 |
if use_quad_boxes:
|
121 |
+
for quad_box, label in zip(data.get('quad_boxes', []), data.get('labels', [])):
|
122 |
quad_box = np.array(quad_box).reshape(-1, 2)
|
123 |
poly = patches.Polygon(quad_box, linewidth=1, edgecolor='r', facecolor='none')
|
124 |
ax.add_patch(poly)
|
125 |
plt.text(quad_box[0][0], quad_box[0][1], label, color='white', fontsize=8, bbox=dict(facecolor='red', alpha=0.5))
|
126 |
else:
|
127 |
+
bboxes = data.get('bboxes', [])
|
128 |
+
labels = data.get('labels', [])
|
129 |
+
for bbox, label in zip(bboxes, labels):
|
130 |
x1, y1, x2, y2 = bbox
|
131 |
rect = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=1, edgecolor='r', facecolor='none')
|
132 |
ax.add_patch(rect)
|
|
|
185 |
|
186 |
if task in IMAGE_TASKS:
|
187 |
if task == "OCR with Region":
|
188 |
+
fig = plot_bbox(image, result.get('<OCR_WITH_REGION>', {}), use_quad_boxes=True)
|
189 |
output_image = fig_to_pil(fig)
|
190 |
text_output = result.get('<OCR_WITH_REGION>', {}).get('recognized_text', 'No text found')
|
191 |
|
|
|
194 |
|
195 |
return output_image, gr.update(visible=True), text_output, gr.update(visible=True)
|
196 |
else:
|
197 |
+
fig = plot_bbox(image, result.get(TASK_PROMPTS[task], {}))
|
198 |
output_image = fig_to_pil(fig)
|
199 |
return output_image, gr.update(visible=True), None, gr.update(visible=False)
|
200 |
else:
|
201 |
return None, gr.update(visible=False), str(result), gr.update(visible=True)
|
202 |
|
203 |
+
|
204 |
def reset_outputs():
|
205 |
return None, gr.update(visible=False), None, gr.update(visible=True)
|
206 |
|
|
|
210 |
|
211 |
with gr.Column():
|
212 |
image_input = gr.Image(type="pil", label="Input Image")
|
213 |
+
task_dropdown = gr.Dropdown(list(TASK_PROMPTS.keys()), label="Task", value="✍🏻Caption")
|
214 |
|
215 |
with gr.Row():
|
216 |
submit_button = gr.Button("Process")
|