MCPcopy Create free account
hub / github.com/PABannier/sam3.cpp / cpp_style_preprocess

Function cpp_style_preprocess

tests/dump_cpp_preprocessed.py:19–31  ·  view source on GitHub ↗

Replicate C++ sam3_preprocess_image exactly.

(image_path, img_size=1008)

Source from the content-addressed store, hash-verified

17from PIL import Image
18
19def 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
33def load_tensor(path):
34 with open(path + ".shape") as f:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected