在Jupyter Notebook中调用Python函数时不识别函数声明
我在Jupyter notebook上写的Python程序中运行一个函数。
但是当我尝试调用这个函数时,它却不认识我在调用时写的名字。
这是我定义函数的地方:
def object_detection_api(img_path, threshold=0.5, rect_th=3, text_size=3,text_th=3):
boxes, pred_cls = get_prediction(img_path, threshold)
# Get predictions
img = cv2.imread(img_path)
# Read image with cv2
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
等等。
这是我在同一个Python文件中,代码的前面调用这个函数的地方:
for images in os.listdir(folder_dir):
# check if the image ends with png
#if (images.endswith(".png")):
#print(images)
boxes, classes = object_detection_api(, imagesthreshold=0.5, rect_th=3, text_size=3, text_th=3)
print("boxes length: ", len(boxes))
print("boxes type: ", type(boxes), " ", type(boxes))
print("classes type: " , type(classes))
#retrieve data from these results
#data.append(thisImageData)
#thisImageData = {"quantity": 0, "area": 0}
break
我试着找拼写错误,但就是找不到。也没有提示我参数错误或返回类型错误。
错误信息:
NameError: name 'object_detection_api' is not defined
我不知道是不是缩进错误,还是在声明时出错,或者是作用域搞错了。
1 个回答
1
首先,试着先运行你的定义单元,这样才能把你的函数添加到Jupyter的环境中。此外,你还需要检查一下object_detection_api的返回值。在循环中调用object_detection_api时,有一个语法错误:在imagesthreshold=0.5之前多了一个逗号,这个逗号应该是函数参数的一部分。试着加上image_path,然后再运行你的代码。