Spaces:
Runtime error
Runtime error
jens
commited on
Commit
·
d4233b7
1
Parent(s):
b10c2c7
fix
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ from inference import SegmentPredictor
|
|
| 11 |
sam = SegmentPredictor() #service.get_sam(configs.model_type, configs.model_ckpt_path, configs.device)
|
| 12 |
red = (255,0,0)
|
| 13 |
blue = (0,0,255)
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
block = gr.Blocks()
|
|
@@ -34,15 +35,16 @@ with block:
|
|
| 34 |
cutout_galary = gr.Gallery(label='Cutouts', object_fit='contain', height=512)
|
| 35 |
with gr.Row():
|
| 36 |
with gr.Column(scale=1):
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
sam_sgmt_everything_btn = gr.Button('Segment Everything!', variant = 'primary')
|
| 39 |
-
sam_encode_btn = gr.Button('Encode')
|
| 40 |
sam_decode_btn = gr.Button('Predict using points!', variant = 'primary')
|
| 41 |
reset_btn = gr.Button('Reset')
|
| 42 |
|
| 43 |
# components
|
| 44 |
components = {point_coords, point_labels, raw_image, masks, cutout_idx, input_image,
|
| 45 |
-
point_label_radio, reset_btn, sam_sgmt_everything_btn,
|
| 46 |
sam_decode_btn, masks_annotated_image}
|
| 47 |
# event - init coords
|
| 48 |
def on_reset_btn_click(raw_image):
|
|
@@ -50,8 +52,10 @@ with block:
|
|
| 50 |
reset_btn.click(on_reset_btn_click, [raw_image], [input_image, point_coords, point_labels], queue=False)
|
| 51 |
|
| 52 |
def on_input_image_upload(input_image):
|
| 53 |
-
print("
|
| 54 |
# encode image on upload
|
|
|
|
|
|
|
| 55 |
return input_image, point_coords_empty(), point_labels_empty(), None
|
| 56 |
input_image.upload(on_input_image_upload, [input_image], [raw_image, point_coords, point_labels], queue=False)
|
| 57 |
|
|
@@ -67,24 +71,15 @@ with block:
|
|
| 67 |
return img, point_coords, point_labels
|
| 68 |
input_image.select(on_input_image_select, [input_image, point_coords, point_labels, point_label_radio], [input_image, point_coords, point_labels], queue=False)
|
| 69 |
|
| 70 |
-
# event - inference
|
| 71 |
-
def on_click_sam_encode_btn(inputs):
|
| 72 |
-
print("encoding")
|
| 73 |
-
image = inputs[raw_image]
|
| 74 |
-
sam.encode(image)
|
| 75 |
-
return [None, None, None]
|
| 76 |
|
| 77 |
def on_click_sam_dencode_btn(inputs):
|
| 78 |
print("inferencing")
|
| 79 |
image = inputs[raw_image]
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
return {masks_annotated_image: (image, [(generated_masks, "fisk")]),
|
| 85 |
-
masks: generated_masks,
|
| 86 |
cutout_idx: set()}
|
| 87 |
-
sam_encode_btn.click(on_click_sam_encode_btn, components, [masks_annotated_image, masks, cutout_idx], queue=True)
|
| 88 |
sam_decode_btn.click(on_click_sam_dencode_btn, components, [masks_annotated_image, masks, cutout_idx], queue=True)
|
| 89 |
#sam_sgmt_everything_btn.click(on_sam_sgmt_everything_click, components, [masks_annotated_image, masks, cutout_idx], queue=True)
|
| 90 |
|
|
|
|
| 11 |
sam = SegmentPredictor() #service.get_sam(configs.model_type, configs.model_ckpt_path, configs.device)
|
| 12 |
red = (255,0,0)
|
| 13 |
blue = (0,0,255)
|
| 14 |
+
annos = []
|
| 15 |
|
| 16 |
|
| 17 |
block = gr.Blocks()
|
|
|
|
| 35 |
cutout_galary = gr.Gallery(label='Cutouts', object_fit='contain', height=512)
|
| 36 |
with gr.Row():
|
| 37 |
with gr.Column(scale=1):
|
| 38 |
+
with gr.Row():
|
| 39 |
+
point_label_radio = gr.Radio(label='Point Label', choices=[1,0], value=1)
|
| 40 |
+
text = gr.Textbox(label='Mask Name')
|
| 41 |
sam_sgmt_everything_btn = gr.Button('Segment Everything!', variant = 'primary')
|
|
|
|
| 42 |
sam_decode_btn = gr.Button('Predict using points!', variant = 'primary')
|
| 43 |
reset_btn = gr.Button('Reset')
|
| 44 |
|
| 45 |
# components
|
| 46 |
components = {point_coords, point_labels, raw_image, masks, cutout_idx, input_image,
|
| 47 |
+
point_label_radio, text, reset_btn, sam_sgmt_everything_btn,
|
| 48 |
sam_decode_btn, masks_annotated_image}
|
| 49 |
# event - init coords
|
| 50 |
def on_reset_btn_click(raw_image):
|
|
|
|
| 52 |
reset_btn.click(on_reset_btn_click, [raw_image], [input_image, point_coords, point_labels], queue=False)
|
| 53 |
|
| 54 |
def on_input_image_upload(input_image):
|
| 55 |
+
print("encoding")
|
| 56 |
# encode image on upload
|
| 57 |
+
sam.encode(input_image)
|
| 58 |
+
print("encoding done")
|
| 59 |
return input_image, point_coords_empty(), point_labels_empty(), None
|
| 60 |
input_image.upload(on_input_image_upload, [input_image], [raw_image, point_coords, point_labels], queue=False)
|
| 61 |
|
|
|
|
| 71 |
return img, point_coords, point_labels
|
| 72 |
input_image.select(on_input_image_select, [input_image, point_coords, point_labels, point_label_radio], [input_image, point_coords, point_labels], queue=False)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
def on_click_sam_dencode_btn(inputs):
|
| 76 |
print("inferencing")
|
| 77 |
image = inputs[raw_image]
|
| 78 |
+
generated_mask, _, _ = sam.cond_pred(pts=np.array(inputs[point_coords]), lbls=np.array(inputs[point_labels]))
|
| 79 |
+
annos.append((generated_mask, inputs[text]))
|
| 80 |
+
return {masks_annotated_image: (image, annos),
|
| 81 |
+
masks: generated_mask,
|
|
|
|
|
|
|
| 82 |
cutout_idx: set()}
|
|
|
|
| 83 |
sam_decode_btn.click(on_click_sam_dencode_btn, components, [masks_annotated_image, masks, cutout_idx], queue=True)
|
| 84 |
#sam_sgmt_everything_btn.click(on_sam_sgmt_everything_click, components, [masks_annotated_image, masks, cutout_idx], queue=True)
|
| 85 |
|