Raspbian上的Python - “TypeError:'numpy.int32'对象不可迭代'”

时间:2014-08-07 10:50:22

标签: python opencv numpy raspberry-pi raspbian

以下代码在我的桌面(Mint 17)上运行没有任何问题,但是当我尝试在运行Raspbian的RPi上运行它时,我在第58行得到了上面提到的错误消息。我唯一能想到的就是&#39 ;导致它的是Python版本:

Mint使用Python 2.7.6

$ python -V
    Python 2.7.6

RPi使用2.7.3,但我已经安装了2.7.8并将其别名

$ python -V
    Python 2.7.3
$ alias python=/usr/local/bin/python2.7
$ python -V
    Python 2.7.8

我知道这里有类似的问题,但问题似乎与代码有关。我知道这段代码没关系,所以它一定是个环境问题,对吧?

无论如何,这是程序:

#!/usr/bin/env python

# Determines if a set of three images contains at least one person
# Images are taken in quick succession, the 2nd and 3rd are diff images of the first
# Adapted from sample file: /opencv/samples/python2/peopledetect.py
#
# Example:  ./pdTriple.py IMG_000.JPG IMG_001.JPG IMG_002.JPG 


import numpy as np
import cv2
import sys
from glob import glob
import itertools as it
from array import *

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:
        # the HOG detector returns slightly larger rectangles than the real objects.
        # so we slightly shrink the rectangles to get a nicer output.
        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__':

    hog = cv2.HOGDescriptor()
    hog.setSVMDetector( cv2.HOGDescriptor_getDefaultPeopleDetector() )

    count = 0
    results = np.array([0,0,0])

    for fn in it.chain(*map(glob, sys.argv[1:])):
        print fn, ' - ',

        try:
            img = cv2.imread(fn)
            if img is None:
                print 'Failed to load image file:', fn
                continue
        except:
            print 'loading error'
            continue

        # modify winstride and padding values to optimise results
        found, w = hog.detectMultiScale(img, winStride=(4,4), padding=(8,8), scale=1.05)

        found_filtered = []
        for ri, r in enumerate(found):
            for qi, q in enumerate(found):
                if ri != qi and inside(r, q):uname # errors here
                    break
            else:
                found_filtered.append(r)

        print '%d found (%d filtered)' % (len(found_filtered), len(found))
        results[count] = len(found_filtered)
        count += 1

if np.all(results>0):
    print '\n--------------', fn, 'contains a person --------------\n'

1 个答案:

答案 0 :(得分:0)

如果你删除'uname',它会工作;

for ri, r in enumerate(found):
        for qi, q in enumerate(found):
            if ri != qi and inside(r, q): # errors here
                break
        else:
            found_filtered.append(r)