Advertisement
jarekmor

faces_haarcascade_cuda

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