Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Example of a request

Code Block
languagepy
import requests

url = f"http://{ip}:{port}/{command}&{required_arguments}&{optional_arguments}"

response = requests.post(
            url,
            data=image,
            headers={"Content-Type": "application/octet-stream"},
            timeout=timeout,
        )

...

Info

It’s recommended to load the .png image data using the Path class from pathlib and simply use the .read_bytes() method like this:

Code Block
languagepy
from pathlib import Path

image = Path("{image_path}").read_bytes()

...

Example of a code sending an image for analyzing using the analyze_image command:

Code Block
languagepy
from pathlib import Path
import requests

image = Path("img.png").read_bytes()
url = f"http://127.0.0.1:8000/analyze_image"
response = requests.post(
            url,
            data=image,
            headers={"Content-Type": "application/octet-stream"},
            timeout=5,
        )

...

You also have to send the numpy array as byte stream which you can do like this:

Code Block
languagepy
image_bytes = image.tobytes()

Finally, if the image is in the Bayer RG format, you also need to specify this in the URL like this:

Code Block
languagepy
url = f"http://127.0.0.1:8000/analyze_raw_image?height={height}&width={width}&bayer"

Example of a full analyze_raw_image request would look like this:

Code Block
languagepy
from pathlib import Path
import cv2
import requests

image = cv2.imread({"image_path"})
height, width = image.shape[:2]
url = f"http://127.0.0.1:8000/analyze_raw_image?height={height}&width={width}"
response = requests.post(
            url,
            data=image.tobytes(),
            headers={"Content-Type": "application/octet-stream"},
            timeout=5,
        )

...

This is another simple command that can be used to stop a running PEKAT project, however it is recommended to use it only if you start PEKAT project as a process in your script. The code for this command would look like this:

Code Block
languagepy
import requests

url = f"http://{ip}:{port}/stop"
requests.get(url=url, timeout=5)

...

The request would look like this:

Code Block
languagepy
import requests

url = f"http://{ip}:{port}/last_image?response_type={response_type}"
response = requests.get(
    url=url,
    headers={'Content-Type': 'application/octet-stream'}
)

...

The simplest option is when you had the response_type set to “context”, because you can just read the .json data from the response:

Code Block
languagepy
import json

response = {your_request}
context = response.json()

If you DON’T have “context_in_body” set in the URL, the image and the context will be sent in the separate parts of the response. Image in the content of the response and context in “ContextBase64utg” attribute of the response. If that attribute is None it means that no image was sent back and you can access the context just like when the response type is set to “context”.

Code Block
languagepy
import json

response = {your_request}

img_bytes = response.content
context_base64 = response.headers.get("ContextBase64utf")
if context_base64 is None:
    return Result(None, response.json())
context_json = base64.b64decode(context_base64)
context = json.loads(context_json)

If you DO have “context_in_body” set in the URL, the image and the context will be sent together and you’ll have to split them apart. You can do this by accessing the “ImageLen“ attribute of the response - it tells you how much of the data is the image (the first part) the rest is context. If ImageLen is None, no image was sent and again, you can just parse the context like when the response type is just “context”.

Code Block
languagepy
import json

response = {your_request}

image_len_str = response.headers.get("ImageLen")
if not image_len_str:
    return Result(None, response.json())
image_len = int(image_len_str)
img_bytes = response.content[:image_len]
context_json = response.content[image_len:].decode()
context = json.loads(context_json)

Finally, you can combine all of the options of parsing into one code if you want:

Code Block
languagepy
import json

context_in_body = {your_option} # either True or False
response_type = {your_response_type}
response = {your_request}

if response_type == "context":
    None, response.json()

if context_in_body:
    image_len_str = response.headers.get("ImageLen")
    if not image_len_str:
        return Result(None, response.json())
    image_len = int(image_len_str)
    img_bytes = response.content[:image_len]
    context_json = response.content[image_len:].decode()
else:
    img_bytes = response.content
    context_base64 = response.headers.get("ContextBase64utf")
    if context_base64 is None:
        return Result(None, response.json())
    context_json = base64.b64decode(context_base64)

return img_bytes, json.loads(context_json)

...