How to save video as frames in list and process the list of frames simultaneously?
Question:
I want to read input from camera and append frame in a list, display the frame from list. The code takes much time to read a frame and display it, after appending into the list.
def test(source_file):
ImagesSequence=[]
i=0
capture = VideoCapture(source_file)
while(1):
ret, frame = capture.read()
while(True):
imshow('Input', frame)
ImagesSequence.append(frame)
imshow('Output',ImagesSequence[i].astype(np.uint8))
i=i+1
if cv2.waitKey(60) & 0xFF == ord('q'):
break
return ImagesSequence
test(0)
Answers:
As Christoph pointed out, you have an actual infinite loop running in the program, removing it will fix your program.
def test(source_file):
ImagesSequence=[]
i=0
capture = cv2.VideoCapture(source_file)
while True:
ret, frame = capture.read()
cv2.imshow('Input', frame)
ImagesSequence.append(frame)
cv2.imshow('Output',ImagesSequence[i].astype(np.uint8))
i=i+1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
return ImagesSequence
test(0)
from cv2 import *
import numpy as np
ImagesSequence=[]
i=0
cap = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('frame', frame)
ImagesSequence.append(cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY))
cv2.imshow('output', ImagesSequence[i].astype(np.uint8))
i=i+1
if cv2.waitKey(60) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I want to read input from camera and append frame in a list, display the frame from list. The code takes much time to read a frame and display it, after appending into the list.
def test(source_file):
ImagesSequence=[]
i=0
capture = VideoCapture(source_file)
while(1):
ret, frame = capture.read()
while(True):
imshow('Input', frame)
ImagesSequence.append(frame)
imshow('Output',ImagesSequence[i].astype(np.uint8))
i=i+1
if cv2.waitKey(60) & 0xFF == ord('q'):
break
return ImagesSequence
test(0)
As Christoph pointed out, you have an actual infinite loop running in the program, removing it will fix your program.
def test(source_file):
ImagesSequence=[]
i=0
capture = cv2.VideoCapture(source_file)
while True:
ret, frame = capture.read()
cv2.imshow('Input', frame)
ImagesSequence.append(frame)
cv2.imshow('Output',ImagesSequence[i].astype(np.uint8))
i=i+1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
return ImagesSequence
test(0)
from cv2 import *
import numpy as np
ImagesSequence=[]
i=0
cap = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('frame', frame)
ImagesSequence.append(cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY))
cv2.imshow('output', ImagesSequence[i].astype(np.uint8))
i=i+1
if cv2.waitKey(60) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()