神经网路学习之利用LabelImg对图像标注

对于监督学习算法而言,数据决定了任务的上限,而算法只是在不断逼近这个上限。世界上最遥远的距离就是我们用同一个模型,但是却有不同的任务。但是数据标注是个耗时耗力的工作,下面介绍一个图像标注工具LabelImg。

LabelImg是一个图形图像标注工具,它是用Python编写的,并使用Qt作为其图形界面。注释以PASCAL VOC格式(ImageNet使用的格式)保存为XML文件。此外,它还支持YOLO和CreateML格式。

这里我们转换成接近YOLO标记的 文件(之所以说接近是因为YOLO是中心坐标模式,而且会对长宽进行归一化操作).

1.准备环境,安装依赖:

在ubuntu系统上,安装依赖只需要敲入几个简单的命令即可

sudo apt-get install pyqt5-dev-tools

 2.获取代码:

git clone https://github.com/tzutalin/labelImg.git

3.安装python依赖包:

sudo pip3 install -r requirements/requirements-linux-python3.txt
czl@czl-VirtualBox:~/biaoqian/labelImg$ sudo pip3 install -r requirements/requirements-linux-python3.txt
[sudo] password for czl: 
^Csudo: 1 incorrect password attempt
czl@czl-VirtualBox:~/biaoqian/labelImg$ sudo pip3 install -r requirements/requirements-linux-python3.txt
[sudo] password for czl: 
WARNING: The directory '/home/czl/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
distutils: /usr/local/lib/python3.6/dist-packages
sysconfig: /usr/lib/python3.6/site-packages
WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
distutils: /usr/local/lib/python3.6/dist-packages
sysconfig: /usr/lib/python3.6/site-packages
WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
distutils: /usr/local/include/python3.6/UNKNOWN
sysconfig: /usr/include/python3.6m
WARNING: Value for scheme.scripts does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
distutils: /usr/local/bin
sysconfig: /usr/bin
WARNING: Value for scheme.data does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
distutils: /usr/local
sysconfig: /usr
WARNING: Additional context:
user = False
home = None
root = None
prefix = None
Collecting pyqt5==5.10.1
  Downloading PyQt5-5.10.1-5.10.1-cp35.cp36.cp37.cp38-abi3-manylinux1_x86_64.whl (107.8 MB)
     |████████████████████████████████| 107.8 MB 39.5 MB/s 
Collecting lxml==4.6.3
  Downloading lxml-4.6.3-cp36-cp36m-manylinux2014_x86_64.whl (6.3 MB)
     |████████████████████████████████| 6.3 MB 29.2 MB/s 
Collecting sip<4.20,>=4.19.4
  Downloading sip-4.19.8-cp36-cp36m-manylinux1_x86_64.whl (66 kB)
     |████████████████████████████████| 66 kB 44.6 MB/s 
Installing collected packages: sip, pyqt5, lxml
  WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local/lib/python3.6/dist-packages
  sysconfig: /usr/lib/python3.6/site-packages
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local/lib/python3.6/dist-packages
  sysconfig: /usr/lib/python3.6/site-packages
  WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local/include/python3.6/sip
  sysconfig: /usr/include/python3.6m/sip
  WARNING: Value for scheme.scripts does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local/bin
  sysconfig: /usr/bin
  WARNING: Value for scheme.data does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local
  sysconfig: /usr
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None
  WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local/lib/python3.6/dist-packages
  sysconfig: /usr/lib/python3.6/site-packages
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local/lib/python3.6/dist-packages
  sysconfig: /usr/lib/python3.6/site-packages
  WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local/include/python3.6/pyqt5
  sysconfig: /usr/include/python3.6m/pyqt5
  WARNING: Value for scheme.scripts does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local/bin
  sysconfig: /usr/bin
  WARNING: Value for scheme.data does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local
  sysconfig: /usr
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None

  WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local/lib/python3.6/dist-packages
  sysconfig: /usr/lib/python3.6/site-packages
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local/lib/python3.6/dist-packages
  sysconfig: /usr/lib/python3.6/site-packages
  WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local/include/python3.6/lxml
  sysconfig: /usr/include/python3.6m/lxml
  WARNING: Value for scheme.scripts does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local/bin
  sysconfig: /usr/bin
  WARNING: Value for scheme.data does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
  distutils: /usr/local
  sysconfig: /usr
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None
WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
distutils: /usr/local/lib/python3.6/dist-packages
sysconfig: /usr/lib/python3.6/site-packages
WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
distutils: /usr/local/lib/python3.6/dist-packages
sysconfig: /usr/lib/python3.6/site-packages
WARNING: Value for scheme.headers does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
distutils: /usr/local/include/python3.6/UNKNOWN
sysconfig: /usr/include/python3.6m
WARNING: Value for scheme.scripts does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
distutils: /usr/local/bin
sysconfig: /usr/bin
WARNING: Value for scheme.data does not match. Please report this to <https://github.com/pypa/pip/issues/9617>
distutils: /usr/local
sysconfig: /usr
WARNING: Additional context:
user = False
home = None
root = None
prefix = None
Successfully installed lxml-4.6.3 pyqt5-5.10.1 sip-4.19.8
WARNING: Running pip as root will break packages and permissions. You should install packages reliably by using venv: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 21.1; however, version 21.2.4 is available.
You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.
czl@czl-VirtualBox:~/biaoqian/labelImg$ 

 4. 编译: 

make qt5py3

 5.运行:

python3 labelImg.py

 6.给图像打标签

打标签为yolo可以识别的数据:

首先,在 data/predefined_classes.txt文件中,已经预先定义了一组用于训练的类名列表,我们先看一下内容,打开后,发现它包括了15中分类的名字。

打开工具,在侧边工具栏save按钮下点击鼠标左键,在不同的标签模型之间切换:

7.选择打开目录,成批量的给图片打标签

打标签的过程,右键选择create rectbox或者直接按快捷键"w"进行选框操作。

 结束后,pic目录如下图所示,一个YOLO格式的txt文件将与您同名的图片保存在同一个文件夹中。一个名为“classes.txt”的文件也被保存到该文件夹中。"classes.txt"定义了YOLO标签所引用的类名列表。

 class.txt文件内容

查看生成的文件以及内容,目测第一列应该是种类,第二到第五列是归一化后的矩形框范围,不同的网络,标签的格式可能有所不同,这里生成的而是针对YOLO训练的网络。

为了验证第一列是否是类别,我们违心的按照类别重新将图片打标签,保证类别序号递增且不重复,得到:

czl@czl-VirtualBox:~/biaoqian/pic$ ls -l
total 284
-rw-rw-r-- 1 czl czl   146 8月  16 19:21 classes.txt
-rw-rw-r-- 1 czl czl  8977 8月  16 18:52 dog0.jpeg
-rw-rw-r-- 1 czl czl    38 8月  16 19:20 dog0.xml.txt
-rw-rw-r-- 1 czl czl 99159 8月  16 18:53 dog1.jpeg
-rw-rw-r-- 1 czl czl    38 8月  16 19:20 dog1.xml.txt
-rw-rw-r-- 1 czl czl  7264 8月  16 18:53 dog2.jpeg
-rw-rw-r-- 1 czl czl    38 8月  16 19:20 dog2.xml.txt
-rw-rw-r-- 1 czl czl 75975 8月  16 18:54 person0.jpeg
-rw-rw-r-- 1 czl czl    38 8月  16 19:21 person0.xml.txt
-rw-rw-r-- 1 czl czl 64941 8月  16 18:54 person1.jpeg
-rw-rw-r-- 1 czl czl    38 8月  16 19:21 person1.xml.txt
czl@czl-VirtualBox:~/biaoqian/pic$ cat *.txt
dog
person
cat
tv
car
meatballs
marinara sauce
tomato soup
chicken noodle soup
french onion soup
chicken breast
ribs
pulled pork
hamburger
cavity
0 0.501953 0.498047 0.996094 0.988281
1 0.531429 0.511143 0.842857 0.776163
2 0.779310 0.286972 0.441379 0.559859
3 0.335840 0.313653 0.406015 0.583026
4 0.524242 0.318627 0.793939 0.586275
czl@czl-VirtualBox:~/biaoqian/pic$ 

可以看到,第一列确实是按照0,1,2,3,4的顺序出现,分别表示dog, person,cat, tv, car。可以看到和我们的猜测是符合的。

8.打标签是否对原图产生影响?

我们对比一下备份的文件和打标签后的图片:

发现它们没有任何变化,也就是说,打标签不会影响到原来的图片.

9.使用自定义标签:

使用自定义标签的命令:

python labelImg.py [IMAGE_PATH] [PRE-DEFINED CLASS FILE]

首先,我们要自定义一个标签文本文件:

输入以下命令:

python3 labelImg.py /home/czl/biaoqian/pic/dog0.jpeg /home/czl/biaoqian/pre-defined-class.txt

可以看到,标签已经替换成了我们预先设置的标签种类!

CNN卷积运算的原理:


结束!

Guess you like

Origin blog.csdn.net/tugouxp/article/details/119738718