Syntax Error with Argument Parser - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Syntax Error with Argument Parser (/thread-21433.html) |
Syntax Error with Argument Parser - aditya_man - Sep-30-2019 Hello all, I was working on a project where I would separate the frames of an image using OpenCV using Argument Parser. It is something to do with the right way to pass argument parse variables as parameters for a function. This is what the error states. File "video-frames.py", line 13 def extractFrames('args.input', 'args.output'): ^ SyntaxError: invalid syntaxBelow is my code: import cv2 import os import argparse ap = argparse.ArgumentParser() ap.add_argument("-i", "--input", required=True, help="path to input video") ap.add_argument("-o", "--output", required=True, help="path to output directory") args = vars(ap.parse_args()) #Function to extract frames def extractFrames('args.input', 'args.output'): #directory path, where my video images will be stored #Capture vidoe from video file cap = cv2.VideoCapture(args["input"]) #Counter Variable count = 0 while (cap.isOpened()): # Capture frame-by-frame ret, frame = cap.read() if ret == True: print('Read %d frame: ' % count, ret) # save frame as JPEG file cv2.imwrite(os.path.join(args["output"], "frame{:d}.png".format(count)), frame) count += 1 else: break # When everything done, release the capture cap.release() cv2.destroyAllWindows() def main(): extractFrames(args["input"] , args["output"]) if __name__=="__main__": main()Any help in fixing this would be greatly appreciated. Thank you so much, Aditya RE: Syntax Error with Argument Parser - ichabod801 - Sep-30-2019 Your parameter list for the extractFrames function is just two string literals. Parameters have to be valid Python tokens, not literal values. You can assign literal values to those tokens as default values for the parameters, so you could do something like this: def extractFrames(input = 'args.input', output = 'args.output'): #directory path, where my video images will be stored #Capture vidoe from video file cap = cv2.VideoCapture(args["input"])However, looking at the code, I think what you really want is this: def extractFrames(args): #directory path, where my video images will be stored #Capture vidoe from video file return cv2.VideoCapture(args["input"]) cap = extractFrames(args)You might want to check out the function tutorial for details on parameters and return values. |