python - 需要整数参数,得到浮点错误并且浮点对象不能被解释为整数错误
问题描述
这是使用手指手势的虚拟鼠标代码。上述两个错误都出现在包含 cv2.circle() 和 cv2.line() 的语句中。我也尝试过使用 int()、round() 和 "//",但它仍然不起作用。在我使用 int() 或 round() 之后,出现了第二个错误。我希望你能帮我解决这个问题。
谢谢
import cv2
import numpy as np
from pynput.mouse import Button, Controller
import wx
mouse = Controller()
app = wx.App(False)
(sx,sy) = wx.GetDisplaySize()
(camx,camy) = (320,240)
lowerBound=np.array([20, 80, 100])
upperBound=np.array([40,200,300])
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
conts,h = cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
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)
错误不断变化。这是两个错误。
Traceback (most recent call last):
File "C:/Users/Dell/PycharmProjects/virtualmouse/main.py", line 49, in <module>
cv2.line(img, (cx1,cy1),(cx2,cy2),(255,0,0),2)
TypeError: integer argument expected, got float
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-pz4stnv8\opencv\modules\videoio\src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Traceback (most recent call last):
File "C:/Users/Dell/PycharmProjects/virtualmouse/main.py", line 63, in <module>
cv2.circle(img,(cx,cy),(w+h)/4,(0,0,255),2)
TypeError: integer argument expected, got float
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-pz4stnv8\opencv\modules\videoio\src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
解决方案
推荐阅读
- reactjs - 动作被触发,但存储值未更新
- javascript - How to short Timing Array Data with From and To keys
- angular - Angular 7 Cli does not applies angular.json schematics options in Ionic 4 project
- amazon-web-services - What is the difference between Amazon managed Blockchain and Amazon Blockchain Templates?
- android - 如何在网格回收器视图中创建加号图标以添加图像?
- php - PHP set_time_limit() 的作用域是什么?
- sql-server - 如何使用动态查询获取表中的列数
- java - 如何在Java中将@写成字符串
- python - 关于修改python列表的初学者问题
- angular - Angular 使用 bsdatepicker 发布日期 1 天回到 asp.net 核心应用程序