Guest User

Untitled

a guest
Jul 16th, 2019
165
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # import the necessary packages
  2. from imutils import face_utils
  3. import numpy as np
  4. import argparse
  5. import imutils
  6. import dlib
  7. import cv2
  8.  
  9. # construct the argument parser and parse the arguments
  10. ap = argparse.ArgumentParser()
  11. ap.add_argument("-p", "--shape-predictor", required=True,
  12. help="path to facial landmark predictor")
  13. ap.add_argument("-i", "--image", required=True,
  14. help="path to input image")
  15. args = vars(ap.parse_args())
  16.  
  17. # initialize dlib's face detector (HOG-based) and then create
  18. # the facial landmark predictor
  19. detector = dlib.get_frontal_face_detector()
  20. predictor = dlib.shape_predictor(args["shape_predictor"])
  21.  
  22. # load the input image, resize it, and convert it to grayscale
  23. image = cv2.imread(args["image"])
  24. image = imutils.resize(image, width=500)
  25. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  26.  
  27. # detect faces in the grayscale image
  28. rects = detector(gray, 1)
  29. FACIAL_LANDMARKS_IDXS = [
  30. ("Right Eye", (36, 42)),
  31. ("Left Eye", (42, 48))
  32. ]
  33.  
  34. # loop over the face detections
  35. for (i, rect) in enumerate(rects):
  36. # determine the facial landmarks for the face region, then
  37. # convert the landmark (x, y)-coordinates to a NumPy array
  38. shape = predictor(gray, rect)
  39. shape = face_utils.shape_to_np(shape)
  40.  
  41. # loop over the face parts individually
  42. for (name, (i, j)) in FACIAL_LANDMARKS_IDXS:
  43. # clone the original image so we can draw on it, then
  44. # display the name of the face part on the image
  45. clone = image.copy()
  46. cv2.putText(clone, name, (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
  47. 0.7, (0, 0, 255), 2)
  48.  
  49. # loop over the subset of facial landmarks, drawing the
  50. # specific face part
  51. for (x, y) in shape[i:j]:
  52. cv2.circle(clone, (x, y), 1, (0, 0, 255), -1)
  53.  
  54. # extract the ROI of the face region as a separate image
  55. (x, y, w, h) = cv2.boundingRect(np.array([shape[i:j]]))
  56. roi = image[y:y + h, x:x + w]
  57. roi = imutils.resize(roi, width=250, inter=cv2.INTER_CUBIC)
  58.  
  59. # show the particular face part
  60. cv2.imshow("Extracted image", roi)
  61. cv2.imshow("Image", clone)
  62.  
  63. cv2.waitKey(0)
  64. cv2.imwrite("/Users/User/Downloads/detect-face-
  65. parts/images/new.jpg", roi);
RAW Paste Data