cv2.VideoCapture leaking memory...
import psutil
import os
import random
import cv2
import gc
def logMem():
uss = psutil.Process(os.getpid()).memory_full_info().uss / 1024 / 1024
print(f"Memory usage: {uss} MB")
def Test(path):
print("-"*100)
print(path)
cap = cv2.VideoCapture(path)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print("init")
logMem()
for i in range(20):
k = random.randint(0, total_frames)
_ = cap.set(cv2.CAP_PROP_POS_FRAMES, k)
ret, frame = cap.read()
del ret, frame
print("finish")
logMem()
cap.release()
del cap
gc.collect()
gc.collect()
gc.collect()
print("del")
logMem()
print("-"*100)
path = 'PATH_TO_VIDEOS_FOLDER'
videos = os.listdir(path)
for i in range(10):
Test(path + random.choice(videos))
Memory keep growing after every loop iter.
What is your OS and library version? What VideoCapture backend is used?
I've also encountered a similar issue. When trying to process videos using h264 encoding, I noticed that memory was being consumed and remained occupied even after the program ended.
OpenCV version: 4.12.0.88
The version of the libx264 library used is as follows:
libx264-155/now 2:0.155.2917+git0a84d98-2 amd64 [installed,local]
libx264-dev/now 2:0.155.2917+git0a84d98-2 amd64 [installed,local]
could you run the process with the following environment variables and share logs here:
export OPENCV_LOG_LEVEL=DEBUG
export OPENCV_VIDEOIO_DEBUG=1
The variables activate more logging.
The logs are as follows, the last part of the log content is similar, omitting several lines:
>>> import cv2
>>> cap = cv2.VideoCapture("y18.mp4")
[DEBUG:[email protected]] global videoio_registry.cpp:225 VideoBackendRegistry VIDEOIO: Builtin backends(9): FFMPEG(1000); FFMPEG(990); GSTREAMER(980); INTEL_MFX(970); V4L2(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930); OBSENSOR(920)
[DEBUG:[email protected]] global videoio_registry.cpp:249 VideoBackendRegistry VIDEOIO: Available backends(9): FFMPEG(1000); FFMPEG(990); GSTREAMER(980); INTEL_MFX(970); V4L2(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930); OBSENSOR(920)
[ INFO:[email protected]] global videoio_registry.cpp:251 VideoBackendRegistry VIDEOIO: Enabled backends(9, sorted by priority): FFMPEG(1000); FFMPEG(990); GSTREAMER(980); INTEL_MFX(970); V4L2(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930); OBSENSOR(920)
[ WARN:[email protected]] global cap.cpp:141 open VIDEOIO(FFMPEG): trying capture filename='y18.mp4' ...
[DEBUG:[email protected]] global cap_ffmpeg_impl.hpp:1268 open FFMPEG: stream[0] is video stream with codecID=27 width=1920 height=1080
[DEBUG:[email protected]] global cap_ffmpeg_hw.hpp:934 HWAccelIterator FFMPEG: allowed acceleration types (none): ''
[ WARN:[email protected]] global cap.cpp:153 open VIDEOIO(FFMPEG): created, isOpened=1
>>> codec = int(cap.get(cv2.CAP_PROP_FOURCC))
>>> fps = int(cap.get(cv2.CAP_PROP_FPS))
>>> height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
>>> width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
>>> out = cv2.VideoWriter("test.mp4", codec, fps, (width, height))
[ WARN:[email protected]] global cap.cpp:737 open VIDEOIO(FFMPEG): trying writer with filename='test.mp4' fourcc=0x34363268 fps=15 sz=1920x1080 isColor=1...
[DEBUG:[email protected]] global cap_ffmpeg_impl.hpp:2959 open Selected pixel format: bgr24
OpenCV: FFMPEG: tag 0x34363268/'h264' is not supported with codec id 27 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x31637661/'avc1'
[DEBUG:[email protected]] global cap_ffmpeg_hw.hpp:934 HWAccelIterator FFMPEG: allowed acceleration types (none): ''
[ WARN:[email protected]] global cap.cpp:753 open VIDEOIO(FFMPEG): created, isOpened=1
>>> while cap.isOpened():
... ret, frame = cap.read()
... if not ret:
... break
... out.write(frame)
...
[DEBUG:[email protected]] global cap_ffmpeg_impl.hpp:1719 retrieveFrame Input picture format: yuv420p
[DEBUG:[email protected]] global cap_ffmpeg_impl.hpp:1719 retrieveFrame Input picture format: yuv420p
[DEBUG:[email protected]] global cap_ffmpeg_impl.hpp:1719 retrieveFrame Input picture format: yuv420p
[DEBUG:[email protected]] global cap_ffmpeg_impl.hpp:1719 retrieveFrame Input picture format: yuv420p
[DEBUG:[email protected]] global cap_ffmpeg_impl.hpp:1719 retrieveFrame Input picture format: yuv420p
[DEBUG:[email protected]] global cap_ffmpeg_impl.hpp:1719 retrieveFrame Input picture format: yuv420p
[DEBUG:[email protected]] global cap_ffmpeg_impl.hpp:1719 retrieveFrame Input picture format: yuv420p
......
Update: I tried using mp4v encoding, and the memory leak issue does not occur.