Spaces:
Building
Building
File size: 1,876 Bytes
3951659 |
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 |
import requests
import argparse
import sys
import base64
# Default examples
# api = "http://localhost:8080/2015-03-31/functions/function/invocations"
# file = "./tests/data/boats.jpg"
def arg_parser():
"""Parse arguments"""
# Create an ArgumentParser object
parser = argparse.ArgumentParser(description='Object detection inference via API call')
# Add arguments
parser.add_argument('--api', type=str, help='URL to server API (with endpoint)', required=True)
parser.add_argument('--file', type=str, help='Path to the input image file', required=True)
parser.add_argument('-v', '--verbose', action='store_true', help='Increase output verbosity')
return parser
def main(args=None):
"""Main function"""
args = arg_parser().parse_args(args)
# Use the arguments
if args.verbose:
print(f'Input file: {args.file}')
# Load image
with open(args.file, 'rb') as image_file:
image_data = image_file.read()
# Encode the image data in base64
encoded_image = base64.b64encode(image_data).decode('utf-8')
# Prepare the payload
payload = {
'body': encoded_image
}
# Send request to API
# Option 'files': A dictionary of files to send to the specified url
# response = requests.post(args.api, files={'image': image_data})
# Option 'json': A JSON object to send to the specified url
response = requests.post(args.api, json = payload)
if response.status_code == 200:
print('Detection Results:')
# Process the response
# processed_data = json.loads(response.content)
# print('processed_data', processed_data)
results = response.json()
print("results: ", results)
else:
print(f"Error: {response.status_code}")
print(response.json())
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
|