-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
80 lines (61 loc) · 2.82 KB
/
evaluate.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
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
import os
import json
import requests
import datetime
from dataset import Dataset
from utils import strip_ext, create_report, print_metrics
from tqdm import tqdm
def get_args_parser():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, required=True, help='Path to dataset folder.')
parser.add_argument('--url', type=str, required=True, help='URL or API endpoint.')
parser.add_argument('--output', type=str, default='./results', help='Path to store the results.')
parser.add_argument('--title', type=str, default=None, help='Path to store the results.')
parser.add_argument('--desc', type=str, default=None, help='Path to store the results.')
args = parser.parse_args()
return args
def send_image(url, image_path):
with open(image_path, "rb") as f:
image = f.read()
content_type = 'image/jpeg'
headers = {'content-type': content_type}
response = requests.post(url, data=image, headers=headers)
assert response.status_code == 200, f"Expected response status code of 200, but received {response.status_code}"
response = json.loads(response.text)
return response
def main(args):
dataset = Dataset(args.dataset)
time = datetime.datetime.now()
timestamp = f"{time.year}_{time.month}_{time.day}_{time.hour}_{time.minute}_{time.second}"
folder_output = os.path.join(args.output, f"{timestamp}_{dataset.dataset_name}_{args.title}")
os.makedirs(folder_output)
folder_predictions = os.path.join(folder_output, "predictions")
os.makedirs(folder_predictions)
for i in tqdm(range(dataset.num_of_images)):
image_path = dataset.get_image_path(i)
response = send_image(args.url, image_path)
predictions = ""
for bbox in response:
class_name = str(bbox['class'])
confidence = float(bbox['confidence'])
xmin = int(bbox['xmin'])
ymin = int(bbox['ymin'])
xmax = int(bbox['xmax'])
ymax = int(bbox['ymax'])
prediction = f"{class_name} {confidence} {xmin} {ymin} {xmax} {ymax}\n"
predictions += prediction
prediction_file_name = strip_ext(os.path.basename(image_path)) + '.txt'
prediction_path = os.path.join(folder_predictions, prediction_file_name)
with open(prediction_path, "w") as f:
f.write(predictions)
report_file_name = "report.md"
report_path = os.path.join(folder_output, report_file_name)
metrics = dataset.calculate_mAP(folder_predictions)
print_metrics(metrics)
create_report(dataset, report_path, metrics, args.title, args.desc)
print(f"Evaluation result saved to {folder_output}")
if __name__ == '__main__':
args = get_args_parser()
print(args)
main(args)