【发布时间】:2018-11-20 12:18:27
【问题描述】:
在回答我的问题之前,我知道这是一个非常常见的问题,但取决于代码,可能有多种解决方案,所以如果有人能帮助我,我将非常感激。
总的来说,我对 Python 和 ML 非常陌生。所以在运行我的 create_gesture.py 文件时,我得到了 NoneType 错误。 我的代码:
create_folder("gestures/" + str(g_id))
pic_no = 0
flag_start_capturing = False
frames = 0
while True:
img = cam.read()[1]
img = cv2.flip(img, 1)
imgCrop = img[y:y + h, x:x + w]
imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([imgHSV], [0, 1], hist, [0, 180, 0, 256], 1)
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10))
cv2.filter2D(dst, -1, disc, dst)
blur = cv2.GaussianBlur(dst, (11, 11), 0)
blur = cv2.medianBlur(blur, 15)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
thresh = cv2.merge((thresh, thresh, thresh))
thresh = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
thresh = thresh[y:y + h, x:x + w]
contours = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]
我收到此错误:
Traceback (most recent call last):
File "create_gestures.py", line 120, in <module>
store_images(g_id)
File "create_gestures.py", line 70, in store_images
imgCrop = img[y:y + h, x:x + w]
TypeError: 'NoneType' object is not subscriptable
编辑:
找出行中的整数值
img = cam.read()[1]
抛出错误。应该是 0 而不是 1。但现在我得到一个不同的错误:
Traceback (most recent call last):
File "create_gestures.py", line 121, in <module>
store_images(g_id)
File "create_gestures.py", line 72, in store_images
imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.error: OpenCV(3.4.1) D:\Build\OpenCV\opencv 3.4.1\modules\imgproc\src\color.cpp:11109: error: (-215) depth == 0 || depth == 2 || depth == 5 in function cv::cvtColor
【问题讨论】: