|
| 1 | +""" |
| 2 | +This module provides a command-line interface to interact with the OmniParser Gradio server. |
| 3 | +
|
| 4 | +Usage: |
| 5 | + python client.py "http://<server_ip>:7861" "path/to/image.jpg" |
| 6 | +""" |
| 7 | + |
| 8 | +import fire |
| 9 | +from gradio_client import Client |
| 10 | +from loguru import logger |
| 11 | +from PIL import Image |
| 12 | +import base64 |
| 13 | +from io import BytesIO |
| 14 | +import os |
| 15 | +import shutil |
| 16 | + |
| 17 | +def predict(server_url: str, image_path: str, box_threshold: float = 0.05, iou_threshold: float = 0.1): |
| 18 | + """ |
| 19 | + Makes a prediction using the OmniParser Gradio client with the provided server URL and image. |
| 20 | +
|
| 21 | + Args: |
| 22 | + server_url (str): The URL of the OmniParser Gradio server. |
| 23 | + image_path (str): Path to the image file to be processed. |
| 24 | + box_threshold (float): Box threshold value (default: 0.05). |
| 25 | + iou_threshold (float): IOU threshold value (default: 0.1). |
| 26 | + """ |
| 27 | + client = Client(server_url) |
| 28 | + |
| 29 | + # Load and encode the image |
| 30 | + with open(image_path, "rb") as image_file: |
| 31 | + encoded_image = base64.b64encode(image_file.read()).decode("utf-8") |
| 32 | + |
| 33 | + # Prepare the image input in the format expected by the server |
| 34 | + image_input = { |
| 35 | + "path": None, |
| 36 | + "url": f"data:image/png;base64,{encoded_image}", |
| 37 | + "size": None, |
| 38 | + "orig_name": image_path, |
| 39 | + "mime_type": "image/png", |
| 40 | + "is_stream": False, |
| 41 | + "meta": {} |
| 42 | + } |
| 43 | + |
| 44 | + # Make the prediction |
| 45 | + try: |
| 46 | + result = client.predict( |
| 47 | + image_input, # image input as dictionary |
| 48 | + box_threshold, # box_threshold |
| 49 | + iou_threshold, # iou_threshold |
| 50 | + api_name="/process" |
| 51 | + ) |
| 52 | + |
| 53 | + # Process and log the results |
| 54 | + output_image, parsed_content = result |
| 55 | + |
| 56 | + logger.info("Prediction completed successfully") |
| 57 | + logger.info(f"Parsed content:\n{parsed_content}") |
| 58 | + |
| 59 | + # Save the output image |
| 60 | + output_image_path = "output_image.png" |
| 61 | + if isinstance(output_image, dict) and 'url' in output_image: |
| 62 | + # Handle base64 encoded image |
| 63 | + img_data = base64.b64decode(output_image['url'].split(',')[1]) |
| 64 | + with open(output_image_path, 'wb') as f: |
| 65 | + f.write(img_data) |
| 66 | + elif isinstance(output_image, str): |
| 67 | + if output_image.startswith('data:image'): |
| 68 | + # Handle base64 encoded image string |
| 69 | + img_data = base64.b64decode(output_image.split(',')[1]) |
| 70 | + with open(output_image_path, 'wb') as f: |
| 71 | + f.write(img_data) |
| 72 | + elif os.path.exists(output_image): |
| 73 | + # Handle file path |
| 74 | + shutil.copy(output_image, output_image_path) |
| 75 | + else: |
| 76 | + logger.warning(f"Unexpected output_image format: {output_image}") |
| 77 | + elif isinstance(output_image, Image.Image): |
| 78 | + output_image.save(output_image_path) |
| 79 | + else: |
| 80 | + logger.warning(f"Unexpected output_image format: {type(output_image)}") |
| 81 | + logger.warning(f"Output image content: {output_image[:100]}...") # Log the first 100 characters |
| 82 | + |
| 83 | + if os.path.exists(output_image_path): |
| 84 | + logger.info(f"Output image saved to: {output_image_path}") |
| 85 | + else: |
| 86 | + logger.warning(f"Failed to save output image to: {output_image_path}") |
| 87 | + |
| 88 | + except Exception as e: |
| 89 | + logger.error(f"An error occurred: {str(e)}") |
| 90 | + logger.exception("Traceback:") |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + fire.Fire(predict) |
| 94 | + |
0 commit comments