0

I have a Django + Python application. I have a python script that takes requirements as command line options.

The issue for me is that when I try to put in the command line arguments the script fails to execute. When I take out the command line arguments, it runs fine. I need those command line arguments.

I am using node JS with Python Shell to execute the python script when the button is clicked in the django HTML front page.

Here is my code:

let {PythonShell} = require('python-shell')
var path = require("path")

function track_object() {
  //document.getElementById("detect").value = "One moment please ..."
  var python = require("python-shell")
  var path = require("path")

//let {PythonShell} = require('python-shell')

    var options = {
    scriptPath : path.join(__dirname, '/../engine/opencv-object-tracking/'),
    pythonPath : '/usr/bin/python'
    }

  **//let pyshell = new PythonShell("opencv_object_tracking.py --video dashcam_boston.mp4 --tracker csrt", options);
  let pyshell = new PythonShell("opencv_object_tracking.py", options);**

}

Note: the two lines in bold are show the calling of the script with and without arguments

Please let me know what is the correct way to pass command line arguments with Python Shell.

***** EDIT ****** here is my edit argparse:

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", type=str,
    help="path to input video file")
ap.add_argument("-t", "--tracker", type=str, default="csrt",
    help="OpenCV object tracker type")
args = vars(ap.parse_args())

# extract the OpenCV version info
(major, minor) = cv2.__version__.split(".")[:2]

# if we are using OpenCV 3.2 OR BEFORE, we can use a special factory
# function to create our object tracker
if int(major) == 3 and int(minor) < 3:
    tracker = cv2.Tracker_create(args["tracker"].upper())

1 Answer 1

3

you should add the arguments on your options variable. For example,

var options = {
    mode: 'text',
    scriptPath : path.join(__dirname, '/../engine/opencv-object-tracking/'),
    pythonPath : '/usr/bin/python',
    args: ['--video dashcam_boston.mp4 --tracker csrt']
    }

Also, you can add pythonOptions: ['-u'] parameter if you require the outputs after the execution.

--Edit1--

It's strange but it's about the quote symbol. If you use " instead of ', it works very well. Here is the code below that works on my computer

 var options = {
   scriptPath : path.join(__dirname, './'),
   pythonPath : '/usr/bin/python',
   args: ["--video", "dashcam_boston.mp4","--tracker", "csrt"],
   mode: 'text'
 }

 let pyshell = new PythonShell("./opencv_object_tracking.py", options);
Sign up to request clarification or add additional context in comments.

6 Comments

Gurses: That actually throws an error: Uncaught PythonShellError: usage: opencv_object_tracking.py [-h] [-v VIDEO] [-t TRACKER] opencv_object_tracking.py: error: unrecognized arguments: --video dashcam_boston.mp4 --tracker csrt
Sorry for the mistake, the args should be element by element. Here the example args: ['--video', 'dashcam_boston.mp4',' --tracker', 'csrt']
i made a small change to the python script itself to make sure only the video is a parameter. WHen I then execute my application calling the python script -- nothing happens.
Is it possible to share your python argparse init?
I edited my answer with a %100 working solution :).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.