Spaces:
Sleeping
Sleeping
add interface logic
Browse files
app.py
CHANGED
@@ -82,33 +82,51 @@ def process_image(image, task):
|
|
82 |
def main_process(image, task):
|
83 |
result = process_image(image, task)
|
84 |
|
85 |
-
if task in ["Object Detection", "Dense Region Caption", "Region Proposal"]:
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
90 |
else:
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
94 |
|
95 |
with gr.Blocks(title="Florence-2 Demo") as iface:
|
96 |
gr.Markdown("# Florence-2 Demo")
|
97 |
gr.Markdown("Upload an image and select a task to process with Florence-2.")
|
98 |
|
99 |
-
with gr.
|
100 |
image_input = gr.Image(type="pil", label="Input Image")
|
101 |
task_dropdown = gr.Dropdown(list(TASK_PROMPTS.keys()), label="Task")
|
102 |
|
103 |
-
|
|
|
|
|
104 |
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
submit_button.click(
|
109 |
-
fn=
|
110 |
inputs=[image_input, task_dropdown],
|
111 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
)
|
113 |
|
114 |
iface.launch()
|
|
|
82 |
def main_process(image, task):
|
83 |
result = process_image(image, task)
|
84 |
|
85 |
+
if task in ["Object Detection", "Dense Region Caption", "Region Proposal", "OCR with Region"]:
|
86 |
+
if task == "OCR with Region":
|
87 |
+
output_image = draw_ocr_bboxes(image.copy(), result[TASK_PROMPTS[task]])
|
88 |
+
else:
|
89 |
+
fig = plot_bbox(image, result[TASK_PROMPTS[task]])
|
90 |
+
output_image = fig_to_pil(fig)
|
91 |
+
return output_image, None
|
92 |
else:
|
93 |
+
return None, str(result)
|
94 |
+
|
95 |
+
def reset_outputs():
|
96 |
+
return None, None
|
97 |
|
98 |
with gr.Blocks(title="Florence-2 Demo") as iface:
|
99 |
gr.Markdown("# Florence-2 Demo")
|
100 |
gr.Markdown("Upload an image and select a task to process with Florence-2.")
|
101 |
|
102 |
+
with gr.Column():
|
103 |
image_input = gr.Image(type="pil", label="Input Image")
|
104 |
task_dropdown = gr.Dropdown(list(TASK_PROMPTS.keys()), label="Task")
|
105 |
|
106 |
+
with gr.Row():
|
107 |
+
submit_button = gr.Button("Process")
|
108 |
+
reset_button = gr.Button("Reset")
|
109 |
|
110 |
+
output_image = gr.Image(label="Processed Image", visible=False)
|
111 |
+
output_text = gr.Textbox(label="Output", visible=False)
|
112 |
+
|
113 |
+
def process_and_show(image, task):
|
114 |
+
img_output, text_output = main_process(image, task)
|
115 |
+
if img_output is not None:
|
116 |
+
return {output_image: gr.update(value=img_output, visible=True), output_text: gr.update(visible=False)}
|
117 |
+
else:
|
118 |
+
return {output_image: gr.update(visible=False), output_text: gr.update(value=text_output, visible=True)}
|
119 |
|
120 |
submit_button.click(
|
121 |
+
fn=process_and_show,
|
122 |
inputs=[image_input, task_dropdown],
|
123 |
+
outputs=[output_image, output_text]
|
124 |
+
)
|
125 |
+
|
126 |
+
reset_button.click(
|
127 |
+
fn=reset_outputs,
|
128 |
+
inputs=[],
|
129 |
+
outputs=[output_image, output_text]
|
130 |
)
|
131 |
|
132 |
iface.launch()
|