0

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.

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
Mafaz Ahsan
  • 105
  • 2
  • 11
  • What do mean `currently being recorded` ? Do you mean video still under recording and not yet flushing to a file? – mootmoot May 23 '19 at 12:12

1 Answers1

-2

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)
J.D.
  • 4,511
  • 2
  • 7
  • 20
  • I want to either save the frame such that Timestamp is written on it or the image is saved with the Timestamp as its name. I tried to add timestamp by time.time() function in cv2.putText() function and It just writes time.time() on every frame rather than the timestamp. font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(frame,'time.time()',(10,500), font, 4,(255,255,255),2,cv2.CV_AA). Also I tried using time.time() in place of image name of cv2.imwrite() function and its not adding timestamp either. cv2.imwrite('time.time()'+str(i)+'.jpg',frame) – Mafaz Ahsan May 23 '19 at 18:27
  • You should not put it in quotes. It is a function, not a string. I modified the answer to show you how to use time.time() – J.D. May 23 '19 at 19:04