正在努力理解导致此关键错误异常的原因

2024-05-18 10:53:34 发布

您现在位置:Python中文网/ 问答频道 /正文

为了我的学习和发展,我决定学习编程,并决定学习python。我从一个使用以下tutorial的颜色检查应用程序开始

但是,每当我运行activation命令打开我的映像时,我都会遇到以下错误

Traceback (most recent call last):
  File "main.py", line 14, in <module>
    PathToImage = args['colour_select']
KeyError: 'colour_select'

我对python比较陌生,因此这可能不是我代码中的唯一错误。我很困惑这是怎么回事

我的代码完整代码:

# imports
import cv2
import numpy as np
import pandas as pd
import argparse

# variable declaration
clicked = False
red = blue = green = xpos = ypos = 0

ap = argparse.ArgumentParser()
ap.add_argument('-i', required=True, help="PathToImage")
args = vars(ap.parse_args())
PathToImage = args['colour_select']

img = cv2.imread(PathToImage)

# read csv file
index = ["colour", "colour_name", "hex", "R", "G", "B"]
csv = pd.read('colours.csv', names=index, header=None)

cv2.namedWindow('colour_select')
cv2.setMouseCallback('colour_select', draw_function)


def draw_function(event, x, y, flags, parameters):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        global blue, green, red, xpos, ypos, clicked
        clicked = True
        xpos = x
        ypos = y
        blue, green, red = img[x, y]
        blue = int(blue)
        green = int(green)
        red = int(red)


def getColourName(red, green, blue):
    minimum = 10000
    for i in range(len(csv)):
        distance = abs(red - int(csv.loc[i, "Red"])) + abs(green - int(csv.loc[i, "Green"])) + abs(
            blue - int(csv.loc[i, "Blue"]))
        if (distance <= minimum):
            minimum = distance
            ColourName = csv.loc[i, "colour_name"]
        return ColourName


while (1):
    cv2.imshow("colour_select", img)
    if (clicked):
        cv2.rectangle(img, (20, 20), (750, 60), (blue, green, red), -1)

        text = getColourName(red, green, blue) + ' R=' + str(red) + 'G=' + str(green) + ' B+' + str(blue)

        cv2.putText(img, text, (50, 50), 2, 0.8, (0, 0, 0), 2, cv2.LINE_AA)

        if (red, blue, green >= 600):
            cv2.putText(img, text, (50, 50), 2, 0.8, (0, 0, 0), 2, cv2.LINE_AA)

        clicked = False

    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()

一如既往,我们非常感谢您的帮助


Tags: csvimportimgifargsgreenbluered
1条回答
网友
1楼 · 发布于 2024-05-18 10:53:34

首先,我将问题的原因隔离开来

import argparse
ap = argparse.ArgumentParser()
ap.add_argument('-i', required=True, help="PathToImage")
args = vars(ap.parse_args())
PathToImage = args['colour_select']

我将代码放入“myscript.py”并运行它;特别是我试过了

python myscript.py -i "hello"

我能够重现这个问题。为了找出原因,我添加了一个打印语句

import argparse
ap = argparse.ArgumentParser()
ap.add_argument('-i', required=True, help="PathToImage")
args = vars(ap.parse_args())
print(args)
PathToImage = args['colour_select']

命令行上显示的是{'i': 'hello'}

如果希望键为colour_select,则应将其用作参数字符串

import argparse
ap = argparse.ArgumentParser()
ap.add_argument('-colour_select', required=True, help="PathToImage")
args = vars(ap.parse_args())
if 'colour_select' not in args.keys():
    print("ERROR!")
PathToImage = args['colour_select']
print('PathToImage =',PathToImage)

相关问题 更多 >