Sicherheitssystem

In diesem Projekt haben wir ein einfaches Sicherheitssystem erstellt. Der PIR-Sensor erkennt, ob jemand vorbeigeht, und dann wird die Kamera aktiviert. Wenn ein Gesicht erkannt wird, macht sie ein Bild und liefert gleichzeitig eine Warnmeldung.

_images/camera.jpg

Schritte

  1. Installieren Sie die vilib-Bibliothek zur Gesichtserkennung.

    cd ~/
    git clone -b picamera2 https://github.com/sunfounder/vilib.git
    cd vilib
    sudo python3 install.py
    
  2. Speichern Sie den folgenden Code auf Ihrem Raspberry Pi und geben Sie ihm einen Namen, zum Beispiel security.ty.

    import os
    from time import sleep, time, strftime, localtime
    from vilib import Vilib
    from robot_hat import Pin, TTS
    
    
    # Initialize the TTS class
    tts = TTS(lang='en-US')
    
    # Display all supported languages
    print(tts.supported_lang())
    
    # Initialize the PIR sensor
    pir = Pin('D0')
    
    def camera_start():
        Vilib.camera_start()
        Vilib.display()
        Vilib.face_detect_switch(True)
    
    def take_photo():
        _time = strftime('%Y-%m-%d-%H-%M-%S', localtime(time()))
        name = f'photo_{_time}'
        username = os.getlogin()
        path = f"/home/{username}/Pictures/"
        Vilib.take_photo(name, path)
        print(f'Photo saved as {path}{name}.jpg')
    
    def main():
        motion_detected = False
        while True:
            # Check for motion
            if pir.value() == 1:
                if not motion_detected:
                    print("Motion detected! Initializing camera...")
                    camera_start()
                    motion_detected = True
                    sleep(2)  # Stabilization delay to confirm motion
    
                # Check for human face and take a photo
                if Vilib.detect_obj_parameter['human_n'] != 0:
                    take_photo()
                    # Read the text
                    tts.say("Security alert: Unrecognized Individual detected. Please verify identity")
                    sleep(2)  # Delay after taking a photo
    
            # If no motion is detected, turn off the camera
            elif motion_detected:
                print("No motion detected. Finalizing camera...")
                Vilib.camera_close()
                motion_detected = False
                sleep(2)  # Delay before re-enabling motion detection
    
            sleep(0.1)  # Short delay to prevent CPU overuse
    
    def destroy():
        Vilib.camera_close()
        print("Camera and face detection stopped.")
    
    if __name__ == '__main__':
        try:
            main()
        except KeyboardInterrupt:
            destroy()
    
  3. Verwenden Sie den Befehl sudo python3 security.py, um diesen Code auszuführen.
  4. Öffnen Sie einen Webbrowser und geben Sie http://rpi_ip:9000/mjpg ein, um das aufgenommene Filmmaterial anzusehen. Zusätzlich finden Sie die aufgenommenen Gesichtsbilder in /home/{Benutzername}/Bilder/.

    _images/browser_camera.jpg