exports all frames from a given video file

This commit is contained in:
Loic Delattre 2023-02-18 15:25:56 +01:00
parent 7f8cd87cbb
commit 3d97712808
1 changed files with 30 additions and 11 deletions

View File

@ -3,14 +3,34 @@ import os.path
import os import os
from PIL import Image from PIL import Image
# Opens the Video file ###############
# def get_frames(input1)
# ex. get_frames(video.mov)
#
# Task: Extracting frames from a video
#
# Inputs:
# - input1: a video
#
# Output:
# -RGB_avg: a directory with all the frames of the video
#
# author: Loic Delattre and Maryne Dey (loic.delattre@ecam.fr, maryne.dey@ecam.fr)
# date: 06/02/2023
###############
video = 'my_face.mov' video = 'my_face.mov'
path_to_script = os.path.dirname(os.path.abspath(__file__))
if os.path.exists(path_to_script + r"\frames") == False:
os.mkdir(path_to_script + r"\frames")
def get_frames(vid): def get_frames(vid):
path_to_script = os.path.dirname(os.path.abspath(__file__))
if os.path.exists(path_to_script + r"\frames") == False:
os.mkdir(path_to_script + r"\frames")
# Opens the Video file
cap= cv2.VideoCapture(vid) cap= cv2.VideoCapture(vid)
i=0 i=0
while cap.isOpened(): while cap.isOpened():
ret, img = cap.read() ret, img = cap.read()
@ -21,15 +41,15 @@ def get_frames(vid):
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5) faces = face_cascade.detectMultiScale(gray, 1.3, 5) #detect the face in the frame
for (x,y,w,h) in faces: for (x,y,w,h) in faces:
# To draw a rectangle in a face # To draw a rectangle in a face for testing purposes
cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2) cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)
roi_gray = gray[y:y+h, x:x+w] roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w] roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray) eyes = eye_cascade.detectMultiScale(roi_gray) #detect the eyes in the frames taking the face as reference
list_ex = [] list_ex = []
list_ey = [] list_ey = []
list_ew = [] list_ew = []
@ -46,10 +66,10 @@ def get_frames(vid):
#rectangle on forhead #rectangle on forhead
try: try:
fx = min(list_ex) + list_ew[list_ex.index(min(list_ex))] fx = min(list_ex) + list_ew[list_ex.index(min(list_ex))] #deducing ROI from the eyes rectangle coordinates
fy = max(list_ey) fy = max(list_ey)
x1 = x + fx x1 = x + fx
x2 = x + fx + 100 x2 = x + fx + 100 #extra values in x and y in the parameters are adjustements made after manual testing
y1 = y + fy - 150 y1 = y + fy - 150
y2 = y + fy - 20 y2 = y + fy - 20
@ -59,8 +79,7 @@ def get_frames(vid):
i+=1 i+=1
except: except:
print('error on min value of ex') print('error on min value of ex')
#extra values in x and y in the parameters are adjustements made after manual testing #cv2.rectangle(roi_color, (fx,fy-150),(fx+100,fy-20),(0,127,255),2) #for testing purposes
#cv2.rectangle(roi_color, (fx,fy-150),(fx+100,fy-20),(0,127,255),2)
cap.release() cap.release()
cv2.destroyAllWindows() cv2.destroyAllWindows()