Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,8 @@ import streamlit as st #Web App
|
|
| 2 |
from PIL import Image, ImageOps #Image Processing
|
| 3 |
import time
|
| 4 |
from unittest import result
|
| 5 |
-
from pythainlp.util import isthai
|
| 6 |
-
import numpy as np
|
| 7 |
from icevision.all import *
|
| 8 |
from icevision.models.checkpoint import *
|
| 9 |
-
import easyocr as ocr #OCR
|
| 10 |
-
import editdistance
|
| 11 |
|
| 12 |
|
| 13 |
st.sidebar.image("./logo.png")
|
|
@@ -17,16 +13,17 @@ def load_image(image_file):
|
|
| 17 |
return img
|
| 18 |
|
| 19 |
|
| 20 |
-
activities = ["Detection", "About"]
|
| 21 |
-
choice = st.sidebar.selectbox("Select option..",activities)
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
def img_resize(input_path,img_size): # padding
|
| 25 |
desired_size = img_size
|
| 26 |
im = Image.open(input_path)
|
| 27 |
im = ImageOps.exif_transpose(im) # fix image rotating
|
| 28 |
width, height = im.size # get img_input size
|
| 29 |
-
if (width ==
|
| 30 |
new_im = im
|
| 31 |
else:
|
| 32 |
#im = im.convert('L') #Convert to gray
|
|
@@ -61,23 +58,25 @@ device=next(model.parameters()).device
|
|
| 61 |
img_size = checkpoint_and_model["img_size"]
|
| 62 |
valid_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(img_size), tfms.A.Normalize()])
|
| 63 |
|
|
|
|
|
|
|
| 64 |
def get_detection(img_path):
|
| 65 |
|
| 66 |
#Get_Idcard_detail(file_path=img_path)
|
| 67 |
img = Image.open(img_path)
|
| 68 |
img = ImageOps.exif_transpose(img) # fix image rotating
|
| 69 |
width, height = img.size # get img_input size
|
| 70 |
-
if (width ==
|
| 71 |
pred_dict = model_type.end2end_detect(img, valid_tfms, model, class_map=class_map, detection_threshold=0.6)
|
| 72 |
else:
|
| 73 |
#im = im.convert('L') #Convert to gray
|
| 74 |
old_size = img.size # old_size[0] is in (width, height) format
|
| 75 |
-
ratio = float(
|
| 76 |
new_size = tuple([int(x*ratio) for x in old_size])
|
| 77 |
img = img.resize(new_size, Image.ANTIALIAS)
|
| 78 |
-
new_im = Image.new("RGB", (
|
| 79 |
-
new_im.paste(img, ((
|
| 80 |
-
(
|
| 81 |
pred_dict = model_type.end2end_detect(new_im, valid_tfms, model, class_map=class_map, detection_threshold=0.6)
|
| 82 |
|
| 83 |
|
|
@@ -91,9 +90,10 @@ def get_detection(img_path):
|
|
| 91 |
acc = acc * 100
|
| 92 |
if labels == "Neg":
|
| 93 |
labels = "Negative"
|
|
|
|
| 94 |
elif labels == "Pos":
|
| 95 |
labels = "Positive"
|
| 96 |
-
|
| 97 |
except IndexError:
|
| 98 |
st.error("Not found ATK image! ; try to take image again..")
|
| 99 |
labels = "None"
|
|
@@ -105,314 +105,30 @@ def get_img_detection(img_path):
|
|
| 105 |
img = Image.open(img_path)
|
| 106 |
img = ImageOps.exif_transpose(img) # fix image rotating
|
| 107 |
width, height = img.size # get img_input size
|
| 108 |
-
if (width ==
|
| 109 |
new_im = img
|
| 110 |
else:
|
| 111 |
#im = im.convert('L') #Convert to gray
|
| 112 |
old_size = img.size # old_size[0] is in (width, height) format
|
| 113 |
-
ratio = float(
|
| 114 |
new_size = tuple([int(x*ratio) for x in old_size])
|
| 115 |
img = img.resize(new_size, Image.ANTIALIAS)
|
| 116 |
-
new_im = Image.new("RGB", (
|
| 117 |
-
new_im.paste(img, ((
|
| 118 |
-
(
|
| 119 |
|
| 120 |
pred_dict = model_type.end2end_detect(new_im, valid_tfms, model, class_map=class_map, detection_threshold=0.6)
|
| 121 |
|
| 122 |
|
| 123 |
return pred_dict['img']
|
| 124 |
|
| 125 |
-
def load_model():
|
| 126 |
-
reader = ocr.Reader(['en'],model_storage_directory='.')
|
| 127 |
-
return reader
|
| 128 |
-
|
| 129 |
-
reader = load_model() #load model
|
| 130 |
-
|
| 131 |
-
def Get_Idcard_detail(file_path):
|
| 132 |
-
raw_data = []
|
| 133 |
-
id_num = {"id_num" : "None"}
|
| 134 |
-
name = file_path
|
| 135 |
-
img = Image.open(name)
|
| 136 |
-
img = ImageOps.exif_transpose(img) # fix image rotating
|
| 137 |
-
|
| 138 |
-
width, height = img.size # get img_input size
|
| 139 |
-
if (width == 1280) and (height == 1280):
|
| 140 |
-
result = reader.readtext(np.array(img))
|
| 141 |
-
else:
|
| 142 |
-
#im = im.convert('L') #Convert to gray
|
| 143 |
-
old_size = img.size # old_size[0] is in (width, height) format
|
| 144 |
-
ratio = float(1280)/max(old_size)
|
| 145 |
-
new_size = tuple([int(x*ratio) for x in old_size])
|
| 146 |
-
img = img.resize(new_size, Image.ANTIALIAS)
|
| 147 |
-
new_im = Image.new("RGB", (1280, 1280))
|
| 148 |
-
new_im.paste(img, ((1280-new_size[0])//2,
|
| 149 |
-
(1280-new_size[1])//2))
|
| 150 |
-
|
| 151 |
-
result = reader.readtext(np.array(new_im))
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
result_text = [] #empty list for results
|
| 157 |
-
for text in result:
|
| 158 |
-
result_text.append(text[1])
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
raw_data = result_text
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
def get_english(raw_list): # Cut only english var
|
| 166 |
-
eng_name = []
|
| 167 |
-
thai_name = []
|
| 168 |
-
|
| 169 |
-
for name in raw_list:
|
| 170 |
-
if isthai(name) == True:
|
| 171 |
-
thai_name.append(name)
|
| 172 |
-
else:
|
| 173 |
-
eng_name.append(name)
|
| 174 |
|
| 175 |
-
return eng_name
|
| 176 |
|
| 177 |
-
raw_data = get_english(raw_data)
|
| 178 |
|
| 179 |
|
| 180 |
-
def Clear_syntax(raw_list):
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
for k in range(len(Clean_syntax)):
|
| 185 |
-
while (Clean_syntax[k] in raw_list): # remove single symbol
|
| 186 |
-
raw_list.remove(Clean_syntax[k])
|
| 187 |
-
|
| 188 |
-
for l in range(len(raw_list)):
|
| 189 |
-
raw_list[l] = raw_list[l].replace("!","l") #split ! --> l (Error OCR Check)
|
| 190 |
-
raw_list[l] = raw_list[l].replace(",",".") #split ! --> l (Error OCR Check)
|
| 191 |
-
raw_list[l] = raw_list[l].replace(" ","") #split " " out from str
|
| 192 |
-
raw_list[l] = raw_list[l].lower() #Set all string to lowercase
|
| 193 |
-
|
| 194 |
-
for m in range(len(raw_list)): #Clear symbol in str "Hi/'" --> "Hi"
|
| 195 |
-
for n in range(len(Clean_syntax)):
|
| 196 |
-
raw_list[m] = raw_list[m].replace(Clean_syntax[n],"")
|
| 197 |
-
return raw_list
|
| 198 |
-
|
| 199 |
-
raw_data = Clear_syntax(raw_data)
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
def get_idnum(raw_list):
|
| 203 |
-
id_num = {"id_num" : "None"}
|
| 204 |
-
# 1. normal check
|
| 205 |
-
for i in range(len(raw_list)): # check if len(list) = 1, 4, 5, 2, 1 (13 digit idcard) and all is int
|
| 206 |
-
try:
|
| 207 |
-
if ((len(raw_list[i]) == 1) and (len(raw_list[i+1]) == 4) and (len(raw_list[i+2]) == 5) and (len(raw_list[i+3]) == 2) and (len(raw_list[i+4]) == 1)) and ((raw_list[i] + raw_list[i+1] + raw_list[i+2] + raw_list[i+3] + raw_list[i+4]).isnumeric()):
|
| 208 |
-
id_num["id_num"] = (raw_list[i] + raw_list[i+1] + raw_list[i+2] + raw_list[i+3] + raw_list[i+4])
|
| 209 |
-
break
|
| 210 |
-
except:
|
| 211 |
-
pass
|
| 212 |
-
|
| 213 |
-
# 2. Hardcore Check
|
| 214 |
-
if id_num["id_num"] == "None":
|
| 215 |
-
id_count = 0
|
| 216 |
-
index_first = 0
|
| 217 |
-
index_end = 0
|
| 218 |
-
for i in range(len(raw_list)):
|
| 219 |
-
if id_count == 13:
|
| 220 |
-
index_end = i-1 #ลบ 1 index เพราะ ครบ 13 รอบก่อนหน้านี้
|
| 221 |
-
#print(f"index_first == {index_first} index_end == {index_end}")
|
| 222 |
-
#print(f"id = {raw_list[index_first:index_end+1]}")
|
| 223 |
-
id_num["id_num"] = ''.join(raw_list[index_first:index_end+1])
|
| 224 |
-
break
|
| 225 |
-
else:
|
| 226 |
-
if raw_list[i].isnumeric() == True and index_first == 0:
|
| 227 |
-
id_count += len(raw_list[i])
|
| 228 |
-
index_first = i
|
| 229 |
-
elif raw_list[i].isnumeric() == True and index_first != 0:
|
| 230 |
-
id_count += len(raw_list[i])
|
| 231 |
-
elif raw_list[i].isnumeric() == False:
|
| 232 |
-
id_count = 0
|
| 233 |
-
index_first = 0
|
| 234 |
-
|
| 235 |
-
return id_num
|
| 236 |
-
|
| 237 |
-
id_num = (get_idnum(raw_data))
|
| 238 |
-
|
| 239 |
-
#Complete list name check
|
| 240 |
-
def list_name_check(raw_list):
|
| 241 |
-
sum_list = raw_list
|
| 242 |
-
name_key = ['name', 'lastname']
|
| 243 |
-
|
| 244 |
-
#1. name_key check
|
| 245 |
-
if ("name" in sum_list) and ("lastname" in sum_list): # if name and lastname in list pass it!
|
| 246 |
-
pass
|
| 247 |
-
else:
|
| 248 |
-
for i in range(len(name_key)):
|
| 249 |
-
for j in range(len(sum_list)):
|
| 250 |
-
if (editdistance.eval(name_key[i], sum_list[j]) <= 2 ):
|
| 251 |
-
sum_list[j] = name_key[i]
|
| 252 |
-
|
| 253 |
-
gender_key = ["mr.", "mrs.", 'master', 'miss']
|
| 254 |
-
#2 gender_key check
|
| 255 |
-
count = 0 # check for break
|
| 256 |
-
for i in range(len(gender_key)):
|
| 257 |
-
for j in range(len(sum_list)):
|
| 258 |
-
if (count == 0):
|
| 259 |
-
try:
|
| 260 |
-
if (sum_list[i] == "name") or (sum_list[i] == "lastname"): # skip "name" and "lastname"
|
| 261 |
-
pass
|
| 262 |
-
else:
|
| 263 |
-
# mr, mrs sensitive case double check with len(gender_key) == len(keyword)
|
| 264 |
-
if (gender_key[i] == "mr." or gender_key[i] == "mrs.") and (editdistance.eval(gender_key[i], sum_list[j]) <= 3 and (len(gender_key[i]) == len(sum_list[j]))):
|
| 265 |
-
sum_list[j] = gender_key[i]
|
| 266 |
-
count+=1
|
| 267 |
-
#print(1)
|
| 268 |
-
elif (gender_key[i] == "master" or gender_key[i] == "miss") and (editdistance.eval(gender_key[i], sum_list[j]) <= 3 ) and (len(gender_key[i]) == len(sum_list[j])):
|
| 269 |
-
sum_list[j] = gender_key[i]
|
| 270 |
-
count+=1
|
| 271 |
-
#print(1)
|
| 272 |
-
except:
|
| 273 |
-
if (gender_key[i] == "mr." or gender_key[i] == "mrs.") and (editdistance.eval(gender_key[i], sum_list[j]) <= 2 and (len(gender_key[i]) == len(sum_list[j]))):
|
| 274 |
-
sum_list[j] = gender_key[i]
|
| 275 |
-
count+=1
|
| 276 |
-
#print(1)
|
| 277 |
-
elif (gender_key[i] == "master" or gender_key[i] == "miss") and (editdistance.eval(gender_key[i], sum_list[j]) <= 3 ) and (len(gender_key[i]) == len(sum_list[j])):
|
| 278 |
-
sum_list[j] = gender_key[i]
|
| 279 |
-
count+=1
|
| 280 |
-
#print(1)
|
| 281 |
-
else:
|
| 282 |
-
break
|
| 283 |
-
|
| 284 |
-
return sum_list
|
| 285 |
-
|
| 286 |
-
raw_data = list_name_check(raw_data)
|
| 287 |
-
|
| 288 |
-
#get_eng_name
|
| 289 |
-
def get_engname(raw_list):
|
| 290 |
-
get_data = raw_list
|
| 291 |
-
engname_list = []
|
| 292 |
-
|
| 293 |
-
name_pos = []
|
| 294 |
-
lastname_pos = []
|
| 295 |
-
mr_pos = []
|
| 296 |
-
mrs_pos = []
|
| 297 |
-
|
| 298 |
-
# check keyword by name, lastname, master, mr, miss, mrs
|
| 299 |
-
for j in range(len(get_data)): #get "name" , "lastname" index
|
| 300 |
-
if "name" == get_data[j]:
|
| 301 |
-
name_pos.append(j)
|
| 302 |
-
elif "lastname" == get_data[j]:
|
| 303 |
-
lastname_pos.append(j)
|
| 304 |
-
elif ("mr." == get_data[j]) or ("master" == get_data[j]):
|
| 305 |
-
mr_pos.append(j)
|
| 306 |
-
elif ("miss" == get_data[j]) or ("mrs." == get_data[j]):
|
| 307 |
-
mrs_pos.append(j)
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
if len(name_pos) != 0: #get_engname ex --> ['name', 'master', 'tanaanan', 'lastname', 'chalermpan']
|
| 311 |
-
engname_list = get_data[name_pos[0]:name_pos[0]+6] # select first index กรณีมี "name" มากกว่า 1 ตัว
|
| 312 |
-
elif len(lastname_pos) != 0:
|
| 313 |
-
engname_list = get_data[lastname_pos[0]-3:lastname_pos[0]+3]
|
| 314 |
-
elif len(mr_pos) != 0:
|
| 315 |
-
engname_list = get_data[mr_pos[0]-1:mr_pos[0]+5]
|
| 316 |
-
elif len(mrs_pos) != 0:
|
| 317 |
-
engname_list = get_data[mrs_pos[0]-1:mrs_pos[0]+5]
|
| 318 |
-
else:
|
| 319 |
-
print("Can't find eng name!!")
|
| 320 |
-
|
| 321 |
-
return engname_list
|
| 322 |
-
|
| 323 |
-
raw_data = get_engname(raw_data)
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
def split_genkey(raw_list): # remove stringname + gender_key ex. "missjate" -> "jate"
|
| 329 |
-
data = raw_list
|
| 330 |
-
key = ['mrs.','mr.','master','miss']
|
| 331 |
-
name = "" #gen_key name
|
| 332 |
-
name_pos = 0
|
| 333 |
-
gen_index = 0
|
| 334 |
-
gen_type = "" #male / female
|
| 335 |
-
# check keyword
|
| 336 |
-
for key_val in key:
|
| 337 |
-
for each_text in data:
|
| 338 |
-
if (each_text[:len(key_val)] == key_val) or (editdistance.eval(each_text[:len(key_val)],key_val) <= 1 and (len(each_text[:len(key_val)]) == len(key_val))):
|
| 339 |
-
#each_text = each_text[len(key):]
|
| 340 |
-
if (each_text == "name") or (each_text == "lastname"):
|
| 341 |
-
pass
|
| 342 |
-
else:
|
| 343 |
-
name = (each_text[:len(key_val)])
|
| 344 |
-
name_pos = data.index(each_text) # get_index
|
| 345 |
-
gen_index = len(key_val)
|
| 346 |
-
break
|
| 347 |
-
if (name_pos != 0):
|
| 348 |
-
data[name_pos] = data[name_pos][gen_index:] # split gender_key on list
|
| 349 |
-
for empty_str in range(data.count('')): # clear "empty string"
|
| 350 |
-
data.remove('')
|
| 351 |
-
return data
|
| 352 |
-
|
| 353 |
-
raw_data = split_genkey(raw_data)
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
def clean_name_data(raw_list): # delete all single string and int string
|
| 357 |
-
for k in range(len(raw_list)):
|
| 358 |
-
try:
|
| 359 |
-
while ((len(raw_list[k]) <= 2) or (raw_list[k].isnumeric() == True)): # remove single symbol
|
| 360 |
-
raw_list.remove(raw_list[k])
|
| 361 |
-
except IndexError:
|
| 362 |
-
pass
|
| 363 |
-
return raw_list
|
| 364 |
-
|
| 365 |
-
raw_data = clean_name_data(raw_data)
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
def name_sum(raw_list):
|
| 369 |
-
info = {"name" : "None",
|
| 370 |
-
"lastname" : "None"}
|
| 371 |
-
key = ['mr.','mrs.', 'master', 'miss', 'mrs','mr']
|
| 372 |
-
name_pos = 0
|
| 373 |
-
lastname_pos = 0
|
| 374 |
-
for key_val in key: # remove gender_key in string
|
| 375 |
-
if key_val in raw_list:
|
| 376 |
-
raw_list.remove(key_val)
|
| 377 |
-
try:
|
| 378 |
-
for i in range(len(raw_list)):
|
| 379 |
-
if raw_list[i] == "name":
|
| 380 |
-
info["name"] = raw_list[i+1]
|
| 381 |
-
name_pos = i+1
|
| 382 |
-
elif raw_list[i] == "lastname":
|
| 383 |
-
info["lastname"] = raw_list[i+1]
|
| 384 |
-
lastname_pos = i+1
|
| 385 |
-
except:
|
| 386 |
-
pass
|
| 387 |
-
|
| 388 |
-
# กรณี หาอย่างใดอย่าหนึ่งเจอให้ลองข้ามไปดู 1 index name, "name_val", lastname , "lastname_val"
|
| 389 |
-
if (info["name"] != "None") and (info["lastname"] == "None"):
|
| 390 |
-
try:
|
| 391 |
-
info["lastname"] = raw_list[name_pos+2]
|
| 392 |
-
except:
|
| 393 |
-
pass
|
| 394 |
-
elif (info["lastname"] != "None") and (info["name"] == "None"):
|
| 395 |
-
try:
|
| 396 |
-
info["name"] = raw_list[lastname_pos-2]
|
| 397 |
-
except:
|
| 398 |
-
pass
|
| 399 |
-
|
| 400 |
-
# remove . on "mr." and "mrs."
|
| 401 |
-
info["name"] = info["name"].replace(".","")
|
| 402 |
-
info["lastname"] = info["lastname"].replace(".","")
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
return info
|
| 406 |
-
|
| 407 |
-
st.subheader("Process Completed!.....")
|
| 408 |
-
st.write(id_num)
|
| 409 |
-
st.write(name_sum(raw_data))
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
if choice =='About' :
|
| 415 |
-
st.header("About...")
|
| 416 |
|
| 417 |
st.subheader("AOC คืออะไร ?")
|
| 418 |
st.write("- เป็นระบบที่สามารถคัดกรองผลตรวจเชื้อของ COVID-19 ได้ผ่าน ที่ตรวจ ATK (Antigen Test Kit) ควบคู่กับบัตรประชาชน จากรูปภาพได้โดยอัตโนมัติ")
|
|
@@ -441,49 +157,25 @@ if choice =='About' :
|
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
-
elif choice == "Detection":
|
| 445 |
st.header(" Antigen test kit + Identification card detector.")
|
| 446 |
-
pages_name = ['ATK + Idcard Detect', 'ATK Detect', 'Idcard Detect']
|
| 447 |
-
page = st.radio('Select option mode :', pages_name)
|
| 448 |
|
| 449 |
image = st.file_uploader(label = "upload ATK + Idcard img here.. OwO",type=['png','jpg','jpeg'])
|
| 450 |
if image is not None:
|
| 451 |
new_img = img_resize(image, 1280)
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
t2 = time.perf_counter()
|
| 460 |
-
st.write('time taken to run: {:.2f} sec'.format(t2-t1))
|
| 461 |
-
|
| 462 |
-
elif page == "ATK Detect":
|
| 463 |
-
st.image(get_img_detection(image))
|
| 464 |
-
with st.spinner("🤖 ATK Working... "):
|
| 465 |
-
t1 = time.perf_counter()
|
| 466 |
-
st.subheader("Process Completed!.....")
|
| 467 |
-
get_detection(image)
|
| 468 |
-
t2 = time.perf_counter()
|
| 469 |
-
st.write('time taken to run: {:.2f} sec'.format(t2-t1))
|
| 470 |
-
|
| 471 |
-
elif page == "Idcard Detect":
|
| 472 |
-
st.image(new_img)
|
| 473 |
-
with st.spinner("🤖 Idcard Working... "):
|
| 474 |
-
t1 = time.perf_counter()
|
| 475 |
-
Get_Idcard_detail(image)
|
| 476 |
-
t2 = time.perf_counter()
|
| 477 |
-
st.write('time taken to run: {:.2f} sec'.format(t2-t1))
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
else:
|
| 484 |
st.write("## Waiting for image..")
|
| 485 |
st.image('atk_idcard.jpeg')
|
| 486 |
|
|
|
|
|
|
|
| 487 |
st.caption("Made by Tanaanan .M")
|
| 488 |
|
| 489 |
|
|
@@ -506,4 +198,4 @@ st.sidebar.write('[Google form](https://forms.gle/zYpYFKcTpBoFGxN58)')
|
|
| 506 |
|
| 507 |
st.sidebar.markdown('---')
|
| 508 |
st.sidebar.subheader('Made by Tanaanan .M')
|
| 509 |
-
st.sidebar.write("Contact : mjsalyjoh@gmail.com")
|
|
|
|
| 2 |
from PIL import Image, ImageOps #Image Processing
|
| 3 |
import time
|
| 4 |
from unittest import result
|
|
|
|
|
|
|
| 5 |
from icevision.all import *
|
| 6 |
from icevision.models.checkpoint import *
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
st.sidebar.image("./logo.png")
|
|
|
|
| 13 |
return img
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
activities = ["Detection (วิเคราะห์โรค)", "About (เกี่ยวกับ)"]
|
| 18 |
+
choice = st.sidebar.selectbox("Select option.. (เลือกโหมด)",activities)
|
| 19 |
+
|
| 20 |
+
#set default size as image_scale x image_scale
|
| 21 |
def img_resize(input_path,img_size): # padding
|
| 22 |
desired_size = img_size
|
| 23 |
im = Image.open(input_path)
|
| 24 |
im = ImageOps.exif_transpose(im) # fix image rotating
|
| 25 |
width, height = im.size # get img_input size
|
| 26 |
+
if (width == image_scale) and (height == image_scale):
|
| 27 |
new_im = im
|
| 28 |
else:
|
| 29 |
#im = im.convert('L') #Convert to gray
|
|
|
|
| 58 |
img_size = checkpoint_and_model["img_size"]
|
| 59 |
valid_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(img_size), tfms.A.Normalize()])
|
| 60 |
|
| 61 |
+
image_scale = 768 # change image input / ouput size here
|
| 62 |
+
|
| 63 |
def get_detection(img_path):
|
| 64 |
|
| 65 |
#Get_Idcard_detail(file_path=img_path)
|
| 66 |
img = Image.open(img_path)
|
| 67 |
img = ImageOps.exif_transpose(img) # fix image rotating
|
| 68 |
width, height = img.size # get img_input size
|
| 69 |
+
if (width == image_scale) and (height == image_scale):
|
| 70 |
pred_dict = model_type.end2end_detect(img, valid_tfms, model, class_map=class_map, detection_threshold=0.6)
|
| 71 |
else:
|
| 72 |
#im = im.convert('L') #Convert to gray
|
| 73 |
old_size = img.size # old_size[0] is in (width, height) format
|
| 74 |
+
ratio = float(image_scale)/max(old_size)
|
| 75 |
new_size = tuple([int(x*ratio) for x in old_size])
|
| 76 |
img = img.resize(new_size, Image.ANTIALIAS)
|
| 77 |
+
new_im = Image.new("RGB", (image_scale, image_scale))
|
| 78 |
+
new_im.paste(img, ((image_scale-new_size[0])//2,
|
| 79 |
+
(image_scale-new_size[1])//2))
|
| 80 |
pred_dict = model_type.end2end_detect(new_im, valid_tfms, model, class_map=class_map, detection_threshold=0.6)
|
| 81 |
|
| 82 |
|
|
|
|
| 90 |
acc = acc * 100
|
| 91 |
if labels == "Neg":
|
| 92 |
labels = "Negative"
|
| 93 |
+
st.success(f"Result : {labels} with {round(acc, 2)}% confidence.")
|
| 94 |
elif labels == "Pos":
|
| 95 |
labels = "Positive"
|
| 96 |
+
st.error(f"Result : {labels} with {round(acc, 2)}% confidence.")
|
| 97 |
except IndexError:
|
| 98 |
st.error("Not found ATK image! ; try to take image again..")
|
| 99 |
labels = "None"
|
|
|
|
| 105 |
img = Image.open(img_path)
|
| 106 |
img = ImageOps.exif_transpose(img) # fix image rotating
|
| 107 |
width, height = img.size # get img_input size
|
| 108 |
+
if (width == image_scale) and (height == image_scale):
|
| 109 |
new_im = img
|
| 110 |
else:
|
| 111 |
#im = im.convert('L') #Convert to gray
|
| 112 |
old_size = img.size # old_size[0] is in (width, height) format
|
| 113 |
+
ratio = float(image_scale)/max(old_size)
|
| 114 |
new_size = tuple([int(x*ratio) for x in old_size])
|
| 115 |
img = img.resize(new_size, Image.ANTIALIAS)
|
| 116 |
+
new_im = Image.new("RGB", (image_scale, image_scale))
|
| 117 |
+
new_im.paste(img, ((image_scale-new_size[0])//2,
|
| 118 |
+
(image_scale-new_size[1])//2))
|
| 119 |
|
| 120 |
pred_dict = model_type.end2end_detect(new_im, valid_tfms, model, class_map=class_map, detection_threshold=0.6)
|
| 121 |
|
| 122 |
|
| 123 |
return pred_dict['img']
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
|
|
|
| 126 |
|
|
|
|
| 127 |
|
| 128 |
|
|
|
|
| 129 |
|
| 130 |
+
if choice =='About (เกี่ยวกับ)' :
|
| 131 |
+
st.header("About... (เกี่ยวกับ)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
st.subheader("AOC คืออะไร ?")
|
| 134 |
st.write("- เป็นระบบที่สามารถคัดกรองผลตรวจเชื้อของ COVID-19 ได้ผ่าน ที่ตรวจ ATK (Antigen Test Kit) ควบคู่กับบัตรประชาชน จากรูปภาพได้โดยอัตโนมัติ")
|
|
|
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
+
elif choice == "Detection (วิเคราะห์โรค)":
|
| 161 |
st.header(" Antigen test kit + Identification card detector.")
|
|
|
|
|
|
|
| 162 |
|
| 163 |
image = st.file_uploader(label = "upload ATK + Idcard img here.. OwO",type=['png','jpg','jpeg'])
|
| 164 |
if image is not None:
|
| 165 |
new_img = img_resize(image, 1280)
|
| 166 |
+
st.image(get_img_detection(image))
|
| 167 |
+
with st.spinner("🤖 On Working... "):
|
| 168 |
+
t1 = time.perf_counter()
|
| 169 |
+
st.subheader("Process Completed!.....")
|
| 170 |
+
get_detection(image)
|
| 171 |
+
t2 = time.perf_counter()
|
| 172 |
+
st.write('time taken to run: {:.2f} sec'.format(t2-t1))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
else:
|
| 174 |
st.write("## Waiting for image..")
|
| 175 |
st.image('atk_idcard.jpeg')
|
| 176 |
|
| 177 |
+
st.warning("[On debugging..] ในตอนนี้ระบบ AOC ที่เป็น Obj detection สามารถใช้ได้แค่ตรวจจับ ATK (กำลังอยู่ในการพัฒนาให้สามารถใช้งานควบคู่กับระบบ OCR บัตรประชาชน)")
|
| 178 |
+
|
| 179 |
st.caption("Made by Tanaanan .M")
|
| 180 |
|
| 181 |
|
|
|
|
| 198 |
|
| 199 |
st.sidebar.markdown('---')
|
| 200 |
st.sidebar.subheader('Made by Tanaanan .M')
|
| 201 |
+
st.sidebar.write("Contact : mjsalyjoh@gmail.com")
|