Я работаю над извлечением данных (например, номера CNIC, имени, пола и т. д.) из изображения. Я использую EasyOCR вместе с OpenCV и другими библиотеками Python (например, matplotlib, scipy и т. д.) для извлечения конкретных деталей из изображения CNIC (компьютеризированного национального удостоверения личности). На данный момент мне удалось извлечь несколько полей, таких как имя, имя отца, дата рождения, дата выдачи и дата истечения срока действия, но я также хочу извлечь номер CNIC и пол.
Проблема:
OCR может извлекать имя, имя отца, дату рождения и т. д., но теперь я хочу также извлечь поля номера CNIC и пола.
Номер CNIC и пол извлекаются неправильно, например Номер CNIC: Хунуаусе и
ПОЛ: Пол?
Я использую предопределенные ограничивающие рамки, чтобы попытаться найти соответствующие поля для CNIC и пола. .
Корректировка ограничивающих рамок: корректны ли заранее заданные ограничивающие рамки для номера CNIC и пола, или их следует скорректировать, чтобы лучше охватить эти поля?
Я был бы признателен любая помощь или предложения по повышению точности и надежности извлечения номеров CNIC и пола с помощью OCR.
вот код, который я использую:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import cv2
from PIL import Image
import easyocr
import numpy as np
import math
import re
from scipy.spatial import distance
# %%
# import pkg_resources
# pkg_resources.get_distribution('scikit-learn').version
# %%
reader = easyocr.Reader(['en'],gpu=False)
# %%
"""
## Reading a sample Image and applying OCR
"""
# %%
# 9
image_path = "/content/drive/MyDrive/CV/T3.jpeg"
im = Image.open(image_path)
image = cv2.imread(image_path)
result = reader.readtext(image_path)
print('Result',result)
# %%
dpi = im.info.get('dpi')
print(f"The DPI of Image is : {dpi}")
# %%
shape = image.shape[:-1]
print(f"The Shape of Image is :{shape}")
# %%
plt.imshow(image)
# %%
"""
## Showing the marked bounding boxes
"""
# %%
bbs = []
values = []
for (bbox, text, prob) in result:
# display the OCR'd text and associated probability
# print("[INFO] {:.4f}: {}".format(prob, text))
values.append(text)
# unpack the bounding box
(tl, tr, br, bl) = bbox
bbs.append(bbox)
tl = (int(tl[0]), int(tl[1]))
tr = (int(tr[0]), int(tr[1]))
br = (int(br[0]), int(br[1]))
bl = (int(bl[0]), int(bl[1]))
# cleanup the text and draw the box surrounding the text along
# with the OCR'd text itself
cv2.rectangle(image, tl, br, (0, 255, 0), 2)
cv2.putText(image, text, (tl[0], tl[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
# show the output image
#cv2.imshow("Image", image)
#cv2.waitKey(0)
# %%
"""
## Normalizing the bounding Boxes
"""
# %%
def normalize(img,result):
w,h = img.shape[:-1]
normalize_bbx = []
detected_labels = []
for (bbox, text, prob) in result:
(tl, tr, br, bl) = bbox
tl[0],tl[1] = round(tl[0] / h,3),round(tl[1] / w,3)
tr[0],tr[1] = round(tr[0] / h,3),round(tr[1] / w,3)
br[0],br[1] = round(br[0] / h,3),round(br[1] / w,3)
bl[0],bl[1] = round(bl[0] / h,3),round(bl[1] / w,3)
normalize_bbx.append([tl,tr,br,bl])
detected_labels.append(text)
return normalize_bbx,detected_labels
# %%
# CNIC 8
norm_boxes,labels = normalize(image,result)
# %%
normalize_output = list(zip(norm_boxes,labels))
# %%
"""
## Measuring Distance
"""
# %%
def calculate_distance(key,bbx):
euc_sum = 0
for val1,val2 in zip(key,bbx):
euc_sum = euc_sum + distance.euclidean(val1,val2)
return euc_sum
# %%
"""
## Defining our Static Card Template Boxes
"""
# %%
name_key = [[0.272, 0.233], [0.323, 0.233], [0.323, 0.27], [0.272, 0.27]]
name_value = [[0.283, 0.271], [0.415, 0.271], [0.415, 0.325], [0.283, 0.325]]
father_value = [[0.29, 0.456], [0.494, 0.456], [0.494, 0.514], [0.29, 0.514]]
dob_value = [[0.529, 0.751], [0.648, 0.751], [0.648, 0.803], [0.529, 0.803]]
doi_value = [[0.285, 0.857], [0.404, 0.857], [0.404, 0.908], [0.285, 0.908]]
doe_value = [[0.531, 0.859], [0.65, 0.859], [0.65, 0.911], [0.531, 0.911]]
#cnic_value = [[0.13, 0.80], [0.451, 0.621], [0.417, 0.570], [0.272, 0.270]]
cnic_value=[[227, 237], [227, 257], [121, 257]] # CNIC number box
gender_value = [[0.12, 0.62], [0.591, 0.665], [0.591, 0.61], [0.301, 0.592]] # Corrected gender coordinates
country_of_stay = [[0.43, 0.62], [0.591, 0.665], [0.591, 0.61], [0.301, 0.592]]
# %%
"""
## Distances Comparison
"""
# %%
def get_value(key,normalize_output):
distances = {}
for bbx,text in normalize_output:
distances[text] = calculate_distance(key,bbx)
return distances
# %%
dict_data = {}
output_dict = {}
output_dict['Name'] = name_value
output_dict['Father Name'] = father_value
output_dict['Date of Birth'] = dob_value
output_dict['Date of Issue'] = doi_value
output_dict['Date of Expiry'] = doe_value
#output_dict['CNIC Number'] = cnic_value # New entry for CNIC number
#output_dict['GENDER'] = gender_value
output_dict['Country of Stay'] = country_of_stay
output_dict['CNIC Number'] = cnic_value
output_dict['GENDER'] = gender_value
# %%
for key,value in output_dict.items():
output_dict = get_value(value,normalize_output)
answer = list(min(output_dict.items(), key=lambda x: x[1]))[0]
dict_data[key] = answer
# %% b
"""
## Output Dictionary
"""
# %%
print('datadd',dict_data)
# %%
# %%
"""
## Writing the Output Dictionary to a Text File
"""
# Define the file path where you want to save the text file
output_file_path = "/content/drive/MyDrive/output.txt"
# Open the file in write mode
with open(output_file_path, "w") as file:
# Loop through the dictionary and write each key-value pair to the file
for key, value in dict_data.items():
file.write(f"{key}: {value}\n")
print(f"Output successfully` written to {output_file_path}")`
Подробнее здесь: https://stackoverflow.com/questions/793 ... -cnic-card
Как извлечь личную информацию из фотографии карты CNIC? ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как извлечь личную информацию из фотографии удостоверения личности из Пакистана?
Anonymous » » в форуме Python - 0 Ответы
- 19 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Как замаскировать личную идентификационную информацию, используя любой язык, например Java?
Anonymous » » в форуме JAVA - 0 Ответы
- 28 Просмотры
-
Последнее сообщение Anonymous
-