RE: Python skript for external Camera
I have tried rtsp but with no luck. but maybe just because of missing knowledge.
RPI Zero 2W has enough power to provide 2 cameras with 1080px and 15 frames with opencv on my side. opencv is indeed to heavy for streams only but very good for the feauters with i described above.
but good news. check the prusa blog. there is some progress for cams.
RE:
Raspberry Pi Preparation:
Open Thonny: Thonny is a Python IDE that comes pre-installed with Raspberry Pi OS. You can find it in the "Programming" menu.
Create a New Python Script:
Click on "File" in the menu bar. Select "New" to create a new file. Paste the following code into the new file:
import os
import subprocess
import time
import requests
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Define camera configurations for Prusa
CAMERAS = [
{
"name": "Camera 1",
"device": "/dev/video0",
"token": ""
}
]
# Prusa settings
HTTP_URL = "https://webcam.connect.prusa3d.com/c/snapshot"
DELAY_SECONDS_FOR_PRUSA = 10
def capture_snapshot(camera):
"""Capture a snapshot from the webcam."""
try:
subprocess.run([
"/usr/bin/ffmpeg",
"-loglevel", "quiet",
"-stats",
"-y",
"-f", "v4l2",
"-i", camera["device"],
"-vf", "format=yuv420p",
"-q:v", "2",
"-frames:v", "1",
"output.jpg"
], check=True)
return True
except subprocess.CalledProcessError as e:
logger.error(f"Error capturing snapshot from {camera['name']}: {e}")
return False
def upload_to_prusa(camera):
"""Upload the captured snapshot to Prusa."""
if not os.path.exists("output.jpg"):
logger.error("Snapshot file not found.")
return False
try:
with open("output.jpg", "rb") as file:
response = requests.put(HTTP_URL,
headers={"accept": "*/*",
"content-type": "image/jpg",
"fingerprint": "",
"token": camera["token"]},
data=file)
response.raise_for_status()
logger.info(f"Image uploaded to Prusa from {camera['name']} successfully.")
return True
except requests.exceptions.RequestException as e:
logger.error(f"Error uploading image to Prusa from {camera['name']}: {e}")
return False
def main():
while True:
for camera in CAMERAS:
if capture_snapshot(camera):
upload_to_prusa(camera)
time.sleep(DELAY_SECONDS_FOR_PRUSA) # Wait 10 seconds before capturing next snapshot
if __name__ == "__main__":
main()
Replace : Replace with your actual Prusa Connect token. You can find this token in your Prusa Connect account settings.
Save the Script: Save the script in a location of your choice. For example, you can save it in the "Documents" folder with the name snapshot.py.
Adding the Script to Startup:
Open Terminal: You can open the terminal by clicking on the terminal icon in the taskbar or by searching for "Terminal" in the applications menu.
Navigate to the Script Location: Use the cd command to navigate to the location where you saved the snapshot.py script. For example:
cd ~/Documents
Set Execution Permissions: Run the following command to ensure that the script has execution permissions:
chmod +x snapshot.py
Edit the Startup Service: Run the following command to create or edit the systemd service file:
[Unit] Description=Snapshot Service After=multi-user.target [Service] Type=simple ExecStart=/usr/bin/python3 /home/pi/Documents/snapshot.py WorkingDirectory=/home/pi/Documents Restart=on-failure RestartSec=5s [Install] WantedBy=multi-user.target
Save and Exit: Press Ctrl + X, then press Y to confirm, and finally press Enter to save the file.
Reload systemd: Run the following command to reload systemd and apply the changes:
sudo systemctl daemon-reload
Enable the Service: Run the following command to enable the service, so it starts automatically on boot:
sudo systemctl enable snapshot.service
Start the Service: Run the following command to start the service:
sudo systemctl start snapshot.service