Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. '''
  2. Sources:
  3. http://opencv.willowgarage.com/documentation/python/cookbook.html
  4. http://www.lucaamore.com/?p=638
  5. '''
  6.  
  7. #Python 2.7.2
  8. #Opencv 2.4.2
  9. #PIL 1.1.7
  10.  
  11. import cv #Opencv
  12. import Image #Image from PIL
  13. import glob
  14. import os
  15.  
  16. def DetectFace(image, faceCascade, returnImage=False):
  17. # This function takes a grey scale cv image and finds
  18. # the patterns defined in the haarcascade function
  19. # modified from: http://www.lucaamore.com/?p=638
  20.  
  21. #variables
  22. min_size = (20,20)
  23. haar_scale = 1.1
  24. min_neighbors = 3
  25. haar_flags = 0
  26.  
  27. # Equalize the histogram
  28. cv.EqualizeHist(image, image)
  29.  
  30. # Detect the faces
  31. faces = cv.HaarDetectObjects(
  32. image, faceCascade, cv.CreateMemStorage(0),
  33. haar_scale, min_neighbors, haar_flags, min_size
  34. )
  35.  
  36. # If faces are found
  37. if faces and returnImage:
  38. for ((x, y, w, h), n) in faces:
  39. # Convert bounding box to two CvPoints
  40. pt1 = (int(x), int(y))
  41. pt2 = (int(x + w), int(y + h))
  42. cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 5, 8, 0)
  43.  
  44. if returnImage:
  45. return image
  46. else:
  47. return faces
  48.  
  49. def pil2cvGrey(pil_im):
  50. # Convert a PIL image to a greyscale cv image
  51. # from: http://pythonpath.wordpress.com/2012/05/08/pil-to-opencv-image/
  52. pil_im = pil_im.convert('L')
  53. cv_im = cv.CreateImageHeader(pil_im.size, cv.IPL_DEPTH_8U, 1)
  54. cv.SetData(cv_im, pil_im.tostring(), pil_im.size[0] )
  55. return cv_im
  56.  
  57. def cv2pil(cv_im):
  58. # Convert the cv image to a PIL image
  59. return Image.fromstring("L", cv.GetSize(cv_im), cv_im.tostring())
  60.  
  61. def imgCrop(image, cropBox, boxScale=1):
  62. # Crop a PIL image with the provided box [x(left), y(upper), w(width), h(height)]
  63.  
  64. # Calculate scale factors
  65. xDelta=max(cropBox[2]*(boxScale-1),0)
  66. yDelta=max(cropBox[3]*(boxScale-1),0)
  67.  
  68. # Convert cv box to PIL box [left, upper, right, lower]
  69. PIL_box=[cropBox[0]-xDelta, cropBox[1]-yDelta, cropBox[0]+cropBox[2]+xDelta, cropBox[1]+cropBox[3]+yDelta]
  70.  
  71. return image.crop(PIL_box)
  72.  
  73. def faceCrop(imagePattern,boxScale=1):
  74. # Select one of the haarcascade files:
  75. # haarcascade_frontalface_alt.xml <-- Best one?
  76. # haarcascade_frontalface_alt2.xml
  77. # haarcascade_frontalface_alt_tree.xml
  78. # haarcascade_frontalface_default.xml
  79. # haarcascade_profileface.xml
  80. faceCascade = cv.Load('haarcascade_frontalface_alt.xml')
  81.  
  82. imgList=glob.glob(imagePattern)
  83. if len(imgList)<=0:
  84. print 'No Images Found'
  85. return
  86.  
  87. for img in imgList:
  88. pil_im=Image.open(img)
  89. cv_im=pil2cvGrey(pil_im)
  90. faces=DetectFace(cv_im,faceCascade)
  91. if faces:
  92. n=1
  93. for face in faces:
  94. croppedImage=imgCrop(pil_im, face[0],boxScale=boxScale)
  95. fname,ext=os.path.splitext(img)
  96. croppedImage.save(fname+'_crop'+str(n)+ext)
  97. n+=1
  98. else:
  99. print 'No faces found:', img
  100.  
  101. def test(imageFilePath):
  102. pil_im=Image.open(imageFilePath)
  103. cv_im=pil2cvGrey(pil_im)
  104. # Select one of the haarcascade files:
  105. # haarcascade_frontalface_alt.xml <-- Best one?
  106. # haarcascade_frontalface_alt2.xml
  107. # haarcascade_frontalface_alt_tree.xml
  108. # haarcascade_frontalface_default.xml
  109. # haarcascade_profileface.xml
  110. faceCascade = cv.Load('haarcascade_frontalface_alt.xml')
  111. face_im=DetectFace(cv_im,faceCascade, returnImage=True)
  112. img=cv2pil(face_im)
  113. img.show()
  114. img.save('test.png')
  115.  
  116.  
  117. # Test the algorithm on an image
  118. #test('testPics/faces.jpg')
  119.  
  120. # Crop all jpegs in a folder. Note: the code uses glob which follows unix shell rules.
  121. # Use the boxScale to scale the cropping area. 1=opencv box, 2=2x the width and height
  122. faceCrop('testPics/*.jpg',boxScale=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement