[LAB 03 · 空间信息:表示、采样与掩码](/course/notebooks/49a2c030eafa4e1db6fc4f1a08c3810a.html) # 3. 实例掩码 · 实现代码 [独立阅读](/course/notebook-blocks/231837089e9a4369aa7529f300a99919.html) 按问题继续 同源场景标签、插值、信息碰撞的轻量解析代码 展开完整实现代码 ``` # 本册独立素材准备:内嵌预览与精确原图坐标,不下载外站,也不依赖其他 notebook 的变量。 from pathlib import Path import base64, json from IPython.display import SVG, display scene_uri = 'https://codingai-lec03.pages.dev/course/knowledge-assets/46ffa1170be676ea38dfc1ee.jpg' annotation = {'version': 'L03-r2-20260922', 'image': 'tabletop-occlusion.png', 'size': [1536, 1024], 'label': '手工教学标注:可见轮廓,非模型输出或数据集真值', 'objects': [{'id': 'cup-red', 'category': 'cup', 'polygon': [[280, 270], [295, 251], [362, 241], [448, 242], [527, 253], [565, 270], [562, 361], [554, 454], [542, 532], [526, 565], [491, 579], [432, 586], [369, 582], [329, 568], [307, 541], [297, 516], [258, 497], [224, 471], [194, 433], [179, 392], [178, 356], [190, 326], [210, 308], [242, 303], [278, 314]], 'holes': [[[280, 351], [251, 328], [227, 328], [209, 343], [200, 368], [204, 400], [219, 432], [244, 459], [276, 480], [290, 481]]], 'box': [177, 238, 390, 351]}, {'id': 'cup-blue', 'category': 'cup', 'polygon': [[642, 266], [663, 248], [732, 238], [814, 238], [888, 249], [925, 267], [925, 306], [954, 296], [985, 296], [1009, 310], [1022, 334], [1025, 366], [1018, 396], [1010, 418], [934, 435], [850, 450], [746, 466], [659, 482], [650, 414], [644, 341]], 'holes': [[[925, 348], [944, 323], [965, 318], [986, 326], [1000, 347], [1002, 373], [993, 404], [984, 424], [919, 438]]], 'box': [641, 235, 386, 249]}, {'id': 'book', 'category': 'book', 'polygon': [[602, 500], [626, 490], [1051, 412], [1450, 548], [1451, 560], [1438, 576], [1438, 633], [1454, 648], [918, 770], [602, 586], [597, 560], [598, 516]], 'holes': [], 'box': [596, 411, 860, 362]}]} asset_dir = Path.cwd() / 'lecture03-assets-r2' asset_dir.mkdir(exist_ok=True) preview_file = asset_dir / 'tabletop-occlusion-preview.jpg' if not preview_file.exists(): preview_file.write_bytes(base64.b64decode(scene_uri.split(',',1)[1])) (asset_dir / 'scene-annotations.json').write_text(json.dumps(annotation,ensure_ascii=False,indent=2)) print('可用材料:', preview_file.name, 'scene-annotations.json') print('标注坐标系:', annotation['size'], ';图片预览768×512,展示时同比例映射。') # 修改这一项为 cup-red、cup-blue 或 book,预测红框和局部窗口怎样改变。 selected_id = 'cup-blue' obj = next(o for o in annotation['objects'] if o['id']==selected_id) x,y,w,h = obj['box'] print('所选对象:',selected_id,'类别:',obj['category'],'框(x,y,w,h):',[x,y,w,h]) svg = f'' display(SVG(svg)) # 将所选框显示在归一化的局部窗口;不把此教学裁剪称为 RoIAlign。 local_svg = f'' display(SVG(local_svg)) ```