Notifications
Clear all

[Solved] Python skript for external Camera  

Page 1 / 2
  RSS
Käsekuchen
(@kasekuchen)
Active Member
Python skript for external Camera

Hi, 

I have a mk4 which is directly connected to prusa connect. i use an external python script to stream two webcams with opencv. now i would like to extend this and send images directly to prusa connect in an interval. similar to what the cell phone can currently do. can anyone show me a simple python script to send only one image to prusa connect? 

i would think that i can just  upload it as an http post and a token? but unfortunately can't find any info. I saw PrusaConnect SDK for Printer but this seems to complex

Thanks for any help

Best Answer by Tergel:

This guy has a good example using a streaming camera.  I have a pi that I was using with my MK3s so instead I just run libcamera-jpeg to grab and image then upload the image as he points out.  https://gist.github.com/nunofgs/84861ee453254823be6b069ebbce9ad2

Posted : 19/07/2023 1:41 pm
Tergel
(@tergel)
New Member
RE:

This guy has a good example using a streaming camera.  I have a pi that I was using with my MK3s so instead I just run libcamera-jpeg to grab and image then upload the image as he points out.  https://gist.github.com/nunofgs/84861ee453254823be6b069ebbce9ad2

This post was modified 1 year ago by Tergel
Posted : 19/07/2023 3:03 pm
Hello
(@hello)
Noble Member
RE: Python skript for external Camera

On prusa connect it asks you to scan a qr code to take you to a link copy that link set up the link then in the python script add a delay to it sending to the url

Please help me out by downloading a model it's free and easy but really helps me out https://www.printables.com/@Hello_474427/models

Posted : 19/07/2023 7:42 pm
Käsekuchen
(@kasekuchen)
Active Member
Topic starter answered:
RE: Python skript for external Camera

Hello Hello,

that did not worked for me.

However thanks tergel for the link! I made myself a working python skript. Hopefully the token and fingerprint do not change. Here is a snippet of my code:

 

import requests

def upload_image(http_url, fingerprint, token, image):
    response = requests.put(
        http_url,
        headers={
            "accept": "*/*",
            "content-type": "image/jpg",
            "fingerprint": fingerprint,
            "token": token,
        },
        data=image,
        stream=True,
        verify=False,
    )
    return response
    

HTTP_URL = "https://webcam.connect.prusa3d.com/c/snapshot"
FINGERPRINT = "XXXXXXXXXXXXXX"
TOKEN = "XXXXXXXXX"
image = YOUR_IMAGE_FUNCTION()
response = upload_image(HTTP_URL, FINGERPRINT, TOKEN, image)
print(response)
Posted : 20/07/2023 12:54 pm
Käsekuchen
(@kasekuchen)
Active Member
Topic starter answered:
RE: Python skript for external Camera

Have a look on the awesome integration which does not really have a benefit. but it is cool 🙂 

Posted : 20/07/2023 1:17 pm
Tojik liked
SAVAGENYHK
(@savagenyhk)
Active Member
RE: Python skript for external Camera

Hi Kasekuchen, 

extreme beginnger here. How do i use this python script to grab images from RTSP camera with login credentials? 

Posted by: @kasekuchen

Hello Hello,

that did not worked for me.

However thanks tergel for the link! I made myself a working python skript. Hopefully the token and fingerprint do not change. Here is a snippet of my code:

 

import requests

def upload_image(http_url, fingerprint, token, image):
    response = requests.put(
        http_url,
        headers={
            "accept": "*/*",
            "content-type": "image/jpg",
            "fingerprint": fingerprint,
            "token": token,
        },
        data=image,
        stream=True,
        verify=False,
    )
    return response
    

HTTP_URL = "https://webcam.connect.prusa3d.com/c/snapshot"
FINGERPRINT = "XXXXXXXXXXXXXX"
TOKEN = "XXXXXXXXX"
image = YOUR_IMAGE_FUNCTION()
response = upload_image(HTTP_URL, FINGERPRINT, TOKEN, image)
print(response)

 

Posted : 27/08/2023 10:51 pm
Käsekuchen
(@kasekuchen)
Active Member
Topic starter answered:
RE: Python skript for external Camera

Hi,

have a look at opencv for python. This should be easy and a good starting point for you. I don't use an rtsp. But here is a snippet which may help you to get on track. "Image" at the end should give you one image. Use this in kind of a loop and functions 

import cv2

RTSP_URL = 'rtsp://user:pass@ADRESS'
cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG)

image = cap.read()
Posted : 28/08/2023 5:06 am
_KaszpiR_
(@_kaszpir_)
Honorable Member
RE: Python skript for external Camera

I would glue images on the sides, and not top/bottom.
Can you provide whole script?

See my GitHub and printables.com for some 3d stuff that you may like.

Posted : 28/08/2023 6:03 am
Käsekuchen
(@kasekuchen)
Active Member
Topic starter answered:
RE: Python skript for external Camera

Hello, hello, hello,

I think it's time I explain a little more about what I do. Maybe I need to start a new thread. My project has grown a lot in the meantime. I have achieved many of my following goals, a rework is still pending:

Current features:

Alpha Printer Cam

Frontend:

  • two video streams in LowQuality with focus on high FPS and low latency
  • Display of HQ photos OnDemand
  • FocusView (switching between FPS modes of both streams)
  • Start / stop PrusaConnect CameraShare
  • Start / stop Timelapse
  • Visualization of Service Status
  • Automatic Day / Night colors according clients “DarkMode” setting.

Backend:

  • Automatic initialisation of cameras by name (UDEV alternative)
  • asynchronous client management (allows streaming to multiple clients with Ptython Flask)
  • Multi threading (one thread per camera, one thread per client. Camera threads send events to clients as soon as a new frame is available, results in low CPU load)
  • Intelligent timelapse (series of images in variable time intervals, but a new image for Timeseries video is stored that most closely resembles the old one. This is to prevent fidgeting on the video. I use SSIM or MSE to compare images and decide which one is the best)
  • Send images to PrusaConnect
  • Provide a merged image from both cameras (merged vertically)
  • Provide individual images in full resolution
  • Change camera resolution
  • Provide JSON API (output status of services, camera, PrusaConnect, timelapse).
  • Auto turn on as soon as somebody connects
  • Auto turn off camera if nobody is watching

(Backend based on FLASK and OPENCV)

Hardware:

  • Raspberry Pi zero 2 w
  • Two cameras (CSI camera, USB camera)

I would describe myself as an intermediate Python programmer. I've been testing my programme for about 2 weeks now and it's running very well so far but has some bugs. However, I'm running out of time and I really need to start commenting my code before I publish it. Also, I could clean up and simplify my code in terms of performance. Unfortunately, I myself have never really worked with Git in the role of author, but I know the principle. I would of course be extremely happy about help and a partner in development. In my private life I am also a young father, which makes it difficult to work on the code constantly.

 

tl;dr: I am happy to share my code. But I need more time to clean it up. The question is also whether you only need the merge function code? Here you go:

    @staticmethod
    def sum_frame(pic1, pic2):
        if pic1 == None or pic2 == None:
            return b''
        image_array1 = np.frombuffer(pic1, np.uint8)
        image_array2 = np.frombuffer(pic2, np.uint8)
        # OpenCV read ToDo: get pics direct in correct format
        image1 = cv2.imdecode(image_array1, cv2.IMREAD_COLOR)
        image2 = cv2.imdecode(image_array2, cv2.IMREAD_COLOR)
        # scale
        min_width = min(image1.shape[1], image2.shape[1])
        image1 = VideoCamera.image_resize(image1, width=min_width)
        image2 = VideoCamera.image_resize(image2, width=min_width)
        c_image = cv2.vconcat([image1, image2])
        cv2.imwrite('./cam_imagem.jpg', c_image)
        ret, jpeg = cv2.imencode('.jpg', c_image)
        return jpeg.tobytes()

    @staticmethod
    def image_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
        # initialize the dimensions of the image to be resized and
        # grab the image size
        dim = None
        (h, w) = image.shape[:2]

        # if both the width and height are None, then return the
        # original image
        if width is None and height is None:
            return image

        # check to see if the width is None
        if width is None:
            # calculate the ratio of the height and construct the
            # dimensions
            r = height / float(h)
            dim = (int(w * r), height)

        # otherwise, the height is None
        else:
            # calculate the ratio of the width and construct the
            # dimensions
            r = width / float(w)
            dim = (width, int(h * r))

        # resize the image
        resized = cv2.resize(image, dim, interpolation=inter)

        # return the resized image
        return resized
Posted : 28/08/2023 4:33 pm
Hello
(@hello)
Noble Member
RE: Python skript for external Camera

Canyou provid all code please I can clean it up

Please help me out by downloading a model it's free and easy but really helps me out https://www.printables.com/@Hello_474427/models

Posted : 28/08/2023 7:40 pm
Käsekuchen
(@kasekuchen)
Active Member
Topic starter answered:
RE: Python skript for external Camera

Hi,

I once again highlite that i am noob accoring github. My code is in chaotic stage at the moment. But here you go:

 

https://github.com/krabbs/PrinterCam

Posted : 30/08/2023 11:54 am
GrumpyNiffler
(@grumpyniffler)
Member
RE: Python skript for external Camera

Would be great to have something like this in a docker container for easy deployment for semi-noobs like me.

Posted : 27/09/2023 9:37 am
vinzcenzo
(@vinzcenzo)
New Member
RE: Python skript for external Camera

Perhaps a basic question, but how can we obtain a fingerprint? I attempted to open the browser developer tools and even used the Postman application to inspect the network requests, but I never came across a fingerprint.

Posted : 13/10/2023 8:49 am
Tojik
(@tojik)
Member Moderator
RE:

The fingerprint is yours to create, there are some limitations on what it can be, try base64 and don't make it too long, i forgot the character limit

Posted : 13/10/2023 8:57 am
vinzcenzo
(@vinzcenzo)
New Member
RE: Python skript for external Camera

 👍 Thanks for the information, it works like a charm with the fingerprint encoded in base64. 😎

Posted by: @tojik

The fingerprint is yours to create, there are some limitations on what it can be, try base64 and don't make it too long, i forgot the character limit

 

Posted : 13/10/2023 4:35 pm
Tojik liked
Fortnite Gamer
(@fortnite-gamer)
Active Member
RE: Python skript for external Camera

Hi! Do you run this script of of a raspberry pi? And what is the .service file for, sorry I am very new to python, as I primarily do front-end work. If you could just explain to me some key points I would need to get this running that would be amazing.

Posted : 16/11/2023 3:35 pm
firetech
(@firetech)
Active Member
RE: Python skript for external Camera

Reviving a semi-dead/dying thread here, but I found this thread the other day, and tried Käsekuchen's PrinterCam. It was cool, but just "import cv2" takes a long time on the Pi Zero (non-2) I intended to use for this, and I had trouble setting appropriate resolutions for my cameras. So, I decided to make my own, taking some cues from how the Pi-based PrusaLink handles cameras.

The result is quite minimal, easy to set up,  requires no additional libraries from a clean installation of Raspberry Pi OS Lite, and runs well even on a first-gen Pi Zero. Might be of use to others as well, so I put it on GitHub: https://github.com/firetech/ConnectCam

Posted : 28/12/2023 2:20 am
GrumpyNiffler
(@grumpyniffler)
Member
RE: Python skript for external Camera

Thanks. That was really easy and works great with my Pi Zero 2 and an old USB Webcam I had.

Posted : 01/01/2024 10:18 pm
_KaszpiR_
(@_kaszpir_)
Honorable Member
RE: Python skript for external Camera

huh opencv2 is pretty heavy for the python, maybe better switch to pillow ( https://python-pillow.org/)? https://note.nkmk.me/en/python-pillow-concat-images/

See my GitHub and printables.com for some 3d stuff that you may like.

Posted : 21/01/2024 9:02 pm
Savvas Keseoglou
(@savvas-keseoglou)
Active Member
RE: Python skript for external Camera

Hi,

 

Can I add my wifi camera  with rtsp protocol to RPI Zero 2W  ?  With the above topic that @kasekuchen mention?

Posted : 05/02/2024 12:31 pm
Page 1 / 2
Share: