Diff of /generate_labels.py [000000] .. [eaa663]

Switch to unified view

a b/generate_labels.py
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Sun Apr 21 13:37:37 2019
4
5
@author: Winham
6
7
generate_labels.py: 用于生成训练时的标签,将json文件中的信息转换为.npy文件存储
8
注意:运行前先在同目录下新建一个文件夹119_LABEL
9
10
"""
11
12
import os
13
import numpy as np
14
import json
15
16
Mask_path = 'G:/ECG_UNet/119_MASK/'
17
Label_path = 'G:/ECG_UNet/119_LABEL/'
18
width = 2378          # 保存图像的宽度(像素数)
19
sig_length = 1800     # 实际信号长度(采样点数)
20
N_label_value = 0.5   # 为不同类型定义不同的标记值
21
V_label_value = 1.0
22
23
files = os.listdir(Mask_path)
24
for i in range(len(files)):
25
    file_name = files[i]
26
    print(file_name+' '+str(i+1))
27
    name = file_name[:-5]
28
    f = open(Mask_path+file_name, encoding='utf-8')  
29
    content = json.load(f)['shapes']
30
    label = np.zeros(sig_length)
31
    for j in range(len(content)):
32
        points = content[j]['points']
33
        # 以下是根据图像宽度和实际信号长度之间的关系计算人工标记的在信号中的实际位置
34
        start = int(np.round((points[0][0]+points[-1][0])/2.0 / width * sig_length))
35
        end = int(np.round((points[1][0]+points[-2][0])/2.0 / width * sig_length))
36
        if content[j]['label'] == 'N':
37
            label[start:(end+1)] = N_label_value
38
        else:
39
            label[start:(end+1)] = V_label_value
40
                  
41
    np.save(Label_path+name+'.npy', label)