Python 局部变量';s';分配前参考
我应该如何修复此引用错误?你能帮我解释一下为什么会这样吗Python 局部变量';s';分配前参考,python,Python,我应该如何修复此引用错误?你能帮我解释一下为什么会这样吗 def detectobjects(image2): print("===== Detect Objects - local =====") image = open(image, "rb") # Call API with local image detect_objects_results_local = computervision_client.detect_
def detectobjects(image2):
print("===== Detect Objects - local =====")
image = open(image, "rb")
# Call API with local image
detect_objects_results_local = computervision_client.detect_objects_in_stream(image)
print("Detecting objects in local image:")
if len(detect_objects_results_local.objects) == 0:
print("No objects detected.")
else:
s=""
for object in detect_objects_results_local.objects:
#print("Object : {} with confidence: {}".format(\object.object_property,object.confidence))
obj="object= {} , confidence= {} ".format(object.object_property, object.confidence)
s+=obj
return s
您需要在if-else语句之外定义
s
def detectobjects(image2):
print("===== Detect Objects - local =====")
image = open(image, "rb")
# Call API with local image
detect_objects_results_local = computervision_client.detect_objects_in_stream(image)
print("Detecting objects in local image:")
s=""
if len(detect_objects_results_local.objects) == 0:
print("No objects detected.")
else:
for object in detect_objects_results_local.objects:
#print("Object : {} with confidence: {}".format(\object.object_property,object.confidence))
obj="object= {} , confidence= {} ".format(object.object_property, object.confidence)
s+=obj
return s
有什么不清楚的错误?如果未检测到任何对象,则无法
返回s
。。。也许你应该修正缩进?这是假设一个空字符串是一个有效的返回条件,而不是none。考虑到正在构建的字符串,空字符串可能是好的,尽管对象/置信字符串的列表(可能是空的)可能会更干净,如果不是元组列表的话。如果需要,让调用者构造一个对象/置信对字符串。