Wie extrahiere ich persönliche Informationen aus einem Foto eines Personalausweises?

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Wie extrahiere ich persönliche Informationen aus einem Foto eines Personalausweises?

by Guest » 14 Jan 2025, 11:49

Ich arbeite daran, Ausweisdaten (wie Ausweisnummer, Name, Geschlecht usw.) aus einem Bild zu extrahieren. Ich verwende EasyOCR zusammen mit OpenCV und anderen Python-Bibliotheken (wie Matplotlib, Scipy usw.), um bestimmte Details aus einem Bild eines CNIC (Computerized National Identity Card) zu extrahieren. Bisher habe ich erfolgreich mehrere Felder wie den Namen, den Namen des Vaters, das Geburtsdatum, das Ausstellungsdatum und das Ablaufdatum extrahiert, aber ich möchte auch die CNIC-Nummer und das Geschlecht extrahieren.
Problem:
Die OCR kann Name, Vatername, Geburtsdatum usw. extrahieren, aber jetzt möchte ich auch die Felder CNIC-Nummer und Geschlecht extrahieren.
Die CNIC-Nummer und das Geschlecht werden falsch extrahiert wie CNIC-Nummer: Hunuauce und
GENDER: Geschlecht?
Ich verwende vordefinierte Begrenzungsrahmen, um zu versuchen, die relevanten Felder für CNIC und Geschlecht zu finden.
Anpassungen der Begrenzungsrahmen: Sind die vordefinierten Begrenzungsrahmen für die CNIC-Nummer und das Geschlecht korrekt oder sollten sie angepasst werden, um diese Felder besser zu erfassen?
Ich würde mich über jede Hilfe oder Hilfe freuen Vorschläge zur Verbesserung der Genauigkeit und Zuverlässigkeit der CNIC-Extraktion Zahlen und Geschlecht mithilfe von OCR.
Hier ist der Code, den ich verwende:

Code: Select all

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=[[0.13, 0.80], [0.451, 0.621], [0.417, 0.570], [0.272, 0.270]]  # 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}")`
Eingabebild: [1]: https://i.sstatic.net/Vvc4DLth.jpg
< strong>Ausgabe: datadd {'Name': 'Umair', 'Vatername': 'Muhammad Rafique', 'Geburtsdatum': '01.01,1986', 'Ausstellungsdatum': „30.12.2023“, „Ablaufdatum“: „30.12.2033“, „Aufenthaltsland“: „Pakistan“, „CNIC-Nummer“: „lu“, „GENDER“: „Gente“

Top