1

I am trying to read the frames using imageio API. I have a reader as an object which I have received using imageio.get_reader(video_path,"ffmpeg"). I have the following frame reader function

def read_frames(reader, frame_q, use_webcam):
    if use_webcam:
        time.sleep(15)
        frame_cnt = 0
        while True:
            #if frame_cnt % 5 == 0:    
            #    ret, frame = reader.read()
            #    cur_img = frame[:,:,::-1]
            #    frame_q.put(cur_img)
            #else:
            #    ret, frame = reader.read()
            ret, frame = reader.read()
            cur_img = frame[:,:,::-1] # bgr to rgb from opencv reader
            frame_q.put(cur_img)
            if frame_q.qsize() > 100:
                time.sleep(1)
            else:
                time.sleep(DELAY/1000.)

            #print(cur_img.shape)

    else:
        #for cur_img in reader: # this is imageio reader, it uses rgb
        nframes = reader.get_length() **#getting error here**
        # if nframes == float('inf') or nframes ==float('-inf'):
        #     return float("nan")
        # return int(nframes)  

        for ii in range(nframes):
            while frame_q.qsize() > 500: # so that we dont use huge amounts of memory
                time.sleep(1)
            cur_img = reader.get_next_data()
            frame_q.put(cur_img)
            #shape = cur_img.shape
            #noisy_img = np.uint8(cur_img.astype(np.float) + np.random.randn(*shape) * 20)
            #frame_q.put(noisy_img)
            if ii % 100 == 0:
                print("%i / %i frames in queue" % (ii, nframes))
        print("All %i frames in queue" % (nframes))

Traceback:

    Traceback (most recent call last):
  File "/home/prashantb/anaconda3/envs/demo/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/home/prashantb/anaconda3/envs/demo/lib/python3.6/multiprocessing/process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "multiprocess_detect_actions.py", line 67, in read_frames
    for ii in range(nframes):
TypeError: 'float' object cannot be interpreted as an integer

I tried to use float('inf')as commented in the code but it is not useful in this case. I also tried to convert a float value to integer but it showing the same error. I would appreciate your advice on this. thank you.

Sam
  • 352
  • 2
  • 4
  • 22
  • Where is the line `for ii ,_ in enumerate(nframes)` in your code? I do not see it anywhere except in the `Traceback`. – Aneesh Palsule Feb 21 '19 at 06:45
  • @AneeshPalsule sorry I have updated the traceback now – Sam Feb 21 '19 at 06:48
  • When the argument you pass to `range()` has to be of type `int`. It looks like your `nframes` is a `float`. Could you try printing `nframes` to make sure? – Aneesh Palsule Feb 21 '19 at 06:58
  • Python docs define `range()` as follows: Rather than being a function, range is actually an immutable sequence type. Source https://docs.python.org/3/library/functions.html#func-range More documentation on ranges https://docs.python.org/3/library/stdtypes.html#typesseq says this: The arguments to the range constructor must be integers. – Aneesh Palsule Feb 21 '19 at 07:01
  • nframes is showing> inf – Sam Feb 21 '19 at 07:04
  • if you check my code I have tried to check for the value of inf. it is the commented section just below the nframes – Sam Feb 21 '19 at 07:05
  • Can you deduce why `nframes` is `inf` (as in infinity)? – Aneesh Palsule Feb 21 '19 at 08:15

1 Answers1

1

In the release notes for the version 2.5.0 of imageio they mention some changes they introduced to the ffmpeg plugin:

"The reader of the ffmpeg plugin now always reports inf as the number of frames. Use reader.count_frames() to get the actual number, or estimate it from the fps and duration in the meta data."

https://imageio.readthedocs.io/en/stable/releasenotes.html#version-2-5-0-06-02-2019

That should fix your problem.

Mr K.
  • 1,064
  • 3
  • 19
  • 22