-4

I try to run this code (small part of my program) I fixed and added whitespaces and now I get new error, maybe this array does not fit to string?

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"])

I get the error:

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

Can you help me?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336

2 Answers2

1

what you probably want is this:

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"])

whitespace is important in python, for a statement to be part of a loop it needs to be indented vs the loop (I've added 2 spaces before the lines)

edit: the OP edited the question.

replace input_image=arrayOfPhotos[name] with input_image=name

Adrian Mester
  • 2,523
  • 1
  • 19
  • 23
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"])

The array of photos is a list not a dictionary...