labelimg源码运行环境配置及打包

作者:Kinglong    发表时间:2023-06-30 05:50   

关键词:No module named 'PyQt5'  No module named 'libs'  

1.更新pip
c:\python39\python.exe -m pip install --upgrade pip

2.虚拟环境
创建虚拟环境
conda create -n labelimg python=3.8
激活虚拟环境
conda activate labelimg

2.解决pyrcc5报错,安装pyrcc5,报错No module named 'PyQt5'
pip install PyQt5
pip install pyqt5-tools

如果pip安装pyqt5太慢,改成以下方式
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQt5
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyqt5-tools

3.解决'lxml'报错,安装 lxml
pip install lxml

4.编译资源文件resources.qrc
Terminal下运行:python -m PyQt5.pyrcc_main resources.qrc -o resources.py

5.复制resources.py文件到libs
将上一个步骤生成的resources.py文件,复制到libs文件夹下.

6.运行labelImg.py
不打包,直接运行labelImg.py,即可进行标注操作
报错 dist.py:275: UserWarning: Unknown distribution option: 'app'
pip install wheel
pip install numpy
加速安装 pip install numpy -i https://pypi.doubanio.com/simple wheel
        pip install numpy -i https://pypi.doubanio.com/simple whenumpyl

7.pyinstaller打包 labelImg.py生成exe
(1)安装pyinstaller:pip install pyinstaller
(2)打包,Terminal运行: pyinstaller -F labelImg.py -p ./libs -p ./
(a)打包过程报错 upx is not available.
      这条有这个提示, 后面的打包肯定不能正常完成了!
     下载安装UPX.exe(https://upx.github.io/,https://github.com/upx/upx/releases), 下载下来是一个压缩包,解压,把exe文件拷贝到pyinstaller目录下, 我的是 C:\Python39\Scripts\
     打包成功。
(b)如果使用命令pyinstaller -F -w labelImg.py 编译,生成的exe运行报错ModuleNotFoundError: No module named 'libs',找不到libs模块,打包没有包含该模块。修改打包命令,使包含libs。
  Terminal运行(-F打包成exe,-w 不显示控制台):pyinstaller -F -w labelImg.py -p ./libs -p ./
  Terminal运行(显示控制台):pyinstaller -F labelImg.py -p ./libs -p ./

8.编译后的labelImg.exe不加载data中的配置文件
运行没有事,编译后不加载data中的配置文件。
用“显示控制台”命令打包,运行时报错 “Not find:/data/predefined_classes.txt (optional)”
打印参数发现路径的前缀不对,不是exe所在目录:args.class_file:C:\Users\HT007\AppData\Local\Temp\_MEI244802\data\predefined_classes.txt

原因:python3下,打包成exe后,data目录路径不能再用__file__来定位,而应该用sys.executable定位。
解决思路:调试模式下sys.executable得到python.exe文件路径,打包成exe后sys.executable得到labelImg.exe文件路径。利用二者区别,对打包成exe的情况,根据labelImg.exe重新定位data所在目录。修改labelImg.py文件的代码如下:
    # argparser.add_argument("class_file",
    #                        default=os.path.join(os.path.dirname(__file__), "data", "predefined_classes.txt"),
    #                        nargs="?")
    base_dir = os.path.dirname(__file__)
    if sys.executable.find("labelImg.exe") > 0:
        base_dir = os.path.dirname(os.path.realpath(sys.executable))
    argparser.add_argument("class_file",
                           default=os.path.join(base_dir, "data", "predefined_classes.txt"),
                           nargs="?")

9. 处理输出json格式时的中文乱码情况
https://blog.csdn.net/qq_26502245/article/details/122072396
# Path(self.output_file).write_text(json.dumps(output_dict), ENCODE_METHOD)
Path(self.output_file).write_text(json.dumps(output_dict, ensure_ascii=False), ENCODE_METHOD)

10.修改导出数据的格式
  默认为xml,修改为json
# self.label_file_format = settings.get(SETTING_LABEL_FILE_FORMAT, LabelFileFormat.PASCAL_VOC)
self.label_file_format = settings.get(SETTING_LABEL_FILE_FORMAT, LabelFileFormat.CREATE_ML)

11.修改样式 修改文件libs/shape.py文件
https://blog.csdn.net/linranran94/article/details/125064610
修改四个顶点的大小:point_size = 6
修改默认边框颜色:DEFAULT_SELECT_LINE_COLOR
修改选中边框颜色:DEFAULT_SELECT_LINE_COLOR
将paint()函数中89行的color变量if后面的语句注释掉
color = self.select_line_color #if self.selected else self.line_color

12.运行报错处理

 打开image目录后,报错

  File "labelImg.py", line 1189, in load_file
  File "labelImg.py", line 1234, in show_bounding_box_from_annotation_file
  File "labelImg.py", line 1678, in load_yolo_txt_by_filename
  File "yolo_io.py", line 97, in __init__
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\ht-gitlab\\DataAnnotation\\osp-projects\\labelImg-master\\dist\\image\\classes.txt'
解决:在image增加一个classes.txt文件,内容为空。
 
13.打开image目录后或打开image目录中的图片,报错‘
解决方案:删除image目录中除了图片之外,运行正常。