Advertisement
wibowoow

face_detector.py

Jun 7th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. ############face detector############
  2.  
  3. import cv2 as cv
  4. import tools
  5. import logging
  6. import parameter
  7.  
  8. class face_detector:
  9.  
  10. def detect_faces(self, image_filename):
  11. """ Detects all faces and returns a list with
  12. images and corresponding coordinates"""
  13.  
  14. logging.debug('Start method "detect_faces" for file %s (face-detector.py)' % image_filename)
  15. cascade = cv.imread(parameter.cascadefile) # load face cascade
  16. image = cv.imread(image_filename) # loads and converts image
  17.  
  18. # detect and save coordinates of detected faces
  19. coordinates = cv.HaarDetectObjects(image, cascade, cv.CreateMemStorage(), parameter.scaleFactor, parameter.minNeighbors, parameter.flags, parameter.min_facesize)
  20.  
  21. # Convert to greyscale - better results when converting AFTER facedetection with viola jones
  22. if image.channels == 3:
  23. logging.debug('Bild %s wird in Graustufenbild umgewandelt (face-detector.py)' % image_filename)
  24. grey_face = (cv.CreateImage((image.width,image.height), 8,1)) # Create grey-scale Image
  25. cv.CvtColor(image, grey_face, cv.CV_RGB2GRAY) # convert Image to Greyscale (necessary for SURF)
  26. image = grey_face
  27.  
  28. logging.debug('%d faces successfully detected in file %s (face-detector.py)' % (len(coordinates), image_filename))
  29. return image, coordinates
  30.  
  31.  
  32. def crop_face(self, image, coordinates, image_filename):
  33. """ Crops all faces from a list of images and coordinates
  34. Returns a list with all faces"""
  35.  
  36. logging.debug('Start method "crop_face" for file %s (face-detector.py)' % image_filename)
  37. cropped_faces = [] # list with all cropped faces (defined with ROI)
  38.  
  39. for i in range(len(coordinates)):
  40. rectangle = coordinates[i][0]
  41. cropped_faces.append(cv.GetSubRect(image, rectangle)) # save faces (with ROI) in new list
  42.  
  43. #check face for max image size
  44. if cropped_faces[i].height > parameter.max_facesize[0] or cropped_faces[i].width > parameter.max_facesize[1]: #start resize
  45. (cropped_faces[i], downsize_factor) = tools.downsize_image(cropped_faces[i])
  46. logging.debug('Face in image %s has been downsized with factor %d (face-detector.py)' % (image_filename, downsize_factor))
  47.  
  48. logging.debug('%d faces successfully cropped (face-detector.py)', len(cropped_faces))
  49. return cropped_faces # faces are defined with ROI
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement