Clement Vachet commited on
Commit
3951659
·
1 Parent(s): a855e72

Add inference / prediction file

Browse files
Files changed (1) hide show
  1. inference_api.py +62 -0
inference_api.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import argparse
3
+ import sys
4
+ import base64
5
+
6
+ # Default examples
7
+ # api = "http://localhost:8080/2015-03-31/functions/function/invocations"
8
+ # file = "./tests/data/boats.jpg"
9
+
10
+
11
+ def arg_parser():
12
+ """Parse arguments"""
13
+
14
+ # Create an ArgumentParser object
15
+ parser = argparse.ArgumentParser(description='Object detection inference via API call')
16
+ # Add arguments
17
+ parser.add_argument('--api', type=str, help='URL to server API (with endpoint)', required=True)
18
+ parser.add_argument('--file', type=str, help='Path to the input image file', required=True)
19
+ parser.add_argument('-v', '--verbose', action='store_true', help='Increase output verbosity')
20
+ return parser
21
+
22
+
23
+ def main(args=None):
24
+ """Main function"""
25
+
26
+ args = arg_parser().parse_args(args)
27
+ # Use the arguments
28
+ if args.verbose:
29
+ print(f'Input file: {args.file}')
30
+
31
+ # Load image
32
+ with open(args.file, 'rb') as image_file:
33
+ image_data = image_file.read()
34
+
35
+ # Encode the image data in base64
36
+ encoded_image = base64.b64encode(image_data).decode('utf-8')
37
+
38
+ # Prepare the payload
39
+ payload = {
40
+ 'body': encoded_image
41
+ }
42
+
43
+ # Send request to API
44
+ # Option 'files': A dictionary of files to send to the specified url
45
+ # response = requests.post(args.api, files={'image': image_data})
46
+ # Option 'json': A JSON object to send to the specified url
47
+ response = requests.post(args.api, json = payload)
48
+
49
+ if response.status_code == 200:
50
+ print('Detection Results:')
51
+ # Process the response
52
+ # processed_data = json.loads(response.content)
53
+ # print('processed_data', processed_data)
54
+ results = response.json()
55
+ print("results: ", results)
56
+ else:
57
+ print(f"Error: {response.status_code}")
58
+ print(response.json())
59
+
60
+
61
+ if __name__ == "__main__":
62
+ sys.exit(main(sys.argv[1:]))