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

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

``````import cv2
import numpy as np
import time
import os

cap = cv2.VideoCapture(0)
pressed = 0

while(1):

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

## 1 个回复1

### ===============>>#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
``````