I am using Python 2.7 and OpenCV 3.3 and want to save images from stereo cameras at every defined frame count or after defined seconds. Like every 5th frame or after every 2 seconds frame should be stored in different folders like cam1folder and cam2folder. I am able to capture videos from both cameras and then used these videos to capture frames after predefined seconds. Codes for these are given below.
Code for videos capturing:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
out1 = cv2.VideoWriter('output1.avi',fourcc, 20.0, (640,480))
while(True):
ret, frame = cap.read()
ret1, frame1 = cap1.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(frame)
out1.write(frame1)
cv2.imshow('frame',gray)
cv2.imshow('frame1', frame1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cap1.release()
out1.release()
cv2.destroyAllWindows()
Code for frames capturing from both cameras after saving videos:
import cv2
import math
import numpy as np
videoFile = "output.avi"
vidcap = cv2.VideoCapture(videoFile)
success,image = vidcap.read()
videoFile1 = "output1.avi"
vidcap1 = cv2.VideoCapture(videoFile1)
success1,image1 = vidcap1.read()
seconds = 3
fps = vidcap.get(cv2.CAP_PROP_FPS) # Gets the frames per second
multiplier = fps * seconds
fps1 = vidcap1.get(cv2.CAP_PROP_FPS) # Gets the frames per second
multiplier1 = fps1 * seconds
while success:
frameId = int(round(vidcap.get(1))) #current frame number, rounded b/c sometimes you get frame intervals which aren't integers...this adds a little imprecision but is likely good enough
success, image = vidcap.read()
frameId1 = int(round(vidcap1.get(1))) #current frame number
success1, image1 = vidcap1.read()
if frameId % multiplier == 0:
cv2.imwrite("right/rframe%d.jpg" % frameId, image) # stored in right folder
if frameId1 % multiplier1 == 0:
cv2.imwrite("left/lframe%d.jpg" % frameId1, image1) # stored in left folder
vidcap.release()
vidcap1.release()
print "Complete"
But what I want is real time capturing of images directly from videos without saving these videos but save captured images in two different folders. How to do this?
Thanks.
You simply need to merge the 2 code snippets that you yourself have written.
In your first code snippet, when you run the while
loop, you can maintain a common counter instead of separate counters for separate cameras. When your counter reaches the required value (5 frames, as per your question), you perform imwrite
on the frame
and frame1
objects directly.
You do not need to intermediate with the VideoWriter
object.
Your final code would look something like:
cap = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
multiplier = 5
frame_count = 0
while(True):
frame_count += 1
ret, frame = cap.read()
ret1, frame1 = cap1.read()
cv2.imshow('frame',frame) #show all frames
cv2.imshow('frame1', frame1)
if frame_count % multiplier == 0:
cv2.imwrite("right/rframe%d.jpg" % frame_count, frame) #write specific frames
cv2.imwrite("left/lframe%d.jpg" % frame_count, frame1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break