Upload OmniParser/omnitool/omniparserserver/omniparserserver.py with huggingface_hub
Browse files
OmniParser/omnitool/omniparserserver/omniparserserver.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''
|
| 2 |
+
python -m omniparserserver --som_model_path ../../weights/icon_detect/model.pt --caption_model_name florence2 --caption_model_path ../../weights/icon_caption_florence --device cuda --BOX_TRESHOLD 0.05
|
| 3 |
+
'''
|
| 4 |
+
|
| 5 |
+
import sys
|
| 6 |
+
import os
|
| 7 |
+
import time
|
| 8 |
+
import json
|
| 9 |
+
import base64
|
| 10 |
+
import tempfile
|
| 11 |
+
import shutil
|
| 12 |
+
import cv2
|
| 13 |
+
import numpy as np
|
| 14 |
+
from pathlib import Path
|
| 15 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
| 16 |
+
from fastapi.responses import JSONResponse, FileResponse
|
| 17 |
+
from pydantic import BaseModel
|
| 18 |
+
import argparse
|
| 19 |
+
import uvicorn
|
| 20 |
+
from PIL import Image
|
| 21 |
+
import io
|
| 22 |
+
|
| 23 |
+
root_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 24 |
+
sys.path.append(root_dir)
|
| 25 |
+
from util.omniparser import Omniparser
|
| 26 |
+
|
| 27 |
+
def parse_arguments():
|
| 28 |
+
parser = argparse.ArgumentParser(description='Omniparser API')
|
| 29 |
+
parser.add_argument('--som_model_path', type=str, default='weights/icon_detect/model.pt', help='Path to the som model')
|
| 30 |
+
parser.add_argument('--caption_model_name', type=str, default=None, help='Name of the caption model')
|
| 31 |
+
parser.add_argument('--caption_model_path', type=str, default='weights/icon_caption_florence', help='Path to the caption model')
|
| 32 |
+
parser.add_argument('--device', type=str, default='cpu', help='Device to run the model')
|
| 33 |
+
parser.add_argument('--BOX_TRESHOLD', type=float, default=0.05, help='Threshold for box detection')
|
| 34 |
+
parser.add_argument('--host', type=str, default='127.0.0.1', help='Host for the API')
|
| 35 |
+
parser.add_argument('--port', type=int, default=8000, help='Port for the API')
|
| 36 |
+
parser.add_argument('--save_cropped_images', action='store_true', help='Save cropped UI images to a folder')
|
| 37 |
+
parser.add_argument('--cropped_images_dir', type=str, default='cropped_images', help='Directory to save cropped images')
|
| 38 |
+
args = parser.parse_args()
|
| 39 |
+
return args
|
| 40 |
+
|
| 41 |
+
args = parse_arguments()
|
| 42 |
+
config = vars(args)
|
| 43 |
+
|
| 44 |
+
# Convert relative paths to absolute paths
|
| 45 |
+
if not os.path.isabs(config['som_model_path']):
|
| 46 |
+
config['som_model_path'] = os.path.normpath(os.path.join(root_dir, config['som_model_path']))
|
| 47 |
+
if not os.path.isabs(config['caption_model_path']):
|
| 48 |
+
config['caption_model_path'] = os.path.normpath(os.path.join(root_dir, config['caption_model_path']))
|
| 49 |
+
|
| 50 |
+
app = FastAPI()
|
| 51 |
+
omniparser = Omniparser(config)
|
| 52 |
+
|
| 53 |
+
class ParseRequest(BaseModel):
|
| 54 |
+
base64_image: str
|
| 55 |
+
|
| 56 |
+
@app.post("/parse/")
|
| 57 |
+
async def parse(parse_request: ParseRequest):
|
| 58 |
+
print('start parsing...')
|
| 59 |
+
start = time.time()
|
| 60 |
+
dino_labled_img, parsed_content_list = omniparser.parse(parse_request.base64_image)
|
| 61 |
+
latency = time.time() - start
|
| 62 |
+
print('time:', latency)
|
| 63 |
+
return {"som_image_base64": dino_labled_img, "parsed_content_list": parsed_content_list, 'latency': latency}
|
| 64 |
+
|
| 65 |
+
@app.get("/probe/")
|
| 66 |
+
async def root():
|
| 67 |
+
return {"message": "Omniparser API ready"}
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
uvicorn.run(app, host=args.host, port=args.port, reload=False)
|