-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.py
40 lines (33 loc) · 1.5 KB
/
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
39
40
import os
import base64
import json
import requests
import pandas as pd
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--directory', type=str, default='images', help='specify the name of the folder where your images are saved')
parser.add_argument('--url', type=str, default='http://127.0.0.1:5000/predict', help='specify the url if you want')
parser.add_argument('--excel_name', type = str, required=False, help='specify the name of excel file if you want to crrate new excel file with ypur data')
args = parser.parse_args()
url = args.url
main_dict = {"photos":[]}
directory = args.directory
true_labels = []
print("Making predictions... It may take several minutes!")
for label in os.listdir(directory):
for image in os.listdir(os.path.join(directory,label)):
with open(os.path.join(directory,label,image), "rb") as image_file:
encoded_image = base64.b64encode(image_file.read()).decode("utf-8")
true_labels.append(0 if label=='cat' else 1)
dict = {"ID":label+image.split('.')[1],"img_code":encoded_image}
main_dict["photos"].append(dict)
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(main_dict), headers=headers)
df = pd.DataFrame(response.json()['results'])
df['true_label'] = true_labels
df[['cat_prob', 'dog_prob']] = df[['cat_prob', 'dog_prob']].astype(float)
if args.excel_name is None:
df.to_excel("probabilities.xlsx", index = False)
else:
df.to_excel(args.excel_name+".xlsx", index = False)
print("Finished")