Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

Image processing through API works on the principle of sending the image via HTTP request and getting a response with the result (contains /wiki/spaces/KB3141/pages/533529551 by default, but there are also other response types available). Only one image can be sent per request.

Enable Process

User must enable ‘Process’ on the left of the panel in Camera tab to be able to process the image sent through API.

Request

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

Find a practical API demonstration in Python at GitHub Image Analyze API.

IP Address

If the project runs on a local computer, the address is 127.0.0.1. If a remote computer is accessed, then you need to use the IP address of the remote computer.

Port

This is selected when starting the project.

API Key

If the project is running with the “Secure image analyze” option enabled, each request in the parameter query must contain the API key that is generated after you enable this option.

'http://127.0.0.1:8000/analyze_image?api_key=553d7790-827c-11ec-978b-6da1176c0b00'

Data

You can add extra information to the request. This string will be added to the context (the key is data). It is available only in the /wiki/spaces/KB3141/pages/533529462 module (context variable).

'http://127.0.0.1:8000/analyze_image?data=SomeInfo'

In the Code module, you can access this string using:

context['data']

Types

analyze_raw_image

Used for sending the image as raw data. For example, the NumPy array in Python is converted to binary format.

You need to send the image dimensions in query parameters width and height.

Example of a 1920x1024 image:

'http://127.0.0.1:8000/analyze_raw_image?width=1920&height=1024'

If you are sending Bayer RG 8 data, it is necessary to add an argument bayer.

'http://127.0.0.1:8000/analyze_raw_image?width=1920&height=1024&bayer'

Example of a Python code sending requests with raw images from the camera:

import cv2
import requests

cap = cv2.VideoCapture(0)

# you can set frame size
# cap.set(cv2.CAP_PROP_FRAME_WIDTH, 960)
# cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 540)

# keep alive
request_session = requests.Session()

while True:
    # get frame from camera
    ret, frame = cap.read()

    # get image shape
    shape = frame.shape

    # send frame to PEKAT VISION
    response = requests.post(
        url='http://127.0.0.1:8000/analyze_raw_image?width='+str(shape[1])+'&height='+str(shape[0]),
        data=frame.tobytes(),
        headers={'Content-Type': 'application/octet-stream'}
    )

    print(response.json())

analyze_image

Is used for sending an image in ‘jpg’ or ‘png’ formats.

Basic example

'http://127.0.0.1:8000/analyze_image'

Example with API key

'http://127.0.0.1:8000/analyze_image?api_key=SUPER_SECRET'

Example Python code that sends images from a folder to the Pekat project with an API key:

import requests
import os

request_session = requests.Session()

for image in os.listdir('images_folder'):
    with open(os.path.join('images_folder', image), 'rb') as image:
        response = requests.post(
            url='http://127.0.0.1:8000/analyze_image?api_key=728a9180-8357-11ec-b645-e917eb5f5d27',
            data=image.read(),
            headers={'Content-Type': 'application/octet-stream'}
        )
        
        print(response.json())

last_image

Gets the last image/context that was analyzed in the given project before.

Basic example (gets just the context)

http://127.0.0.1:8000/last_image

Python code which gets the last processed image with heatmap and shows it then prints out the context.

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

response = requests.post(
            url='http://127.0.0.1:8000/analyze_image?response_type=annotated_image',
            data=image.read(),
            headers={'Content-Type': 'application/octet-stream'}
        )

If there are multiple query parameters, connect with '&', e.g. with an API key:

url='http://127.0.0.1:8000/analyze_image?api_key=728a9180-8357-11ec-b645-e917eb5f5d27&response_type=annotated_image'

Context

context – a serialized context in JSON format. The contents are explained on the /wiki/spaces/KB3141/pages/533529551 page. You can access the context json from the response using:

j = response.json()

Images

Further options return a PNG image in binary form in the response. It needs to be decoded afterward to further work with it as an image.

Example of decoding and showing the image with the PIL library:

from PIL import Image
from io import BytesIO

img = Image.open(BytesIO(response.content))
img.show()

Example of decoding and showing the image with OpenCV library:

import cv2
import numpy as np

img = cv2.imdecode(np.frombuffer(response.content, np.uint8), 1)
cv2.imshow("Image", img)
cv2.waitKey(0)

image – processed image - output image that passed through the flow, but without any heatmaps or rectangles (e.g. if the image is scaled during the flow, it returns a scaled image).

annotated_image – processed image with annotations - heatmaps over the image.

heatmap - response contains only the heatmaps over the image (without the image).

For all those image response types, the serialized context is by default added to the headers with the header title 'ContextBase64utf'. To get it to JSON form, you can use this code:

import base64
import json

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

Context in body

When getting an image in the response, by default the context is sent in headers. However, with a very large number of defects, the maximum header length limit can be reached. To solve this, you can use the option to send context in the body of the response together with the image and get the image length in a header 'ImageLen' to be able to divide the two parts after. To activate this mode, add the following to the request:

context_in_body=t

Example of the usage in Python code when getting the last image:

import requests
import cv2
import numpy as np
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&context_in_body=t',
    headers={'Content-Type': 'application/octet-stream'}
)

# Get image length
img_len = int(response.headers['ImageLen'])

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

# Get and print context
context = json.loads(response.content[img_len:])
print(context)

Testing the API functionality

If you have a running project on a port e.g. 8000, you can test the API by accessing this address in your browser (if PEKAT is running on the local computer, otherwise replace localhost with the IP address of the remote PC):

This displays the testing app, where you can choose to display the last processed image or send a new image to analyze. You can choose the Response type - /wiki/spaces/KB3141/pages/533529551, Image, Annotated image (image with a heatmap), or Heatmap. If the project has Secure image analyze enabled, you also need to enter the API key.

If Analyze image is chosen, you can also select the image that will be sent through the API. If the response type is different than the /wiki/spaces/KB3141/pages/533529551, the default behavior is that the image is sent in the body of the response and the context is sent in the headers - to change this and send both in the body, check the box with Context in the body. The detail of the request structure will be displayed.

After you click Send, you will also see the structure of the response in the blue info box, context (as you would see it in /wiki/spaces/KB3141/pages/534446081), and optionally an image based on the response type.

  • No labels