Guest User

Untitled

a guest
Jan 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. # USAGE
  2. # python facial_landmarks.py --shape-predictor shape_predictor_68_face_landmarks.dat --image images/example_01.jpg
  3.  
  4. # import the necessary packages
  5. from imutils import face_utils
  6. import numpy as np
  7. import argparse
  8. import imutils
  9. import dlib
  10. import cv2
  11.  
  12. # construct the argument parser and parse the arguments
  13. ap = argparse.ArgumentParser()
  14. ap.add_argument("-p", "--shape-predictor", required=True,
  15. help="path to facial landmark predictor")
  16. ap.add_argument("-i", "--image", required=True,
  17. help="path to input image")
  18. args = vars(ap.parse_args())
  19.  
  20. # initialize dlib's face detector (HOG-based) and then create
  21. # the facial landmark predictor
  22. detector = dlib.get_frontal_face_detector()
  23. predictor = dlib.shape_predictor(args["shape_predictor"])
  24.  
  25. # load the input image, resize it, and convert it to grayscale
  26. image = cv2.imread(args["image"])
  27. image = imutils.resize(image, width=500)
  28. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  29.  
  30. # detect faces in the grayscale image
  31. rects = detector(gray, 1)
  32.  
  33. # loop over the face detections
  34. for (i, rect) in enumerate(rects):
  35. # determine the facial landmarks for the face region, then
  36. # convert the facial landmark (x, y)-coordinates to a NumPy
  37. # array
  38. shape = predictor(gray, rect)
  39. shape = face_utils.shape_to_np(shape)
  40.  
  41. # convert dlib's rectangle to a OpenCV-style bounding box
  42. # [i.e., (x, y, w, h)], then draw the face bounding box
  43. (x, y, w, h) = face_utils.rect_to_bb(rect)
  44. cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
  45.  
  46. # show the face number
  47. cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10),
  48. cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
  49.  
  50. # loop over the (x, y)-coordinates for the facial landmarks
  51. # and draw them on the image
  52. for (x, y) in shape:
  53. cv2.circle(image, (x, y), 1, (0, 0, 255), -1)
  54.  
  55. # show the output image with the face detections + facial landmarks
  56. cv2.imshow("Output", image)
  57. cv2.waitKey(0)
  58.  
  59. cv2.rectangle(image,(shape[36][0], shape[36][1]), (shape[45][0],shape[33][1]), (255,0,0), 1)
Add Comment
Please, Sign In to add comment