-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final-Project.py
69 lines (53 loc) · 1.99 KB
/
Final-Project.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
57
58
59
60
61
62
63
64
65
66
67
68
import zipfile
import os
from PIL import Image
import pytesseract
import cv2 as cv
import numpy as np
import os
face_cascade = cv.CascadeClassifier("readonly/haarcascade_frontalface_default.xml")
local_zip = "readonly/small_img.zip"
zip_ref = zipfile.ZipFile(local_zip, "r")
zip_ref.extractall("small_img")
zip_ref.close()
os.mkdir("images")
local_zip = "readonly/images.zip"
zip_ref = zipfile.ZipFile(local_zip, "r")
zip_ref.extractall("images")
zip_ref.close()
pages_list = os.listdir("small_img")
Global_list = []
for page_name in pages_list:
local_list = []
local_list.append(page_name)
img = Image.open("small_img/" + page_name)
local_list.append(pytesseract.image_to_string(img).replace("-\n", ""))
Global_list.append(local_list)
def search(text: str, folder: str):
for local_list in Global_list:
if text in local_list[1]:
print("Results found in file", local_list[0])
try:
img = Image.open(str(folder + local_list[0]))
faces = (
face_cascade.detectMultiScale(np.array(img), 1.35, 4)
).tolist()
faces_in_each = []
for x, y, w, h in faces:
faces_in_each.append(img.crop((x, y, x + w, y + h)))
contact_sheet = Image.new(
img.mode, (550, 110 * int(np.ceil(len(faces_in_each) / 5)))
)
x = 0
y = 0
for face in faces_in_each:
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
contact_sheet.show()
except:
print("But there were no faces in that file!")