Skip to content

Commit

Permalink
Update detect_picamera.py
Browse files Browse the repository at this point in the history
updated to add buzzer and LED
  • Loading branch information
Abdulfattah12-coded authored Jan 15, 2025
1 parent 6a784c2 commit 41d4068
Showing 1 changed file with 19 additions and 1 deletion.
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

0 comments on commit 41d4068

Please sign in to comment.