Versions Compared

Key

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

REST API - Send Image to PEKATImage processing through API works on the principle of sending the image via HTTP request and getting a response with the result (contains Context by default, but there are also other response types available). Only one image can be sent per request. (MODIFY & DELETE)

Widget Connector
overlayyoutube
_templatecom/atlassian/confluence/extra/widgetconnector/templates/youtube.vm
width680px
urlhttps://www.youtube.com/watch?v=WkEYKFe9wMo&feature=youtu.be
height400px

Enable Process

Info

User must enable ‘Process’ enabled in 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 IP address, port, type and typeoptionally further query parameters. The request method must be of ‘POST’ type, and the content type of ‘application/octet-stream’ type.

...

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.

...

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

...

Code Block
languagepy
'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 add added to the context (key is data). It is available only in the Code module (context variable).

Code Block
languagepy
'http://127.0.0.1:8000/analyze_image?width=1920&height=1024&data=SomeInfo'

In Code module you can access this string using:

Code Block
languagepy
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 a query parameterparameters width and height.

Example of a 1920x1024 image:

Code Block
languagepy
'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.

Code Block
languagepy
'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 camera:

Code Block
breakoutModewide
languagepy
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

For Is used for sending an image in ‘jpg’ or ‘png’ formats. - PASS THROUGH THE FLOW,TRAINED MODELS…

Example

...

Basic example

Code Block
languagepy
'http://127.0.0.1:8000/analyze_image'

...

Example with API key

Code Block
languagepy
'http://127.0.0.1:8000/analyze_image?api_key=SUPER_SECRET'

Example Python code which sends images from a folder to Pekat project with API key:

Code Block
breakoutModewide
languagepy
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())

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

Code Block
languagepy
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 API key:

Code Block
breakoutModewide
languagepy
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. context will be always there by result

context starts empty by the deafult, JSON dictonary.. also contatins images

priintscreen

‘image’ The contents are explained on the Context page. You can access the context json from response using:

Code Block
languagepy
j = response.json()

Images

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

Example of decoding and showing the image with PIL library:

Code Block
languagepy
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:

Code Block
languagepy
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…..‘annotated_image’ flow, but without any heatmaps or rectangles (e.g. if the image is scaled during the flow, it returns scaled image).

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

‘heatmap’ heatmap - response is heatmap ‘.png’ image format - contains only the heat maps over the image (without the image!)…….

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

Code Block
languagepy
import base64
import json

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