| | import axengine |
| | import cv2 |
| | import numpy as np |
| |
|
| | class SAMEncoder: |
| | def __init__(self,model_path): |
| | self.sess = axengine.InferenceSession(model_path) |
| | for input in self.sess.get_inputs(): |
| | print(input.name, input.shape) |
| | for output in self.sess.get_outputs(): |
| | print(output.name, output.shape) |
| | self.input_shape = (1024, 1024) |
| | |
| | def letterbox(self, image, target_size, color=(114, 114, 114)): |
| | """ |
| | 将图像调整为目标大小,同时保持原始长宽比,并填充空白区域。 |
| | |
| | :param image: 输入图像 (H, W, C) |
| | :param target_size: 目标尺寸 (width, height) |
| | :param color: 填充颜色 (B, G, R) |
| | :return: 调整后的图像,缩放比例,填充区域 |
| | """ |
| | original_height, original_width = image.shape[:2] |
| | target_width, target_height = target_size |
| |
|
| | |
| | scale = min(target_width / original_width, target_height / original_height) |
| | new_width = int(original_width * scale) |
| | new_height = int(original_height * scale) |
| |
|
| | |
| | resized_image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_LINEAR) |
| |
|
| | |
| | pad_width = (target_width - new_width) // 2 |
| | pad_height = (target_height - new_height) // 2 |
| |
|
| | |
| | padded_image = cv2.copyMakeBorder( |
| | resized_image, |
| | 0 , target_height - new_height , |
| | 0, target_width - new_width , |
| | cv2.BORDER_CONSTANT, |
| | value=color |
| | ) |
| |
|
| | return padded_image, scale, (pad_width, pad_height) |
| | |
| | def preprocess(self,image): |
| | padded_image, scale, (pad_width, pad_height) = self.letterbox(image, self.input_shape) |
| | |
| | padded_image = cv2.cvtColor(padded_image, cv2.COLOR_BGR2RGB) |
| | padded_image = np.expand_dims(padded_image, axis=0) |
| | return padded_image, scale |
| | |
| | def encode(self,image): |
| | padded_image, scale = self.preprocess(image) |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | return self.sess.run(None,{self.sess.get_inputs()[0].name:padded_image}), scale |