首页 > 解决方案 > TypeError:迭代 0 维数组 Python(图像检测)

问题描述

我正在尝试制作一个对象检测器,该检测器将从def(x1,y1,x2,y2)

但是我在运行代码时得到的错误是 TypeError: iteration over a 0-d array

错误行,它显示第 28 行,所以for (x,y,w,h) in inp

这段代码甚至可以工作吗?

我制作的代码:

import cv2
import numpy as np
import time
from PIL import ImageGrab
import pyautogui

def object_detection(File_Name, x1 ,y1 ,x2 ,y2):
    F1 = File_Name
    while (True):
        screen =  np.array(ImageGrab.grab(bbox=(x1,y1,x2,y2)))
        last_time = time.time()
        cv2.imshow('window',cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))
            
        inp = pyautogui.locateOnScreen(File_Name)
        inp = np.array(inp)
            
            
        if inp is None:
            print("No " + F1 + ' Found On Screen')
            
            
        if inp is not None:
            print(F1 + 'Found On' + File_Name)
            
            
        for (x,y,w,h) in inp:
            rect = cv2.rectangle(screen,(x,y),(x+w,y+h),(0,255,0),2)

        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

object_detection('Capture.PNG', 6, 138,767, 827)

标签: python

解决方案


推荐阅读