尝试解析 Python shell 参数时出现语法错误 [英] Syntax error when trying to parse arguments Python shell

查看:31
本文介绍了尝试解析 Python shell 参数时出现语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码试图在 Python shell (IDLE) 中运行,但我在 Python shell 中解析参数的方式似乎存在问题.

I have some code that I am trying to run in a Python shell (IDLE) but there seems to be a problem with the way I am parsing arguments in the Python shell.

代码如下:

# import the necessary packages
from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
from skimage import io
import matplotlib.pyplot as plt
import argparse

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())

# load the image and convert it to a floating point data type
image = img_as_float(io.imread(args["image"]))

# loop over the number of segments
for numSegments in (100, 200, 300):
    # apply SLIC and extract (approximately) the supplied number
    # of segments
    segments = slic(image, n_segments = numSegments, sigma = 5)

    # show the output of SLIC
    fig = plt.figure("Superpixels -- %d segments" % (numSegments))
    ax = fig.add_subplot(1, 1, 1)
    ax.imshow(mark_boundaries(image, segments))
    plt.axis("off")

# show the plots
plt.show()

当我尝试使用 Slic.py --image 0021.jpg 行运行程序时,我得到 SyntaxError: invalid syntax.不确定我做错了什么,对某些人来说可能很明显,但非常感谢任何帮助.

When I try to run the program with the line Slic.py --image 0021.jpg I get SyntaxError: invalid syntax. Not sure what I am doing wrong, probably really obvious to some but any help is greatly appreciated.

代码是在页面底部SLIC示例下的链接中找到的,他甚至展示了如何运行代码,但它对我不起作用:

The code was found at the link below under the SLIC example at the bottom of the page, he even shows how to run the code but it doesn't work for me:

www.pyimagesearch.com/2014/07/28/a-slic-superpixel-tutorial-using-python

www.pyimagesearch.com/2014/07/28/a-slic-superpixel-tutorial-using-python

推荐答案

只是一个想法 - 这可能与 IDLE 发生冲突.尝试从 shell 运行(您可能已经猜到了,python Slic.py --image 0021.jpg).当然,由于这是一个 5 个月前的问题,我假设您现在已经解决了 :)

Just a thought - this may be a clash with IDLE. Try running from the shell (as you probably have guessed, python Slic.py --image 0021.jpg). Of course, since this is a 5 month old question, I assume that you've resolved it by now :)

这篇关于尝试解析 Python shell 参数时出现语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆