Displaying photos from a notebook camera on the Pi's Node-RED Dashboard

Hi,

I wrote some Python code to transfer photos taken with a standard notebook build-in camera (e.g. using Windows camera app) to the Pi and displaying them on the Node-RED Dashboard. This was motivated by my experience that, at the beginning of a Pi project, people might not want to buy a USB- or a Pi camera because a camera is already integrated in every notebook. For me it works fine. Maybe someone is interested in the code.


Node-RED flow



Photo on dashboard



Python-code (incl. description)

# Requirements:
# Camera app installed (e.g. Windows Camera app) on your local machine
# Python 3 installed on your local machine
#  --> I used a conda environment with Python 3.9
#  --> for required Python packages see below
# Raspberry Pi (I used a Pi 4 model B) with the lates Node-RED installed
# VNC Viewer to connect to the Raspberry Pi running in 'headless' mode

# Steps:
# Open a photo app on your local machine and take some photos
# Look in which folder the photo app stores the photos
#  --> use the path to that folder to define 'path_local' below
# Look for the file name pattern of photos
#  --> use it to define 'pattern' below (e.g. 'WIN_20221211_10_51_00_Pro.jpg' converts to 'WIN_*.jpg')
# Define a folder on your Raspberry Pi to upload the latest photo
#  --> use the path to define 'path_remote' below
# On your Raspberry Pi, open the settings.js file
#  --> sudo nano /home/pi/.node-red/settings.js
#  --> define the static folder in which the latest photo should be stored, e.g.
#      httpStatic: '/home/pi/Desktop/Fotos/',
#  --> Note that this must be the same path as defined in 'path_remote' below
# Define your credentials to create a connection to the Raspberry Pi
#  --> i.e. hostname, username, password (see below)
# Start Node-RED and create a new flow with connected nodes in the following order:
#  --> inject, http request, base64, image-info, template
# In the 'inject' node I defined an intervall of 1s 
# In the 'http request' node, include the url: http://127.0.0.1:1880/photo.jpg
# On your local machine, run the Python code below
#  --> be sure that all the Python libraries are installed first
#  --> the code searches for the latest photo and uploads it to the Pi
# Open the Node-RED Dashboard to show the lates photo taken with your lacal camera

# libraries
import os
import glob
import shutil
import time
import paramiko

# Settings
path_local = 'C:/Users/.../'
path_remote = '/home/pi/Desktop/Fotos/'
pattern = 'WIN_*.jpg'

hostname = 'xxxxxxxx'
username = 'xxxxxxxx'
password = 'xxxxxxxx'

# Create connection to Raspberry Pi
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=hostname,
                   username=username,
                   password=password)

# Wait for latest photo from your photo app and upload it to Raspberry Pi
while True:

    try:
        
        # Get list (sorted by 'getctime') of all photo on local machine
        files_path = os.path.join(path_local, 
                                  pattern)
        files = sorted(glob.iglob(files_path), 
                       key = os.path.getctime, 
                       reverse = True)
        
        # Copy and rename latest photo on local machine
        src = files[0]
        dst = path_local + "photo.jpg"
        shutil.copy(src, dst)
        
        # Execute code on Raspberry Pi (if required)
        # stdin, stdout, stderr = ssh_client.exec_command('ls')
        # line = stdout.readlines()
        # stripped_line = [s.rstrip() for s in line]
        # for i in stripped_line:
        #     print(i)

        # Downloading a file from Raspberry Pi (if required)
        # ftp_client = ssh_client.open_sftp()
        # ftp_client.get(path_remote + 'log_file.json', 
        #                path_local + 'log_file.json')
        # ftp_client.close()

        # Uploading lates photo to Raspberry Pi
        ftp_client = ssh_client.open_sftp()
        ftp_client.put(path_local + "photo.jpg", 
                       path_remote + "photo.jpg")
        ftp_client.close()

        # Delete all photos in local folder
        for i in files[1:]:
            os.remove(i)
      
    except:
        pass
            
    # Wait for next photo
    time.sleep(1)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.