Advertisement
Guest User

Animal Faces Bot

a guest
Oct 7th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*- #
  3.  
  4. from twitterbot import TwitterBot
  5. from picbot import PicBot
  6. from PIL import Image, ImageDraw
  7. from sys import argv
  8.  
  9. import os
  10. import random
  11. import numpy
  12. import cv2
  13.  
  14. import keys
  15.  
  16.  
  17. classifier = "haarcascade_frontalface_default.xml"
  18. faceCascade = cv2.CascadeClassifier(classifier)
  19.  
  20.  
  21. def face_detect(image):
  22.     """
  23.     Return rectangles of identified face regions
  24.     """
  25.  
  26.     # numpy grayscale image for face detection
  27.     array = numpy.asarray(image)
  28.     gray_image = cv2.cvtColor(array, cv2.COLOR_BGR2GRAY)
  29.  
  30.     # tweak this for better results ..
  31.  
  32.     faces = faceCascade.detectMultiScale(
  33.         gray_image,
  34.         scaleFactor=1.1,
  35.         minNeighbors= 5,
  36.         minSize=(25, 25),
  37.         flags=cv2.cv.CV_HAAR_SCALE_IMAGE
  38.     )
  39.  
  40.     # convert boxes from arrays to tuples
  41.     boxes = [(x, y, x + w, y + h) for (x, y, w, h) in faces]
  42.     return boxes
  43.        
  44.  
  45. HORIZONTAL = Image.FLIP_TOP_BOTTOM
  46. VERTICAL = Image.FLIP_LEFT_RIGHT
  47.  
  48.  
  49. def animals(image):
  50.     """
  51.     Paste animal faces on human faces :)!
  52.     """
  53.  
  54.     # work on a copy
  55.     image = image.copy()
  56.    
  57.     # identify boxes
  58.     boxes = face_detect(image)
  59.  
  60.    
  61.     # grabing random files of img from the folder of animals_faces 
  62.     directory = os.path.join(os.path.dirname(__file__), "animal_faces")
  63.     #print directory
  64.  
  65.  
  66.     # esto era lo q tenia antes --- directory = './animal_faces/'
  67.     directory_list = os.listdir(directory)
  68.     directory_list.remove('.DS_Store')
  69.  
  70.     # for item in directory_list:
  71.     #   print (item)
  72.  
  73.     # draw boxes
  74.     for box in boxes:
  75.         #print(box)
  76.  
  77.         # get the coordinates for the box
  78.         x1, y1, x2, y2 = box
  79.  
  80.         # taking a random choice from the list created before
  81.         one_animal = random.choice(directory_list)
  82.         # adding the name of the file on the rute so it can be opend later...
  83.         imgfile = os.path.join(directory, one_animal)
  84.  
  85.         # open the cat img on the boxes
  86.         cat = Image.open(imgfile)
  87.  
  88.         #resize the img of the cat
  89.         cat = cat.resize(((x2-x1)* 2 , (y2-y1)*2), resample=Image.BICUBIC)
  90.        
  91.         # change the referense point from the upper left corner to half of it (center)
  92.         dx = (x2-x1)/2
  93.         dy = (y2-y1)/2
  94.  
  95.         # paste the cat into the original image with the faces
  96.         image.paste(cat, (x1 - dx, y1 - dy), cat)
  97.  
  98.  
  99.     return image, boxes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement