-
Notifications
You must be signed in to change notification settings - Fork 0
/
img-classify.py
44 lines (42 loc) · 1.74 KB
/
img-classify.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
import math
import zipfile
from PIL import Image, ImageOps, ImageDraw
import pytesseract
import cv2 as cv
import numpy as np
# loading the face detection classifier
face_cascade = cv.CascadeClassifier('readonly/haarcascade_frontalface_default.xml')
parsed_img_src = {}
with zipfile.ZipFile('readonly/images.zip', 'r') as archive:
for entry in archive.infolist():
with archive.open(entry) as file:
img = Image.open(file).convert('RGB')
parsed_img_src[entry.filename] = {'pil_img':img}
for img_name in parsed_img_src.keys():
text = pytesseract.image_to_string(parsed_img_src[img_name]['pil_img'])
parsed_img_src[img_name]['text'] = text
for img_name in parsed_img_src.keys():
for face in parsed_img_src[img_name]['faces']:
face.thumbnail((100,100),Image.ANTIALIAS)
def search(keyword):
for img_name in parsed_img_src:
if (keyword in parsed_img_src[img_name]['text']):
if(len(parsed_img_src[img_name]['faces']) != 0):
print("Result found in file {}".format(img_name))
h = math.ceil(len(parsed_img_src[img_name]['faces'])/5)
contact_sheet=Image.new('RGB',(500, 100*h))
xc = 0
yc = 0
for img in parsed_img_src[img_name]['faces']:
contact_sheet.paste(img, (xc, yc))
if xc + 100 == contact_sheet.width:
xc = 0
yc += 100
else:
xc += 100
display(contact_sheet)
else:
print("Result found in file {} \nBut there were no faces in that file\n\n".format(img_name))
return
search('Christopher')
search('pizza')