Advertisement
vchebolu

Venkata C - Coursera Project

Apr 25th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.56 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. # In[1]:
  5.  
  6.  
  7. import zipfile
  8.  
  9. from PIL import Image
  10. import pytesseract
  11. import cv2 as cv
  12. import numpy as np
  13.  
  14. # loading the face detection classifier
  15. face_cascade = cv.CascadeClassifier('readonly/haarcascade_frontalface_default.xml')
  16. from PIL import ImageDraw, ImageFont
  17. import os
  18.  
  19. def img2txt(image):
  20.     grayimg = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
  21.     tempfl = "temp{}.png".format(os.getpid())
  22.     cv.imwrite(tempfl, grayimg)
  23.     img = Image.open(tempfl)
  24.     ret = pytesseract.image_to_string(img)
  25.     os.remove(tempfl)
  26.     return ret
  27.  
  28.  
  29. def img2faces(image):
  30.     grayimg = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
  31.     faces = face_cascade.detectMultiScale(
  32.         grayimg,
  33.         scaleFactor=1.3,
  34.         minNeighbors=5,
  35.         minSize=(40, 40),
  36.         flags = cv.CASCADE_SCALE_IMAGE
  37.     )
  38.     return faces
  39.  
  40. class ImgEntry:
  41.     def __init__(self, txt, faces, img):
  42.         self.txt = txt
  43.         self.faces = faces
  44.         self.img = img
  45.  
  46. def proczip(zipfl):
  47.     ret = dict()
  48.     with zipfile.ZipFile(zipfl, "r") as z:
  49.         for name in z.namelist():
  50.             print("Processing " + name)
  51.             filedata = z.read(name)
  52.             img = cv.imdecode(np.frombuffer(filedata, np.uint8), 1)
  53.             ret[name] = ImgEntry(img2txt(img), img2faces(img), img)
  54.     return ret
  55.  
  56. def lookup(imgdir, srchtxt):
  57.     ret = []
  58.     for (fl, imgent) in imgdir.items():
  59.         if srchtxt in imgent.txt:
  60.             ret.append((fl, imgent.img, imgent.faces))
  61.     return ret
  62.  
  63.  
  64. def draw_text(txt, x, y, img, d, f):
  65.     d.text((x,y), txt, font=f, fill="black")
  66.  
  67.  
  68. def getface(img, face):    
  69.     x, y, w, h = face
  70.     faceimg = img[y:y+h, x:x+w, :]
  71.     tempfl = "temp{}.png".format(os.getpid())
  72.     try:
  73.         os.remove(tempfl)
  74.     except:
  75.         pass
  76.     cv.imwrite(tempfl, faceimg)
  77.     return Image.open(tempfl)
  78.        
  79. def showlst(lst):
  80.     txth = 35
  81.     imgwidth = 200
  82.     imgheight = 200
  83.     offs = 20
  84.    
  85.     per_row = 5
  86.    
  87.     sheet_width = per_row * imgwidth
  88.     sheet_height = 0
  89.     if len(lst) == 0:
  90.         sheet_height += txth
  91.  
  92.     for (fl, img, faces) in lst:  
  93.         if len(faces) == 0:
  94.             sheet_height += txth
  95.         else:
  96.             sheet_height += (int(len(faces) / per_row) + 1) * imgheight
  97.             sheet_height += 2 * offs
  98.         sheet_height += txth
  99.     contact_sheet=Image.new("RGB", (sheet_width,sheet_height))
  100.    
  101.     d = ImageDraw.Draw(contact_sheet)
  102.     d.rectangle(((0, 0), (sheet_width, sheet_height)), fill="white")
  103.    
  104.     f = ImageFont.truetype("readonly/fanwood-webfont.ttf", size=30)
  105.    
  106.     x = 0
  107.     y = 0
  108.     if len(lst) == 0:
  109.         draw_text("No results were found in any of the files.", x, y, contact_sheet, d, f)
  110.         return contact_sheet
  111.        
  112.     for (fl, img, faces) in lst:
  113.         draw_text("Results found in file " + fl, x, y, contact_sheet, d, f)
  114.         y += txth
  115.         if len(faces) == 0:
  116.             draw_text("But there were no faces in that file.", x, y, contact_sheet, d, f)
  117.             x = 0
  118.             y += txth
  119.         else:
  120.             imgs_on_this_row = 0
  121.             y += offs
  122.             for face in faces:
  123.                 if imgs_on_this_row == 0:
  124.                     d.rectangle(((x, y), (x+sheet_width, y+imgheight)), fill="black")
  125.                 face = getface(img, face)
  126.                 face.thumbnail((imgwidth, imgheight))
  127.                 contact_sheet.paste(face, (x, y))
  128.                 imgs_on_this_row += 1
  129.                 x += imgwidth
  130.                 if imgs_on_this_row == per_row:
  131.                     imgs_on_this_row = 0
  132.                     x = 0
  133.                     y = y + imgheight
  134.             if imgs_on_this_row != 0:
  135.                 x = 0
  136.                 y = y + imgheight
  137.             y += offs
  138.    
  139.     new_sheet = Image.new("RGB", (sheet_width + 2 * offs,y))
  140.     d = ImageDraw.Draw(new_sheet)
  141.     d.rectangle(((0, 0), (sheet_width + 2 * offs, y)), fill="white")
  142.     cropped_sheet = contact_sheet.crop((0,0,sheet_width,y))
  143.     new_sheet.paste(cropped_sheet, (offs,0))
  144.    
  145.     return new_sheet
  146.  
  147.  
  148.  
  149.  
  150. # In[2]:
  151.  
  152.  
  153. imgs = proczip("readonly/images.zip")
  154.  
  155.  
  156. # In[3]:
  157.  
  158.  
  159. smimgs = proczip("readonly/small_img.zip")
  160.  
  161.  
  162. # In[4]:
  163.  
  164.  
  165. Mark = showlst(lookup(imgs, 'Mark'))
  166. display(Mark)
  167.  
  168.  
  169. # In[5]:
  170.  
  171.  
  172. Chris = showlst(lookup(smimgs, 'Chris'))
  173. display(Chris)
  174.  
  175.  
  176. # In[6]:
  177.  
  178.  
  179. display(showlst(lookup(imgs, "wontfind")))
  180.  
  181.  
  182. # In[ ]:
  183.  
  184.  
  185. # Download as html skips the output of the very last step in the notebook, for some reason.
  186. # So, put in a dummy last step.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement