landmarks_demo.py 1.0 KB

1234567891011121314151617181920212223242526272829
  1. import random
  2. import numpy as np
  3. import cv2 as cv
  4. frame1 = cv.imread(cv.samples.findFile('lena.jpg'))
  5. if frame1 is None:
  6. print("image not found")
  7. exit()
  8. frame = np.vstack((frame1,frame1))
  9. facemark = cv.face.createFacemarkLBF()
  10. try:
  11. facemark.loadModel(cv.samples.findFile('lbfmodel.yaml'))
  12. except cv.error:
  13. print("Model not found\nlbfmodel.yaml can be download at")
  14. print("https://raw.githubusercontent.com/kurnianggoro/GSOC2017/master/data/lbfmodel.yaml")
  15. cascade = cv.CascadeClassifier(cv.samples.findFile('lbpcascade_frontalface_improved.xml'))
  16. if cascade.empty() :
  17. print("cascade not found")
  18. exit()
  19. faces = cascade.detectMultiScale(frame, 1.05, 3, cv.CASCADE_SCALE_IMAGE, (30, 30))
  20. ok, landmarks = facemark.fit(frame, faces=faces)
  21. cv.imshow("Image", frame)
  22. for marks in landmarks:
  23. couleur = (random.randint(0,255),
  24. random.randint(0,255),
  25. random.randint(0,255))
  26. cv.face.drawFacemarks(frame, marks, couleur)
  27. cv.imshow("Image Landmarks", frame)
  28. cv.waitKey()