如何解决从函数更改为类时的 self.attribute 错误
python 和一般编码的新手.. 不知道为什么代码会抛出这个错误:
文件“c:\Users...\Python Projects\Trials\Trial 4\findClickPositions.py”,第 18 行,在 findclickpositions
AttributeError: 'numpy.ndarray' 对象没有属性 'needle_img_path'
第 18 行是:
self.needle_img_path = str("./Trees/" + str(Tree) + ".png")
完整代码如下:
import cv2 as cv
import numpy as np
import os
from random import randint
from windowcapture import Windowcapture
from time import time
class find_object:
def __init__(self):
self.whatami = []
self.needle_img_path = str("")
def findclickpositions(self,img,Tree='tree',threshold=0.95,debug_mode=None):
os.makedirs('Trees',exist_ok=True)
self.needle_img_path = str("./Trees/" + str(Tree) + ".png")
self.treeimage = cv.imread(self.needle_img_path,cv.IMREAD_REDUCED_COLOR_2)
if not self.treeimage:
raise Exception('Image file of requested tree not found')
self.needle_img = cv.imread(self.needle_img_path,cv.IMREAD_REDUCED_COLOR_2)
self.method = cv.TM_CCOEFF_norMED
self.result = cv.matchTemplate(self.img,self.needle_img,self.method)
#cv.imshow('Result',result)
#cv.waitKey()
self.min_val,self.max_val,self.min_loc,self.max_loc = cv.minMaxLoc(self.result) #gets the best match position
#print('Best match top left postion: %s' % str(max_loc))
#print('Best match confidence: %s' % str(max_val))
self.locations = np.where(self.result >=self.threshold)
self.locations = list(zip(*self.locations[::-1]))
#print(locations)
self.needle_w = self.needle_img.shape[1]
self.needle_h = self.needle_img.shape[0]
self.rectangles = []
for self.loc in self.locations:
self.rect = [int(loc[0]),int(self.loc[1]),self.needle_w,self.needle_h]
self.rectangles.append(self.rect)
self.rectangles.append(self.rect)
self.rectangles,self.weights = cv.groupRectangles(self.rectangles,1,0.5)
#print(rectangles)
if len(self.rectangles):
#print('Matches found')
line_colour = (0,255)
line_type = cv.LINE_4
self.marker_colour = (255,255)
self.marker_type = cv.MARKER_CROSS
for (x,y,w,h) in rectangles:
self.top_left = (x,y)
self.bottom_right = (x + w,y + h) #finding bottom right corner to draw the rectangle
cv.rectangle(self.screenshot_img,self.top_left,self.bottom_right,self.line_colour,self.line_type)
self.centre_x = x + int(w/2)
self.centre_y = y + int(h/2)
self.points = []
self.points.append((centre_x,centre_y))
cv.drawMarker(self.screenshot_img,(self.centre_x,self.centre_y),self.marker_colour,self.marker_type)
#cv.imshow('Results',self.screenshot_img)
#cv.waitKey()
else:
print('No good match found')
return self.screenshot_img
我正在从主程序运行它,并向方法传递“img”的图像和“tree”的“tree”图像。我什至不知道 numpy 在哪里进入代码的这一部分,其中说有错误。我尝试将其初始化为字符串(如您在此代码中所见),但没有任何区别。即使我在这一行注释掉它,我在下一行也得到了完全相同的东西;当我输入地址时,问题出现在“self.treeimage”上。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。