import cv2 import numpy as np import matplotlib.pyplot as plt # Load video using OpenCV video = cv2.VideoCapture("Sophia.mp4") # Get video information fps = video.get(cv2.CAP_PROP_FPS) num_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) # Load face detection model face_detector = cv2.CascadeClassifier("Haar_Cascade.xml") # Lists to store information for each frame frame_matrices = [] average_rgb = [] # Loop through frames for i in range(num_frames): ret, frame = video.read() # Check if frame was successfully retrieved if not ret: break # Skip frames to get 15 frames per second if i % int(fps/15) != 0: continue # Convert to grayscale gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect faces face = face_detector.detectMultiScale( gray_frame, scaleFactor=1.1, minNeighbors=5) # print(face) # Draw a rectangle on each detected face and if face is detected store information if len(face) > 0: x, y, w, h = face[0] face = frame[y:y+h, x:x+w] frame = cv2.rectangle(frame, (int(x*1.15), int(y*1)), (x + int(w*0.7), y + int(h*0.2)), (0, 255, 0), 3) # Show image to check if the rectangle is well positioned on the forehead # cv2.imshow("Faces", frame) # cv2.waitKey(0) # Split into RGB channels b, g, r = cv2.split(face) # Calculate average on each channel avg_b = np.mean(b) / 255 avg_g = np.mean(g) / 255 avg_r = np.mean(r) / 255 # Add to list average_rgb.append([avg_b, avg_g, avg_r]) # frame_matrices.append(face) print(average_rgb) # print(frame_matrices) # Convert to numpy array average_rgb = np.array(average_rgb) print(average_rgb) np.savetxt("dataRGB", average_rgb) # Get number of frames num_frames = average_rgb.shape[0] # Create an array of frame numbers frame_numbers = np.arange(num_frames) # Plot the line graph plt.plot(frame_numbers, average_rgb[:, 0], 'b', label='Blue') plt.plot(frame_numbers, average_rgb[:, 1], 'g', label='Green') plt.plot(frame_numbers, average_rgb[:, 2], 'r', label='Red') # Add labels and legend plt.xlabel('Frame Number') plt.ylabel('Normalized Average RGB Value') plt.legend(loc='upper right') # Show the plot plt.show()