1

I am writing a python script to process a video using OpenCV package. I am quite new to python, so I am facing the following issue.

In the end of the code I want to add the code below. However, I do not know how to add the input and the output file of my code.

import argparse

def main(input_, output_):
    pass # do something with the parameters


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='OpenCV video processing')
    parser.add_argument('-i', "--input", help='full path to input video that will be processed')
    parser.add_argument('-o', "--output", help='full path for saving processed video output')
    args = parser.parse_args()

    if args.input is None or args.output is None:
        sys.exit("Please provide path to input and output video files! See --help")

    main(args.input, args.output)

Can you help me understand what I am filling in wrongly?

hpaulj
  • 221,503
  • 14
  • 230
  • 353
Alice
  • 11
  • 3
  • You don't put the file names in the code itself (as in your 2nd block), but in the command line used to call this script. How are you invoking this script? From a OS shell (windows or Linux) or from an ide like `sypder`? Looks like you need more fundamental reading on how to run Python programs. – hpaulj Mar 02 '21 at 21:36
  • So you mean that I have to fill in the input and output file paths in the command line and not in the code itself? I am new to Python indeed. I am using spyder. Any resource that can help me understand more is more than welcome – Alice Mar 03 '21 at 18:59
  • The whole point to using `argparse` is to let you specify different filenames for different runs, without changing the code each time. If you were calling the script from a OS shell it would be easy to tell you how to supply those names, but with `spyder` you run by clicking a button and have to specify the names in some other window. I don't use `spyder` so can't help with the details. – hpaulj Mar 03 '21 at 19:51
  • I changed the tags to better reflect to core of your problem. – hpaulj Mar 03 '21 at 19:51

1 Answers1

0

Here you go:

import argparse
import sys


def main(args):
    print(args.input, args.output)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='OpenCV video processing')
    parser.add_argument('-i', "--input", dest='input', help='full path to input video that will be processed')
    parser.add_argument('-o', "--output", dest='output', help='full path for saving processed video output')
    args = parser.parse_args()

    if args.input is None or args.output is None:
        sys.exit("Please provide path to input and output video files! See --help")

    main(args)

You need to define a dest and then use that.

PS: I would recommend that you use the click library instead...

PS[2]: Dunno if it was intentional, but the code in your question is wrongly indented, it would be a miracle if that code worked for whatever reason

Arjix
  • 146
  • 9
  • If my answer answered your question then please mark it as the accepted answer. – Arjix Mar 03 '21 at 11:47
  • Thanks for the quick and proper reply, could you also let me know where I can define the input and output? I have added in the beginning of my code after def (main), but it doesn't seem to pull them. cap = cv2.VideoCapture('input.mp4') out = cv2.VideoWriter('output.avi', fourcc, fps, (frame_width, frame_height)) – Alice Mar 03 '21 at 18:44
  • The reason I am asking is that when I run the code I am getting an error saying: Please provide path to input and output video files! See --help Can you explain on what do you mean with the second comment? Where the code is wrongly indented? – Alice Mar 03 '21 at 18:51
  • The problem isn't with his `argparse` setup, nor will `click` help. It's a more basic issue of what it means to provide commandline arguments, especially in a `spyder` environment. – hpaulj Mar 03 '21 at 19:54