Ask Your Question
0

stop recording of IP camera stream after x minutes

asked 2016-02-11 11:24:50 -0600

herculas gravatar image

updated 2016-02-12 01:20:20 -0600

berak gravatar image

Hi, I am pretty much new to opencv and I am trying my hands in using it for one of my IP camera video recording projects. My question is how to stop or end the camera recording or showing the preview after x minutes without pressing ESC or Q key .

Example from openCV: cap = cv2.VideoCapture(0)

while(True): # Capture frame-by-frame ret, frame = cap.read() print frame, type(frame)

# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

When everything done, release the capture

del(cap) cap.release() cv2.destroyAllWindows()

Above example says that User need to press "Q" key to stop the script . Apologies in advance if this question sounds not great and silly.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-02-12 00:41:23 -0600

berak gravatar image

updated 2016-02-12 01:25:45 -0600

you can just use python's builtin time() function to do this, it's really trivial:

import cv2
import time

t0 = time.time() # start time in seconds

cap = cv2.VideoCapture(0)
while(True):
   ret, frame = cap.read()
   # ... processing or preview
   t1 = time.time() # current time
   num_seconds = t1 - t0 # diff
   if num_seconds > 30:  # e.g. break after 30 seconds
      break
edit flag offensive delete link more

Comments

Thanks for the answer .

Just added cv2.waitkey(duration) to the above code and it is working as desired.

herculas gravatar imageherculas ( 2016-02-12 04:27:00 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-02-11 11:24:50 -0600

Seen: 3,773 times

Last updated: Feb 12 '16