gokaygokay
commited on
Commit
•
2f7ee0a
1
Parent(s):
b9ff28b
Update app.py
Browse files
app.py
CHANGED
@@ -14,6 +14,12 @@ model_id = 'microsoft/Florence-2-large'
|
|
14 |
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).eval()
|
15 |
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def run_example(task_prompt, image, text_input=None):
|
18 |
if text_input is None:
|
19 |
prompt = task_prompt
|
@@ -103,19 +109,19 @@ def process_image(image, task_prompt, text_input=None):
|
|
103 |
elif task_prompt == '<OD>':
|
104 |
results = run_example(task_prompt, image)
|
105 |
fig = plot_bbox(image, results['<OD>'])
|
106 |
-
return "", fig
|
107 |
elif task_prompt == '<DENSE_REGION_CAPTION>':
|
108 |
results = run_example(task_prompt, image)
|
109 |
fig = plot_bbox(image, results['<DENSE_REGION_CAPTION>'])
|
110 |
-
return "", fig
|
111 |
elif task_prompt == '<REGION_PROPOSAL>':
|
112 |
results = run_example(task_prompt, image)
|
113 |
fig = plot_bbox(image, results['<REGION_PROPOSAL>'])
|
114 |
-
return "", fig
|
115 |
elif task_prompt == '<CAPTION_TO_PHRASE_GROUNDING>':
|
116 |
results = run_example(task_prompt, image, text_input)
|
117 |
fig = plot_bbox(image, results['<CAPTION_TO_PHRASE_GROUNDING>'])
|
118 |
-
return "", fig
|
119 |
elif task_prompt == '<REFERRING_EXPRESSION_SEGMENTATION>':
|
120 |
results = run_example(task_prompt, image, text_input)
|
121 |
output_image = copy.deepcopy(image)
|
@@ -130,7 +136,7 @@ def process_image(image, task_prompt, text_input=None):
|
|
130 |
results = run_example(task_prompt, image, text_input)
|
131 |
bbox_results = convert_to_od_format(results['<OPEN_VOCABULARY_DETECTION>'])
|
132 |
fig = plot_bbox(image, bbox_results)
|
133 |
-
return "", fig
|
134 |
elif task_prompt == '<REGION_TO_CATEGORY>':
|
135 |
results = run_example(task_prompt, image, text_input)
|
136 |
return results, None
|
|
|
14 |
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).eval()
|
15 |
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
16 |
|
17 |
+
def fig_to_pil(fig):
|
18 |
+
buf = io.BytesIO()
|
19 |
+
fig.savefig(buf, format='png')
|
20 |
+
buf.seek(0)
|
21 |
+
return Image.open(buf)
|
22 |
+
|
23 |
def run_example(task_prompt, image, text_input=None):
|
24 |
if text_input is None:
|
25 |
prompt = task_prompt
|
|
|
109 |
elif task_prompt == '<OD>':
|
110 |
results = run_example(task_prompt, image)
|
111 |
fig = plot_bbox(image, results['<OD>'])
|
112 |
+
return "", fig_to_pil(fig)
|
113 |
elif task_prompt == '<DENSE_REGION_CAPTION>':
|
114 |
results = run_example(task_prompt, image)
|
115 |
fig = plot_bbox(image, results['<DENSE_REGION_CAPTION>'])
|
116 |
+
return "", fig_to_pil(fig)
|
117 |
elif task_prompt == '<REGION_PROPOSAL>':
|
118 |
results = run_example(task_prompt, image)
|
119 |
fig = plot_bbox(image, results['<REGION_PROPOSAL>'])
|
120 |
+
return "", fig_to_pil(fig)
|
121 |
elif task_prompt == '<CAPTION_TO_PHRASE_GROUNDING>':
|
122 |
results = run_example(task_prompt, image, text_input)
|
123 |
fig = plot_bbox(image, results['<CAPTION_TO_PHRASE_GROUNDING>'])
|
124 |
+
return "", fig_to_pil(fig)
|
125 |
elif task_prompt == '<REFERRING_EXPRESSION_SEGMENTATION>':
|
126 |
results = run_example(task_prompt, image, text_input)
|
127 |
output_image = copy.deepcopy(image)
|
|
|
136 |
results = run_example(task_prompt, image, text_input)
|
137 |
bbox_results = convert_to_od_format(results['<OPEN_VOCABULARY_DETECTION>'])
|
138 |
fig = plot_bbox(image, bbox_results)
|
139 |
+
return "", fig_to_pil(fig)
|
140 |
elif task_prompt == '<REGION_TO_CATEGORY>':
|
141 |
results = run_example(task_prompt, image, text_input)
|
142 |
return results, None
|