REST API - Send Image to PEKAT
Image 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 |
---|
overlay | youtube |
---|
_template | com/atlassian/confluence/extra/widgetconnector/templates/youtube.vm |
---|
width | 680px |
---|
url | https://www.youtube.com/watch?v=WkEYKFe9wMo&feature=youtu.be |
---|
height | 400px |
---|
|
...
Code Block |
---|
|
'http://127.0.0.1:8000/analyze_image?api_key=SUPER_SECRET' |
Example of sending Python code which sends images from a folder to Pekat project with API key:
Code Block |
---|
breakoutMode | wide |
---|
language | py |
---|
|
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 |
---|
|
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 |
---|
breakoutMode | wide |
---|
language | py |
---|
|
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 Context page. context will be always there by result
context starts empty by the deafult, JSON dictonary.. also contatins images
priintscreen
‘image’ You can access the context json from response using:
Code Block |
---|
|
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 |
---|
|
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 |
---|
|
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 |
---|
|
import base64
import json
# Decode from Base64
context_str = base64.b64decode(response.headers['ContextBase64utf'])
# Load json from string
context = json.loads(context_str) |