Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update detect_picamera.py #508

Open
wants to merge 1 commit into
base: demo
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lite/examples/object_detection/raspberry_pi/detect_picamera.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Example using TF Lite to detect objects with the Raspberry Pi camera."""
# GPIO setup
BUZZER_PIN = 23
LED_PIN = 18

GPIO.setmode(GPIO.BCM) # Use Broadcom pin numbering
GPIO.setup(BUZZER_PIN, GPIO.OUT) # Set buzzer pin as output
GPIO.setup(LED_PIN, GPIO.OUT) # Set LED pin as output

from __future__ import absolute_import
from __future__ import division
Expand Down Expand Up @@ -103,6 +110,16 @@ def annotate_objects(annotator, results, labels):
annotator.text([xmin, ymin],
'%s\n%.2f' % (labels[obj['class_id']], obj['score']))

def handle_signals(results):
"""Activates the buzzer and LED if objects are detected."""
if results:
GPIO.output(BUZZER_PIN, GPIO.HIGH) # Turn on the buzzer
GPIO.output(LED_PIN, GPIO.HIGH) # Turn on the LED
else:
GPIO.output(BUZZER_PIN, GPIO.LOW) # Turn off the buzzer
GPIO.output(LED_PIN, GPIO.LOW) # Turn off the LED



def main():
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -138,7 +155,7 @@ def main():
start_time = time.monotonic()
results = detect_objects(interpreter, image, args.threshold)
elapsed_ms = (time.monotonic() - start_time) * 1000

handle_signals(results) # Trigger buzzer and LED based on detection
annotator.clear()
annotate_objects(annotator, results, labels)
annotator.text([5, 0], '%.1fms' % (elapsed_ms))
Expand All @@ -149,6 +166,7 @@ def main():

finally:
camera.stop_preview()
GPIO.cleanup() # Reset GPIO states


if __name__ == '__main__':
Expand Down