Repository navigation
Python Examples
Semyon Gritsenko edited this page Jan 1, 2024
·
1 revision
from typing import Tuple, Dict, List
import cv2
import requests
import base64
import json as json_lib
def infer(image_paths: List[str], write_to_file: bool = False) -> Dict[str, object]:
json = {
"images": [],
"useCuda": False,
"unitTypes": [
"FACE_DETECTOR"
]
}
for image_path in image_paths:
image = cv2.imread(image_path, cv2.IMREAD_COLOR)
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
json["images"].append({
"width": rgb_image.shape[1],
"height": rgb_image.shape[0],
"channels": rgb_image.shape[2],
"data": base64.b64encode(rgb_image.tobytes()).decode(),
"depthType": "uint8_t",
})
response = requests.post("http://127.0.0.1/infer", json=json)
if response.status_code >= 400:
print(response.text)
raise Exception(response.text)
if write_to_file:
with open("infer.json", "w") as file:
file.write(response.text)
return json_lib.loads(response.text)from typing import Dict, List
import cv2
import requests
import base64
import json as json_lib
def recognize(reference_image_path: str, image_paths: List[str], write_to_file: bool = False) -> Dict[str, object]:
reference_image = cv2.imread(reference_image_path)
reference_rgb_image = cv2.cvtColor(reference_image, cv2.COLOR_BGR2RGB)
reference_image_bytes = reference_rgb_image.tobytes()
json = {
"referenceImage": {
"width": reference_rgb_image.shape[1],
"height": reference_rgb_image.shape[0],
"channels": reference_rgb_image.shape[2],
"data": base64.b64encode(reference_image_bytes).decode(),
"depthType": "uint8_t"
},
"images": [],
"useCuda": True
}
for image_path in image_paths:
image = cv2.imread(image_path)
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_bytes = rgb_image.tobytes()
json["images"].append({
"width": rgb_image.shape[1],
"height": rgb_image.shape[0],
"channels": rgb_image.shape[2],
"data": base64.b64encode(image_bytes).decode(),
"depthType": "uint8_t"
})
response = requests.post("http://127.0.0.1/recognize", json=json)
if response.status_code >= 400:
raise Exception(response.text)
if write_to_file:
with open("recognize.json", "w") as file:
file.write(response.text)
return json_lib.loads(response.text)
def quick_recognize(first_image_path: str, second_image_path: str) -> bool:
first_image = cv2.imread(first_image_path)
first_rgb_image = cv2.cvtColor(first_image, cv2.COLOR_BGR2RGB)
second_image = cv2.imread(second_image_path)
second_rgb_image = cv2.cvtColor(second_image, cv2.COLOR_BGR2RGB)
json = {
"firstImage": {
"width": first_rgb_image.shape[1],
"height": first_rgb_image.shape[0],
"channels": first_rgb_image.shape[2],
"data": base64.b64encode(first_rgb_image.tobytes()).decode(),
"depthType": "uint8_t"
},
"secondImage": {
"width": second_rgb_image.shape[1],
"height": second_rgb_image.shape[0],
"channels": second_rgb_image.shape[2],
"data": base64.b64encode(second_rgb_image.tobytes()).decode(),
"depthType": "uint8_t"
}
}
response = requests.get("http://127.0.0.1/recognize", json=json)
if response.status_code >= 400:
raise Exception(response.text)
return bool(json_lib.loads(response.text)["verdict"])from typing import List, Tuple, Dict
import cv2
import requests
import base64
import json as json_lib
class ExtractedBodies:
def __init__(self, rgb_image: cv2.Mat):
self.image = rgb_image
self.bounding_boxes: List[Tuple[Tuple[int, int], Tuple[int, int]]] = []
def extract_bodies(image_paths: List[str]) -> List[ExtractedBodies]:
json = {
"images": [],
"unitTypes": [
"HUMAN_BODY_DETECTOR"
],
"useCuda": True
}
extracted_bodies: List[ExtractedBodies] = []
for image_path in image_paths:
image = cv2.imread(image_path)
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
extracted_bodies.append(ExtractedBodies(rgb_image))
json["images"].append({
"width": rgb_image.shape[1],
"height": rgb_image.shape[0],
"channels": rgb_image.shape[2],
"data": base64.b64encode(rgb_image.tobytes()).decode(),
"depthType": "uint8_t"
})
response = requests.post("http://127.0.0.1/infer", json=json)
if response.status_code >= 400:
raise Exception(response.text)
result_json = json_lib.loads(response.text)
index = 0
for result in result_json["result"]:
extracted_body = extracted_bodies[index]
for data in result["data"]:
bounding_box = data["boundingBox"]
top_left = (int(bounding_box["topLeft"]["x"]), int(bounding_box["topLeft"]["y"]))
bottom_right = (int(bounding_box["bottomRight"]["x"]), int(bounding_box["bottomRight"]["y"]))
extracted_body.bounding_boxes.append((top_left, bottom_right))
index += 1
return extracted_bodies
def body_re_identification(reference_image_path: str, image_paths: List[str], write_to_file: bool = False) -> Dict[str, object]:
reference_image = cv2.imread(reference_image_path)
reference_rgb_image = cv2.cvtColor(reference_image, cv2.COLOR_BGR2RGB)
json = {
"referenceImage": {
"width": reference_rgb_image.shape[1],
"height": reference_rgb_image.shape[0],
"channels": reference_rgb_image.shape[2],
"data": base64.b64encode(reference_rgb_image.tobytes()).decode(),
"depthType": "uint8_t"
},
"images": [],
"allowMultipleBodies": True,
"verifyThreshold": 80.0
}
bodies = extract_bodies(image_paths)
for data in bodies:
source_image = data.image
for points in data.bounding_boxes:
top_left = points[0]
bottom_right = points[1]
crop = source_image[top_left[1]:bottom_right[1], top_left[0]:bottom_right[0]]
json["images"].append({
"width": crop.shape[1],
"height": crop.shape[0],
"channels": crop.shape[2],
"data": base64.b64encode(crop.tobytes()).decode(),
"depthType": "uint8_t"
})
response = requests.post("http://127.0.0.1/body_reidentify", json=json)
if response.status_code >= 400:
raise Exception(response.text)
if write_to_file:
with open("body_re_identification.json", "w") as file:
file.write(response.text)
return json_lib.loads(response.text)