Versions Compared

Key

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

...

URL arguments provide additional context to PEKAT, either required or optional and they differ based on the request and command.

Request Arguments

The arguments sent in the request are always

POST request

Post request is used to send images for analyzing to PEKAT project and get result. A general POST request to PEKAT project would look like this:

...

Info

For testing, you can also generate random numpy array that could represent an image (in this example, the image size is 512x512 and it’s an rgb image) like this:

Code Block
languagepy
import numpy as np

image = np.random.randint(0, 256, (512, 512, 3), dtype=np.uint8)

Additional arguments

The additional arguments

Warning
Continue from here

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

Code Block
image_bytes = image.tobytes()

Example of a full analyze_raw_image request would look like this:

Code Block
from pathlib import Path
import cv2

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,
        )

Additional optional arguments

The additional optional arguments that can be send in the URL of the request can be:

  • response_type - specifies the response type that is to be returned from the project, meaning what information it should contain

  • data - additional data to be send to project as a string that the project can use to separate the images in different flows and other things

  • context_in_body - the context of the analysis will be returned in the body of the response, meaning that the response will need to be parsed in a different way

Response type

Response type can be either:

  • context - the response will only contain context resulting from the analysis - this is the default response type

  • image - the response will contain processed image (without rectangles or heatmaps but with the changes from preprocess module)

  • annotated_image - the response will contain processed image with detected rectangles from all active modules

  • heatmap - response will contain layered heatmaps on the processed image

An example of a full POST request

Full analyze_raw_image POST request with all optional arguments would look like this:

Code Block
from pathlib import Path
import cv2

image = cv2.imread({"image_path"})
height, width = image.shape[:2]


url = f"http://127.0.0.1:8000/analyze_raw_image?response_type=image&height={height}&width={width}&data={project_data}"
response = requests.post(
            url,
            data=image.tobytes(),
            headers={"Content-Type": "application/octet-stream"},
            timeout=5,
        )

The response returns byte data so to be able to use it in a meaningful way, you will first need to parse it into a usable data type.

GET request

Get request has 4 different types of commands and each is filling a different role. The commands available are

Parsing response from request

In case the request you sent is supposed to return some response (as is the case for the POST request and with some types of commands for GET request) you will have to parse the response from bytes to usable data. The parsing required varies based on the request

The sending address consists of the IP address, port, type, and optional further query parameters. The request method must be of ‘POST’ type and the content type of ‘application/octet-stream’ type.

...

Code Block
import requests
import cv2
import numpy as np
import base64
import json

port = '8000'

request_session = requests.Session()

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

# Decode and show image with heatmap
img = cv2.imdecode(np.frombuffer(response.content, np.uint8), 1)
cv2.imshow("Image", img)
cv2.waitKey(0)

# Decode context from Base64
context_str = base64.b64decode(response.headers['ContextBase64utf'])
# Load json from string
context = json.loads(context_str)
print(context)

Response type

Query parameter ‘response_type’ determines what content will be sent in the response that is returned from PEKAT VISION after processing the request. The request URL changes in the following way (example for annotated_image response type):

...