

前言
- 由于本人水平有限,难免出现错漏,敬请批评改正。
- 更多精彩内容,可点击进入Python日常小操作专栏、OpenCV-Python小应用专栏、YOLO系列专栏、自然语言处理专栏、人工智能混合编程实践专栏或我的个人主页查看
- 人工智能混合编程实践:C++调用Python ONNX进行YOLOv8推理
- 人工智能混合编程实践:C++调用封装好的DLL进行YOLOv8实例分割
- 人工智能混合编程实践:C++调用Python ONNX进行图像超分重建
- 人工智能混合编程实践:C++调用Python AgentOCR进行文本识别
- 通过计算实例简单地理解PatchCore异常检测
- Python将YOLO格式实例分割数据集转换为COCO格式实例分割数据集
- YOLOv8 Ultralytics:使用Ultralytics框架训练RT-DETR实时目标检测模型
- 基于DETR的人脸伪装检测
- YOLOv7训练自己的数据集(口罩检测)
- YOLOv8训练自己的数据集(足球检测)
- YOLOv5:TensorRT加速YOLOv5模型推理
- YOLOv5:IoU、GIoU、DIoU、CIoU、EIoU
- 玩转Jetson Nano(五):TensorRT加速YOLOv5目标检测
- YOLOv5:添加SE、CBAM、CoordAtt、ECA注意力机制
- YOLOv5:yolov5s.yaml配置文件解读、增加小目标检测层
- Python将COCO格式实例分割数据集转换为YOLO格式实例分割数据集
- YOLOv5:使用7.0版本训练自己的实例分割模型(车辆、行人、路标、车道线等实例分割)
- 使用Kaggle GPU资源免费体验Stable Diffusion开源项目
前提条件
- 熟悉Python
相关介绍
- Python是一种跨平台的计算机程序设计语言。是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的、大型项目的开发。
- PyTorch 是一个深度学习框架,封装好了很多网络和深度学习相关的工具方便我们调用,而不用我们一个个去单独写了。它分为 CPU 和 GPU 版本,其他框架还有 TensorFlow、Caffe 等。PyTorch 是由 Facebook 人工智能研究院(FAIR)基于 Torch 推出的,它是一个基于 Python 的可续计算包,提供两个高级功能:1、具有强大的 GPU 加速的张量计算(如 NumPy);2、构建深度神经网络时的自动微分机制。
- YOLOv5是一种单阶段目标检测算法,该算法在YOLOv4的基础上添加了一些新的改进思路,使其速度与精度都得到了极大的性能提升。它是一个在COCO数据集上预训练的物体检测架构和模型系列,代表了Ultralytics对未来视觉AI方法的开源研究,其中包含了经过数千小时的研究和开发而形成的经验教训和最佳实践。
- Labelme是一款图像标注工具,由麻省理工(MIT)的计算机科学和人工智能实验室(CSAIL)研发。它是用Python和PyQT编写的,开源且免费。Labelme支持Windows、Linux和Mac等操作系统。
- 这款工具提供了直观的图形界面,允许用户在图像上标注多种类型的目标,例如矩形框、多边形、线条等,甚至包括更复杂的形状。标注结果以JSON格式保存,便于后续处理和分析。这些标注信息可以用于目标检测、图像分割、图像分类等任务。
- 总的来说,Labelme是一款强大且易用的图像标注工具,可以满足不同的图像处理需求。
- Labelme标注json文件是一种用于存储标注信息的文件格式,它包含了以下几个主要的字段:
version: Labelme的版本号,例如"4.5.6"。flags: 一些全局的标志,例如是否是分割任务,是否有多边形,等等。shapes: 一个列表,每个元素是一个字典,表示一个标注对象。每个字典包含了以下几个字段:
label: 标注对象的类别名称,例如"dog"。points: 一个列表,每个元素是一个坐标对,表示标注对象的边界点,例如[[10, 20], [30, 40]]。group_id: 标注对象的分组编号,用于表示属于同一组的对象,例如1。shape_type: 标注对象的形状类型,例如"polygon",“rectangle”,“circle”,等等。flags: 一些针对该标注对象的标志,例如是否是难例,是否被遮挡,等等。lineColor: 标注对象的边界线颜色,例如[0, 255, 0, 128]。fillColor: 标注对象的填充颜色,例如[255, 0, 0, 128]。imagePath: 图像文件的相对路径,例如"img_001.jpg"。imageData: 图像文件的二进制数据,经过base64编码后的字符串,例如"iVBORw0KGgoAAAANSUhEUgAA…"。imageHeight: 图像的高度,例如600。imageWidth: 图像的宽度,例如800。
以下是一个Labelme标注json文件的示例:
{
"version": "4.5.6",
"flags": {},
"shapes": [
{
"label": "dog",
"points": [
[
121.0,
233.0
],
[
223.0,
232.0
],
[
246.0,
334.0
],
[
121.0,
337.0
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
}
],
"lineColor": [
0,
255,
0,
128
],
"fillColor": [
255,
0,
0,
128
],
"imagePath": "img_001.jpg",
"imageData": "iVBORw0KGgoAAAANSUhEUgAA...",
"imageHeight": 600,
"imageWidth": 800
}
实验环境
- Python 3.x (面向对象的高级语言)
- albumentations==1.3.1
- opencv-python==4.12.0.88
Python将Labelme实例分割数据集和JSON标注文件生成黑白掩码

- png:图片数据集。
- json:Labelme标注文件。

{
"version": "5.5.0",
"flags": {},
"shapes": [
{
"label": "2",
"points": [
[
328.401273,
330.659604
],
[
328.401273,
335.183068
],
[
327.49687500000005,
336.087664
],
[
327.49687500000005,
336.99226
],
[
326.592477,
337.896856
],
[
326.592477,
338.80145200000004
],
[
325.6875,
339.706048
],
[
325.6875,
341.515724
],
[
324.782523,
342.42032
],
[
324.782523,
344.229512
],
[
323.87812499999995,
345.13459200000005
],
[
323.87812499999995,
346.03918799999997
],
[
322.06875,
347.84837999999996
],
[
322.06875,
348.752976
],
[
321.16435199999995,
349.658056
],
[
321.16435199999995,
350.562652
],
[
320.259375,
351.46724800000004
],
[
320.259375,
352.371844
],
[
318.45000000000005,
354.18152
],
[
318.45000000000005,
355.086116
],
[
317.545602,
355.99071200000003
],
[
317.545602,
357.799904
],
[
316.640625,
358.7045
],
[
316.640625,
359.60958
],
[
315.735648,
360.51417599999996
],
[
315.735648,
361.418772
],
[
314.83124999999995,
362.323368
],
[
314.83124999999995,
363.227964
],
[
313.926273,
364.13256
],
[
313.926273,
365.942236
],
[
312.11689800000005,
367.751428
],
[
309.40312500000005,
367.751428
],
[
309.40312500000005,
427.461056
],
[
312.11689800000005,
427.461056
],
[
313.021875,
428.365652
],
[
313.926273,
428.365652
],
[
314.83124999999995,
429.270248
],
[
317.545602,
429.270248
],
[
318.45000000000005,
428.365652
],
[
319.354398,
428.365652
],
[
321.16435199999995,
426.55645999999996
],
[
322.06875,
426.55645999999996
],
[
323.87812499999995,
424.746784
],
[
325.6875,
424.746784
],
[
326.592477,
423.842188
],
[
344.686227,
423.842188
],
[
345.59062500000005,
424.746784
],
[
346.495023,
423.842188
],
[
378.15937499999995,
423.842188
],
[
379.063773,
424.746784
],
[
393.53935199999995,
424.746784
],
[
394.44375,
425.65138
],
[
403.490625,
425.65138
],
[
404.395023,
426.55645999999996
],
[
407.109375,
426.55645999999996
],
[
408.014352,
427.461056
],
[
408.91875000000005,
427.461056
],
[
409.823148,
428.365652
],
[
410.728125,
428.365652
],
[
411.63310199999995,
429.270248
],
[
412.5375,
429.270248
],
[
413.441898,
430.174844
],
[
418.870023,
430.174844
],
[
419.775,
429.270248
],
[
420.67939800000005,
429.270248
],
[
422.488773,
427.461056
],
[
422.488773,
426.55645999999996
],
[
423.39374999999995,
425.65138
],
[
423.39374999999995,
424.746784
],
[
424.298148,
423.842188
],
[
424.298148,
422.937592
],
[
425.203125,
422.032996
],
[
425.203125,
421.12791599999997
],
[
426.108102,
420.22332
],
[
426.108102,
417.50953200000004
],
[
427.01250000000005,
416.604936
],
[
427.01250000000005,
414.79526
],
[
427.916898,
413.89066399999996
],
[
427.916898,
412.081472
],
[
428.821875,
411.176392
],
[
428.821875,
410.271796
],
[
429.72685199999995,
409.3672
],
[
429.72685199999995,
405.748332
],
[
430.63125,
404.84373600000004
],
[
430.63125,
393.987616
],
[
429.72685199999995,
393.08302
],
[
429.72685199999995,
388.559556
],
[
428.821875,
387.654476
],
[
428.821875,
383.131012
],
[
427.916898,
382.226416
],
[
427.916898,
379.51262799999995
],
[
427.01250000000005,
378.60803200000004
],
[
427.01250000000005,
376.798356
],
[
426.108102,
375.89376
],
[
426.108102,
374.084568
],
[
425.203125,
373.17948800000005
],
[
425.203125,
372.274892
],
[
424.298148,
371.370296
],
[
424.298148,
370.4657
],
[
423.39374999999995,
369.561104
],
[
423.39374999999995,
367.751428
],
[
421.584375,
365.942236
],
[
421.584375,
365.03764
],
[
419.775,
363.227964
],
[
419.775,
362.323368
],
[
417.96562500000005,
360.51417599999996
],
[
417.96562500000005,
359.60958
],
[
416.15625,
357.799904
],
[
416.15625,
356.895308
],
[
415.251273,
355.99071200000003
],
[
415.251273,
355.086116
],
[
412.5375,
352.371844
],
[
412.5375,
351.46724800000004
],
[
411.63310199999995,
350.562652
],
[
411.63310199999995,
349.658056
],
[
408.91875000000005,
346.943784
],
[
408.91875000000005,
346.03918799999997
],
[
406.204398,
343.32491600000003
],
[
406.204398,
342.42032
],
[
399.87187500000005,
336.087664
],
[
398.967477,
336.087664
],
[
398.0625,
335.183068
],
[
396.25312499999995,
335.183068
],
[
394.44375,
333.37339199999997
],
[
394.44375,
330.659604
]
],
"group_id": null,
"description": null,
"shape_type": "polygon",
"flags": {},
"mask": null
},
{
"label": "2",
"points": [
[
88.65937500000001,
312.565748
],
[
88.65937500000001,
315.279536
],
[
84.135648,
319.803
],
[
84.135648,
320.70759599999997
],
[
83.23124999999999,
321.612676
],
[
83.23124999999999,
322.517272
],
[
82.326273,
323.421868
],
[
82.326273,
324.326464
],
[
81.421875,
325.23106
],
[
81.421875,
326.13613999999995
],
[
80.517477,
327.040736
],
[
80.517477,
328.849928
],
[
79.61250000000001,
329.754524
],
[
79.61250000000001,
330.659604
],
[
78.707523,
331.5642
],
[
78.707523,
333.37339199999997
],
[
77.803125,
334.277988
],
[
77.803125,
336.087664
],
[
76.898148,
336.99226
],
[
76.898148,
337.896856
],
[
75.99375,
338.80145200000004
],
[
75.99375,
340.611128
],
[
75.088773,
341.515724
],
[
75.088773,
343.32491600000003
],
[
74.18437499999999,
344.229512
],
[
74.18437499999999,
346.03918799999997
],
[
73.279398,
346.943784
],
[
73.279398,
348.752976
],
[
72.375,
349.658056
],
[
72.375,
351.46724800000004
],
[
71.470602,
352.371844
],
[
71.470602,
353.27644
],
[
70.565625,
354.18152
],
[
70.565625,
355.086116
],
[
69.660648,
355.99071200000003
],
[
66.946875,
355.99071200000003
],
[
66.946875,
409.3672
],
[
69.660648,
409.3672
],
[
71.470602,
411.176392
],
[
71.470602,
412.081472
],
[
74.18437499999999,
414.79526
],
[
74.18437499999999,
415.699856
],
[
77.803125,
419.31872400000003
],
[
78.707523,
419.31872400000003
],
[
84.135648,
424.746784
],
[
85.040625,
424.746784
],
[
88.65937500000001,
428.365652
],
[
90.46875,
428.365652
],
[
91.373148,
429.270248
],
[
95.896875,
429.270248
],
[
96.801273,
428.365652
],
[
97.70625000000001,
428.365652
],
[
99.515625,
426.55645999999996
],
[
100.420023,
426.55645999999996
],
[
104.039352,
422.937592
],
[
104.94375,
422.937592
],
[
105.848148,
422.032996
],
[
132.084375,
422.032996
],
[
132.988773,
422.937592
],
[
144.75,
422.937592
],
[
145.65439800000001,
423.842188
],
[
151.08252299999998,
423.842188
],
[
151.9875,
424.746784
],
[
158.320602,
424.746784
],
[
159.22500000000002,
425.65138
],
[
166.46249999999998,
425.65138
],
[
167.366898,
426.55645999999996
],
[
170.08125,
426.55645999999996
],
[
170.985648,
427.461056
],
[
172.79502300000001,
427.461056
],
[
173.7,
428.365652
],
[
174.60497700000002,
428.365652
],
[
175.50937499999998,
429.270248
],
[
176.414352,
429.270248
],
[
177.31875000000002,
430.174844
],
[
178.22314799999998,
430.174844
],
[
179.128125,
431.079924
],
[
180.032523,
431.079924
],
[
181.84189800000001,
432.889116
],
[
183.65185200000002,
432.889116
],
[
184.55624999999998,
433.79371199999997
],
[
189.079398,
433.79371199999997
],
[
190.88877300000001,
431.98452000000003
],
[
190.88877300000001,
431.079924
],
[
192.69872700000002,
429.270248
],
[
192.69872700000002,
428.365652
],
[
193.60312499999998,
427.461056
],
[
193.60312499999998,
426.55645999999996
],
[
194.508102,
425.65138
],
[
194.508102,
424.746784
],
[
195.41250000000002,
423.842188
],
[
195.41250000000002,
422.937592
],
[
196.31689799999998,
422.032996
],
[
196.31689799999998,
421.12791599999997
],
[
199.03125,
418.414128
],
[
199.03125,
417.50953200000004
],
[
201.74560200000002,
414.79526
],
[
202.64999999999998,
414.79526
],
[
204.45937500000002,
412.986068
],
[
205.36377299999998,
412.986068
],
[
206.26875,
412.081472
],
[
208.98252300000001,
412.081472
],
[
209.8875,
411.176392
],
[
220.74374999999998,
411.176392
],
[
221.648148,
412.081472
],
[
222.55312500000002,
411.176392
],
[
223.45752299999998,
411.176392
],
[
224.3625,
410.271796
],
[
225.266898,
410.271796
],
[
227.07627300000001,
408.462604
],
[
227.07627300000001,
407.558008
],
[
227.98125,
406.65292800000003
],
[
227.98125,
403.93914
],
[
228.88622700000002,
403.034544
],
[
228.88622700000002,
389.464152
],
[
229.79062499999998,
388.559556
],
[
232.50439799999998,
388.559556
],
[
232.50439799999998,
384.940688
],
[
229.79062499999998,
384.940688
],
[
228.88622700000002,
384.036092
],
[
228.88622700000002,
382.226416
],
[
227.98125,
381.32182
],
[
227.98125,
378.60803200000004
],
[
227.07627300000001,
377.702952
],
[
227.07627300000001,
375.89376
],
[
225.266898,
374.084568
],
[
225.266898,
372.274892
],
[
224.3625,
371.370296
],
[
224.3625,
369.561104
],
[
223.45752299999998,
368.656024
],
[
223.45752299999998,
367.751428
],
[
222.55312500000002,
366.846832
],
[
222.55312500000002,
365.942236
],
[
221.648148,
365.03764
],
[
221.648148,
363.227964
],
[
220.74374999999998,
362.323368
],
[
220.74374999999998,
360.51417599999996
],
[
219.83935200000002,
359.60958
],
[
219.83935200000002,
357.799904
],
[
218.934375,
356.895308
],
[
218.934375,
355.086116
],
[
218.02939800000001,
354.18152
],
[
218.02939800000001,
353.27644
],
[
217.125,
352.371844
],
[
217.125,
351.46724800000004
],
[
216.220023,
350.562652
],
[
216.220023,
349.658056
],
[
214.41064799999998,
347.84837999999996
],
[
214.41064799999998,
346.943784
],
[
213.50625000000002,
346.03918799999997
],
[
213.50625000000002,
345.13459200000005
],
[
212.601852,
344.229512
],
[
212.601852,
343.32491600000003
],
[
211.69687499999998,
342.42032
],
[
211.69687499999998,
341.515724
],
[
210.79247700000002,
340.611128
],
[
210.79247700000002,
338.80145200000004
],
[
209.8875,
337.896856
],
[
209.8875,
336.99226
],
[
208.98252300000001,
336.087664
],
[
208.98252300000001,
334.277988
],
[
208.078125,
333.37339199999997
],
[
208.078125,
331.5642
],
[
207.173148,
330.659604
],
[
207.173148,
329.754524
],
[
205.36377299999998,
327.945332
],
[
205.36377299999998,
327.040736
],
[
203.554398,
325.23106
],
[
202.64999999999998,
325.23106
],
[
199.93564800000001,
322.517272
],
[
199.03125,
322.517272
],
[
198.126273,
321.612676
],
[
197.221875,
321.612676
],
[
196.31689799999998,
320.70759599999997
],
[
194.508102,
320.70759599999997
],
[
193.60312499999998,
319.803
],
[
191.79375,
319.803
],
[
190.88877300000001,
318.898404
],
[
189.079398,
318.898404
],
[
188.175,
317.993808
],
[
185.460648,
317.993808
],
[
184.55624999999998,
317.08921200000003
],
[
181.84189800000001,
317.08921200000003
],
[
180.9375,
316.184616
],
[
180.032523,
316.184616
],
[
179.128125,
315.279536
],
[
179.128125,
312.565748
]
],
"group_id": null,
"description": null,
"shape_type": "polygon",
"flags": {},
"mask": null
},
{
"label": "2",
"points": [
[
208.078125,
330.659604
],
[
208.078125,
340.611128
],
[
210.79247700000002,
340.611128
],
[
213.50625000000002,
343.32491600000003
],
[
213.50625000000002,
344.229512
],
[
217.125,
347.84837999999996
],
[
217.125,
348.752976
],
[
218.02939800000001,
349.658056
],
[
218.02939800000001,
350.562652
],
[
218.934375,
351.46724800000004
],
[
218.934375,
352.371844
],
[
219.83935200000002,
353.27644
],
[
219.83935200000002,
355.086116
],
[
220.74374999999998,
355.99071200000003
],
[
220.74374999999998,
357.799904
],
[
221.648148,
358.7045
],
[
221.648148,
361.418772
],
[
222.55312500000002,
362.323368
],
[
222.55312500000002,
364.13256
],
[
223.45752299999998,
365.03764
],
[
223.45752299999998,
365.942236
],
[
224.3625,
366.846832
],
[
224.3625,
367.751428
],
[
225.266898,
368.656024
],
[
225.266898,
370.4657
],
[
226.171875,
371.370296
],
[
226.171875,
373.17948800000005
],
[
229.79062499999998,
376.798356
],
[
229.79062499999998,
377.702952
],
[
231.60000000000002,
377.702952
],
[
232.50439799999998,
378.60803200000004
],
[
236.12314800000001,
378.60803200000004
],
[
237.028125,
379.51262799999995
],
[
239.741898,
379.51262799999995
],
[
240.64687500000002,
378.60803200000004
],
[
242.45625,
378.60803200000004
],
[
243.360648,
377.702952
],
[
244.265625,
377.702952
],
[
247.88437499999998,
374.084568
],
[
247.88437499999998,
373.17948800000005
],
[
248.789352,
372.274892
],
[
248.789352,
371.370296
],
[
249.69375000000002,
370.4657
],
[
249.69375000000002,
367.751428
],
[
250.59814799999998,
366.846832
],
[
250.59814799999998,
361.418772
],
[
249.69375000000002,
360.51417599999996
],
[
249.69375000000002,
355.086116
],
[
248.789352,
354.18152
],
[
248.789352,
348.752976
],
[
247.88437499999998,
347.84837999999996
],
[
247.88437499999998,
345.13459200000005
],
[
246.97997700000002,
344.229512
],
[
246.97997700000002,
341.515724
],
[
245.17002300000001,
339.706048
],
[
245.17002300000001,
337.896856
],
[
243.360648,
336.087664
],
[
242.45625,
336.087664
],
[
240.64687500000002,
334.277988
],
[
240.64687500000002,
330.659604
]
],
"group_id": null,
"description": null,
"shape_type": "polygon",
"flags": {},
"mask": null
},
{
"label": "7",
"points": [
[
229.79062499999998,
316.184616
],
[
229.79062499999998,
331.5642
],
[
234.313773,
331.5642
],
[
235.21875,
332.468796
],
[
237.93310200000002,
332.468796
],
[
238.83749999999998,
333.37339199999997
],
[
239.741898,
333.37339199999997
],
[
241.55127299999998,
335.183068
],
[
242.45625,
335.183068
],
[
245.17002300000001,
337.896856
],
[
245.17002300000001,
338.80145200000004
],
[
246.075,
339.706048
],
[
246.075,
340.611128
],
[
246.97997700000002,
341.515724
],
[
246.97997700000002,
344.229512
],
[
247.88437499999998,
345.13459200000005
],
[
247.88437499999998,
347.84837999999996
],
[
248.789352,
348.752976
],
[
248.789352,
354.18152
],
[
249.69375000000002,
355.086116
],
[
249.69375000000002,
359.60958
],
[
250.59814799999998,
360.51417599999996
],
[
250.59814799999998,
363.227964
],
[
251.503125,
364.13256
],
[
251.503125,
365.03764
],
[
254.21689800000001,
367.751428
],
[
276.834375,
367.751428
],
[
277.739352,
368.656024
],
[
279.548148,
368.656024
],
[
280.453125,
369.561104
],
[
282.2625,
369.561104
],
[
283.166898,
370.4657
],
[
283.166898,
373.17948800000005
],
[
283.166898,
369.561104
],
[
284.071875,
368.656024
],
[
284.071875,
367.751428
],
[
284.976852,
366.846832
],
[
284.976852,
333.37339199999997
],
[
284.071875,
332.468796
],
[
284.071875,
329.754524
],
[
283.166898,
328.849928
],
[
283.166898,
327.040736
],
[
282.2625,
326.13613999999995
],
[
282.2625,
324.326464
],
[
281.358102,
323.421868
],
[
281.358102,
322.517272
],
[
279.548148,
320.70759599999997
],
[
279.548148,
319.803
],
[
278.64375,
318.898404
],
[
278.64375,
316.184616
]
],
"group_id": null,
"description": null,
"shape_type": "polygon",
"flags": {},
"mask": null
},
{
"label": "2",
"points": [
[
310.307523,
334.277988
],
[
310.307523,
337.896856
],
[
309.40312500000005,
338.80145200000004
],
[
309.40312500000005,
339.706048
],
[
307.59375,
341.515724
],
[
307.59375,
342.42032
],
[
306.688773,
343.32491600000003
],
[
306.688773,
344.229512
],
[
304.879398,
346.03918799999997
],
[
302.165625,
346.03918799999997
],
[
302.165625,
359.60958
],
[
305.78437499999995,
359.60958
],
[
306.688773,
360.51417599999996
],
[
310.307523,
360.51417599999996
],
[
311.2125,
361.418772
],
[
314.83124999999995,
361.418772
],
[
315.735648,
360.51417599999996
],
[
316.640625,
360.51417599999996
],
[
317.545602,
359.60958
],
[
317.545602,
358.7045
],
[
318.45000000000005,
357.799904
],
[
318.45000000000005,
356.895308
],
[
319.354398,
355.99071200000003
],
[
319.354398,
355.086116
],
[
320.259375,
354.18152
],
[
320.259375,
353.27644
],
[
321.16435199999995,
352.371844
],
[
321.16435199999995,
350.562652
],
[
322.06875,
349.658056
],
[
322.06875,
348.752976
],
[
322.973148,
347.84837999999996
],
[
322.973148,
346.943784
],
[
323.87812499999995,
346.03918799999997
],
[
323.87812499999995,
345.13459200000005
],
[
324.782523,
344.229512
],
[
324.782523,
343.32491600000003
],
[
325.6875,
342.42032
],
[
325.6875,
341.515724
],
[
327.49687500000005,
339.706048
],
[
330.21064800000005,
339.706048
],
[
330.21064800000005,
334.277988
]
],
"group_id": null,
"description": null,
"shape_type": "polygon",
"flags": {},
"mask": null
},
{
"label": "2",
"points": [
[
284.071875,
334.277988
],
[
284.071875,
351.46724800000004
],
[
293.11875,
351.46724800000004
],
[
293.11875,
348.752976
],
[
294.02314800000005,
347.84837999999996
],
[
296.73749999999995,
347.84837999999996
],
[
297.641898,
346.943784
],
[
302.165625,
346.943784
],
[
303.07060199999995,
346.03918799999997
],
[
303.975,
346.03918799999997
],
[
303.975,
345.13459200000005
],
[
304.879398,
344.229512
],
[
308.498727,
344.229512
],
[
308.498727,
334.277988
]
],
"group_id": null,
"description": null,
"shape_type": "polygon",
"flags": {},
"mask": null
}
],
"imagePath": "1.png",
"imageData": null,
"imageHeight": 484,
"imageWidth": 579
}
代码实现
import os
import json
import numpy as np
from PIL import Image
from tqdm import tqdm
def create_binary_mask_from_labelme(json_path, output_path, image_shape, target_classes):
"""
从 labelme 的 json 文件生成黑白 mask 图像。
参数:
json_path (str): json 文件路径
output_path (str): mask 输出路径
image_shape (tuple): (height, width)
target_classes (list): 需要生成掩码的目标类别列表
"""
with open(json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
height, width = image_shape
mask = np.zeros((height, width), dtype=np.uint8)
for shape in data['shapes']:
label = shape['label']
if label not in target_classes:
continue
points = shape['points']
polygon = [(int(p[0]), int(p[1])) for p in points]
# 使用 PIL 的 ImageDraw 绘制多边形区域
from PIL import ImageDraw
img_mask = Image.new('L', (width, height), 0)
ImageDraw.Draw(img_mask).polygon(polygon, outline=255, fill=255)
mask += np.array(img_mask)
mask_img = Image.fromarray(mask)
mask_img.save(output_path)
def convert_labelme_to_binary_masks(label_dir, image_dir, mask_dir, target_classes):
"""
批量将 labelme json 转换为黑白 mask 图像。
参数:
label_dir (str): json 文件目录
image_dir (str): 原始图像目录(用于获取尺寸)
mask_dir (str): 输出 mask 目录
target_classes (list): 需要生成掩码的目标类别列表
"""
os.makedirs(mask_dir, exist_ok=True)
json_files = [f for f in os.listdir(label_dir) if f.endswith('.json')]
for json_file in tqdm(json_files, desc="生成黑白 mask"):
base_name = os.path.splitext(json_file)[0]
json_path = os.path.join(label_dir, json_file)
# 获取原始图像尺寸
image_path = os.path.join(image_dir, base_name + ".jpg")
if not os.path.exists(image_path):
image_path = os.path.join(image_dir, base_name + ".png")
if not os.path.exists(image_path):
raise FileNotFoundError(f"找不到与 {json_file} 对应的图像文件")
with Image.open(image_path) as img:
image_shape = (img.height, img.width)
output_path = os.path.join(mask_dir, base_name + ".png")
create_binary_mask_from_labelme(json_path, output_path, image_shape, target_classes)
if __name__ == "__main__":
# 修改以下路径和类别名称
label_dir = r"segment"
image_dir = r"segment"
mask_dir = r"segment\masks"
# 替换为你自己的类别列表
# class_names = ['background', 'person', 'car', 'dog'] # 第一个是 background(可选)
class_names = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'10', '11', '12', '13', '14', '15', '16', '17', '18', '19',
'20', '21', '22', '23', '24', '25', '26', '27', '28', '29',
'30', '31', '32', '33', '34', '35', '36', '37', '38', '39',
'40', '41', '42', '43', '44', '45', '46', '47', '48', '49',
'50', '51', '52', '53', '54', '55', '56', '57', '58', '59',
'60', '61', '62', '63', '64', '65', '66', '67', '68', '69',
'70', '71', '72', '73', '74', '75', '76', '77', '78', '79'] # 标签类别名
convert_labelme_to_binary_masks(label_dir, image_dir, mask_dir, class_names)
输出结果

- 由于本人水平有限,难免出现错漏,敬请批评改正。
- 更多精彩内容,可点击进入Python日常小操作专栏、OpenCV-Python小应用专栏、YOLO系列专栏、自然语言处理专栏、人工智能混合编程实践专栏或我的个人主页查看
- 人工智能混合编程实践:C++调用Python ONNX进行YOLOv8推理
- 人工智能混合编程实践:C++调用封装好的DLL进行YOLOv8实例分割
- 人工智能混合编程实践:C++调用Python ONNX进行图像超分重建
- 人工智能混合编程实践:C++调用Python AgentOCR进行文本识别
- 通过计算实例简单地理解PatchCore异常检测
- Python将YOLO格式实例分割数据集转换为COCO格式实例分割数据集
- YOLOv8 Ultralytics:使用Ultralytics框架训练RT-DETR实时目标检测模型
- 基于DETR的人脸伪装检测
- YOLOv7训练自己的数据集(口罩检测)
- YOLOv8训练自己的数据集(足球检测)
- YOLOv5:TensorRT加速YOLOv5模型推理
- YOLOv5:IoU、GIoU、DIoU、CIoU、EIoU
- 玩转Jetson Nano(五):TensorRT加速YOLOv5目标检测
- YOLOv5:添加SE、CBAM、CoordAtt、ECA注意力机制
- YOLOv5:yolov5s.yaml配置文件解读、增加小目标检测层
- Python将COCO格式实例分割数据集转换为YOLO格式实例分割数据集
- YOLOv5:使用7.0版本训练自己的实例分割模型(车辆、行人、路标、车道线等实例分割)
- 使用Kaggle GPU资源免费体验Stable Diffusion开源项目

1435

被折叠的 条评论
为什么被折叠?



