-
Notifications
You must be signed in to change notification settings - Fork 1
/
IdCardReader.py
45 lines (31 loc) · 1.18 KB
/
IdCardReader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Settings
import pytesseract
import math
from PIL import Image
from datetime import datetime
settings = Settings.Settings()
pytesseract.pytesseract.tesseract_cmd = settings.TesseractPath
def GetAgeFromIdCard(path):
image = Image.open(path)
stringList = list(pytesseract.image_to_string(image).split("\n"))
image.close()
count = 0
dateOfBirthLocation = 0
for line in stringList:
if line == " ":
count += 1
continue
if "date of birth" in line:
dateOfBirthLocation = count + 1
if stringList[dateOfBirthLocation] == "":
dateOfBirthLocation += 1
count += 1
dateOfBirthStringSliced = list(stringList[dateOfBirthLocation].split(" "))
now = datetime.now()
dateOnIdCard = datetime.strptime(
dateOfBirthStringSliced[0] + "-" +
list(dateOfBirthStringSliced[1].split("/"))[1] + "-" +
dateOfBirthStringSliced[2] + " 00:00:00", '%d-%b-%Y %H:%M:%S')
currentDate = datetime.strptime(str(
now.day) + "-" + str(now.month) + "-" + str(now.year) + " 00:00:00", '%d-%m-%Y %H:%M:%S')
return math.trunc((currentDate - dateOnIdCard).days / 365)