...
Example of a request
Code Block |
---|
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,
) |
...
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:
Code Block | ||
---|---|---|
| ||
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,
) |
...
Code Block |
---|
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, ) |
...
Code Block |
---|
image_bytes = image.tobytes() |
Example of a full analyze_raw_image request would look Finally, if the image is in the Bayer RG format, you also need to specify this in the URL like this:
Code Block |
---|
from pathlib import Path import cv2 image = cv2.imread({"image_path"}) height, width = image.shape[:2] url = f"http://127url = f"http://127.0.0.1:8000/analyze_raw_image?height={height}&width={width}&bayer" response = requests.post( url, |
Example of a full analyze_raw_image request would look like this:
Code Block |
---|
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( data=image.tobytes(), url, data=image.tobytes(), headers={"Content-Type": "application/octet-stream"}, timeout=5, ) |
...
Full analyze_raw_image POST request with all optional arguments would look like this:
Code Block | ||
---|---|---|
| ||
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?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 3 different types of commands and each is filling a different role. The commands available are:
ping - check if a project is running
stop - stop a project
last_image - get last analyzed image from a project
ping
This is a simple command that you can use to check whether the project is running or not. The URL of this command doesn’t take any additional arguments and it looks like this:
Code Block |
---|
url = f"http://{self.ip}:{self.port}/ping" |
To check whether a server is running you can use this code:
Code Block |
---|
import requests
url = f"http://{self.ip}:{self.port}/ping"
try:
response = requests.get(url=url, timeout=5)
print("server is running")
except requests.exceptions.Timeout as e:
print("server is offline")
|
stop
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 |
---|
import requests
url = f"http://{self.host}:{self.port}/stop"
requests.get(url=url, timeout=5) |
last_image
You can use this command to get the last analyzed image from a running project. This can perhaps be useful when you’re sending images to a project from one script and then check the results in another one.
Parsing response from request
...
Code Block | ||||
---|---|---|---|---|
| ||||
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)
...