Spaces:
Running
Running
import cv2 | |
import torch | |
# Load the video | |
video_path = "path_to_video.mp4" | |
cap = cv2.VideoCapture(video_path) | |
# Video properties | |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
fps = cap.get(cv2.CAP_PROP_FPS) | |
# Create a VideoWriter object for the output video | |
out = cv2.VideoWriter("output_with_CAM.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (frame_width, frame_height)) | |
# Process each frame | |
while True: | |
ret, frame = cap.read() | |
if not ret: | |
break # End of the video | |
# Detect landmarks for left and right eye bounding boxes (example) | |
left_eye_bbox = (x_left, y_left, width_left, height_left) | |
right_eye_bbox = (x_right, y_right, width_right, height_right) | |
# Crop the eyes | |
left_eye = frame[y_left : y_left + height_left, x_left : x_left + width_left] | |
right_eye = frame[y_right : y_right + height_right, x_right : x_right + width_right] | |
# Generate CAMs for left and right eyes | |
CAM_left = generate_CAM(left_eye) # Use your model here | |
CAM_right = generate_CAM(right_eye) # Use your model here | |
# Resize CAMs if necessary | |
CAM_left_resized = cv2.resize(CAM_left, (width_left, height_left)) | |
CAM_right_resized = cv2.resize(CAM_right, (width_right, height_right)) | |
# Overlay the CAMs onto the original frame | |
frame[y_left : y_left + height_left, x_left : x_left + width_left] = CAM_left_resized | |
frame[y_right : y_right + height_right, x_right : x_right + width_right] = CAM_right_resized | |
# Write the processed frame to the output video | |
out.write(frame) | |
# Release resources | |
cap.release() | |
out.release() | |
cv2.destroyAllWindows() | |