我正按照此处显示的步骤尝试制作手势控制灯 https://makezine.com/projects/raspberry-pi-potter-wand/
但是我遇到了一个问题,我不断收到断言
def TrackWand():
global rval,old_frame,old_gray,p0,mask,color,ig,img,frame
color = (0,0,255)
rval, old_frame = cam.read()
cv2.flip(old_frame,1,old_frame)
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
equalizeHist(old_gray,old_gray)
old_gray = GaussianBlur(old_gray,(9,9),1.5)
dilate_kernel = np.ones(dilation_params, np.uint8)
old_gray = cv2.dilate(old_gray, dilate_kernel, iterations=1)
# Take first frame and find circles in it
p0 = cv2.HoughCircles(old_gray,cv2.cv.CV_HOUGH_GRADIENT,3,100,param1=100,param2=30,minRadius=4,maxRadius=15)
try:
p0.shape = (p0.shape[1], 1, p0.shape[2])
p0 = p0[:,:,0:2]
except:
print ("No points found")
# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)
while True:
rval, frame = cam.read()
cv2.flip(frame,1,frame)
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#equalizeHist(frame_gray,frame_gray)
#frame_gray = GaussianBlur(frame_gray,(9,9),1.5)
#dilate_kernel = np.ones(dilation_params, np.uint8)
#frame_gray = cv2.dilate(frame_gray, dilate_kernel, iterations=1)
try:
# calculate optical flow
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
# Select good points
good_new = p1[st==1]
good_old = p0[st==1]
# draw the tracks
for i,(new,old) in enumerate(zip(good_new,good_old)):
a,b = new.ravel()
c,d = old.ravel()
# only try to detect gesture on highly-rated points (below 15)
if (i<15):
IsGesture(a,b,c,d,i)
dist = math.hypot(a - c, b - d)
if (dist<movment_threshold):
cv2.line(mask, (a,b),(c,d),(0,255,0), 2)
cv2.circle(frame,(a,b),5,color,-1)
cv2.putText(frame, str(i), (a,b), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,0,255))
except IndexError:
print ("Index error")
cam.release()
cv2.destroyAllWindows()
break
except:
e = sys.exc_info()[0]
print ("Error: %s" % (e,))
cam.release()
cv2.destroyAllWindows()
break
img = cv2.add(frame,mask)
cv2.putText(img, "Press ESC to close.", (5, 25),
cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255,255,255))
cv2.imshow("Raspberry Potter", frame)
# get next frame
rval, frame = cam.read()
# Now update the previous frame and previous points
old_gray = frame_gray.copy()
p0 = good_new.reshape(-1,1,2)
key = cv2.waitKey(20)
if key in [27, ord('Q'), ord('q')]: # exit on ESC
break
这是断言错误
初始化点跟踪 错误:模块&#39; cv2&#39;没有属性&#39; cv&#39; OpenCV错误:断言失败(scn == 3 || scn == 4)在cvtColor中,文件/home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp,第10638行 Traceback(最近一次调用最后一次): 文件&#34; rpotter2.py&#34;,第202行,in TrackWand()
在TrackWand中输入文件&#34; rpotter2.py&#34;,第131行 old_gray = cv2.cvtColor(old_frame,cv2.COLOR_BGR2GRAY) cv2.error:/home/pi/opencv-3.3.0/modules/imgproc/src/color.cpp:10638:错误:(-215)scn == 3 || scn == 4 in function cvtColor
我对此非常陌生,所以任何帮助都会非常感激!