簡體   English   中英

在 python/opencv 中創建倒數計時器

[英]Creating a count down timer in python/opencv

我的程序

使用帶有 python 的 open CV 我正在創建一個虛擬鍵盤,它使用一個被跟蹤的對象(目前是一支藍色筆),當所述被跟蹤的對象進入矩形的邊界內時,會打印一個字母(此時我只有一個當對象相交時打印出“A”的矩形)。 這一切正常,但是正如您可以想象的那樣,當對象進入矩形邊界時,該字母會很快被打印多次。

我的問題

我需要一種方法來確保用戶可以正確輸入正確的密鑰並打印出預期數量的所述密鑰字符。 我打算這樣做的方法是創建一個計時器,該計時器僅在對象位於矩形內 3 秒后才注冊“按鍵”。 我在實際創建計時器時遇到了麻煩,這可能是一件非常簡單的事情,但是實際上我在想出解決方案時遇到了麻煩。

到目前為止我嘗試過的

我創建了一個簡單的 for 循環,它將一個整數變量設置為一個高值,然后一旦對象與矩形相交,整數就減一,然后當它等於 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()

任何建議將不勝感激謝謝。

使用時間模塊怎么樣?

這是一個偽代碼:

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  

這段代碼主要是在OpenCV中設置定時器。

  • 我在這里使用日期時間庫。 持續時間為 5 秒,如果您想要不同的持續時間,您可以將duration = 5更改為您的選擇。
  • 我使用了兩個 while 循環,外部用於大型機,內部用於持續時間。
  • 計時器的概念很簡單diff = (datetime.now() - start_time).seconds 我們只需要從當前時間中減去開始時間,並在.seconds的幫助下將毫秒轉換為秒。
  • 在第二個 while 循環中while( diff <= duration ):如果diff低於持續時間,那么它將使用此函數cv2.putText()打印幀上剩余的時間。

筆記:

  • r重置時間,按q退出

代碼從這里開始

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM