MCPcopy Index your code
hub / github.com/MrSyee/SAM-remove-background / preprocess

Method preprocess

src/server/tasks/sam.py:64–96  ·  view source on GitHub ↗

Preprocess image to input to encoder. Return: preprocessed: If longest size of target image is 1024, shape of tensor is [B, 3, 1024, 1024]. And dtype of tensor is float32.

(self, image_byte, target_size=1024)

Source from the content-addressed store, hash-verified

62 return outputs
63
64 def preprocess(self, image_byte, target_size=1024) -> torch.Tensor:
65 """
66 Preprocess image to input to encoder.
67
68 Return:
69 preprocessed: If longest size of target image is 1024,
70 shape of tensor is [B, 3, 1024, 1024].
71 And dtype of tensor is float32.
72 """
73 # Convert the bytes to numpy array
74 image = np.frombuffer(image_byte, dtype=np.uint8)
75 image = cv2.imdecode(image, cv2.IMREAD_COLOR)[:, :, ::-1] # RGB
76
77 # Get image shape and convert type
78 if image.shape != (1024, 1024, 3):
79 origin_shape = image.shape[:2]
80 height, width = self.get_preprocess_shape(*origin_shape)
81 image = cv2.resize(image, dsize=(width, height))
82 height, width = image.shape[:2]
83 image_fp = image.astype(np.float32)
84
85 # Normalize
86 image_fp -= np.array([123.675, 116.28, 103.53], dtype=np.float32) # mean
87 image_fp /= np.array([58.395, 57.12, 57.375], dtype=np.float32) # std
88
89 # Padding
90 preprocessed = np.zeros((target_size, target_size, 3), dtype=np.float32)
91 preprocessed[:height, :width, :] = image_fp
92
93 # Convert torch tensor
94 preprocessed = np.moveaxis(preprocessed, -1, 0)[None, :, :, :]
95
96 return preprocessed
97
98 def postprocess(self, image_embedding: torch.Tensor) -> Dict[str, Any]:
99 """Postprocess the inference results for exporting as API response."""

Callers 1

runMethod · 0.95

Calls 1

get_preprocess_shapeMethod · 0.95

Tested by

no test coverage detected