-
Notifications
You must be signed in to change notification settings - Fork 8
/
adfi_api.py
167 lines (142 loc) · 6.17 KB
/
adfi_api.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import base64
import datetime
import json
import os
from io import BytesIO
import cv2
import numpy as np
import requests
from PIL import Image
class AdfiApi:
def __init__(
self, apikey, aimodel_id, model_type, url, ok_dir, not_clear_dir, ng_dir
):
self.apikey = apikey
self.aimodel_id = aimodel_id
self.model_type = model_type
if url[-1] != "/":
self.url = url + "/"
else:
self.url = url
self.height = 800
self.width = 800
current_day = datetime.datetime.now().strftime("%Y%m%d")
if ok_dir[-1] != "/":
self.ok_dir = ok_dir + "/" + current_day + "/"
else:
self.ok_dir = ok_dir + current_day + "/"
if not_clear_dir[-1] != "/":
self.not_clear_dir = not_clear_dir + "/" + current_day + "/"
else:
self.not_clear_dir = not_clear_dir + current_day + "/"
if ng_dir[-1] != "/":
self.ng_dir = ng_dir + "/" + current_day + "/"
else:
self.ng_dir = ng_dir + current_day + "/"
def inspect_image(self, img_filepath, result_image=True):
img = Image.open(img_filepath).convert("RGB")
# Notice: Please make the image size smaller than 800 pix.
MAX_SIZE = 800
self.height = img.height
self.width = img.width
if img.height > MAX_SIZE:
self.height = MAX_SIZE
if img.width > MAX_SIZE:
self.width = MAX_SIZE
img = img.resize((self.width, self.height), Image.ANTIALIAS)
img_bytes = BytesIO()
img.save(img_bytes, format="PNG")
img_bytes = img_bytes.getvalue()
files = {"image_data": (img_filepath, img_bytes, "image/png")}
data = {
"apikey": self.apikey,
"aimodel_id": self.aimodel_id,
"model_type": self.model_type,
"result_image": result_image,
}
# Send a request.
response = requests.post(self.url, files=files, data=data)
# if the request fails.
if response.status_code != 200:
return None, None
result_json = response.json()
result_image_save_path = None
if result_image:
# Convert a string to a result image
img_binary = base64.b64decode(
result_json["result_image_base64_data"].encode("utf-8")
)
img_array = np.frombuffer(img_binary, dtype=np.uint8)
result_image_np = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
result_image_save_path = os.path.basename(img_filepath)
if "Anomaly" in result_json["result"]:
if not os.path.exists(self.ng_dir):
os.makedirs(self.ng_dir)
result_image_save_path = self.ng_dir + result_image_save_path
elif "Not-clear" in result_json["result"]:
if not os.path.exists(self.not_clear_dir):
os.makedirs(self.not_clear_dir)
result_image_save_path = self.not_clear_dir + result_image_save_path
else:
if not os.path.exists(self.ok_dir):
os.makedirs(self.ok_dir)
result_image_save_path = self.ok_dir + result_image_save_path
# Save the result image
result_image_np = cv2.resize(result_image_np, (img.width, img.height))
cv2.imwrite(result_image_save_path, result_image_np)
return result_json, result_image_save_path
class AdfiLocalModelApi:
model = None
info_dict = None
model_path = None
def __init__(self, model_path, ok_dir, not_clear_dir, ng_dir):
if os.path.isfile("./adfi_local/adfi.py"):
self.model_path = model_path
from adfi_local import adfi
self.model = adfi.AI_Model()
self.model.load(self.model_path)
self.info_dict = self.model.get_info()
print("License expiration date: ", self.info_dict["expiration_date"])
current_day = datetime.datetime.now().strftime("%Y%m%d")
if ok_dir[-1] != "/":
self.ok_dir = ok_dir + "/" + current_day + "/"
else:
self.ok_dir = ok_dir + current_day + "/"
if not_clear_dir[-1] != "/":
self.not_clear_dir = not_clear_dir + "/" + current_day + "/"
else:
self.not_clear_dir = not_clear_dir + current_day + "/"
if ng_dir[-1] != "/":
self.ng_dir = ng_dir + "/" + current_day + "/"
else:
self.ng_dir = ng_dir + current_day + "/"
def inspect_image(self, img_filepath, result_image=True):
prediction_dict = self.model.predict(
img_filepath, get_result_image=result_image
)
result_dict = {
"image_name": os.path.basename(img_filepath),
"result": str(prediction_dict["result"]),
"time": str(datetime.datetime.now()),
"anomaly_score": str(prediction_dict["score"]),
"main_prediction_result": str(prediction_dict["main"]),
"sub_prediction_result": str(prediction_dict["sub"]),
}
result_image_save_path = None
if result_image:
result_image_save_path = os.path.basename(img_filepath)
if "Anomaly" in prediction_dict["result"]:
if not os.path.exists(self.ng_dir):
os.makedirs(self.ng_dir)
result_image_save_path = self.ng_dir + result_image_save_path
elif "Not-clear" in prediction_dict["result"]:
if not os.path.exists(self.not_clear_dir):
os.makedirs(self.not_clear_dir)
result_image_save_path = self.not_clear_dir + result_image_save_path
else:
if not os.path.exists(self.ok_dir):
os.makedirs(self.ok_dir)
result_image_save_path = self.ok_dir + result_image_save_path
# Save the result image
prediction_dict["result_image"].save(result_image_save_path)
return result_dict, result_image_save_path