import zipfile
from PIL import Image, ImageDraw
import pytesseract
import cv2 as cv
import numpy as np
from IPython.display import display
# loading the face detection classifier
face_cascade = cv.CascadeClassifier('readonly/haarcascade_frontalface_default.xml')
def search(search_text,path):
# extraxt all images from zip
z = zipfile.ZipFile(path, mode="r")
images_list = z.namelist()
save_path = path.replace('.zip','/')
z.extractall(save_path)
z.close()
# create Global_list of {file_name : image text,...}
Global_list = {}
for image_name in images_list:
file_name= save_path + image_name
img = Image.open(file_name)
text = pytesseract.image_to_string(img)
text = text.replace("\n"," ").replace(" "," ")
Global_list[file_name] = text
for file_name in Global_list:
try:
# now check search_text in text of image file
if(Global_list[file_name].find(search_text) >= 0):
print('Results found in file',file_name)
# now find cordinates of faces in image.
img = cv.imread(file_name)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # convert image RGB to gray
faces_cordinates = face_cascade.detectMultiScale(gray,1.35,4) # array([x,y,wid,hgt])
# now check how many faces detected, otherwise error will occur for some functions.
if (len(faces_cordinates) > 0) :
#pil_img = Image.fromarray(img)
#drawing = ImageDraw.Draw(pil_img)
# now crop all faces from images as per faces coordinates
img = Image.open(file_name)
faces_img = []
for x,y,w,h in faces_cordinates:
faces_img.append(img.crop((x,y,x+w,y+h)))
#drawing.rectangle((x,y,x+w,y+h), outline="red")
#display(pil_img)
# thumbnail to convert into small image (100x100)(w x h)
thum_size = 100
for img in faces_img:
img.thumbnail((thum_size,thum_size)) # thumbnail to convert into small image (100x100)(w x h)
# now creating base contact sheet as total images and max-5 image in single row then paste all images.
contact_sheet = Image.new(img.mode, ((5*thum_size),(thum_size * int(np.ceil(len(faces_img)/5)))))
x = 0
y = 0
for img in faces_img:
contact_sheet.paste(img, (x, y))
if(contact_sheet.width == x+thum_size):
x = 0
y += thum_size
else:
x += thum_size
display(contact_sheet)
else:
print('But there were no faces in that file!',file_name)
except Exception as err:
print('Error :',err)
search('Christopher',"readonly/small_img.zip")
search('Mark',"readonly/images.zip")
Last active
August 1, 2024 16:14
-
-
Save jaydattpatel/85bb91a10b8832a6dce84fce781b5067 to your computer and use it in GitHub Desktop.
Python Project : pillow, tesseract, and opencv - Coursera - Python 3 Programming Specialization - University of Michigan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
author : Jaydatt Patel | |
Python Project : pillow, tesseract, and opencv Course on Coursera (University of Michigan) | |
Week - 1 Project | |
''' | |
import PIL | |
from PIL import Image | |
from PIL import ImageEnhance | |
from PIL import ImageDraw | |
from PIL import ImageFont | |
# read image and convert to RGB | |
image=Image.open("readonly/msi_recruitment.gif") | |
image=image.convert('RGB') | |
font=ImageFont.truetype(r'readonly/fanwood-webfont.ttf', 70) | |
# build a list of 9 images which have different brightnesses | |
newimage=PIL.Image.new(image.mode, (image.width, image.height+70)) | |
newimage.paste(image, (0,0)) | |
images=[] | |
for i in range(1,10): | |
if i%3==1: | |
intensity=0.1 | |
elif i%3==2: | |
intensity=0.5 | |
elif i%3==0: | |
intensity=0.9 | |
if i<=3: | |
channel=0 | |
elif i<=6: | |
channel=1 | |
else: | |
channel=2 | |
new_image=newimage.copy() | |
text="channel {} intensity {}".format(channel, intensity) | |
ImageDraw.Draw(new_image).text((0, new_image.height-70), text, font=font, align='left') | |
r,g,b=new_image.split() | |
if channel==0: | |
r=r.point(lambda x: x*intensity) | |
elif channel==1: | |
g=g.point(lambda x: x*intensity) | |
elif channel==2: | |
b=b.point(lambda x: x*intensity) | |
result=Image.merge('RGB', (r,g,b)) | |
images.append(result) | |
# create a contact sheet from different brightnesses | |
first_image=images[0] | |
contact_sheet=PIL.Image.new(first_image.mode, (first_image.width*3,first_image.height*3)) | |
x=0 | |
y=0 | |
for img in images: | |
# Lets paste the current image into the contact sheet | |
contact_sheet.paste(img, (x, y) ) | |
# Now we update our X position. If it is going to be the width of the image, then we set it to 0 | |
# and update Y as well to point to the next "line" of the contact sheet. | |
if x+first_image.width == contact_sheet.width: | |
x=0 | |
y=y+first_image.height | |
else: | |
x=x+first_image.width | |
# resize and display the contact sheet | |
contact_sheet = contact_sheet.resize((int(contact_sheet.width/2),int(contact_sheet.height/2) )) | |
display(contact_sheet) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment