Take a ZIP file) of images and process them, using a library built into python that you need to learn how to use. A ZIP file takes several different files and compresses them, thus saving space, into one single file. The files in the ZIP file we provide are newspaper images (like you saw in week 3). Your task is to write python code which allows one to search through the images looking for the occurrences of keywords and faces. E.g. if you search for "pizza" it will return a contact sheet of all of the faces which were located on the newspaper page which mentions "pizza". This will test your ability to learn a new (library), your ability to use OpenCV to detect faces, your ability to use tesseract to do optical character recognition, and your ability to use PIL to composite images together into contact sheets.
Each page of the newspapers is saved as a single PNG image in a file called images.zip. These newspapers are in english, and contain a variety of stories, advertisements and images. Note: This file is fairly large (~200 MB) and may take some time to work with, I would encourage you to use small_img.zip for testing.
Here's an example of the output expected. Using the small_img.zip file, if I search for the string "Christopher" I should see the following image:
If I were to use the images.zip file and search for "Mark" I should see the following image (note that there are times when there are no faces on a page, but a word is found!):
Note: That big file can take some time to process - for me it took nearly ten minutes! Use the small one for testing.
import zipfile
from PIL import Image
import pytesseract
import cv2 as cv
import numpy as np
import io
# loading the face detection classifier
face_cascade = cv.CascadeClassifier('readonly/haarcascade_frontalface_default.xml')
# the rest is up to you!
# specifying the zip file name
file_name = "readonly/images.zip"
# create empty list for file info as dictionary
fileList = []
# opening the zip file in READ mode
with zipfile.ZipFile(file_name, 'r') as zip:
for name in zip.namelist():
# reading the file data
data = zip.read(name)
# convert file data to PIL object
bindata = io.BytesIO(data)
image = Image.open(bindata)
# storing file name and PIL object
fileList.append({'fileName': name, 'PILObject': image})
# OCR and face recognition
for i in range(0, len(fileList)):
# convert to b/w and OCR
image_bw = fileList[i]['PILObject'].convert('1')
text = pytesseract.image_to_string(image_bw)
# store text from OCR scan
fileList[i]['textOCR'] = text
# convert image tot OpenCV object
image_px = np.array(fileList[i]['PILObject'])
# convert to b/w and face recognition
gray = cv.cvtColor(image_px, cv.COLOR_RGB2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5, minSize=(40,40))
# store rectangles from face recognition
fileList[i]['facesREC'] = faces
textString = "Mark"
# create contact sheet
for i in range(0, len(fileList)):
# check if the name is in the textOCR
if textString in fileList[i]['textOCR']:
print("Results found in in file {}".format(fileList[i]['fileName']))
if len(fileList[i]['facesREC']) != 0:
# create empty contact sheet for thumbnailes
thumbWidth, thumbHeight = 100, 100
sheetSize = int(len(fileList[i]['facesREC'])/5)
if len(fileList[i]['facesREC'])%5 != 0:
sheetSize += 1
contact_sheet = Image.new('RGB', (thumbWidth * 5, thumbHeight * sheetSize))
# create image for cropping faces
cropimage = fileList[i]['PILObject']
cx, cy = 0, 0
# loop over the rectangles
for x, y, w, h in fileList[i]['facesREC']:
img = cropimage.crop((x, y, x + w, y + h))
img.thumbnail((thumbWidth, thumbHeight))
contact_sheet.paste(img, (cx, cy))
# Next position
if cx + thumbWidth == contact_sheet.width:
cx = 0
cy += thumbHeight
else:
cx += thumbWidth
display(contact_sheet)
else:
print("But there were no faces in that file!")