Python Forum
Syntax Error with Argument Parser
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax Error with Argument Parser
#1
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 syntax
Below 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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,178 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 380 Jan-19-2024, 01:20 PM
Last Post: rob101
  Strange argument count error rowan_bradley 3 718 Aug-06-2023, 10:58 AM
Last Post: rowan_bradley
  Syntax error while executing the Python code in Linux DivAsh 8 1,584 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,227 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,316 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,252 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 891 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,845 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,355 May-18-2022, 06:50 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020