Anna / label_tool.py
JohnChiu's picture
add elegant algorithm
6fa13e0
raw
history blame contribute delete
868 Bytes
import gradio as gr
import numpy as np
def get_mask(im):
"""
im: ImageEditor 返回的数据结构
"""
layers = im.get("layers", [])
if not layers:
return "No drawing yet"
# 取第一层作为示例
layer0 = layers[0]
mask = layer0 > 0 # 布尔数组,True 表示用户绘制的像素
coords = np.argwhere(mask) # 获取绘制像素的坐标
print("Mask shape:", mask.shape)
print("Number of drawn pixels:", np.sum(mask))
print("Coordinates of drawn pixels (y, x):", coords)
return mask[...,3].astype(np.uint8)*255 # 可视化 mask
with gr.Blocks() as demo:
with gr.Row():
im_editor = gr.ImageEditor(type="numpy", crop_size="1:1")
mask_preview = gr.Image()
im_editor.change(get_mask, inputs=im_editor, outputs=mask_preview)
if __name__ == "__main__":
demo.launch()