MCPcopy
hub / github.com/chenfei-wu/TaskMatrix / segment_image_with_coordinate

Method segment_image_with_coordinate

visual_chatgpt.py:947–988  ·  view source on GitHub ↗

Args: img (numpy.ndarray): the given image, shape: H x W x 3. is_positive: whether the click is positive, if want to add mask use True else False. coordinate: the position of the click If the position is (x,y), me

(self, img, is_positive: bool,
                            coordinate: tuple)

Source from the content-addressed store, hash-verified

945 return img
946
947 def segment_image_with_coordinate(self, img, is_positive: bool,
948 coordinate: tuple):
949 '''
950 Args:
951 img (numpy.ndarray): the given image, shape: H x W x 3.
952 is_positive: whether the click is positive, if want to add mask use True else False.
953 coordinate: the position of the click
954 If the position is (x,y), means click at the x-th column and y-th row of the pixel matrix.
955 So x correspond to W, and y correspond to H.
956 Output:
957 img (PLI.Image.Image): the result image
958 result_mask (numpy.ndarray): the result mask, shape: H x W
959
960 Other parameters:
961 transparency (float): the transparenccy of the mask
962 to control he degree of transparency after the mask is superimposed.
963 if transparency=1, then the masked part will be completely replaced with other colors.
964 '''
965 self.sam_predictor.set_image(img)
966 self.saved_points.append([coordinate[0], coordinate[1]])
967 self.saved_labels.append(1 if is_positive else 0)
968 input_point = np.array(self.saved_points)
969 input_label = np.array(self.saved_labels)
970
971 # Predict the mask
972 with torch.cuda.amp.autocast():
973 masks, scores, logits = self.sam_predictor.predict(
974 point_coords=input_point,
975 point_labels=input_label,
976 multimask_output=False,
977 )
978
979
980 img = self.show_mask(masks[0], img, random_color=False, transparency=0.3)
981
982 img = self.show_points(input_point, input_label, img)
983
984 img = Image.fromarray(img)
985
986 result_mask = masks[0]
987
988 return img, result_mask
989
990 @prompts(name="Segment the Image",
991 description="useful when you want to segment all the part of the image, but not segment a certain object."

Callers

nothing calls this directly

Calls 3

show_maskMethod · 0.95
show_pointsMethod · 0.95
set_imageMethod · 0.80

Tested by

no test coverage detected