问题描述
我尝试运行此代码(程序的一小部分) 我修复并添加了空格,现在又出现了新错误,也许此数组不适合字符串?
arrayOfPhotos = ["1.jpg","2.jpg","3.jpg","4.jpg"]
for name in arrayOfPhotos:
detections = detector.detectObjectsFromImage(input_image=arrayOfPhotos[name],output_image_path="holo3-detected.jpg")
for detection in detections:
print(arrayOfPhotos[name]," : ",detection["percentage_probability"])
我得到了错误:
Traceback (most recent call last):
File "dTEST.py",line 13,in <module>
detections = detector.detectObjectsFromImage(input_image=arrayOfPhotos[name],output_image_path="holo3-detected.jpg")
TypeError: list indices must be integers or slices,not str
你能帮我吗?
解决方法
这可能是您想要的:
arrayOfPhotos = ["1.jpg","2.jpg","3.jpg","4.jpg"]
for name in arrayOfPhotos:
detections = detector.detectObjectsFromImage(input_image=arrayOfPhotos[name],output_image_path="holo3-detected.jpg")
for detection in detections:
print(arrayOfPhotos[name]," : ",detection["percentage_probability"])
空白在python中很重要,要使语句成为循环的一部分,它需要与循环缩进(我在各行之前添加了2个空格)
编辑:OP编辑了问题。
将input_image = arrayOfPhotos [name]替换为input_image = name
,arrayOfPhotos = ["1.jpg","4.jpg"]
for name in arrayOfPhotos:
detections = detector.detectObjectsFromImage(input_image=name,output_image_path="holo3-detected.jpg")
for detection in detections:
print(name,detection["percentage_probability"])
照片阵列是一个列表,而不是字典...