1

I am trying to run a function which takes frames from various camera feeds and saves them to disk.

I have three cameras connected to my laptop USB ports and trying to start all the cameras at once and trying to perform some action on those captured images/frames. For simplicity sake, i am just saving them to disk. Here is the below code:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from multiprocessing import Process

import cv2
#from scipy.stats import math
import datetime
count = 0
def saving_frame(frame):
    count = 0
    date_string = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M")
    count = count +1 
    cv2.imwrite('Frame_'+str(count)+date_string+'.png',frame)
    return

def runInParallel(*fns):
      proc = []
      for fn in fns:
        p = Process(target=fn)
        p.start()
        proc.append(p)
      for p in proc:
        p.join()


video_capture_1 = cv2.VideoCapture(0)

video_capture_2 = cv2.VideoCapture(1)

video_capture_3 = cv2.VideoCapture(2)

print ('Start Rec')

while True:
    ret_1, frame_1 = video_capture_1.read()
    ret_2, frame_2 = video_capture_2.read()
    ret_3, frame_3 = video_capture_3.read()

    runInParallel(saving_frame(frame_1),saving_frame(frame_2),saving_frame(frame_3))

    cv2.imshow('frame1', frame_1)
    cv2.imshow('frame2', frame_2)
    cv2.imshow('frame3', frame_3)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture_1.release()
video_capture_2.release()
video_capture_3.release()

cv2.destroyAllWindows()

However, the above code is not saving any frames. Could you please help me with this?

pavan subhash
  • 368
  • 6
  • 14
  • are any frames being shown on screen by imshow() command ? – tkhurana96 Sep 08 '18 at 07:13
  • @tkhurana96 yes but they are stuck, frames are moving at a very slow pace – pavan subhash Sep 08 '18 at 07:22
  • 1
    You are starting three whole new processes for every single frame of video, and writing each frame to disk in the slowest to encode PNG format and not allowing any parallelism between the acquisition phase, writing phase and displaying phase. Those are three areas to address if you want to speed things up. – Mark Setchell Sep 08 '18 at 08:18

0 Answers0