Guest User

Untitled

a guest
Mar 17th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from scipy.spatial.distance import pdist,squareform
  4. from matplotlib import pyplot as plt
  5. from skimage.transform import resize
  6. if __name__=='__main__':
  7. picture_with_face_path=r'IlanAtCampNou.jpg'
  8. video_path=r'Hightechzone_video.mp4'
  9. face_cascade1 = cv2.CascadeClassifier(r'lbpcascade_frontalface_improved.xml')
  10. face_cascade2=cv2.CascadeClassifier(r'haarcascade_frontalface_default.xml')
  11. capture = cv2.VideoCapture(video_path)
  12.  
  13. width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
  14. height =capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
  15. fps = capture.get(cv2.CAP_PROP_FPS)
  16.  
  17. fourcc = cv2.VideoWriter_fourcc(*'XVID')
  18.  
  19. out = cv2.VideoWriter('Output_video.avi',fourcc , fps, (int(width), int(height)))
  20. image=cv2.imread(picture_with_face_path)
  21. distance_threshold = 50
  22. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  23. faces = face_cascade1.detectMultiScale(gray, 1.5, 5)
  24. for (x, y, w, h) in faces:
  25. cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
  26.  
  27. Ilan_color = image[y:y + h, x:x + w]
  28.  
  29. ret,im = capture.read()
  30. while(ret):
  31. gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
  32. if np.any(gray):
  33. faces1 = face_cascade1.detectMultiScale(gray, 1.05,15)
  34. faces2 = face_cascade2.detectMultiScale(gray, 1.05,12)
  35. if (len(faces1)>0) & (len (faces2)>0):
  36. xs_ys_lengths_width=np.vstack((faces1,faces2))
  37. mean_points=np.array((xs_ys_lengths_width[:,0]+xs_ys_lengths_width[:,2]/2,xs_ys_lengths_width[:,1]+xs_ys_lengths_width[:,3]/2)).T
  38. distances=squareform(pdist(mean_points))
  39. if np.all(distances<distance_threshold):
  40. faces=faces1 if len(faces1) >len(faces2) else faces2
  41. else:
  42. needed_to_be_deleted=np.bitwise_xor(distances<distance_threshold,np.eye(distances.shape[0]).astype(np.bool))
  43. indices=np.argwhere(needed_to_be_deleted)
  44. if len(indices)>0:
  45. try:
  46. indices=indices[indices[:,0]>indices[:,1]]
  47.  
  48. for ind in indices[:,0]:
  49. xs_ys_lengths_width=np.delete(xs_ys_lengths_width,ind,axis=0)
  50. except:
  51. print('Error is here')
  52. faces=xs_ys_lengths_width
  53. else:
  54. faces=faces1 if len(faces1) else faces2
  55. for (x, y, w, h) in faces:
  56. face_1=resize(Ilan_color,(h,w))
  57. im[y:y + h, x:x + w]=(255*face_1).astype(np.uint8)
  58.  
  59. # for (x, y, w, h) in faces_at_profile:
  60. # face_1=resize(Ilan_color,(h,w))
  61. # im[y:y + h, x:x + w]=(255*face_1).astype(np.uint8)
  62. out.write(im)
  63. ret, im = capture.read()
  64. capture.release()
  65. out.release()
  66. cv2.imshow('img', image)
  67. cv2.waitKey(0)
  68. cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment