问题描述
我正在尝试使用带有openCV4.4的CUDA进行级联分类,但是当我运行detectMultiScale函数时,它给我带来了分段错误。我在做什么错了?
openCV中python中CUDA的文档有限,因此很难找到使用CUDA进行级联分类的正确过程。
我的系统:
- Quadro P620
- Debian 6.3.0-18
- Python 3.5.3
- 使用CUDA = ON,CUDNN = ON构建OpenCV 4.5.0
我想出的代码:
vidcap = cv2.VideoCapture('video_file.mp4')
classifier_cuda = cv2.cuda_CascadeClassifier('cascades_file.xml')
while True:
success,frame = vidcap.read()
cuda_frame = cv2.cuda_GpuMat(frame)
result = classifier_cuda.detectMultiScale(cuda_frame)
print (result)
classifier_cuda和cuda_frame分别被识别为<cuda_GpuMat 0x7fffa9446d10> <cuda_CascadeClassifier 0x7fffa9446cf0>
此问题通过将代码更改为:
classifier_cuda = cv2.cuda.CascadeClassifier_create('model.xml')
while True:
success,frame = vidcap.read()
cuFrame = cv2.cuda_GpuMat(frame)
output2 = cv2.cuda_GpuMat()
output = classifier_cuda.detectMultiScale(cuFrame,output2)
# And then its unclear what to use to get the detections
# I Tried:
final = classifier_cuda.convert(output)
# And:
final = classifier_cuda.convert(output2)
# And:
final = output.download()
# And:
final = output2.download()
现在的问题是结果全是空的。因此,我应该如何从我的detectmultiscale提取数据?我需要一个边界框列表[x,y,w,h]。
解决方法
output.download()
实际上对我有用,但是如果未检测到任何内容,则为None
。
我认为您的问题是您需要将帧转换为COLOR_BGR2GRAY
。
这应该有效:
source = cv2.VideoCapture('video_file.mp4')
classifier = cv2.cuda.CascadeClassifier_create('model.xml')
while True:
success,frame = source.read()
gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gpu_frame = cv2.cuda_GpuMat(gray_frame)
gpu_result = classifier.detectMultiScale(gpu_frame) # cuda_GpuMat
result = gpu_result.download() # UMat
if result is not None:
for item in result[0]:
(x,y,w,h) = item
print(x,h)
(OpenCV 4.4)