...
The POST and GET requests and their different uses will be explained later on this page, but first we’ll focus on how to find out the IP address and port number of the target project.
URL
The URL for the requests would generally look like this:
...
Example of a request
Code Block |
---|
url = f"http://{ip}:{port}/{path}?response_type={response_type}" |
But depending on the response type you may need to add arguments to the URL, in which case it would be modified to look like this:
Code Block | ||
---|---|---|
| ||
url = f"http://{ip}:{port}/{path}?response_type={response_type}&{argument1}={value1}&{argument2}={value2}&..." |
IP address
The IP address that needs to be in the URL is the address of the machine on which the project is running. Alternatively, if the project is running on the same machine on from which you’ll be sending the requests, you can use address 127.0.0.1.
Port
The port in the URL is the port of the PEKAT project you want to connect to. The port can be found in Projects manager and it can be modified before starting the project.
...
command}&{required_arguments}&{optional_arguments}"
response = requests.post(
url,
data=image,
headers={"Content-Type": "application/octet-stream"},
timeout=timeout,
) |
Each request consists of at least:
URL that specifies where to send the request to the project and what is the project supposed to do
Timeout specifying how long to wait for response before aborting (for example, if the project isn’t running, the request would wait indefinitely)
Furthermore as you can see, the request can have additional arguments:
data - they are present only in case of the POST request. The data argument contains data of the image for processing
headers - in case that the request returns data as a result, the headers argument specifies the format of the data sent
URL
The URL for the requests would generally look like this:
Code Block | ||
---|---|---|
| ||
url = f"http://{ip}:{port}/{command}" |
But depending on the response type you may need to add arguments to the URL, in which case it would be modified to look like this:
Code Block | ||
---|---|---|
| ||
url = f"http://{ip}:{port}/{command}?{argument1}={value1}&{argument2}={value2}&..." |
In total, URL consists of:
IP address of the computer when the project is running
Port on which the project is running
Command that is telling the project what to do
required arguments - information that the project needs to perform the requested task
optional arguments - additional arguments that can be used to change the response type of the project as well as other things
IP address
The IP address that needs to be in the URL is the address of the machine on which the project is running. Alternatively, if the project is running on the same machine on from which you’ll be sending the requests, you can use address 127.0.0.1.
Port
The port in the URL is the port of the PEKAT project you want to connect to. The port can be found in Projects manager and it can be modified before starting the project.
...
Once the project is running, both IP address and port are shown in the project window. The localhost is equivalent top IP “127.0.0.1“:
...
URL Command
Commands differ based on the used request. With POST request, the command specifies the format in which the image data is sent, whereas the commands with GET request can allow you to stop or ping the project or get the last analyzed image.
URL Arguments
URL arguments provide additional context to PEKAT, either required or optional and they differ based on the request and command.
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:
Code Block | ||
---|---|---|
| ||
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 commands
POST request has two different commands, depending on the type of data you’re sending for analyzing. Based on the command you also need to provide certain arguments and optional arguments. The two commands available to the POST requests are “analyze_image” and “analyze_raw_image”.
analyze_image
Is used for sending byte stream with data about .png image. This byte stream data also contain information about the width and height of the image, so it requires less arguments compared to the analyze_raw_image command.
Info | ||
---|---|---|
It’s recommended to load the .png image data using the Path class from pathlib and simply use the .read_bytes() method like this:
|
Note |
---|
The sent data must be of a .png file. The project won’t be able to process other formats. |
The URL of this command doesn’t nee any other arguments, but it can be provided with some optional ones, that will be explained later.
Example of a code sending an image for analyzing using the analyze_image command:
Code Block |
---|
from pathlib import Path
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,
) |
analyze_image command is useful when you don’t want to preprocess the image, because it allows you to load and send images without the need to install and import any additional libraries.
analyze_raw_image
Is used for sending image as a raw numpy byte stream. This byte stream of a numpy array doesn’t contain information about the height and width of the image, so that has to be provided as additional required arguments in the URL.
You can load an image into a numpy image using the cv2 library like this:
Code Block | ||
---|---|---|
| ||
import cv2
image = cv2.imread({"image_path"}) |
After reading the image using cv2, you’ll have to find out it’s dimensions like this:
Code Block | ||
---|---|---|
| ||
height, width = image.shape[:2] |
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:
|
Additional arguments
The additional arguments
Warning |
---|
Continue from here |
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 | ||
---|---|---|
| ||
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.
...
Code Block | ||||
---|---|---|---|---|
| ||||
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.
...