File size: 2,603 Bytes
fcc5f8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import requests
import json
import base64
from PIL import Image
import io
import argparse

def test_omniparser_api(api_url, image_path):
    """

    Test the OmniParser API by sending an image and displaying the results

    

    Args:

        api_url: URL of the OmniParser API

        image_path: Path to the image file to test

    """
    print(f"Testing OmniParser API at: {api_url}")
    print(f"Using image: {image_path}")
    
    # Upload the image file
    files = {'image': open(image_path, 'rb')}
    
    try:
        # Send request to the API
        response = requests.post(api_url, files=files)
        
        # Check if the request was successful
        if response.status_code == 200:
            # Parse the JSON response
            result = response.json()
            
            if "error" in result:
                print(f"Error: {result['error']}")
                return
            
            # Display the parsed elements
            print("\nParsed Elements:")
            print("---------------")
            
            elements = result["elements"]
            for element in elements:
                print(f"Element {element['id']}:")
                print(f"  Text: {element['text']}")
                print(f"  Caption: {element['caption']}")
                print(f"  Coordinates: {element['coordinates']}")
                print(f"  Interactable: {element['is_interactable']}")
                print(f"  Confidence: {element['confidence']}")
                print("---")
            
            # Display the visualization
            if "visualization" in result:
                print("\nVisualization received. Saving to 'visualization.png'")
                img_data = base64.b64decode(result["visualization"])
                img = Image.open(io.BytesIO(img_data))
                img.save("visualization.png")
                print("Visualization saved to 'visualization.png'")
            
        else:
            print(f"Error: Request failed with status code {response.status_code}")
            print(response.text)
    
    except Exception as e:
        print(f"Error: {str(e)}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Test the OmniParser API")
    parser.add_argument("--api_url", type=str, required=True, help="URL of the OmniParser API")
    parser.add_argument("--image_path", type=str, required=True, help="Path to the image file to test")
    
    args = parser.parse_args()
    test_omniparser_api(args.api_url, args.image_path)