python - 我收到此错误---> TypeError:预期为整数参数,得到 float

标签 python opencv gesture-recognition

这是使用python,opencv的简单手势识别
使用中的图书馆
在本教程中,我们将仅使用2个库

OpenCV的
脾气暴躁的
导入cv2
将numpy导入为np
从pynput.mouse导入按钮, Controller
导入wx
mouse = Controller()

app=wx.App(False)
(sx,sy)=wx.GetDisplaySize()
(camx,camy)=(320,240)

lowerBound=np.array([33,80,40])
upperBound=np.array([102,255,255])

cam= cv2.VideoCapture(0)

kernelOpen=np.ones((5,5))
kernelClose=np.ones((20,20))
pinchFlag=0

while True:
ret, img=cam.read()
img=cv2.resize(img,(340,220))

#convert BGR to HSV
imgHSV= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
# create the Mask
mask=cv2.inRange(imgHSV,lowerBound,upperBound)
#morphology
maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen)
maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose)

maskFinal=maskClose
result = cv2.findContours(maskFinal.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
conts,h=result if len(result) == 2 else result[1:3]

if(len(conts)==2):
    if(pinchFlag==1):
        pinchFlag=0
        mouse.release(Button.left)
    x1,y1,w1,h1=cv2.boundingRect(conts[0])
    x2,y2,w2,h2=cv2.boundingRect(conts[1])
    cv2.rectangle(img,(x1,y1),(x1+w1,y1+h1),(255,0,0),2)
    cv2.rectangle(img,(x2,y2),(x2+w2,y2+h2),(255,0,0),2)
    cx1=x1+w1/2
    cy1=y1+h1/2
    cx2=x2+w2/2
    cy2=y2+h2/2
    cx=(cx1+cx2)/2
    cy=(cy1+cy2)/2
    cv2.line(img, (cx1,cy1),(cx2,cy2),(255,0,0),2)
    cv2.circle(img, (cx,cy),2,(0,0,255),2)
    mouseLoc=(sx-(cx*sx/camx), cy*sy/camy)
    mouse.position=mouseLoc 
    while mouse.position!=mouseLoc:
        pass
elif(len(conts)==1):
    x,y,w,h=cv2.boundingRect(conts[0])
    if(pinchFlag==0):
        pinchFlag=1
        mouse.press(Button.left)
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    cx=x+w/2
    cy=y+h/2
    cv2.circle(img,(cx,cy),(w+h)/4,(0,0,255),2)
    mouseLoc=(sx-(cx*sx/camx), cy*sy/camy)
    mouse.position=mouseLoc 
    while mouse.position!=mouseLoc:
        pass
cv2.imshow("cam",img)
cv2.waitKey(5)
Python

我收到此错误:-
cv2.line(img,(cx1,cy1),(cx2,cy2),(255,0,0),2)。
TypeError:预期为整数参数,得到浮点数

最佳答案

cv2.line期望坐标的值是整数,即cx1,cx2,cy1,cy2的值应该是整数。因此,在编写此语句之前,请为其他代码编写cx1 = int(cx1)等,并且代码应运行。

关于python - 我收到此错误---> TypeError:预期为整数参数,得到 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62148271/

相关文章:

python - 使用生成器生成排列

python - 在本地使用 django celery beat 我得到错误 'PeriodicTask' object has no attribute '_default_manager'

python - 我如何用python中的逻辑索引替换for循环中的图像屏蔽?

math - 如何确定一个数字是否在另一个数字的百分比范围内

c++ - OpenCV - 手骨架节点

python - 如何在 Python 中美化 JSON?

python - Pandas 灵活确定指标

c++ - 如何将变换矩阵应用于 OpenCV 中的一个点?

c++ - OpenCV 减慢网络摄像头捕获

iPhone开发: gesture recognition scrolling enabled simultaneously