Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- # -*- coding: utf-8 -*- #
- from twitterbot import TwitterBot
- from picbot import PicBot
- from PIL import Image, ImageDraw
- from sys import argv
- import os
- import random
- import numpy
- import cv2
- import keys
- classifier = "haarcascade_frontalface_default.xml"
- faceCascade = cv2.CascadeClassifier(classifier)
- def face_detect(image):
- """
- Return rectangles of identified face regions
- """
- # numpy grayscale image for face detection
- array = numpy.asarray(image)
- gray_image = cv2.cvtColor(array, cv2.COLOR_BGR2GRAY)
- # tweak this for better results ..
- faces = faceCascade.detectMultiScale(
- gray_image,
- scaleFactor=1.1,
- minNeighbors= 5,
- minSize=(25, 25),
- flags=cv2.cv.CV_HAAR_SCALE_IMAGE
- )
- # convert boxes from arrays to tuples
- boxes = [(x, y, x + w, y + h) for (x, y, w, h) in faces]
- return boxes
- HORIZONTAL = Image.FLIP_TOP_BOTTOM
- VERTICAL = Image.FLIP_LEFT_RIGHT
- def animals(image):
- """
- Paste animal faces on human faces :)!
- """
- # work on a copy
- image = image.copy()
- # identify boxes
- boxes = face_detect(image)
- # grabing random files of img from the folder of animals_faces
- directory = os.path.join(os.path.dirname(__file__), "animal_faces")
- #print directory
- # esto era lo q tenia antes --- directory = './animal_faces/'
- directory_list = os.listdir(directory)
- directory_list.remove('.DS_Store')
- # for item in directory_list:
- # print (item)
- # draw boxes
- for box in boxes:
- #print(box)
- # get the coordinates for the box
- x1, y1, x2, y2 = box
- # taking a random choice from the list created before
- one_animal = random.choice(directory_list)
- # adding the name of the file on the rute so it can be opend later...
- imgfile = os.path.join(directory, one_animal)
- # open the cat img on the boxes
- cat = Image.open(imgfile)
- #resize the img of the cat
- cat = cat.resize(((x2-x1)* 2 , (y2-y1)*2), resample=Image.BICUBIC)
- # change the referense point from the upper left corner to half of it (center)
- dx = (x2-x1)/2
- dy = (y2-y1)/2
- # paste the cat into the original image with the faces
- image.paste(cat, (x1 - dx, y1 - dy), cat)
- return image, boxes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement