Tensorflow lite object detection values on raspberry pi

Is there any way to make it so the percentages with the detected object output into a .txt file? I am using a Raspberry Pi 4 (4gb) to run Tensorflow lite, but I need to actually use the values that are displayed, E.G Tree, 89% certainty.

I want to convert the percentage into an integer to make a very basic self-flying ai that just avoids certain detected objects. I know there are easier ways of doing this but I want to learn how to do this with object detection regardless.

I am just going to make a separate code that constantly runs and checks for certain objects, then if it does see an object, if the percentage is above a certain threshold then it will just turn.

Currently, I am using raspbian but I have a spare 32gb micro sd card if a different OS is required or better

Hi @Games-Galore,

Sorry for the delayed response. A Raspbian with 32GB storage is fair enough to store TFlite models. Please try this code to capture object scores as integer percentages into .txt file and loop this code for continuous detection of objects greater than specified threshold level.

threshold = 0.5

with open("detections.txt", "w") as file:
    for i in range(num_detections):
        class_id = int(class_ids[i])
        score = scores[i]

        if score > threshold:
            label = "Object Class {}".format(class_id)  
            percentage = int(score * 100)  
            file.write(f"Detected {label} with {percentage}% certainty\n")
            print(f"Detected {label} with {percentage}% certainty")

Thank You