在python/opencv中创建一个倒数计时器 [英] Creating a count down timer in python/opencv

查看:296
本文介绍了在python/opencv中创建一个倒数计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序

My Program

使用带有python的开放式CV,我正在创建一个虚拟键盘,该键盘使用被跟踪的对象(此刻是一支蓝色的笔),并且当所述被跟踪的对象进入矩形的边界内时,会打印一个字母(此时我只有一个矩形,当对象相交时会打印出"A").一切正常,但是您可以想象,当对象进入矩形的边界内时,字母会很快被多次打印出来.

Using open CV with python I am creating a virtual keyboard which uses a tracked object (which at the moment is a blue pen) and when said tracked object goes within the boundary of a rectangle a letter is printed (at this time I only have one rectangle which prints out "A" when the object intersects). This is all working fine however as you can imagine when the object goes within the boundary of the rectangle the letter is printed out multiple times very quickly.

我的问题

My Problem

我需要一种方法来确保用户可以正确输入正确的键,并打印出预期数量的所述键字符.我打算这样做的方法是创建一个计时器,该计时器仅在对象进入矩形内部3秒钟后才注册按键".我在实际创建计时器时遇到了麻烦,这可能非常容易,但是在实际提出解决方案时却遇到了麻烦.

I need a way to ensure the user can enter the correct key correctly and the intended amount of said keys character is printed out. The way I intend to do this is by creating a timer that will only register the "key press" once the object has been inside the rectangle for say 3 seconds. I am having trouble however with actually creating the timer, it is probably something incredibly easy however I am having trouble actually coming up with a solution.

到目前为止我尝试过的

What I have tried so far

我创建了一个简单的for循环,该循环将整数变量设置为较高的值,然后一旦对象与矩形相交,则将整数减1,然后将其等于0,然后打印字母.代码如下:

I have created a simple for loop which set a integer variable to a high value then once the object intersects with the rectangle the integer is reduced by one then once it equals 0 the letter is printed. code is as follows:

n = 600000
while n > 0:
n=n-1
print("A")

问题是程序进入循环时几乎停滞不前,这使程序难以置信地跳动,视觉效果也很恐怖.我认为这是由代码执行的常量减法引起的,因此这不是实现我的目标的好方法.

The problem with this is that the program pretty much comes to a standstill when it gets into the loop, this make the program incredibly jumpy and the visuals look horrible. I assume this is caused by the constant subtractions the code is performing thus this is not a good method for accomplishing my goal.

我尝试过的另一种方法是使用time.sleep()并将其设置为3秒,但是由于这会暂停程序,因此再次不合适,因为在视觉上,当对象进入矩形时屏幕会冻结.

The other method I have tried is using time.sleep() and set it to say 3 seconds however as this pauses the program it again is unsuitable as visually the screen is frozen when the object enters the rectangle.

我的代码

My Code

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()

任何建议将不胜感激 谢谢.

Any suggestions would be much appreciated Thanks.

推荐答案

如何使用时间模块?

这是一个伪代码:

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  

这篇关于在python/opencv中创建一个倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆