我的计划
使用python打开CV我正在创建一个虚拟键盘,它使用一个跟踪对象(此刻是一支蓝色笔),当所述跟踪对象进入一个矩形边界时,会打印一个字母(此时我只有一个矩形打印出来" A"当物体相交时)。这一切都正常,但是你可以想象当对象进入矩形的边界内时,字母会很快打印出来多次。
我的问题
我需要一种方法来确保用户可以正确输入正确的密钥,并打印出所需的密钥字符数量。我打算这样做的方法是创建一个只注册"按键"一旦物体在矩形内部持续3秒钟。我实际上创建计时器时遇到了麻烦,但这可能非常容易,但实际上我找不到解决方案。
到目前为止我尝试了
我创建了一个简单的for循环,它将一个整数变量设置为一个高值,然后一旦该对象与矩形相交,则整数减1,然后一旦等于0则打印出一个字母。代码如下:
n = 600000
while n > 0:
n=n-1
print("A")
这个问题是程序在进入循环时几乎陷入停顿,这使程序难以置信地跳跃,视觉效果看起来很糟糕。我认为这是由代码执行的持续减法引起的,因此这不是实现我的目标的好方法。
我尝试的另一种方法是使用time.sleep()并将其设置为3秒,但是当暂停程序时它再次不合适,因为当对象进入矩形时屏幕被冻结。
我的代码
import cv2
import numpy as np
import time
import os
cap = cv2.VideoCapture(0)
pressed = 0
while(1):
# read the frames
_,frame = cap.read()
# smooth it
frame = cv2.blur(frame,(3,3))
# convert to hsv and find range of colors
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
thresh = cv2.inRange(hsv,np.array((75, 96, 205)), np.array((144, 233, 255)))
thresh2 = thresh.copy()
# find contours in the threshold image
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
# finding contour with maximum area and store it as best_cnt
max_area = 0
for cnt in contours:
area = cv2.contourArea(cnt)
if area > max_area:
max_area = area
best_cnt = cnt
# finding centroids of best_cnt and draw a circle there
M = cv2.moments(best_cnt)
cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
cv2.circle(frame,(cx,cy),5,255,-1)
if cx < 100 and cy < 100:
cv2.rectangle(frame,(10,0),(100,100),(255,0,0),3)
pressed = 1
if pressed == 1:
n = 9000000
while n > 0:
n=n-1
print("A")
pressed = 0
else:
cv2.rectangle(frame,(10,0),(100,100),(0,255,0),3)
pressed = 0
# Show it, if key pressed is 'Esc', exit the loop
cv2.imshow('frame',frame)
cv2.imshow('thresh',thresh2)
if cv2.waitKey(33)== 27:
break
# Clean up everything before leaving
cv2.destroyAllWindows()
cap.release()
我们非常感谢任何建议 感谢。
答案 0 :(得分:1)
这段代码主要是在OpenCV中设置定时器。
duration = 5
更改为您的选择。diff = (datetime.now() - start_time).seconds
。我们只需要从当前时间中减去开始时间,然后借助 .seconds
将毫秒转换为秒。while( diff <= duration ):
中,如果 diff 低于 duration,那么它将使用这个打印帧上剩余的时间函数 cv2.putText()
。import cv2
from datetime import datetime
# the duration (in seconds)
duration = 5
cap = cv2.VideoCapture(0+cv2.CAP_DSHOW)
qu = 0
while True:
ret, frame = cap.read()
start_time = datetime.now()
diff = (datetime.now() - start_time).seconds # converting into seconds
while( diff <= duration ):
ret, frame = cap.read()
cv2.putText(frame, str(diff), (70,70), cv2.FONT_HERSHEY_SIMPLEX , 1, (255, 0, 0), 2, cv2.LINE_AA)# adding timer text
cv2.imshow('frame',frame)
diff = (datetime.now() - start_time).seconds
k = cv2.waitKey(10)
if k & 0xFF == ord("r"): # reset the timer
break
if k & 0xFF == ord("q"): # quit all
qu = 1
break
if qu == 1:
break
cap.release()
cv2.destroyAllWindows()
答案 1 :(得分:0)
如何使用时间模块?
这是一个伪代码:
import time
time_count = 0 # init
#processing routine start
start_time = time.time()
processing
#processing ends
end_time = time.time()
if(object_in_square):
time_count + = end_time - start_time
if(time_count > time_defined_by_you (ie 3 sec or whatever you choose to keep):
# press confirm
# take action
else:
time_count = 0