简体   繁体   中英

Raspberry Pi GPIO pin issue in tflite object detection

I have successfully trained my custom model and used it for object detection. However, I am facing a slight issue regarding the if-else condition on my custom model. I only have one object/class in my trained model, when it is detected I want to make a gpio pin to go HIGH (LED) and when the object is removed from the webcam feed the pin should go LOW. When I run the code the pin does go HIGH on a perfect detection but stays HIGH even if I remove the object from webcam feed. I used the following if-else condition:

if (object_name == labels[int(classes[i])]) and (scores[i]) >= 0.95):
GPIO.output(13, GPIO.HIGH)
else:
GPIO.output(13, GPIO.LOW)

I am using the code found in Edje Electronics's tflite object detection code:

https://github.com/EdjeElectronics/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi/blob/master/TFLite_detection_webcam.py

There are two loops in the object detection code one for the webcam feed and second for creating the bounding boxes when object is detected . If I try to LOW the gpio pin after the second loop the LED does not turn on.

It is difficult to tell the exact cause of it without seeing your code, but based on the example code you linked, it sounds like you are doing something like this:

# First loop for webcam frame
while True:

  # ...

  # Second loop for detection / drawing bounding box
  for i in range(len(scores)):
    if <your match condition>:
      GPIO.output(13, GPIO.HIGH)
    else:
      GPIO.output(13, GPIO.LOW)

    # ...

In case this is what your code looks like, what you're doing is turning on/off your LED always based on the last item in the scores list, which is probably not what you want. I would suggest revising the code something like the following:

# First loop for webcam frame
while True:

  # ...

  found_match = False  
  # Second loop
  for i in range(len(scores)):
    if <your match condition>:
      found_match = True

    # ...

  # Outside the second loop
  GPIO.output(13, found_match)

This would make your LED turn on only when there is at least one detected object according to your match condition, and off otherwise.

If this was not your issue, please update your question with your full code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM