-4

我尝试运行这段代码(我的程序的一小部分)我修复并添加了空格,现在我得到了新的错误,也许这个数组不适合字符串?

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

你能帮助我吗?

4

2 回答 2

1

你可能想要的是这个:

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

于 2020-08-30T10:54:32.010 回答
0
arrayOfPhotos = ["1.jpg", "2.jpg", "3.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"])

照片数组是一个列表而不是字典......

于 2020-08-30T19:34:38.463 回答