Spaces:
Paused
Paused
Upload OmniParser/util/omniparser.py with huggingface_hub
Browse files
OmniParser/util/omniparser.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from util.utils import get_som_labeled_img, get_caption_model_processor, get_yolo_model, check_ocr_box
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import base64
|
| 6 |
+
from typing import Dict
|
| 7 |
+
import os
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
class Omniparser(object):
|
| 11 |
+
def __init__(self, config: Dict):
|
| 12 |
+
self.config = config
|
| 13 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 14 |
+
|
| 15 |
+
self.som_model = get_yolo_model(model_path=config['som_model_path'])
|
| 16 |
+
# Disable caption generation for speed on CPU
|
| 17 |
+
self.caption_model_processor = None
|
| 18 |
+
|
| 19 |
+
# Setup for saving cropped images
|
| 20 |
+
self.save_cropped_images = config.get('save_cropped_images', False)
|
| 21 |
+
self.cropped_images_dir = config.get('cropped_images_dir', 'cropped_images')
|
| 22 |
+
|
| 23 |
+
if self.save_cropped_images:
|
| 24 |
+
Path(self.cropped_images_dir).mkdir(parents=True, exist_ok=True)
|
| 25 |
+
print(f'Cropped images will be saved to: {self.cropped_images_dir}')
|
| 26 |
+
|
| 27 |
+
print('Omniparser initialized!!!')
|
| 28 |
+
|
| 29 |
+
def parse(self, image_base64: str):
|
| 30 |
+
try:
|
| 31 |
+
print("[1] Decoding image...")
|
| 32 |
+
image_bytes = base64.b64decode(image_base64)
|
| 33 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 34 |
+
print(f'[1] Image decoded, size: {image.size}')
|
| 35 |
+
|
| 36 |
+
print("[2] Calculating overlay config...")
|
| 37 |
+
box_overlay_ratio = max(image.size) / 3200
|
| 38 |
+
draw_bbox_config = {
|
| 39 |
+
'text_scale': 0.8 * box_overlay_ratio,
|
| 40 |
+
'text_thickness': max(int(2 * box_overlay_ratio), 1),
|
| 41 |
+
'text_padding': max(int(3 * box_overlay_ratio), 1),
|
| 42 |
+
'thickness': max(int(3 * box_overlay_ratio), 1),
|
| 43 |
+
}
|
| 44 |
+
print("[2] Config ready")
|
| 45 |
+
|
| 46 |
+
print("[3] Checking OCR boxes...")
|
| 47 |
+
(text, ocr_bbox), _ = check_ocr_box(image, display_img=False, output_bb_format='xyxy', easyocr_args={'text_threshold': 0.8}, use_paddleocr=False)
|
| 48 |
+
print(f"[3] OCR complete: {len(text)} text boxes, {len(ocr_bbox) if ocr_bbox else 0} OCR bboxes")
|
| 49 |
+
|
| 50 |
+
print("[4] Getting SOM labeled image...")
|
| 51 |
+
dino_labled_img, label_coordinates, parsed_content_list = get_som_labeled_img(image, self.som_model, BOX_TRESHOLD = self.config['BOX_TRESHOLD'], output_coord_in_ratio=True, ocr_bbox=ocr_bbox,draw_bbox_config=draw_bbox_config, caption_model_processor=self.caption_model_processor, ocr_text=text,use_local_semantics=True, iou_threshold=0.7, scale_img=False, batch_size=128, save_cropped_images=self.save_cropped_images, cropped_images_dir=self.cropped_images_dir)
|
| 52 |
+
print("[4] SOM complete")
|
| 53 |
+
|
| 54 |
+
print("[5] Returning results...")
|
| 55 |
+
return dino_labled_img, parsed_content_list
|
| 56 |
+
except Exception as e:
|
| 57 |
+
import traceback
|
| 58 |
+
print(f"[ERROR] Parse failed: {str(e)}")
|
| 59 |
+
print(traceback.format_exc())
|
| 60 |
+
raise
|