-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathsimple_request.py
38 lines (29 loc) · 1.03 KB
/
simple_request.py
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
# encoding: utf-8
"""
@author: xyliao
@contact: [email protected]
"""
import requests
import argparse
# Initialize the PyTorch REST API endpoint URL.
PyTorch_REST_API_URL = 'http://127.0.0.1:5000/predict'
def predict_result(image_path):
# Initialize image path
image = open(image_path, 'rb').read()
payload = {'image': image}
# Submit the request.
r = requests.post(PyTorch_REST_API_URL, files=payload).json()
# Ensure the request was successful.
if r['success']:
# Loop over the predictions and display them.
for (i, result) in enumerate(r['predictions']):
print('{}. {}: {:.4f}'.format(i + 1, result['label'],
result['probability']))
# Otherwise, the request failed.
else:
print('Request failed')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Classification demo')
parser.add_argument('--file', type=str, help='test image file')
args = parser.parse_args()
predict_result(args.file)