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 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=(30, 30),
- flags=cv2.cv.CV_HAAR_SCALE_IMAGE
- )
- # convert boxes from 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 face_rectangle(image):
- """
- Make white rectangle over the eyes
- """
- # work on a copy of the image
- image = image.copy()
- # face detection with opencv
- boxes = face_detect(image)
- # get drawing context for the current image
- ctx = ImageDraw.Draw(image)
- # define colors
- white = (255, 255, 255)
- # draw boxes
- for box in boxes:
- # draw a white box around it
- x1, y1, x2, y2 = box
- box = x1 - 1 , y1 + 10, x2 + 1, y2 - 10
- ctx.rectangle(box, fill=white, outline=None)
- return image, boxes
- if __name__ == '__main__':
- print("Loading source image")
- src = Image.open("gt.jpg")
- #src.show()
- print("Testing face rectangle")
- img, boxes = face_rectangle(src)
- img.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement