Notifications
Clear all

Camera API  

  RSS
Mike
 Mike
(@mike-46)
New Member
Camera API

Hey I think I'm failing to understand something. I can't get anything other than 401 responses from the Camera Connect API. I've gone to the camera tab in prusa connect and clicked the "add new other camera" button. That gave me a Token to use. Then I've tried sending a request to https://connect.prusa3d.com/c/info with the header

{
  "Token": <the token provided in the connect camera tab>
}

and body

{
  "config": {
    "name": "TESTING"
  }
}

But when I send the request, I get a 401 unauthorized response. I've used dev tools to get a cookie from authenticated requests and then I added them with the SESSID header with no luck.

Obviously, I'm missing a step. From the OpenAPI spec, it looks like I can maybe send images as often as every 10 seconds once I can figure out how to get authenticated. Does anyone know how I can make this work?

Best Answer by Mike:

Figured it out finally. For posterity, here's how to send a single image.

  1. In your printer in the Prusa Connect console, click on the camera tab.
  2. In the camera tab, click the "Add new other camera" button. This will generate a token that you will need later.
  3. Find (or capture) an image in a .jpg format with 640x480 pixels (or so) and save it to some path on your local filesystem.
  4. Think of some not-too-long string, it can be any printiable ascii for sure. (Maybe other characters, I don't know, probably best to just stick to ascii). This will be your fingerprint that you will use later.
  5. Make sure you've installed some recent version of python 3 and that you've pip-installed requests, httpx, aiohttp, or some other http client (or be familiar with urllib3 in python).
  6. Use code that is something like this (Note: to use this you'll need both httpx and loguru):
import base64
from pathlib import Path

import httpx
from loguru import logger


URL = 'https://webcam.connect.prusa3d.com/c/snapshot'
TOKEN = # Replace this comment with the token from the camera tab
FINGERPRINT = base64.b64encode(b'your-fingerprint-string-here')
image_path = Path('/the/path/to/your/image/here.jpg')

with image_path.open(mode='rb') as f:
    image = f.read()

headers = {
    'Content-Type': 'image/jpg',
    'fingerprint': FINGERPRINT,
    'token': TOKEN,
}

with httpx.AsyncClient() as client:
    response = await client.put(url=URL, headers=headers, content=image)

if response.is_error:
    logger.error('Error sending response: {}', response.text)
else:
    logger.info('Image sent successfully')

 

Now, to set up a webcam, find a raspberry pi or some computer. Pip install open cv and anything else you might need to read from the camera (v4l2 for example). Create a script modeled after the above. Create a cron that runs every 10 seconds and does something like the above, but reads the file from open cv instead of the file system.

Opublikowany : 02/11/2023 10:17 pm
Tojik
(@tojik)
Member Moderator
RE: Camera API

Hi, I had luck just sending images to the snapshot endpoint. Just please supply a fingerprint. It should be unique and you can't go wrong with base64 encoding

Opublikowany : 02/11/2023 10:21 pm
Mike
 Mike
(@mike-46)
New Member
Topic starter answered:
RE: Camera API

Thanks for that. I tried just sending a request to https://connect.prusa3d.com/c/snapshot with headers:

  • Content-Type: image/jpg
  • Token: <the token from the Other cameras section>

And I sent the binary data as content. I got a 401 unauthorized response. Should I be using a different url or something? Not sure what I'm doing wrong here.

Opublikowany : 03/11/2023 3:00 am
Mike
 Mike
(@mike-46)
New Member
Topic starter answered:
RE: Camera API

Figured it out finally. For posterity, here's how to send a single image.

  1. In your printer in the Prusa Connect console, click on the camera tab.
  2. In the camera tab, click the "Add new other camera" button. This will generate a token that you will need later.
  3. Find (or capture) an image in a .jpg format with 640x480 pixels (or so) and save it to some path on your local filesystem.
  4. Think of some not-too-long string, it can be any printiable ascii for sure. (Maybe other characters, I don't know, probably best to just stick to ascii). This will be your fingerprint that you will use later.
  5. Make sure you've installed some recent version of python 3 and that you've pip-installed requests, httpx, aiohttp, or some other http client (or be familiar with urllib3 in python).
  6. Use code that is something like this (Note: to use this you'll need both httpx and loguru):
import base64
from pathlib import Path

import httpx
from loguru import logger


URL = 'https://webcam.connect.prusa3d.com/c/snapshot'
TOKEN = # Replace this comment with the token from the camera tab
FINGERPRINT = base64.b64encode(b'your-fingerprint-string-here')
image_path = Path('/the/path/to/your/image/here.jpg')

with image_path.open(mode='rb') as f:
    image = f.read()

headers = {
    'Content-Type': 'image/jpg',
    'fingerprint': FINGERPRINT,
    'token': TOKEN,
}

with httpx.AsyncClient() as client:
    response = await client.put(url=URL, headers=headers, content=image)

if response.is_error:
    logger.error('Error sending response: {}', response.text)
else:
    logger.info('Image sent successfully')

 

Now, to set up a webcam, find a raspberry pi or some computer. Pip install open cv and anything else you might need to read from the camera (v4l2 for example). Create a script modeled after the above. Create a cron that runs every 10 seconds and does something like the above, but reads the file from open cv instead of the file system.

Opublikowany : 05/11/2023 6:51 pm
Tojik, Keno i polubić
Share: