-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_api.py
More file actions
86 lines (77 loc) · 3.17 KB
/
test_api.py
File metadata and controls
86 lines (77 loc) · 3.17 KB
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
import logging
import requests
import argparse
import tqdm
import json
import os
import socket
import sys
from IPython.display import clear_output
from pathlib import Path
FILE = Path(__file__).resolve()
ROOT = FILE.parents[0]
if str(ROOT) not in sys.path:
sys.path.append(str(ROOT)) # add ROOT to PATH
ROOT = Path(os.path.abspath(ROOT / Path.cwd())) # relative
hostname = socket.gethostname()
IP_ADDRESS = socket.gethostbyname(hostname)
def parse_arg():
parser = argparse.ArgumentParser()
parser.add_argument('--local_host', type=str, help='your local host connection', default=IP_ADDRESS)
parser.add_argument('--port', type=str, help='your port connection', default='8000')
parser.add_argument('--json_results', type=str, help='your json path results',
default=ROOT / 'results/results.json')
parser.add_argument('--url', type=str, help='link API', default='http://10.40.2.215:8000/id-card-yolo/detect/')
parser.add_argument('--source', type=str, help='folder image test', default=ROOT / 'image')
# parser.add_argument('--folder', type=str, help='folder image test', default=ROOT / 'image')
# parser.add_argument('--image', type=str, help='image path', default=os.path.join(ROOT, 'image/long.jpg'))
return parser.parse_args()
def main():
args = parse_arg()
response = {}
listResults = []
url = 'http://' + args.local_host + ':' + args.port + '/id-card-yolo/detect/'
# with open(args.image, "rb") as image_file:
# data = base64.b64encode(image_file.read()).decode('utf-8')
option = input('Do you want to show encoded image[Y/N]: ')
if os.path.isdir(args.source):
for image in tqdm.tqdm(os.listdir(args.source), total=len(args.source)):
file = {'image': open(os.path.join(args.source, image), 'rb')}
data = {'option': option}
response = requests.post(url=url, files=file, params=data)
if args.url:
response = requests.post(url=args.url, files=file, params=data)
clear_output(wait=True)
listResults.append(response.json())
print(response.json(), '\n')
with open(args.json_results, 'w') as fileSave:
json.dump(listResults, fileSave, indent=4)
elif os.path.isfile(args.source):
file = {'image': open(args.source, 'rb')}
data = {'option': option}
response = requests.post(url=url, files=file, params=data)
with open(args.json_results, 'w') as fileSave:
fileSave.seek(0)
json.dump(response.json(), fileSave, indent=4)
fileSave.truncate()
# fileSave.write(json_object)
# response = requests.post(
# url='http://10.40.2.223:8000/yolo-id-card/request/',
# data=str(data)
# )
# image_name = os.path.basename(args.image)
# response = requests.post(
# url='http://10.40.2.223:8000/id-card-yolo/detect/',
# data={
# "image": data,
# "image_name": image_name
# }
# )
#
# # Print output
# print(requests.status_codes, response.json())
if __name__ == "__main__":
try:
main()
except Exception as e:
logging.error(e)