0
votes

I want to capture the frames of video with timestamps in real time using Raspberry pi. The video is made by USB webcam using ffmpeg() function in python code. How do I save the frames of video which is currently made by USB webcam in Raspberry pi?

I tried using three functions of opencv. cv2.VideoCapture to detect the video, video.read() to capture the frame and cv2.imwrite() to save the frame.Here is the code, the libraries included is not mentioned for conciseness.

  os.system('ffmpeg -f v4l2 -r 25 -s 640x480 -i /dev/video0 out.avi')
  video=cv2.VideoCapture('out.avi')
  ret, frame=video.read()
  cv2.imwrite('image'+str(i)+'.jpg',frame)     
  i+=1

The code saves the frames of video which was previously made by webcam. It is not saving the frames of video which is currently being recorded by webcam.

1
What do mean currently being recorded ? Do you mean video still under recording and not yet flushing to a file?mootmoot

1 Answers

-1
votes

As you can read here, you can access the camera with camera=cv2.VideoCapture(0). 0 is an index of the connected camera. You may have to try a different index, but 0 usually works.
Similar as a video file you can use ret, frame = camera.read() to grab a frame. Always check the ret value before continuing processing a frame.
Next you can add text to the frame as described here. You can use time or datetime to obtain a timestamp. Finally save the frame.

Note: if you use imwrite you will quicky get a LOT of images. Depending on your project you could also consider saving the frames as video-file. Explained here.

Edit after comment:

This is how you can use time.time(). First import the time module at the top of your code. time.time() returns the number of seconds since January 1, 1970, 00:00:00. So to get a timestamp, you have to store the starttime - when the program/video starts running.
Then, on every frame, you call time.time() and subtract the starttime. The result is the time your program/video has been running. You can use that value for a timestamp.

import time

starttime = time.time()

# get frame
timestamp = time.time() - starttime
cv2.putText(frame,timestamp,(10,500), font, 4,(255,255,255),2,cv2.CV_AA)