1

can anyone tell me how to make this code works in jupyter or any notebook enter image description here Code

import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to input image")
ap.add_argument("-p", "--prototxt", required=True, help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "--model", required=True, help="path to Caffe pre-trained model")
ap.add_argument("-c", "--confidence", type=float, default=0.2, help="minimum probability to filter weak detections")
args = vars(ap.parse_args())

Error

An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

I've tried some solutions but none work

Michael Butscher
  • 10,028
  • 4
  • 24
  • 25
Shady Emad
  • 56
  • 1
  • 5
  • 1
    Have you checked out this SO answer? https://stackoverflow.com/questions/30656777/how-to-call-module-written-with-argparse-in-ipython-notebook – Sheldon Feb 19 '23 at 20:35
  • 2
    _use %tb to see the full traceback_ Did you try doing that? – John Gordon Feb 19 '23 at 20:47
  • `argparse` handles commandline arguments provided by the OS shell. You can't do that in a notebook, since the commandline supplies arguments to the server. Why are you trying to use argparse in a notebook? What code is going to use that `args`? – hpaulj Feb 19 '23 at 22:02
  • It specifically looks like you are trying to run [this](https://pyimagesearch.com/2018/02/26/face-detection-with-opencv-and-deep-learning/). (So my question would be why not point to that in you post?) And so it says to save that code in a file called `detect_faces.py` right above that, as hpaulj is sort of getting at. Because others had similar questions, the author even posted more about this, see [here](https://pyimagesearch.com/2018/03/12/python-argparse-command-line-arguments/), in particular where it says, "but the honest truth is that nearly all of these errors can be avoided by ... – Wayne Feb 19 '23 at 23:49
  • taking the time to educate yourself on command line arguments." Note that if you want to include use of an `.ipynb` file with `detect_faces.py` you can run the Python script file with the extension `.py` from inside the notebook file with extension `.ipynb` by using in a Jupyter cell `%run detect_faces.py --image rooster.jpg --prototxt deploy.prototxt.txt --model res10_300x300_ssd_iter_140000.caffemodel`, or the appropriate variation. – Wayne Feb 19 '23 at 23:49
  • I was going to add to the end of my last comment that I don't know if the output from `detect_faces.py` is in a form that Jupyter is going to handle and so you may not want to do that, but that is how you'd run a python script from inside a Jupyter `.ipynb` file: use in a cell `%run .py `. See about the `%run` magic [here](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-run). – Wayne Feb 19 '23 at 23:58

1 Answers1

0

You just need to change "required=True" to "required=False" and add default path in your code to works in jupyter or any notebook.

Also need to add "ap.add_argument("-f", required=False)" in the end of line.

In your case code will work like this:

import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=False, default="/content/your_image.jpg", help="path to input image")
ap.add_argument("-p", "--prototxt", required=False, default="/content/your_prototxt.txt", help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "--model", required=False, default="/content/your_model.caffemodel", help="path to Caffe pre-trained model")
ap.add_argument("-c", "--confidence", type=float, default=0.2, help="minimum probability to filter weak detections")
ap.add_argument("-f", required=False)
args = vars(ap.parse_args())

Be sure to change default path with your own file path.

Worked in Google Colab notebook.

CB Acnt
  • 36
  • 4