Scanning directory and processing multiple files with cv2.VideoCapture()

Man… I’ve been staring at this too long, I missed the actual difference in the paths (Motiondetection_Batch_Heatmap\Burglary.mp4 != Motiondetection_Batch_Heatmap/videofiles/Burglary.mp4)

Just changed:

mp4Files = [[f.name[:-4], os.path.abspath(f.name)] for f in dircontent if f.name.lower().endswith('.mp4')]

To:

mp4Files = [[f.name[:-4], os.path.abspath(f)] for f in dircontent if f.name.lower().endswith('.mp4')]

So to conclude, this works as expected:

# file operations
import os
# Motion detection
import cv2


# Scan target directory
# os.scandir returns an iterator, make it a list to be re-usable

# Target directory is in the same directory as the script
targetDirectory = "videofiles"
dircontent = list(os.scandir(targetDirectory))

# Search for mp4 files and make a list [ [filename, path], [filename, path], ... ]
mp4Files = [[f.name[:-4], os.path.abspath(f)] for f in dircontent if f.name.lower().endswith('.mp4')]


for videofile in mp4Files:

    cap = cv2.VideoCapture(videofile[1])

    ret, frame1 = cap.read()
    heatmap = frame1.copy()