Guest User

Untitled

a guest
Jan 29th, 2020
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. # import required packages
  2. import cv2
  3. import dlib
  4. import argparse
  5. import time
  6. import numpy as np
  7.  
  8. # initialize hog + svm based face detector
  9. hog_face_detector = dlib.get_frontal_face_detector()
  10.  
  11. def enhance_image(image):
  12.     image_YCrCb = cv2.cvtColor(image, cv2.COLOR_BGR2YCR_CB)
  13.     Y, Cr, Cb = cv2.split(image_YCrCb)
  14.     Y = cv2.equalizeHist(Y)
  15.     image_YCrCb = cv2.merge([Y, Cr, Cb])
  16.     image = cv2.cvtColor(image_YCrCb, cv2.COLOR_YCR_CB2BGR)
  17.     return image
  18.  
  19. def adjust_gamma(image, gamma=1.0):
  20.     invGamma = 1.0 / gamma
  21.     table = np.array([((i / 255.0) ** invGamma) * 255
  22.                       for i in np.arange(0, 256)]).astype("uint8")
  23.     return cv2.LUT(image, table)
  24.    
  25. def scale_faces(face_rects, down_scale=1.5):
  26.     faces = []    
  27.     for face in face_rects:
  28.         scaled_face = dlib.rectangle(int(face.left() * down_scale),
  29.                                     int(face.top() * down_scale),
  30.                                     int(face.right() * down_scale),
  31.                                     int(face.bottom() * down_scale))
  32.         faces.append(scaled_face)
  33.     return faces
  34.  
  35. def detect_faces(srcimage, down_scale=1.5):
  36.     image_scaled = cv2.resize(adjust_gamma(enhance_image(srcimage), gamma=1.0), None, fx=1.0/down_scale, fy=1.0/down_scale, interpolation=cv2.INTER_LINEAR)
  37.    
  38.     start = time.time()
  39.     hog_faces = hog_face_detector(image_scaled, 1)
  40.     end = time.time()
  41.     hog_time = str(format(end - start, '.2f'))
  42.    
  43.     for face in scale_faces(hog_faces, down_scale):
  44.         x,y,w,h = face.left(), face.top(), face.right(), face.bottom()
  45.         cv2.rectangle(srcimage, (x,y), (w,h), (0,255,0), 2)
  46.        
  47.     # write at the top left corner of the image
  48.     # for color identification
  49.     img_height, img_width = srcimage.shape[:2]
  50.     cv2.putText(srcimage, "HOG=" + str(len(hog_faces)) + "|" + hog_time, (img_width-125,20), cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0,255,0), 2)
  51.     return srcimage
  52.  
  53. # handle command line arguments
  54. ap = argparse.ArgumentParser()
  55. ap.add_argument('-i', '--image', required=True, help='path to image file')
  56. ap.add_argument('-w', '--weights', default='./mmod_human_face_detector.dat',
  57.                 help='path to weights file')
  58. args = ap.parse_args()
  59. # load input image
  60. image = cv2.imread(args.image)
  61.  
  62. if image is None:
  63.     print("Could not read input image")
  64.     exit()
  65.    
  66. image = detect_faces(image, down_scale=0.5)
  67.  
  68.  
  69. # save output image
  70. cv2.imwrite(args.image, image)
  71. exit()
Add Comment
Please, Sign In to add comment