How can I run a python program without passing external files a an argument

It is a Face Detection program using OpenCV and DNN and I’ve two external files (deploy.prototxt.txt) and caffe model file (res10_300x300_ssd_iter_140000.caffemodel) is there any way to include the file so I don’t have to pass arguments everytime.
Terminal command:
$ python3 face-detection.py –image image –prototxt deploy.prototxt.txt –model res10_300x300_ssd_iter_140000.caffemodel

import numpy as np
import argparse
import cv2

print("Program running.....")

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.5,
                help="Minimum probability to filter weak detections")
args = vars(ap.parse_args())

print("[INFO] loading model....")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])

image = cv2.imread("Input_Images/image.jpeg")
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,
                             (300, 300), (104.0, 177.0, 123.0))

print("[INFO] computing object detections...")
net.setInput(blob)
detections = net.forward()

for i in range(0, detections.shape[2]):
    confidence = detections[0, 0, i, 2]
    if confidence > args["confidence"]:
        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype("int")
        text = "{:.2f}%".format(confidence * 100)
        y = startY - 10 if startY - 10 > 10 else startY + 10
        cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
        cv2.putText(image, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)

cv2.imshow("Output", image)
cv2.waitKey(0)

The program is working just fine,I just want to run the program just by clicking the run button.

  • 1

    Just… remove the argument parsers? Or set the required parameter to False?

    – 




  • Are you looking for alternative ways to pass a file to a script? Or is it fine to hardcore it?

    – 

You could use default values for your arguments.

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
                help="Path to input image" default="image")
ap.add_argument("-p", "--prototxt", required=True,
                help="Path to Caffe 'Deploy' prototxt file", default="deploy.prototxt.txt")
ap.add_argument("-m", "--model", required=True,
                help="Path to Caffe pre-trained model", default="res10_300x300_ssd_iter_140000.caffemodel")
ap.add_argument("-c", "--confidence", type=float, default=0.5,
                help="Minimum probability to filter weak detections")
args = vars(ap.parse_args())

Another possible solution would be to scan the current directory for files with the file extensions *.caffemodel, *.prototxt.txt.
This could look like this:

from pathlib import Path

cwd = Path.cwd()

prototxt = cwd.glob('*.prototxt.txt')[0]
model = cwd.glob('*.caffemodel')[0]

please note that this implementation would raise an index error, if no matching file was found in the working directory.

Leave a Comment