如何通过终端传递参数时删除此错误?

时间:2017-12-15 17:42:33

标签: macos python-3.x matplotlib command-line-arguments

我试图将输入作为图像提供给以下程序,该程序将生成直方图。但是当我通过终端将图像作为输入传递给程序时,它会出错,但是当我对输入进行硬编码时,它不会给出错误。
Mac OS Sierra - 10.13.2
matplotlib - 2.1.1
OpenCV - 3.3
python - 3.6
代码: -

from sys import platform as sys_pf
if sys_pf == 'darwin':
    import matplotlib
    matplotlib.use("TkAgg")
# import matplotlib
# matplotlib.use("TkAgg")
import cv2
# import numpy as np
from matplotlib import pyplot as plt
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",help="path to image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])

gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow("BGR",image)
cv2.imshow("Gray",gray)
hist = cv2.calcHist([gray],[0],None,[256],[0, 256])
plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("Bins")
plt.ylabel("# of pixel")
plt.plot(hist)
plt.xlim([0,256])
img = plt.show()

错误: -

(cv) Arjuns-MacBook-Air:computer vision arjungoyal$ python grayscale_hist.py -i grant.jpg
2017-12-15 19:52:21.304 python[2005:77022] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7fdcdad1dd30
2017-12-15 19:52:21.306 python[2005:77022] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7fdcdad1dd30'
*** First throw call stack:
(
        0   CoreFoundation                      0x00007fff30a7400b __exceptionPreprocess + 171
        1   libobjc.A.dylib                     0x00007fff57652c76 objc_exception_throw + 48
        2   CoreFoundation                      0x00007fff30b0ccd4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
        3   CoreFoundation                      0x00007fff309ea3f0 ___forwarding___ + 1456
        4   CoreFoundation                      0x00007fff309e9db8 _CF_forwarding_prep_0 + 120
        5   Tk                                  0x00007fff3ceae328 TkpInit + 467
        6   Tk                                  0x00007fff3ce2d262 Tk_Init + 1710
        7   _tkinter.cpython-36m-darwin.so      0x0000000109848de3 Tcl_AppInit + 82
        8   _tkinter.cpython-36m-darwin.so      0x000000010984454d _tkinter_create + 1040
        9   Python                              0x000000010297a6c1 _PyCFunction_FastCallDict + 166
        10  Python                              0x00000001029df00f call_function + 489
        11  Python                              0x00000001029d7ecc _PyEval_EvalFrameDefault + 4930
        12  Python                              0x00000001029df7df _PyEval_EvalCodeWithName + 1829
        13  Python                              0x00000001029e0096 _PyFunction_FastCallDict + 444
        14  Python                              0x0000000102942ab9 _PyObject_FastCallDict + 196
        15  Python                              0x0000000102942bdc _PyObject_Call_Prepend + 156
        16  Python                              0x000000010294293a PyObject_Call + 101
        17  Python                              0x000000010298d8fa slot_tp_init + 57
        18  Python                              0x000000010298a846 type_call + 184
        19  Python                              0x0000000102942a84 _PyObject_FastCallDict + 143
        20  Python                              0x00000001029defdf call_function + 441
        21  Python                              0x00000001029d7ecc _PyEval_EvalFrameDefault + 4930
        22  Python                              0x00000001029e0166 _PyFunction_FastCall + 121
        23  Python                              0x00000001029defe6 call_function + 448
        24  Python                              0x00000001029d7ecc _PyEval_EvalFrameDefault + 4930
        25  Python                              0x00000001029df7df _PyEval_EvalCodeWithName + 1829
        26  Python                              0x00000001029d6b4f PyEval_EvalCodeEx + 52
        27  Python                              0x0000000102962c1b function_call + 338
        28  Python                              0x000000010294293a PyObject_Call + 101
        29  Python                              0x00000001029d8121 _PyEval_EvalFrameDefault + 5527
        30  Python                              0x00000001029df7df _PyEval_EvalCodeWithName + 1829
        31  Python                              0x00000001029dfecb fast_function + 227
        32  Python                              0x00000001029defe6 call_function + 448
        33  Python                              0x00000001029d7ecc _PyEval_EvalFrameDefault + 4930
        34  Python                              0x00000001029df7df _PyEval_EvalCodeWithName + 1829
        35  Python                              0x00000001029d6b15 PyEval_EvalCode + 43
        36  Python                              0x00000001029fe25a run_mod + 54
        37  Python                              0x00000001029fe53a PyRun_FileExFlags + 180
        38  Python                              0x00000001029fdac7 PyRun_SimpleFileExFlags + 280
        39  Python                              0x0000000102a11878 Py_Main + 3268
        40  python                              0x0000000102935e1d python + 7709
        41  libdyld.dylib                       0x00007fff58242115 start + 1
        42  ???                                 0x0000000000000004 0x0 + 4
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6

此外,在此错误之后,python意外退出。 如何删除此错误?

0 个答案:

没有答案