title: python opencv display time countdown using putText in webcam video
body: <p><strong>Aim:</strong> </p>
  
  <p>I'd like to put text on each frame obtained by the webcam so that the text "3", "2", "1" can be displayed for a second each. Thus portraying a countdown timer. Following the countdown, a frame should be written to file i.e. saved to disk. This should be repeatable as long as the video stream has not been closed.  </p>
  
  <p>Each frame is obtained within a while loop and due to hardware configuration the camera may have an unknown frame rate or worse, the number of frames retrieved by the camera per second can vary during the course of running the camera.</p>
  
  <p><code>time.sleep()</code> cannot be used because it freezes the while loop and disrupts the video stream being displayed in the window.</p>
  
  <p>another while loop inside the main while loop is unacceptable because it slows down the processor greatly and fewer frames are processed per second making the video stream very choppy. </p>
  
  <p><strong>What I've tried:</strong></p>
  
  <pre><code>import cv2
  import sys
  import time
  
  # Initialize variables
  camSource = -1
  running = True
  saveCount = 0
  nSecond = 1
  totalSec = 3.0
  keyPressTime = 0.0
  startTime = 0.0
  timeElapsed = 0.0
  startCounter = False
  endCounter = False
  
  # Start the camera
  camObj = cv2.VideoCapture(camSource)
  if not camObj.isOpened():
      sys.exit('Camera did not provide frame.')
  
  frameWidth = int(camObj.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
  frameHeight = int(camObj.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
  
  # Start video stream
  while running:
      readOK, frame = camObj.read()
  
      # Display counter on screen before saving a frame
      if startCounter:
          if nSecond &lt;= totalSec: 
              # draw the Nth second on each frame 
              # till one second passes  
              cv2.putText(img = frame, 
                          text = str(nSecond),
                          org = (int(frameWidth/2 - 20),int(frameHeight/2)), 
                          fontFace = cv2.FONT_HERSHEY_DUPLEX, 
                          fontScale = 3, 
                          color = (255,0,0),
                          thickness = 2, 
                          lineType = cv2.CV_AA)
  
              timeElapsed += (time.time() - startTime)
              print 'timeElapsed:{}'.format(timeElapsed)
  
              if timeElapsed &gt;= 1:
                  nSecond += 1
                  print 'nthSec:{}'.format(nSecond)
                  timeElapsed = 0
                  startTime = time.time()
  
          else:
              # Save the frame
              cv2.imwrite('img' + str(saveCount) + '.jpg', frame)  
              print 'saveTime: {}'.format(time.time() - keyPressTime)
  
              saveCount += 1
              startCounter = False
              nSecond = 1
  
      # Get user input
      keyPressed = cv2.waitKey(3)
      if keyPressed == ord('s'):
          startCounter = True
          startTime = time.time()
          keyPressTime = time.time()
          print 'startTime: {}'.format(startTime)
          print 'keyPressTime: {}'.format(keyPressTime)
  
      elif keyPressed == ord('q'):
          # Quit the while loop
          running = False
          cv2.destroyAllWindows()
  
      # Show video stream in a window    
      cv2.imshow('video', frame)
  
  camObj.release()
  </code></pre>
  
  <p><strong>Problem:</strong></p>
  
  <p>I can see that my method almost works but the cpu ticks returned by <code>time.time()</code> is not the same as real world seconds. The text for each numeral isn't displayed for a whole second and the file is saved too quickly (within 1.5 seconds instead of 3).</p>
  
  <p><strong>I'll accept as an answer:</strong></p>
  
  <p>If you can show how to get the timing correct and how to display "3", "2", "1" instead of my current method which displays "1", "2", "3"</p>