Python ';非类型';对象没有属性';形状'。。。即使我能打印出来
我试图在视频中找到垂直线。Python 2.7和OpenCV 3。 我使用背景减法,然后应用Canny边缘检测滤波器。 我已经能够将HoughLinesP方法应用于单个图像,但需要将其扩展到视频 我在运行一组基本代码时收到此错误(我认为这与下面的行(30-33)对应): 但是,只要我尝试使用hlines.shape属性,事情就会变得不稳定Python ';非类型';对象没有属性';形状'。。。即使我能打印出来,python,opencv,houghlinesp,Python,Opencv,Houghlinesp,我试图在视频中找到垂直线。Python 2.7和OpenCV 3。 我使用背景减法,然后应用Canny边缘检测滤波器。 我已经能够将HoughLinesP方法应用于单个图像,但需要将其扩展到视频 我在运行一组基本代码时收到此错误(我认为这与下面的行(30-33)对应): 但是,只要我尝试使用hlines.shape属性,事情就会变得不稳定 import cv2 import numpy as np import imutils np.set_printoptions(threshold=np
import cv2
import numpy as np
import imutils
np.set_printoptions(threshold=np.inf) #to print entire array, no truncation
LOWER_BOUND = 55 #cv2.threshold()
UPPER_BOUND = 255 #cv2.threshold()
CANNY_LOWER_BOUND = 10 #cv2.Canny()
CANNY_UPPER_BOUND = 250 #cv2.Canny()
MIN_LINE_LENGTH = 2 #HoughLinesP()
MAX_LINE_GAP = 100 #HoughLinesP()
HOUGH_THETA = np.pi/180 #HoughLinesP() angle resolution of the accumulator, radians
HOUGH_THRESHOLD = 25 #HoughLinesP()
HOUGH_RHO = 1 #HoughLinesP() rho, Distance resolution of the accumulator, pixels
#background subtraction parameter
bkgnd = cv2.bgsegm.createBackgroundSubtractorMOG()
camera =cv2.VideoCapture('/home/odroid/Desktop/python_scripts/test/test_images/Edited_Foam_Dispense_Short.mp4')
while(True):
(grabbed, frame) = camera.read()
img_sub = bkgnd.apply(frame)#, learningRate = 0.001)
canny_threshold = cv2.Canny(img_sub, CANNY_LOWER_BOUND, CANNY_UPPER_BOUND)
hlines = cv2.HoughLinesP(canny_threshold, HOUGH_RHO, HOUGH_THETA, MIN_LINE_LENGTH, MAX_LINE_GAP)
print(hlines)
print(hlines.shape)
#a,b,c = hlines.shape
#for k in range(a):
#print(hlines.shape)
#break
#cv2.line(frame, (hlines[k][0][0], hlines[k][0][1]), (hlines[k][0][2], hlines[k][0][3]), (0,255,0), 3, cv2.LINE_AA)
# print("getting hlines")
# break
cv2.imshow('canny_threshold', canny_threshold)
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
camera.release()
cv2.destroyAllWindows()
如有任何建议,将不胜感激
编辑:@伊万·波兹代夫指给我看 它看起来确实适用。但是,在尝试遵循他们的解决方案时,我发现以下错误: 回溯(最近一次呼叫最后一次): 文件“test3.py”,第37行,在 对于hline中的l: TypeError:“非类型”对象不可编辑 这是修改后的代码段:
hlines = cv2.HoughLinesP(canny_threshold, HOUGH_RHO, HOUGH_THETA, MIN_LINE_LENGTH, MAX_LINE_GAP)
for l in hlines:
leftx, boty, rightx, topy = l[0]
line = Line((leftx, boty), (rightx, topy))
line.draw(frame, (0, 255, 0), 2)
现在,如果我没有数据,这对我来说是有意义的……但我确实有数据在hlines中返回。我可以使用print(hlines)语句将其打印出来。但是,它的格式与OpenCV 2.4不同……但是,现在我想知道为什么它在另一个问题中起作用
编辑2:啊哈!正在进步。这是脚本的输出(print(hlines)),如果我只循环它两次……即,当run_once=0,而(run_once遵循@ivan_pozdeev在上面发布的链接并检查返回类型None时,修复了该问题。
HoughLinesP函数在第一帧返回一个空数组(返回“None”)。这导致了行中的错误。添加检查修复了该问题
if hlines is None: #in case HoughLinesP fails to return a set of lines
#make sure that this is the right shape [[ ]] and ***not*** []
hlines = [[0,0,0,0]]
else:
for l in hlines:
leftx, boty, rightx, topy = l[0]
cv2.line(frame, (leftx, boty), (rightx, topy), (0, 255, 0), 2)
按照@ivan_pozdeev在上面发布的链接,并检查返回类型是否为None,解决了该问题。
HoughLinesP函数在第一帧返回一个空数组(返回“None”)。这导致了行中的错误。添加检查修复了该问题
if hlines is None: #in case HoughLinesP fails to return a set of lines
#make sure that this is the right shape [[ ]] and ***not*** []
hlines = [[0,0,0,0]]
else:
for l in hlines:
leftx, boty, rightx, topy = l[0]
cv2.line(frame, (leftx, boty), (rightx, topy), (0, 255, 0), 2)
您
而True
语句没有缩进。hlines
不是数组。shape
属性只能用于数组。为什么要执行后台减法将其从数组更改为…而不是数组?这在我的脑海中是没有意义的。可能重复的错误意味着行
是非数组的e。你while True
语句没有缩进。hlines
不是数组。shape
属性只能用于数组。为什么做后台减法会将其从数组更改为…而不是数组?这在我的头脑中没有意义。可能重复的错误意味着行
是错误的None
。剩下的就是找出它为什么是None
。文档中没有提到这种可能性。我检查过,没有发现任何立即出现的情况。仔细检查了链接-没关系。可能在你的国家或其他地方被阻止了。可能正在工作……我会摆弄它。你检查的是哪个来源?HoughLinesP?剩下的就是找出它为什么无
。文档中没有提到这种可能性。我检查过,没有发现任何明显的案例。仔细检查了链接-没关系。可能在你的国家或其他地方被阻止了。可能正在工作……我会摆弄它。你检查的是哪一个来源?th我是HoughLinesP?
None
[[[690 419 693 419]]
[[672 419 679 419]]
[[696 417 701 417]]
[[713 419 714 419]]
[[504 418 504 418]]
[[688 419 688 419]]
[[672 417 679 417]]
[[510 419 511 419]]
[[693 418 693 417]]
[[688 417 688 417]]
[[692 417 692 417]]
[[696 419 699 419]]
[[713 417 714 417]]
[[686 419 686 419]]
[[622 417 622 417]]
[[690 417 690 417]]
[[506 417 506 417]]
[[622 419 623 419]]
[[505 419 505 419]]
[[686 417 687 417]]
[[506 419 506 419]]
[[700 419 701 418]]
[[509 418 509 418]]
[[623 417 623 417]]
[[510 417 511 417]]
[[712 418 712 418]]
[[685 418 685 418]]
[[689 417 689 417]]
[[689 419 689 419]]
[[671 418 671 418]]
[[691 417 691 417]]]
if hlines is None: #in case HoughLinesP fails to return a set of lines
#make sure that this is the right shape [[ ]] and ***not*** []
hlines = [[0,0,0,0]]
else:
for l in hlines:
leftx, boty, rightx, topy = l[0]
cv2.line(frame, (leftx, boty), (rightx, topy), (0, 255, 0), 2)