AdamSpannbauer / python_video_stab

A Python package to stabilize videos using OpenCV
https://adamspannbauer.github.io/python_video_stab/html/index.html
MIT License
682 stars 118 forks source link

deque index out of range #95

Closed korabelnikov closed 4 years ago

korabelnikov commented 4 years ago

Describe the bug Failed code sample

Provide version info What version of Python are you running? 3.7.5 What version of OpenCV are you running? 4.1.2 What version of vidstab are you running? master

Provide error message

File "/home/user/anaconda3/envs/mmdet2/lib/python3.7/site-packages/vidstab/VidStab.py", line 140, in _process_first_frame
   prev_frame = self.frame_queue.frames[-1]
IndexError: deque index out of range

Provide code snippet

import os

from vidstab import VidStab
import matplotlib.pyplot as plt

folder = '/home/user/images/tsr/scanning_car_front2019'
folder_out = os.path.join(folder, 'stabilyzed')
if not os.path.exists(folder_out):
    os.makedirs(folder_out)

path_in = os.path.join(folder, '4.mp4')
path_out = os.path.join(folder_out, '4.mp4')
assert os.path.exists(path_in)
stabilizer = VidStab()
stabilizer.stabilize(input_path=path_in, output_path=path_out)

stabilizer.plot_trajectory()
plt.show()

stabilizer.plot_transforms()
plt.show()

Edited by @AdamSpannbauer for some formatting stuff.

korabelnikov commented 4 years ago

short investigation shows me that path was wrong. So we just need one assert to prevent such cases

AdamSpannbauer commented 4 years ago

Hi, @korabelnikov I apologize for the delay in responding to this.

A clarifying statement about what you mean when you say the path was wrong. Do you mean that the path_in was referencing a file that didn't exist and that the assert os.path.exists(path_in) statement fixed the issue?

AdamSpannbauer commented 4 years ago

@korabelnikov I've been unable to reproduce the error using the below script. The script identifies that the input file doesn't exist and throws the exception shown below the code block. Am I going about reproducing your issue wrong? Would you please advise? Thanks.

from vidstab import VidStab

# Path that does not exist
fake_path = 'fakerino.mp4'
output_path = 'doesnt_matter.mp4'

stabilizer = VidStab()
stabilizer.stabilize(input_path=fake_path, output_path=output_path)
Traceback (most recent call last):
  File "/Users/aspannbauer/Documents/github/python_video_stab/reproduce_95.py", line 8, in <module>
    stabilizer.stabilize(input_path=fake_path, output_path=output_path)
  File "/Users/aspannbauer/Documents/github/python_video_stab/vidstab/VidStab.py", line 479, in stabilize
    raise FileNotFoundError(f'{input_path} does not exist')
FileNotFoundError: fakerino.mp4 does not exist
korabelnikov commented 4 years ago

@AdamSpannbauer what OS you use?

Under linux (ubuntu) I got the error

AdamSpannbauer commented 4 years ago

I personally tested on macOS Catalina v10.15.1, and there is also a unit test for an invalid path and the tests are being run by Travis CI on Ubuntu (last build was on Ubuntu 16.04.6 LTS). This test is also passing, so I'm not sure what next steps would be.

To confirm, if you run the same exact code block I posted in my last message, you get an error about deque index rather than a FileNotFoundError?

korabelnikov commented 4 years ago

I've re-checked file exists but it's empty. Sorry, for that

AdamSpannbauer commented 4 years ago

Interesting. I'll look into outputting a more informative error message for this case.

Code to reproduce error:

from pathlib import Path
from vidstab import VidStab

empty_path = 'empty.mp4'
output_path = 'doesnt_matter.mp4'

Path(empty_path).touch()

stabilizer = VidStab()
stabilizer.stabilize(input_path=empty_path, output_path=output_path)

Relevant portion of traceback

 File "/Users/aspannbauer/Documents/github/python_video_stab/reproduce_95.py", line 10, in <module>
   stabilizer.stabilize(input_path=empty_path, output_path=output_path)
 File "/Users/aspannbauer/Documents/github/python_video_stab/vidstab/VidStab.py", line 501, in stabilize
   bar = self._init_trajectory(smoothing_window, max_frames, show_progress=show_progress)
 File "/Users/aspannbauer/Documents/github/python_video_stab/vidstab/VidStab.py", line 158, in _init_trajectory
   self._process_first_frame()
 File "/Users/aspannbauer/Documents/github/python_video_stab/vidstab/VidStab.py", line 140, in _process_first_frame
   prev_frame = self.frame_queue.frames[-1]
IndexError: deque index out of range
AdamSpannbauer commented 4 years ago

Assuming #98 CI passes. A more informative error message has been added for this case. The below code will result in a ValueError.

from pathlib import Path
from vidstab import VidStab

empty_path = 'empty.mp4'
output_path = 'doesnt_matter.mp4'

Path(empty_path).touch()

stabilizer = VidStab()
stabilizer.stabilize(input_path=empty_path, output_path=output_path)
ValueError: First frame is None. Check if input file/stream is correct.