From fba9ef8743b9c91a02cb822f5d441583bc3deba6 Mon Sep 17 00:00:00 2001 From: bsw215583320 <baoshiwei121@163.com> Date: 星期三, 16 四月 2025 18:51:18 +0800 Subject: [PATCH] 增加上料机位置识别 --- identifier.py | 27 ++++++++++++++++++++++----- 1 files changed, 22 insertions(+), 5 deletions(-) diff --git a/identifier.py b/identifier.py index 41e7b22..293bffe 100644 --- a/identifier.py +++ b/identifier.py @@ -21,7 +21,7 @@ self.get_output_details() def idengify(self, image): - input_tensor, ratio = self.prepare_input(image) + input_tensor = self.prepare_input(image) # Perform inference on the image outputs = self.inference(input_tensor) @@ -36,14 +36,16 @@ input_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Resize鍥剧墖涓嶈鐩存帴浣跨敤resize锛岄渶瑕佹寜姣斾緥缂╂斁锛岀┖鐧藉尯鍩熷~绌虹函鑹插嵆鍙� - input_img, ratio = self.ratioresize(input_img) + # input_img = self.ratioresize(input_img) + # 鍚敤涓績瑁佸壀 + input_img = self.center_crop(input_img) # Scale input pixel values to 0 to 1 input_img = input_img / 255.0 input_img = input_img.transpose(2, 0, 1) input_tensor = input_img[np.newaxis, :, :, :].astype(np.float32) - return input_tensor, ratio + return input_tensor def inference(self, input_tensor): start = time.perf_counter() @@ -83,6 +85,21 @@ padded_img[: new_unpad[1], : new_unpad[0]] = im padded_img = np.ascontiguousarray(padded_img) - return padded_img, 1 / r + return padded_img - + def center_crop(self, img): + # 鏂板涓績瑁佸壀鏂规硶 + h, w = img.shape[:2] + desired_h = self.input_height + desired_w = self.input_width + + # 濡傛灉鍥剧墖灏哄澶т簬鐩爣灏哄锛屽垯杩涜涓績瑁佸壀 + if h > desired_h and w > desired_w: + start_y = (h - desired_h) // 2 + start_x = (w - desired_w) // 2 + end_y = start_y + desired_h + end_x = start_x + desired_w + return img[start_y:end_y, start_x:end_x] + else: + # 鍚﹀垯杩涜缂╂斁 + return self.ratioresize(img) -- Gitblit v1.9.3