roboflow / inference

A fast, easy-to-use, production-ready inference server for computer vision supporting deployment of many popular model architectures and fine-tuned models.
https://inference.roboflow.com
Other
1.12k stars 84 forks source link

replace usage of np_image_to_base64 with encode_image_to_jpeg_bytes #469

Closed grzegorz-roboflow closed 3 weeks ago

grzegorz-roboflow commented 3 weeks ago

Description

Use cv2.imencode to convert image into buffer, avoid converting between BGR/RGB manually.

Type of change

Please delete options that are not relevant.

How has this change been tested, please provide a testcase or example of how you tested the change?

import cv2 as cv
from inference.core.utils.image_utils import load_image_from_encoded_bytes, np_image_to_base64
x = cv.imread("/path/to/my/image.jpg")
y = np_image_to_base64(x)  # should convert to RGB first - cv2.cvtColor(x, cv2.COLOR_RGB2BGR)
z = load_image_from_encoded_bytes(y)
cv.imshow("original image", x)
cv.waitKey(0)
cv.imshow("loaded", z)  # colors inverted
cv.waitKey(0)
cv.destroyAllWindows()

Also tested through SDK:

import base64

import cv2 as cv
import numpy as np

from inference.core.utils.image_utils import load_image_from_encoded_bytes, np_image_to_base64
from inference_sdk import InferenceHTTPClient
from inference_sdk.http.entities import InferenceConfiguration

client = InferenceHTTPClient(
    api_url="http://localhost:9001",
    api_key="...",
)

client.use_configuration(InferenceConfiguration(visualize_predictions=True, visualize_labels=True))
client.configure(InferenceConfiguration(visualize_predictions=True, visualize_labels=True))
print(client.inference_configuration.visualize_predictions)

with client.use_model(model_id="yolov8n-640"):
    predictions = client.infer("/path/to/image.jpg")

b64_encoded_img = predictions.get("visualization")
blob = base64.b64decode(b64_encoded_img)
np_array = np.frombuffer(blob, dtype=np.uint8)
img = cv.imdecode(np_array, cv.IMREAD_COLOR)
cv.imshow("", img)
cv.waitKey(0)

Any specific deployment considerations

N/A

Docs

N/A