Hello all
I am trying to optimize a bigger project for GPU but got stuck with uploading GpuMat into cv2.cuda.cvtColor in Python. I am quite a newbie, so I apologise in advance.
Here is the code:
import cv2 as cv
import timeinput_video_filename = “filename.mp4”
cap = cv.VideoCapture(input_video_filename)
gpu_frame = cv.cuda.GpuMat()
stream = cv.cuda.Stream()start_time = time.time()
frame_count = 0
while cap.isOpened():
cap.set(cv.CAP_PROP_POS_FRAMES, frame_count) ret, frame_cpu = cap.read() if not ret or frame_cpu is None: break print(f'Frame_cpu shape: {frame_cpu.shape}') print(f'ret status:{ret}') gpu_frame.upload(frame_cpu, stream) # Debugging statement to check the type of gpu_frame print(f"Type of gpu_frame: {type(gpu_frame)}\n\n") # Ensure gpu_frame is a cv.cuda.GpuMat if not isinstance(gpu_frame, cv.cuda.GpuMat): raise TypeError("gpu_frame is not a cv.cuda.GpuMat") gpu_gray = cv.cuda.cvtColor(gpu_frame, cv.COLOR_BGR2GRAY, stream) frame_gray_cpu = gpu_gray.download(stream) stream.waitForCompletion() frame_count += 1
end_time = time.time()
print(f"Processing time for stream: {end_time - start_time:.3f} seconds")
And here is the output:
Frame_cpu shape: (1080, 1920, 3)
ret status:True
Type of gpu_frame: <class ‘cv2.cuda.GpuMat’>Traceback (most recent call last):
gpu_gray = cv.cuda.cvtColor(gpu_frame, cv.COLOR_BGR2GRAY, stream) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.10.0) error: (-5:Bad argument) in function ‘cvtColor’
Overload resolution failed:
- src is not a numpy array, neither a scalar
- Expected Ptrcv::cuda::GpuMat for argument ‘dst’
- Expected Ptrcv::UMat for argument ‘src’
All in all, I have no idea why cv2.cuda.cvtColor doesn’t like the frame I use. I tried to check the parameters of the first argument so they fit those that are provided in the documentation for cvtColor, but gpu_frame.depth() returns 0, and I am having trouble interpreting that.
I am quite in the woods. Please send helpz.
Thanks!