Advertisement
jarekmor

faces_haarcascade_cpu

Apr 12th, 2023
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. import cv2
  2. import face_recognition
  3. from datetime import datetime
  4.  
  5. # Load pre-trained face detection classifier
  6. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  7.  
  8. # Load pre-registered face encodings
  9. registered_encodings = []
  10.  
  11. # Register faces
  12. image1 = face_recognition.load_image_file('person1.jpg')
  13. face_locations1 = face_recognition.face_locations(image1)
  14. face_encodings1 = face_recognition.face_encodings(image1, face_locations1)
  15. for encoding in face_encodings1:
  16.     registered_encodings.append(encoding)
  17.  
  18. image2 = face_recognition.load_image_file('person2.jpg')
  19. face_locations2 = face_recognition.face_locations(image2)
  20. face_encodings2 = face_recognition.face_encodings(image2, face_locations2)
  21. for encoding in face_encodings2:
  22.     registered_encodings.append(encoding)
  23.  
  24. # Open the video file
  25. cap = cv2.VideoCapture('recorded_movie.mp4')
  26.  
  27. # Set the output video codec and dimensions
  28. fourcc = cv2.VideoWriter_fourcc(*'mp4v')
  29. width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
  30. height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  31. out = cv2.VideoWriter('output_movie.mp4', fourcc, 20.0, (width, height))
  32.  
  33. # Initialize the log file
  34. log_file = open('recognized_persons.log', 'w')
  35.  
  36. # Loop through each frame of the video
  37. while cap.isOpened():
  38.     # Read the frame
  39.     ret, frame = cap.read()
  40.     if not ret:
  41.         break
  42.    
  43.     # Convert the frame to grayscale for face detection
  44.     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  45.    
  46.     # Detect faces in the frame
  47.     faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
  48.    
  49.     # Loop through each detected face
  50.     for (x, y, w, h) in faces:
  51.         # Extract the face region from the frame
  52.         face_region = frame[y:y+h, x:x+w]
  53.        
  54.         # Convert the face region to a fixed-size encoding
  55.         encoding = face_recognition.face_encodings(face_region)
  56.        
  57.         # Compare the encoding to the pre-registered face encodings
  58.         for i, registered_encoding in enumerate(registered_encodings):
  59.             if face_recognition.compare_faces([encoding], registered_encoding)[0]:
  60.                 # If a match is found, draw a green rectangle around the face
  61.                 cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
  62.                
  63.                 # Write the recognized person's name and the current timestamp to the log file
  64.                 timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  65.                 log_file.write(f'{timestamp}: Recognized person {i+1}\n')
  66.                
  67.                 break
  68.    
  69.     # Write the modified frame to the output video
  70.     out.write(frame)
  71.  
  72. # Release the video capture and output objects
  73. cap.release()
  74. out.release()
  75. cv2.destroyAllWindows()
  76.  
  77. # Close the log file
  78. log_file.close()
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement