Spaces:
Running
Running
Upload 11 files
Browse files- .gitattributes +7 -0
- Data.png +3 -0
- Model.png +3 -0
- Poster.png +3 -0
- README.md +70 -14
- Tasks.png +3 -0
- app.py +290 -0
- image-demo.png +3 -0
- image1.jpg +3 -0
- image2.jpg +3 -0
- requirements.txt +10 -0
- toeic-part2.jpg +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,10 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
Data.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
image-demo.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
image1.jpg filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
image2.jpg filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
Model.png filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
Poster.png filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
Tasks.png filter=lfs diff=lfs merge=lfs -text
|
Data.png
ADDED
|
Git LFS Details
|
Model.png
ADDED
|
Git LFS Details
|
Poster.png
ADDED
|
Git LFS Details
|
README.md
CHANGED
|
@@ -1,14 +1,70 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Florence-2 Demo: Advancing a Unified Representation for a Variety of Vision Tasks
|
| 2 |
+
|
| 3 |
+
This is a Gradio-based demo showcasing **Florence-2**, a unified vision foundation model that advances the state-of-the-art in various computer vision tasks through a single, versatile architecture.
|
| 4 |
+
|
| 5 |
+
## Demo Preview
|
| 6 |
+
|
| 7 |
+

|
| 8 |
+
|
| 9 |
+
## About Florence-2
|
| 10 |
+
|
| 11 |
+
Florence-2 represents a significant breakthrough in computer vision by providing a unified representation that can handle a diverse range of vision tasks including:
|
| 12 |
+
|
| 13 |
+
- Object detection
|
| 14 |
+
- Image captioning
|
| 15 |
+
- Visual question answering
|
| 16 |
+
- OCR (Optical Character Recognition)
|
| 17 |
+
- Region proposal
|
| 18 |
+
- Segmentation
|
| 19 |
+
- And many more vision tasks
|
| 20 |
+
|
| 21 |
+
The model demonstrates how a single architecture can be effectively applied across multiple vision domains, eliminating the need for task-specific models.
|
| 22 |
+
|
| 23 |
+
## Paper & Resources
|
| 24 |
+
|
| 25 |
+
📄 **CVPR 2024 Paper**: [Florence-2: Advancing a Unified Representation for a Variety of Vision Tasks](https://openaccess.thecvf.com/content/CVPR2024/papers/Xiao_Florence-2_Advancing_a_Unified_Representation_for_a_Variety_of_Vision_CVPR_2024_paper.pdf)
|
| 26 |
+
|
| 27 |
+
🎥 **CVPR Virtual Presentation**: [https://cvpr.thecvf.com/virtual/2024/poster/30529](https://cvpr.thecvf.com/virtual/2024/poster/30529)
|
| 28 |
+
|
| 29 |
+
🖼️ **Research Poster**: [Poster.png](./Poster.png)
|
| 30 |
+
|
| 31 |
+
## Demo Features
|
| 32 |
+
|
| 33 |
+
This Gradio demo allows you to:
|
| 34 |
+
- Upload images and interact with Florence-2's various capabilities
|
| 35 |
+
- Test different vision tasks on your own images
|
| 36 |
+
- Experience the unified model's performance across multiple domains
|
| 37 |
+
|
| 38 |
+
## Getting Started
|
| 39 |
+
|
| 40 |
+
1. Install the required dependencies:
|
| 41 |
+
```bash
|
| 42 |
+
pip install -r requirements.txt
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
2. Run the demo:
|
| 46 |
+
```bash
|
| 47 |
+
python app.py
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
3. Open your browser and navigate to the provided local URL to start using the demo.
|
| 51 |
+
|
| 52 |
+
## References
|
| 53 |
+
|
| 54 |
+
**Hugging Face Spaces**:
|
| 55 |
+
- [Florence-2 Demo by gokaygokay](https://huggingface.co/spaces/gokaygokay/Florence-2)
|
| 56 |
+
- [Florence-SAM Integration by SkalskiP](https://huggingface.co/spaces/SkalskiP/florence-sam)
|
| 57 |
+
|
| 58 |
+
## Citation
|
| 59 |
+
|
| 60 |
+
If you use this demo or find Florence-2 useful in your research, please cite:
|
| 61 |
+
|
| 62 |
+
```bibtex
|
| 63 |
+
@inproceedings{xiao2024florence,
|
| 64 |
+
title={Florence-2: Advancing a unified representation for a variety of vision tasks},
|
| 65 |
+
author={Xiao, Bin and Wu, Haiping and Xu, Weijian and Dai, Xiyang and Hu, Houdong and Lu, Yumao and Zeng, Michael and Liu, Ce and Yuan, Lu},
|
| 66 |
+
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
|
| 67 |
+
pages={4818--4829},
|
| 68 |
+
year={2024}
|
| 69 |
+
}
|
| 70 |
+
```
|
Tasks.png
ADDED
|
Git LFS Details
|
app.py
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 3 |
+
import spaces
|
| 4 |
+
import torch
|
| 5 |
+
import requests
|
| 6 |
+
import copy
|
| 7 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 8 |
+
import io
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
import matplotlib.patches as patches
|
| 11 |
+
import random
|
| 12 |
+
import numpy as np
|
| 13 |
+
|
| 14 |
+
# import subprocess
|
| 15 |
+
# subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 16 |
+
|
| 17 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 19 |
+
|
| 20 |
+
models = {
|
| 21 |
+
# 'microsoft/Florence-2-large-ft': AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-large-ft', trust_remote_code=True).to(device).eval(),
|
| 22 |
+
# 'microsoft/Florence-2-large': AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-large', trust_remote_code=True).to(device).eval(),
|
| 23 |
+
'microsoft/Florence-2-base-ft': AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base-ft', trust_remote_code=True).to(device).eval(),
|
| 24 |
+
'microsoft/Florence-2-base': AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base", torch_dtype=torch_dtype, trust_remote_code=True).to(device) # AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval(),
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
processors = {
|
| 28 |
+
# 'microsoft/Florence-2-large-ft': AutoProcessor.from_pretrained('microsoft/Florence-2-large-ft', trust_remote_code=True),
|
| 29 |
+
# 'microsoft/Florence-2-large': AutoProcessor.from_pretrained('microsoft/Florence-2-large', trust_remote_code=True),
|
| 30 |
+
'microsoft/Florence-2-base-ft': AutoProcessor.from_pretrained('microsoft/Florence-2-base-ft', trust_remote_code=True),
|
| 31 |
+
'microsoft/Florence-2-base': AutoProcessor.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True) # AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True),
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# Updated DESCRIPTION with the title
|
| 36 |
+
DESCRIPTION = """
|
| 37 |
+
# [Florence-2: Advancing a Unified Representation for a Variety of Vision Tasks](https://arxiv.org/pdf/2311.06242)
|
| 38 |
+
""" # [Florence-2 Demo](https://huggingface.co/microsoft/Florence-2-large)
|
| 39 |
+
|
| 40 |
+
colormap = ['blue','orange','green','purple','brown','pink','gray','olive','cyan','red',
|
| 41 |
+
'lime','indigo','violet','aqua','magenta','coral','gold','tan','skyblue']
|
| 42 |
+
|
| 43 |
+
def fig_to_pil(fig):
|
| 44 |
+
buf = io.BytesIO()
|
| 45 |
+
fig.savefig(buf, format='png')
|
| 46 |
+
buf.seek(0)
|
| 47 |
+
return Image.open(buf)
|
| 48 |
+
|
| 49 |
+
@spaces.GPU
|
| 50 |
+
def run_example(task_prompt, image, text_input=None, model_id='microsoft/Florence-2-base'):
|
| 51 |
+
model = models[model_id]
|
| 52 |
+
processor = processors[model_id]
|
| 53 |
+
if text_input is None:
|
| 54 |
+
prompt = task_prompt
|
| 55 |
+
else:
|
| 56 |
+
prompt = task_prompt + text_input
|
| 57 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
|
| 58 |
+
generated_ids = model.generate(
|
| 59 |
+
input_ids=inputs["input_ids"],
|
| 60 |
+
pixel_values=inputs["pixel_values"],
|
| 61 |
+
max_new_tokens=1024,
|
| 62 |
+
early_stopping=False,
|
| 63 |
+
do_sample=False,
|
| 64 |
+
num_beams=3,
|
| 65 |
+
)
|
| 66 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 67 |
+
parsed_answer = processor.post_process_generation(
|
| 68 |
+
generated_text,
|
| 69 |
+
task=task_prompt,
|
| 70 |
+
image_size=(image.width, image.height)
|
| 71 |
+
)
|
| 72 |
+
return parsed_answer
|
| 73 |
+
|
| 74 |
+
def plot_bbox(image, data):
|
| 75 |
+
fig, ax = plt.subplots()
|
| 76 |
+
ax.imshow(image)
|
| 77 |
+
for bbox, label in zip(data['bboxes'], data['labels']):
|
| 78 |
+
x1, y1, x2, y2 = bbox
|
| 79 |
+
rect = patches.Rectangle((x1, y1), x2-x1, y2-y1, linewidth=1, edgecolor='r', facecolor='none')
|
| 80 |
+
ax.add_patch(rect)
|
| 81 |
+
plt.text(x1, y1, label, color='white', fontsize=8, bbox=dict(facecolor='red', alpha=0.5))
|
| 82 |
+
ax.axis('off')
|
| 83 |
+
return fig
|
| 84 |
+
|
| 85 |
+
def draw_polygons(image, prediction, fill_mask=False):
|
| 86 |
+
draw = ImageDraw.Draw(image)
|
| 87 |
+
scale = 1
|
| 88 |
+
for polygons, label in zip(prediction['polygons'], prediction['labels']):
|
| 89 |
+
color = random.choice(colormap)
|
| 90 |
+
fill_color = random.choice(colormap) if fill_mask else None
|
| 91 |
+
for _polygon in polygons:
|
| 92 |
+
_polygon = np.array(_polygon).reshape(-1, 2)
|
| 93 |
+
if len(_polygon) < 3:
|
| 94 |
+
print('Invalid polygon:', _polygon)
|
| 95 |
+
continue
|
| 96 |
+
_polygon = (_polygon * scale).reshape(-1).tolist()
|
| 97 |
+
if fill_mask:
|
| 98 |
+
draw.polygon(_polygon, outline=color, fill=fill_color)
|
| 99 |
+
else:
|
| 100 |
+
draw.polygon(_polygon, outline=color)
|
| 101 |
+
draw.text((_polygon[0] + 8, _polygon[1] + 2), label, fill=color)
|
| 102 |
+
return image
|
| 103 |
+
|
| 104 |
+
def convert_to_od_format(data):
|
| 105 |
+
bboxes = data.get('bboxes', [])
|
| 106 |
+
labels = data.get('bboxes_labels', [])
|
| 107 |
+
od_results = {
|
| 108 |
+
'bboxes': bboxes,
|
| 109 |
+
'labels': labels
|
| 110 |
+
}
|
| 111 |
+
return od_results
|
| 112 |
+
|
| 113 |
+
def draw_ocr_bboxes(image, prediction):
|
| 114 |
+
scale = 1
|
| 115 |
+
draw = ImageDraw.Draw(image)
|
| 116 |
+
bboxes, labels = prediction['quad_boxes'], prediction['labels']
|
| 117 |
+
for box, label in zip(bboxes, labels):
|
| 118 |
+
color = random.choice(colormap)
|
| 119 |
+
new_box = (np.array(box) * scale).tolist()
|
| 120 |
+
draw.polygon(new_box, width=3, outline=color)
|
| 121 |
+
draw.text((new_box[0]+8, new_box[1]+2),
|
| 122 |
+
"{}".format(label),
|
| 123 |
+
align="right",
|
| 124 |
+
fill=color)
|
| 125 |
+
return image
|
| 126 |
+
|
| 127 |
+
def process_image(image, task_prompt, text_input=None, model_id='microsoft/Florence-2-base'):
|
| 128 |
+
image = Image.fromarray(image) # Convert NumPy array to PIL Image
|
| 129 |
+
if task_prompt == 'Caption':
|
| 130 |
+
task_prompt = '<CAPTION>'
|
| 131 |
+
results = run_example(task_prompt, image, model_id=model_id)
|
| 132 |
+
return results, None
|
| 133 |
+
elif task_prompt == 'Detailed Caption':
|
| 134 |
+
task_prompt = '<DETAILED_CAPTION>'
|
| 135 |
+
results = run_example(task_prompt, image, model_id=model_id)
|
| 136 |
+
return results, None
|
| 137 |
+
elif task_prompt == 'More Detailed Caption':
|
| 138 |
+
task_prompt = '<MORE_DETAILED_CAPTION>'
|
| 139 |
+
results = run_example(task_prompt, image, model_id=model_id)
|
| 140 |
+
return results, None
|
| 141 |
+
elif task_prompt == 'Caption + Grounding':
|
| 142 |
+
task_prompt = '<CAPTION>'
|
| 143 |
+
results = run_example(task_prompt, image, model_id=model_id)
|
| 144 |
+
text_input = results[task_prompt]
|
| 145 |
+
task_prompt = '<CAPTION_TO_PHRASE_GROUNDING>'
|
| 146 |
+
results = run_example(task_prompt, image, text_input, model_id)
|
| 147 |
+
results['<CAPTION>'] = text_input
|
| 148 |
+
fig = plot_bbox(image, results['<CAPTION_TO_PHRASE_GROUNDING>'])
|
| 149 |
+
return results, fig_to_pil(fig)
|
| 150 |
+
elif task_prompt == 'Detailed Caption + Grounding':
|
| 151 |
+
task_prompt = '<DETAILED_CAPTION>'
|
| 152 |
+
results = run_example(task_prompt, image, model_id=model_id)
|
| 153 |
+
text_input = results[task_prompt]
|
| 154 |
+
task_prompt = '<CAPTION_TO_PHRASE_GROUNDING>'
|
| 155 |
+
results = run_example(task_prompt, image, text_input, model_id)
|
| 156 |
+
results['<DETAILED_CAPTION>'] = text_input
|
| 157 |
+
fig = plot_bbox(image, results['<CAPTION_TO_PHRASE_GROUNDING>'])
|
| 158 |
+
return results, fig_to_pil(fig)
|
| 159 |
+
elif task_prompt == 'More Detailed Caption + Grounding':
|
| 160 |
+
task_prompt = '<MORE_DETAILED_CAPTION>'
|
| 161 |
+
results = run_example(task_prompt, image, model_id=model_id)
|
| 162 |
+
text_input = results[task_prompt]
|
| 163 |
+
task_prompt = '<CAPTION_TO_PHRASE_GROUNDING>'
|
| 164 |
+
results = run_example(task_prompt, image, text_input, model_id)
|
| 165 |
+
results['<MORE_DETAILED_CAPTION>'] = text_input
|
| 166 |
+
fig = plot_bbox(image, results['<CAPTION_TO_PHRASE_GROUNDING>'])
|
| 167 |
+
return results, fig_to_pil(fig)
|
| 168 |
+
elif task_prompt == 'Object Detection':
|
| 169 |
+
task_prompt = '<OD>'
|
| 170 |
+
results = run_example(task_prompt, image, model_id=model_id)
|
| 171 |
+
fig = plot_bbox(image, results['<OD>'])
|
| 172 |
+
return results, fig_to_pil(fig)
|
| 173 |
+
elif task_prompt == 'Dense Region Caption':
|
| 174 |
+
task_prompt = '<DENSE_REGION_CAPTION>'
|
| 175 |
+
results = run_example(task_prompt, image, model_id=model_id)
|
| 176 |
+
fig = plot_bbox(image, results['<DENSE_REGION_CAPTION>'])
|
| 177 |
+
return results, fig_to_pil(fig)
|
| 178 |
+
elif task_prompt == 'Region Proposal':
|
| 179 |
+
task_prompt = '<REGION_PROPOSAL>'
|
| 180 |
+
results = run_example(task_prompt, image, model_id=model_id)
|
| 181 |
+
fig = plot_bbox(image, results['<REGION_PROPOSAL>'])
|
| 182 |
+
return results, fig_to_pil(fig)
|
| 183 |
+
elif task_prompt == 'Caption to Phrase Grounding':
|
| 184 |
+
task_prompt = '<CAPTION_TO_PHRASE_GROUNDING>'
|
| 185 |
+
results = run_example(task_prompt, image, text_input, model_id)
|
| 186 |
+
fig = plot_bbox(image, results['<CAPTION_TO_PHRASE_GROUNDING>'])
|
| 187 |
+
return results, fig_to_pil(fig)
|
| 188 |
+
elif task_prompt == 'Referring Expression Segmentation':
|
| 189 |
+
task_prompt = '<REFERRING_EXPRESSION_SEGMENTATION>'
|
| 190 |
+
results = run_example(task_prompt, image, text_input, model_id)
|
| 191 |
+
output_image = copy.deepcopy(image)
|
| 192 |
+
output_image = draw_polygons(output_image, results['<REFERRING_EXPRESSION_SEGMENTATION>'], fill_mask=True)
|
| 193 |
+
return results, output_image
|
| 194 |
+
elif task_prompt == 'Region to Segmentation':
|
| 195 |
+
task_prompt = '<REGION_TO_SEGMENTATION>'
|
| 196 |
+
results = run_example(task_prompt, image, text_input, model_id)
|
| 197 |
+
output_image = copy.deepcopy(image)
|
| 198 |
+
output_image = draw_polygons(output_image, results['<REGION_TO_SEGMENTATION>'], fill_mask=True)
|
| 199 |
+
return results, output_image
|
| 200 |
+
elif task_prompt == 'Open Vocabulary Detection':
|
| 201 |
+
task_prompt = '<OPEN_VOCABULARY_DETECTION>'
|
| 202 |
+
results = run_example(task_prompt, image, text_input, model_id)
|
| 203 |
+
bbox_results = convert_to_od_format(results['<OPEN_VOCABULARY_DETECTION>'])
|
| 204 |
+
fig = plot_bbox(image, bbox_results)
|
| 205 |
+
return results, fig_to_pil(fig)
|
| 206 |
+
elif task_prompt == 'Region to Category':
|
| 207 |
+
task_prompt = '<REGION_TO_CATEGORY>'
|
| 208 |
+
results = run_example(task_prompt, image, text_input, model_id)
|
| 209 |
+
return results, None
|
| 210 |
+
elif task_prompt == 'Region to Description':
|
| 211 |
+
task_prompt = '<REGION_TO_DESCRIPTION>'
|
| 212 |
+
results = run_example(task_prompt, image, text_input, model_id)
|
| 213 |
+
return results, None
|
| 214 |
+
elif task_prompt == 'OCR':
|
| 215 |
+
task_prompt = '<OCR>'
|
| 216 |
+
results = run_example(task_prompt, image, model_id=model_id)
|
| 217 |
+
return results, None
|
| 218 |
+
elif task_prompt == 'OCR with Region':
|
| 219 |
+
task_prompt = '<OCR_WITH_REGION>'
|
| 220 |
+
results = run_example(task_prompt, image, model_id=model_id)
|
| 221 |
+
output_image = copy.deepcopy(image)
|
| 222 |
+
output_image = draw_ocr_bboxes(output_image, results['<OCR_WITH_REGION>'])
|
| 223 |
+
return results, output_image
|
| 224 |
+
else:
|
| 225 |
+
return "", None
|
| 226 |
+
|
| 227 |
+
css = """
|
| 228 |
+
#output {
|
| 229 |
+
height: 500px;
|
| 230 |
+
overflow: auto;
|
| 231 |
+
border: 1px solid #ccc;
|
| 232 |
+
}
|
| 233 |
+
"""
|
| 234 |
+
|
| 235 |
+
single_task_list = [
|
| 236 |
+
'Caption', 'Detailed Caption', 'More Detailed Caption', 'Object Detection',
|
| 237 |
+
'Dense Region Caption', 'Region Proposal', 'Caption to Phrase Grounding',
|
| 238 |
+
'Referring Expression Segmentation', 'Region to Segmentation',
|
| 239 |
+
'Open Vocabulary Detection', 'Region to Category', 'Region to Description',
|
| 240 |
+
'OCR', 'OCR with Region'
|
| 241 |
+
]
|
| 242 |
+
|
| 243 |
+
cascased_task_list = [
|
| 244 |
+
'Caption + Grounding', 'Detailed Caption + Grounding', 'More Detailed Caption + Grounding'
|
| 245 |
+
]
|
| 246 |
+
|
| 247 |
+
def update_task_dropdown(choice):
|
| 248 |
+
if choice == 'Cascased task':
|
| 249 |
+
return gr.Dropdown(choices=cascased_task_list, value='Caption + Grounding')
|
| 250 |
+
else:
|
| 251 |
+
return gr.Dropdown(choices=single_task_list, value='Caption')
|
| 252 |
+
|
| 253 |
+
with gr.Blocks(css=css) as demo:
|
| 254 |
+
gr.Markdown(DESCRIPTION)
|
| 255 |
+
with gr.Row():
|
| 256 |
+
with gr.Column():
|
| 257 |
+
# Add overview.png image display
|
| 258 |
+
gr.Image("Tasks.png", label="Task Overview Image", type="pil", width=444, height=500)
|
| 259 |
+
with gr.Column():
|
| 260 |
+
gr.Image("Data.png", label="Data Engine", type="pil", elem_id="model_image", height=168)
|
| 261 |
+
gr.Image("Model.png", label="Model", type="pil", elem_id="model_image", height=316)
|
| 262 |
+
with gr.Tab(label="Florence-2 Demo"):
|
| 263 |
+
with gr.Row():
|
| 264 |
+
with gr.Column():
|
| 265 |
+
model_selector = gr.Dropdown(choices=list(models.keys()), label="Model", value='microsoft/Florence-2-base')
|
| 266 |
+
task_type = gr.Radio(choices=['Single task', 'Cascased task'], label='Task type selector', value='Single task')
|
| 267 |
+
task_prompt = gr.Dropdown(choices=single_task_list, label="Task Prompt", value="Caption")
|
| 268 |
+
task_type.change(fn=update_task_dropdown, inputs=task_type, outputs=task_prompt)
|
| 269 |
+
text_input = gr.Textbox(label="Text Input (optional)")
|
| 270 |
+
input_img = gr.Image(label="Input Picture")
|
| 271 |
+
submit_btn = gr.Button(value="Submit")
|
| 272 |
+
with gr.Column():
|
| 273 |
+
output_text = gr.Textbox(label="Output Text")
|
| 274 |
+
output_img = gr.Image(label="Output Image")
|
| 275 |
+
|
| 276 |
+
gr.Examples(
|
| 277 |
+
examples=[
|
| 278 |
+
["image1.jpg", 'Object Detection'],
|
| 279 |
+
["image2.jpg", 'OCR with Region']
|
| 280 |
+
],
|
| 281 |
+
inputs=[input_img, task_prompt],
|
| 282 |
+
outputs=[output_text, output_img],
|
| 283 |
+
fn=process_image,
|
| 284 |
+
cache_examples=True,
|
| 285 |
+
label='Try examples'
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
submit_btn.click(process_image, [input_img, task_prompt, text_input, model_selector], [output_text, output_img])
|
| 289 |
+
|
| 290 |
+
demo.launch(debug=True)
|
image-demo.png
ADDED
|
Git LFS Details
|
image1.jpg
ADDED
|
Git LFS Details
|
image2.jpg
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
spaces
|
| 2 |
+
timm
|
| 3 |
+
flask==2.3.3
|
| 4 |
+
werkzeug==2.3.7
|
| 5 |
+
flask-cors==4.0.0
|
| 6 |
+
torch>=2.0.0
|
| 7 |
+
transformers>=4.30.0
|
| 8 |
+
Pillow>=9.0.0
|
| 9 |
+
matplotlib>=3.5.0
|
| 10 |
+
numpy>=1.21.0
|
toeic-part2.jpg
ADDED
|