无法pickle序列化cv2.VideoCapture对象问题求助
Hey there, let's break down what's happening here and how to fix it!
First off, let's get one thing straight: cv2.VideoCapture objects were never meant to be pickled in the first place. These objects are tightly tied to system-level resources (like video file handles, codec contexts, etc.) that can't be safely serialized and reconstructed across sessions or processes. The fact that it worked before was likely a quirk of an older OpenCV version or specific system setup—recent changes (either to OpenCV itself or your system's underlying libraries) probably closed that loophole, which is why you're now hitting the TypeError.
Here are the most reliable solutions to get your workflow back on track:
1. Serialize state parameters instead of the VideoCapture object
Instead of trying to save the entire VideoCapture instance, extract and save the critical state you need to reconstruct it later. This is the safest and most maintainable approach.
Example code for saving state:
import cv2 import pickle import gzip # Initialize your video capture video = cv2.VideoCapture('some_video.mp4') # Extract key state values video_state = { 'video_path': 'some_video.mp4', 'current_frame': video.get(cv2.CAP_PROP_POS_FRAMES), 'total_frames': video.get(cv2.CAP_PROP_FRAME_COUNT), 'is_opened': video.isOpened() } # Save the state with pickle/gzip with gzip.open("video_state.pkl", "wb") as pickle_out: pickle.dump(video_state, pickle_out, protocol=pickle.HIGHEST_PROTOCOL) # Clean up the original capture video.release() print("State saved successfully")
And to reconstruct the VideoCapture later:
import cv2 import pickle import gzip # Load the saved state with gzip.open("video_state.pkl", "rb") as pickle_in: video_state = pickle.load(pickle_in) # Reinitialize and restore state video = cv2.VideoCapture(video_state['video_path']) if video_state['is_opened']: video.set(cv2.CAP_PROP_POS_FRAMES, video_state['current_frame']) # Now you can continue working with the video capture as before
2. Roll back to your previous OpenCV version
If you're set on keeping the original workflow (though I don't recommend it long-term), you can try reverting to the specific OpenCV version that worked for you. Since you're using Python 3.6, you'll need a compatible older version. For example:
pip uninstall opencv-python pip install opencv-python==3.4.15.55
Just make sure to use the exact version number that was installed when your code worked previously.
3. Verify system-level dependencies
Sometimes updates to underlying libraries (like FFmpeg, which OpenCV uses for video handling) can change how VideoCapture objects behave. Check if your system has recently updated FFmpeg or related multimedia libraries—if so, you might need to roll those back too, though this is more complex and less recommended than the first two options.
Remember: Even if you get the old setup working again, relying on pickling VideoCapture objects is fragile. The state-serialization approach is far more robust and future-proof.
内容的提问来源于stack exchange,提问作者Nadav Schweiger

