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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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 import tfms
|
| 8 |
+
from icevision.models import model_from_checkpoint
|
| 9 |
+
import easyocr as ocr #OCR
|
| 10 |
+
import editdistance
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
st.sidebar.image("./logo.png")
|
| 14 |
+
st.sidebar.header("ATK-OCR classification (AOC) Webapp.")
|
| 15 |
+
def load_image(image_file):
|
| 16 |
+
img = Image.open(image_file)
|
| 17 |
+
return img
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
activities = ["Detection", "About"]
|
| 21 |
+
choice = st.sidebar.selectbox("Select option..",activities)
|
| 22 |
+
|
| 23 |
+
#set default size as 1280 x 1280
|
| 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 == 1280) and (height == 1280):
|
| 30 |
+
new_im = im
|
| 31 |
+
else:
|
| 32 |
+
#im = im.convert('L') #Convert to gray
|
| 33 |
+
old_size = im.size # old_size[0] is in (width, height) format
|
| 34 |
+
ratio = float(desired_size)/max(old_size)
|
| 35 |
+
new_size = tuple([int(x*ratio) for x in old_size])
|
| 36 |
+
im = im.resize(new_size, Image.ANTIALIAS)
|
| 37 |
+
new_im = Image.new("RGB", (desired_size, desired_size))
|
| 38 |
+
new_im.paste(im, ((desired_size-new_size[0])//2,
|
| 39 |
+
(desired_size-new_size[1])//2))
|
| 40 |
+
|
| 41 |
+
return new_im
|
| 42 |
+
|
| 43 |
+
checkpoint_path = "./AOC_weight_97.4.pth"
|
| 44 |
+
|
| 45 |
+
checkpoint_and_model = model_from_checkpoint(checkpoint_path,
|
| 46 |
+
model_name='ross.efficientdet',
|
| 47 |
+
backbone_name='tf_d2',
|
| 48 |
+
img_size=384,
|
| 49 |
+
is_coco=False)
|
| 50 |
+
|
| 51 |
+
model_type = checkpoint_and_model["model_type"]
|
| 52 |
+
backbone = checkpoint_and_model["backbone"]
|
| 53 |
+
class_map = checkpoint_and_model["class_map"]
|
| 54 |
+
img_size = checkpoint_and_model["img_size"]
|
| 55 |
+
#model_type, backbone, class_map, img_size
|
| 56 |
+
|
| 57 |
+
model = checkpoint_and_model["model"]
|
| 58 |
+
|
| 59 |
+
device=next(model.parameters()).device
|
| 60 |
+
|
| 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 == 1280) and (height == 1280):
|
| 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(1280)/max(old_size)
|
| 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", (1280, 1280))
|
| 79 |
+
new_im.paste(img, ((1280-new_size[0])//2,
|
| 80 |
+
(1280-new_size[1])//2))
|
| 81 |
+
pred_dict = model_type.end2end_detect(new_im, valid_tfms, model, class_map=class_map, detection_threshold=0.6)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
#st.write(new_im.size)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
labels, acc = pred_dict['detection']['labels'][0], pred_dict['detection']['scores'][0]
|
| 91 |
+
acc = acc * 100
|
| 92 |
+
if labels == "Neg":
|
| 93 |
+
labels = "Negative"
|
| 94 |
+
elif labels == "Pos":
|
| 95 |
+
labels = "Positive"
|
| 96 |
+
st.success(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"
|
| 100 |
+
acc = 0
|
| 101 |
+
|
| 102 |
+
def get_img_detection(img_path):
|
| 103 |
+
|
| 104 |
+
#Get_Idcard_detail(file_path=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 == 1280) and (height == 1280):
|
| 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(1280)/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", (1280, 1280))
|
| 117 |
+
new_im.paste(img, ((1280-new_size[0])//2,
|
| 118 |
+
(1280-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 |
+
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 |
+
Clean_syntax = ["","#","{","}","=","/","@","#","$","—","|","%","-","(",")","¥", "[", "]", "‘",':',';']
|
| 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) ควบคู่กับบัตรประชาชน จากรูปภาพได้โดยอัตโนมัติ")
|
| 419 |
+
|
| 420 |
+
st.subheader("AOC ทำอะไรได้บ้าง ?")
|
| 421 |
+
st.write("- ตรวจจับผลตรวจ ATK (Obj detection)")
|
| 422 |
+
st.write("- ตรวจจับชื่อ-นามสกุล (OCR)")
|
| 423 |
+
st.write("- ตรวจจับเลขบัตรประชาชน (OCR)")
|
| 424 |
+
|
| 425 |
+
st.subheader("AOC ดีกว่ายังไง ?")
|
| 426 |
+
st.write("จากผลที่ได้จากการเปรียบเทียบกันระหว่าง model (AOC) กับ คน (Baseline) จำนวน 30 ภาพ / คน ได้ผลดังนี้")
|
| 427 |
+
st.image("./acc_table.png")
|
| 428 |
+
st.write("จากผลที่ได้สรุปได้ว่า ส่วนที่ผ่าน Baseline และมีประสิทธิภาพดีกว่าคัดกรองด้วยคนคือ ผลตรวจ ATK ได้ผลที่ 100 %, เลขบัตรประชน ได้ผลที่ 100 % และ ความเร็วในการคัดกรอง ได้ผลที่ 4.84 วินาที ซึ่งมีความเร็วมากกว่า 81% เมื่อเทียบกับคัดกรองด้วยคน ถือว่ามีประสิทธิภาพที่สูงมากในการคัดกรอง และ มีประสิทธิภาพมากกว่าการคัดแยกด้วยมนุษย์")
|
| 429 |
+
st.write("** ความเร็วที่โมเดลทำได้อาจไม่ตรงตามที่ deploy บนเว็บ เนื่องจากในเว็บ ไม่มี GPU ในการประมวลผลอาจทำให้โมเดลใช้เวลาในการประมวลที่นานกว่าตอนใช้ GPU")
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
st.subheader("คำแนะนำในการใช้งาน")
|
| 433 |
+
st.write("- ในการใช้งานให้ถ่ายรูปภาพบัตรประชาชนในแนวตั้งเท่านั้น เนื่องจากถ้าเป็นแนวอื่นอาจทำให้การตรวจจับคลาดเคลื่อนเอาได้")#3
|
| 434 |
+
st.write("- ภาพไม่ควรมีแสงที่สว่างมากเกืนไป และ มืดเกินไป มิฉะนั้นอาจทำให้การตรวจจับคลาดเคลื่อนเอาได้")#4
|
| 435 |
+
st.write("- ภาพไม่ควรที่จะอยู่ไกลเกินไป และ ควรมีความชัด มิฉะนั้นอาจทำให้การตรวจจับคลาดเคลื่อน หรือ ไม่สามารถตรวจจับได้")#5
|
| 436 |
+
|
| 437 |
+
st.subheader("รายละเอียดเพิ่มเติม")
|
| 438 |
+
st.write('[Medium blog](https://medium.com/@mjsalyjoh/atk-ocr-classification-aoc-%E0%B8%A3%E0%B8%B0%E0%B8%9A%E0%B8%9A%E0%B8%84%E0%B8%B1%E0%B8%94%E0%B8%81%E0%B8%A3%E0%B8%AD%E0%B8%87%E0%B8%9C%E0%B8%A5%E0%B8%95%E0%B8%A3%E0%B8%A7%E0%B8%88-atk-%E0%B9%81%E0%B8%A5%E0%B8%B0-%E0%B8%9A%E0%B8%B1%E0%B8%95%E0%B8%A3%E0%B8%9B%E0%B8%A3%E0%B8%B0%E0%B8%8A%E0%B8%B2%E0%B8%8A%E0%B8%99-fa32a8d47599)')
|
| 439 |
+
st.write('[Github Link](https://github.com/Tanaanan/AOC_ATK_OCR_Classification)')
|
| 440 |
+
|
| 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 |
+
if page == "ATK + Idcard Detect":
|
| 453 |
+
st.image(get_img_detection(image))
|
| 454 |
+
with st.spinner("🤖 ATK + Idcard Working... "):
|
| 455 |
+
|
| 456 |
+
t1 = time.perf_counter()
|
| 457 |
+
Get_Idcard_detail(image)
|
| 458 |
+
get_detection(image)
|
| 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 |
+
|
| 490 |
+
st.sidebar.subheader('More image for test..')
|
| 491 |
+
st.sidebar.write('[Github img test set.](https://github.com/Tanaanan/AOC_ATK_OCR_Classification/tree/main/test_set(img))')
|
| 492 |
+
|
| 493 |
+
|
| 494 |
+
|
| 495 |
+
|
| 496 |
+
|
| 497 |
+
|
| 498 |
+
|
| 499 |
+
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
st.sidebar.markdown('---')
|
| 503 |
+
st.sidebar.subheader('Recomend / Issues report..')
|
| 504 |
+
st.sidebar.write('[Google form](https://forms.gle/zYpYFKcTpBoFGxN58)')
|
| 505 |
+
|
| 506 |
+
|
| 507 |
+
st.sidebar.markdown('---')
|
| 508 |
+
st.sidebar.subheader('Made by Tanaanan .M')
|
| 509 |
+
st.sidebar.write("Contact : mjsalyjoh@gmail.com")
|