|
|
|
|
|
from os import listdir |
|
|
import json |
|
|
import cv2 |
|
|
import easyocr |
|
|
reader = easyocr.Reader(['en']) |
|
|
|
|
|
from numpy import zeros |
|
|
from numpy import asarray |
|
|
from numpy import expand_dims |
|
|
from matplotlib import pyplot |
|
|
from matplotlib.patches import Rectangle |
|
|
from bboxcnn.config import Config |
|
|
from bboxcnn.model import BBoxCNN |
|
|
from bboxcnn.model import mold_image |
|
|
from bboxcnn.utils import Dataset |
|
|
|
|
|
|
|
|
|
|
|
class PredictionConfig(Config): |
|
|
|
|
|
NAME = "eid_cfg" |
|
|
|
|
|
NUM_CLASSES = 1 + 10 |
|
|
|
|
|
GPU_COUNT = 1 |
|
|
IMAGES_PER_GPU = 1 |
|
|
|
|
|
cfg = PredictionConfig() |
|
|
|
|
|
model = BBoxCNN(mode='inference', model_dir='./', config=cfg) |
|
|
|
|
|
model_path = 'model/bboxcnn_eid_cfg_0033.h5' |
|
|
model.load_weights(model_path, by_name=True) |
|
|
|
|
|
|
|
|
class_ids_to_class_name = {1: 'Sex', 2: 'DOB', 3: 'Country', |
|
|
4: 'DOE', 5: 'Card Number', 6: 'Document Type', |
|
|
7: 'Id Number', 8: 'MRZ', 9: 'Name', 10: 'Nationality'} |
|
|
|
|
|
def load_image(path): |
|
|
source_image = cv2.imread(path, cv2.IMREAD_COLOR) |
|
|
scaled_image = mold_image(source_image, cfg) |
|
|
|
|
|
sample = expand_dims(scaled_image, 0) |
|
|
return source_image, sample |
|
|
|
|
|
def extract_text(image_path): |
|
|
|
|
|
dict_ = {} |
|
|
class_ids_to_class_name = {1: 'Sex', 2: 'DOB', 3: 'Country', |
|
|
4: 'DOE', 5: 'Card Number', 6: 'Document Type', |
|
|
7: 'Id Number', 8: 'MRZ', 9: 'Name', 10: 'Nationality'} |
|
|
source_image, scaled_samp_image = load_image(image_path) |
|
|
yhat = model.detect(scaled_samp_image, verbose=0)[0] |
|
|
bboxes, class_ids, mask = yhat['rois'], yhat['class_ids'], yhat['masks'] |
|
|
for bbox, class_id in zip(bboxes, class_ids): |
|
|
xmin, ymin, xmax, ymax = bbox |
|
|
piece_image = source_image[xmin:xmax, ymin:ymax] |
|
|
classname = class_ids_to_class_name[class_id] |
|
|
text = reader.readtext(piece_image, detail=0) |
|
|
dict_.update({class_ids_to_class_name[class_id]: text}) |
|
|
return dict_ |
|
|
|
|
|
def process(im_path): |
|
|
eid_data = extract_text(im_path) |
|
|
return eid_data |