Replicate C++ sam3_preprocess_image exactly.
(image_path, img_size=1008)
| 17 | from PIL import Image |
| 18 | |
| 19 | def cpp_style_preprocess(image_path, img_size=1008): |
| 20 | """Replicate C++ sam3_preprocess_image exactly.""" |
| 21 | img = Image.open(image_path).convert("RGB") |
| 22 | # PIL bilinear resize (this may differ from C++ implementation) |
| 23 | img_resized = img.resize((img_size, img_size), Image.BILINEAR) |
| 24 | pixels = np.array(img_resized, dtype=np.float32) # [H, W, 3] |
| 25 | |
| 26 | # Normalize: (pixel / 255.0 - 0.5) / 0.5 |
| 27 | pixels = (pixels / 255.0 - 0.5) / 0.5 |
| 28 | |
| 29 | # Convert HWC → CHW |
| 30 | chw = pixels.transpose(2, 0, 1) # [3, H, W] |
| 31 | return chw |
| 32 | |
| 33 | def load_tensor(path): |
| 34 | with open(path + ".shape") as f: |
nothing calls this directly
no outgoing calls
no test coverage detected