forked from DPrinceKumar/HacktoberFest2020-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage processing using tesseract.py
56 lines (44 loc) · 1.57 KB
/
image processing using tesseract.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
46
47
48
49
50
51
52
53
54
55
56
import zipfile
from PIL import Image
import pytesseract
import cv2 as cv
import numpy as np
from IPython.display import display
import pytesseract
# loading the face detection classifier
face_cascade = cv.CascadeClassifier('readonly/haarcascade_frontalface_default.xml')
# the rest is up to you!
zp = zipfile.ZipFile(file = 'readonly/images.zip')
lst = zp.infolist()
imnamelst = []
imdic = {}
for item in lst :
imdic[item.filename] = []
imdic[item.filename].append(Image.open(zp.open(item.filename)))
imnamelst.append(item.filename)
for name in imnamelst :
pic = imdic[name][0]
text = pytesseract.image_to_string(pic).replace('\n','')
imdic[name].append(text)
if "Mark" in imdic[name][1] :
print("Results found in file",name)
try :
faces = (face_cascade.detectMultiScale(np.array(pic),1.35,4)).tolist()
imdic[name].append(faces)
facesinit = []
for x,y,w,h in imdic[name][2] :
facesinit.append(pic.crop((x,y,x+w,y+h)))
contact_sheet = Image.new(pic.mode, (550,110*int(np.ceil(len(facesinit)/5))))
x = 0
y = 0
for face in facesinit :
face.thumbnail((110,110))
contact_sheet.paste(face, (x, y))
if x+110 == contact_sheet.width :
x=0
y=y+110
else :
x=x+110
display(contact_sheet)
except :
print('But there were no faces in that file!')