Spaces:
Sleeping
Sleeping
File size: 2,379 Bytes
67f4974 3951659 67f4974 3951659 67f4974 3951659 67f4974 3951659 67f4974 3951659 b58c566 3951659 67f4974 3951659 67f4974 3951659 67f4974 3951659 67f4974 3951659 67f4974 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
"""
Object detection - command line inference via API
"""
import sys
import base64
import argparse
import requests
# 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(
"--model",
type=str,
choices=["detr-resnet-50", "detr-resnet-101", "yolos-tiny", "yolos-small"],
help="Model type",
required=False,
)
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}")
# Retrieve model type
if args.model:
model_name = args.model
else:
model_name = ""
# 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,
"isBase64Encoded": True,
"model": model_name,
}
# 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, timeout=60)
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:]))
|