Skip to content

Commit 0d73929

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
chore: Add Image Segmentation binary color thresholding parameter
PiperOrigin-RevId: 726182257
1 parent 0aedb1e commit 0d73929

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

tests/unit/aiplatform/test_vision_models.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -1280,8 +1280,27 @@ def test_get_image_segmentation_results(self):
12801280
target=prediction_service_client.PredictionServiceClient,
12811281
attribute="predict",
12821282
return_value=gca_prediction_response,
1283-
):
1284-
segmentation_response = model.segment_image(base_image=image)
1283+
) as mock_predict:
1284+
binary_color_threshold = 48
1285+
segmentation_response = model.segment_image(
1286+
base_image=image,
1287+
confidence_threshold=0.1,
1288+
binary_color_threshold=binary_color_threshold,
1289+
)
1290+
mock_predict.assert_called_once_with(
1291+
endpoint="projects/123456789/locations/us-central1/publishers/google/models/image-segmentation-001",
1292+
instances=[
1293+
{
1294+
"base64EncodedImage": image._image_bytes,
1295+
"parameters": {
1296+
"mode": "foreground",
1297+
"confidenceThreshold": 0.1,
1298+
"binaryColorThreshold": 48,
1299+
},
1300+
}
1301+
],
1302+
retry=base._DEFAULT_RETRY,
1303+
)
12851304
assert len(segmentation_response) == 1
12861305

12871306

vertexai/vision_models/_vision_models.py

+7
Original file line numberDiff line numberDiff line change
@@ -1938,6 +1938,7 @@ def segment_image(
19381938
max_predictions: Optional[int] = None,
19391939
confidence_threshold: Optional[float] = 0.1,
19401940
mask_dilation: Optional[float] = None,
1941+
binary_color_threshold: Optional[float] = None,
19411942
) -> ImageSegmentationResponse:
19421943
"""Segments an image.
19431944
@@ -1967,6 +1968,10 @@ def segment_image(
19671968
mask_dilation: A value to dilate the masks by. The value must be in the
19681969
range of 0.0 (no dilation) and 1.0 (the whole image will be masked).
19691970
The default is 0.0.
1971+
binary_color_threshold: The threshold to convert the grayscale soft
1972+
mask to a binary color black and white mask. The value must be
1973+
in the range of 0 and 255, or -1 to disable the thresholding.
1974+
The default is 96.
19701975
19711976
Returns:
19721977
An `ImageSegmentationResponse` object with the generated masks,
@@ -2000,6 +2005,8 @@ def segment_image(
20002005
parameters["confidenceThreshold"] = confidence_threshold
20012006
if mask_dilation:
20022007
parameters["maskDilation"] = mask_dilation
2008+
if binary_color_threshold:
2009+
parameters["binaryColorThreshold"] = binary_color_threshold
20032010

20042011
response = self._endpoint.predict(
20052012
instances=[instance],

0 commit comments

Comments
 (0)