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

2 投票
1 回答
1366 浏览
提问于 2025-04-18 16:22

以下代码在我的桌面电脑(Mint 17)上运行得很好,但当我在运行Raspbian的树莓派上尝试运行时,在第58行出现了上面提到的错误信息。我能想到的唯一原因就是Python的版本问题:

Mint使用的是Python 2.7.6

$ python -V
    Python 2.7.6

而树莓派上用的是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

如果你删除 '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)

撰写回答