ValueError:HOG检测脚本python中数组的真值

时间:2018-01-04 21:24:41

标签: python arrays opencv raspberry-pi valueerror

当使用RaspberryPi3,Opencv3和Python3.5通过网络摄像头(logitech c170)检测到身体时,我正在编写一个带有自动img安全的全身检测脚本

这是我到目前为止所拥有的

sites = [19, 173, 1002] # the number and elements of this list vary

执行时我收到以下错误:

import numpy as np
import cv2
import os


def inside(r, q):
    rx, ry, rw, rh = r
    qx, qy, qw, qh = q
    return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh


def draw_detections(img, rects, thickness = 1):
    for x, y, w, h in rects:

        pad_w, pad_h = int(0.15*w), int(0.05*h)
        cv2.rectangle(img, (x+pad_w, y+pad_h), (x+w-pad_w, y+h-pad_h), (0, 255, 0), thickness)


if __name__ == '__main__':

    num = 0
    hog = cv2.HOGDescriptor()
    hog.setSVMDetector( cv2.HOGDescriptor_getDefaultPeopleDetector() )
    cap=cv2.VideoCapture(0)
    previously_found = False

    while True:
        _,frame=cap.read()
        found,w=hog.detectMultiScale(frame, winStride=(8,8), padding=(32,32), scale=1.05)
        if all(found) and not previously_found:
            previously_found = True
            cv2.imwrite('/home/pi/jebenter/'+'opencv'+str(num)+'.jpg',frame)
            num = num+1
        elif not all(found):
            previously_found = False

        draw_detections(frame,found)
        cv2.imshow('feed',frame)
        ch = 0xFF & cv2.waitKey(1)
        if ch == 27:
            break

    cv2.destroyAllWindows()

我尝试用Traceback (most recent call last): File "peopledetectF.py, line 30, in <module> if all(found) and not previously_found: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 替换all,但没有用。我读过有关使用any&代替* and

的内容

但是真的不知道如何应用这个。有人知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

found是一个NumPy数组。回溯告诉您解决方案。

因此,改变:

if all(found) and not previously_found:

为:

if found.all() and not previously_found:

答案 1 :(得分:0)

为什么不检查all(found),而不是检查len(found)?这应该告诉你包含你试图检测的对象的矩形的数量。

if len(found) == 0:
   print "no detections"
else:
   print str(len(found)) + " detections(s) found"