MCPcopy Index your code
hub / github.com/CASIA-LMC-Lab/FastSAM / fast_process

Function fast_process

utils/tools_gradio.py:8–84  ·  view source on GitHub ↗
(
    annotations,
    image,
    device,
    scale,
    better_quality=False,
    mask_random_color=True,
    bbox=None,
    use_retina=True,
    withContours=True,
)

Source from the content-addressed store, hash-verified

6
7
8def fast_process(
9 annotations,
10 image,
11 device,
12 scale,
13 better_quality=False,
14 mask_random_color=True,
15 bbox=None,
16 use_retina=True,
17 withContours=True,
18):
19 if isinstance(annotations[0], dict):
20 annotations = [annotation['segmentation'] for annotation in annotations]
21
22 original_h = image.height
23 original_w = image.width
24 if better_quality:
25 if isinstance(annotations[0], torch.Tensor):
26 annotations = np.array(annotations.cpu())
27 for i, mask in enumerate(annotations):
28 mask = cv2.morphologyEx(mask.astype(np.uint8), cv2.MORPH_CLOSE, np.ones((3, 3), np.uint8))
29 annotations[i] = cv2.morphologyEx(mask.astype(np.uint8), cv2.MORPH_OPEN, np.ones((8, 8), np.uint8))
30 if device == 'cpu':
31 annotations = np.array(annotations)
32 inner_mask = fast_show_mask(
33 annotations,
34 plt.gca(),
35 random_color=mask_random_color,
36 bbox=bbox,
37 retinamask=use_retina,
38 target_height=original_h,
39 target_width=original_w,
40 )
41 else:
42 if isinstance(annotations[0], np.ndarray):
43 annotations = torch.from_numpy(annotations)
44 inner_mask = fast_show_mask_gpu(
45 annotations,
46 plt.gca(),
47 random_color=mask_random_color,
48 bbox=bbox,
49 retinamask=use_retina,
50 target_height=original_h,
51 target_width=original_w,
52 )
53 if isinstance(annotations, torch.Tensor):
54 annotations = annotations.cpu().numpy()
55
56 if withContours:
57 contour_all = []
58 temp = np.zeros((original_h, original_w, 1))
59 for i, mask in enumerate(annotations):
60 if type(mask) == dict:
61 mask = mask['segmentation']
62 annotation = mask.astype(np.uint8)
63 if use_retina == False:
64 annotation = cv2.resize(
65 annotation,

Callers 2

segment_everythingFunction · 0.90
segment_with_pointsFunction · 0.90

Calls 2

fast_show_maskFunction · 0.70
fast_show_mask_gpuFunction · 0.70

Tested by

no test coverage detected