制作自己的标注数据集

因为毕业要求嘛,自己做标注数据集,基础数据是舌动超声图像,从医院采集的舌动超声视频。

 

主要思路是:

  1. 把视频划分成图像
  2. 逐帧进行标注
  3. 将标注信息显示在图像上,生成新的图像
  4. 将标图像合成视频

话不多说开始上代码:

首先我是使用matlab把视频划分成逐帧的图像

%%将视频转换成帧图片
clc;
clear;
close all;

%% 读取视频
video_path='path/***.avi';    
video_obj=VideoReader(video_path);   

frame_number=video_obj.NumFrames;


%% 存储每一帧图片到文件夹image
if ~exist('image_tongue_move1','dir')
    mkdir('image_tongue_move1');
    disp('successfully create directory image!');
end



%%生成逐帧图像
for i=1:frame_number
    image_name=strcat('./image_tongue_move1/im_',num2str(i),'.jpg');   
    frame=read(video_obj,i);  
    frame0=frame(245:501,414:670);
    imwrite(frame0,image_name,'jpg');
end

disp('all images are written into directory image')

第二步是把图像进行标注,我使用的是百度的easydl半自动标注平台,具体使用方法可以查看我的上一篇帖子:

飞浆EasyDL半自动标注语义分割及数据导出_codting的博客-CSDN博客

第三步是json格式的bbox放到图片上去,并生成新的图片

import json
import cv2
import os
#获取图片顺序,跳过未被标注的图像
annpath='./annFile/'
imgpath='./imageFile/'
respath='./resultFile/'
files = os.listdir(annpath)
file_seq=[]
#这里之所以要获得file_seq是因为在百度easydl里面下载的标注图像是不连续的
#有一些难以标注的图像没有json标注文件,因此我们需要从json标注文件里面获得
#已标注图像序列
for i in range(len(files)):
    file_num=files[i].strip('im_')
    file_num=file_num.strip('.json')
    file_seq.append(file_num)

#设置bbox参数
font = cv2.FONT_HERSHEY_SIMPLEX
class_name='tongue'
#给每张图片画bbox,并保存在resultfile里面
for i in file_seq:
    i=str(i)
    with open(annpath+'im_'+i+'.json', 'r') as f:
        a = json.load(f)
        xmin=a['labels'][0]['x1']
        xmax=a['labels'][0]['x2']
        ymin = a['labels'][0]['y1']
        ymax = a['labels'][0]['y2']
    image = cv2.imread(imgpath+'im_'+i+'.jpg')
    cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2)
    cv2.putText(image,class_name,(xmin,ymin+11),font,0.6,(0,255,0),2)
    cv2.imwrite(respath+'im_'+i+'.jpg', image)
print("over!!!")

第四步把标注图片合成视频

#定义图片合成视频程序,
def imgs2video(imgs_dir, save_name):
    fps = 30
    #fourcc这个参数我也不知道有啥用,需要了解记得查官方文档
    fourcc = cv2.VideoWriter_fourcc('I', '4', '2', '0')
    video_writer = cv2.VideoWriter(save_name, fourcc, fps, (281,281))

    for i in file_seq:
        frame=cv2.imread(respath+'im_'+i+'.jpg')
        video_writer.write(frame)

    video_writer.release()

imgs2video(respath, "DatasetId_313104_1646987593_1.avi")

最终效果:

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值