def _add_images_doc(self, path: str, import_path: str, url_prefix: str): """ 为文件夹下的图片添加信息字典文件(id为不含扩展的文件名) @param {str} path - 要处理的文件目录 """ _import_path = os.path.realpath(import_path) # 处理当前文件夹 _file_list = FileTool.get_filelist(path, regex_str=r'^((?!\.json$).)*$', is_fullname=True) for _file in _file_list: _ext = FileTool.get_file_ext(_file) _json_file = _file[0:-len(_ext)] + 'json' if os.path.exists(_json_file): # 字典文件已存在,无需处理 continue # 生成并写入字典 _file_name = FileTool.get_file_name_no_ext(_file) _url = os.path.realpath(_file)[len(_import_path):].replace( '\\', '/').lstrip('/') _url = '%s/%s' % (url_prefix, _url) _image_doc = {'id': _file_name, 'url': _url, 'path': _file} _json_str = json.dumps(_image_doc, ensure_ascii=False) with open(_json_file, 'wb') as _fid: _fid.write(_json_str.encode(encoding='utf-8')) # 处理子文件夹 _sub_dir_list = FileTool.get_dirlist(path) for _sub_dir in _sub_dir_list: self._add_images_doc(_sub_dir, import_path, url_prefix)
def auto_install(self, is_init: bool): """ 自动检索目录并进行安装 @param {bool} is_init - 指示是否第一次运行 """ _deal_plugins = list() _dirs = FileTool.get_dirlist(self.plugin_path, is_fullpath=False) for _dir_name in _dirs: _plugin_config = SimpleXml( os.path.join(self.plugin_path, _dir_name, 'plugin.xml')).to_dict()['config'] # 登记信息 _deal_plugins.append(_plugin_config['info']['plugin_name']) if self.is_installed(_plugin_config['info']['plugin_name']): # 已安装, 判断是否要强制更新 if not (is_init and self.always_update): # 非强制更新 continue # 执行安装处理 self.install(_dir_name) # 卸载插件目录没有的插件 _fetchs = self._exec_sql('select plugin_name from t_installed', is_fetchall=True) for _row in _fetchs: if _row[0] not in _deal_plugins: # 插件已不存在,卸载处理 self.uninstall(_row[0])
def rename_file_to_num(cls, path: str, start_index: int = 1) -> int: """ 重命名文件为数字序号 @param {str} path - 要处理的文件夹 @param {int} start_index=1 - 开始序号 @returns {int} - 返回当前序号 """ # 处理当前目录 _start_index = start_index _path = os.path.realpath(path) _file_list = FileTool.get_filelist(_path, is_fullname=False) for _file in _file_list: _ext = FileTool.get_file_ext(_file) os.rename( os.path.join(_path, _file), os.path.join( _path, StringTool.fill_fix_string(str(_start_index), 10, '0') + '.' + _ext)) _start_index += 1 # 处理子目录 _sub_dir_list = FileTool.get_dirlist(_path) for _sub_dir in _sub_dir_list: _start_index = cls.rename_file_to_num(_sub_dir, start_index=_start_index) # 返回当前序号值 return _start_index
def _import_images(self, path: str, pipeline: str): """ 按目录结构导入文件 @param {str} path - 要处理的路径 @param {str} pipeline - 管道标识 """ # 先处理当前目录 self.search_engine.import_images(path, pipeline) # 处理子文件夹 _sub_dir_list = FileTool.get_dirlist(path) for _sub_dir in _sub_dir_list: self._import_images(_sub_dir, pipeline)
def _get_dom_files(cls, path: str, files: list): """ 获取指定目录下的所有dom.html文件 @param {str} path - 路径 @param {list} files - 找到的文件清单 """ # 先找当前目录下的文件 _temp_list = FileTool.get_filelist(path, regex_str=r'^dom\.html$', is_fullname=True) files.extend(_temp_list) # 遍历所有子目录获取文件 _dirs = FileTool.get_dirlist(path) for _dir in _dirs: cls._get_dom_files(_dir, files)
def _write_product_info_to_xls(cls, path: str, sheet, title: dict, current_row: list): """ 按目录逐个将产品信息写入excel文件> @param {str} path - 要处理的目录 @param {object} sheet - excel的sheet对象 @param {dict} title - 标题清单 @param {list} current_row - 当前行 """ # 先处理自己 _info_file = os.path.join(path, 'info.json') if os.path.exists(_info_file): # 有信息文件才处理 _info = dict() with open(_info_file, 'rb') as f: _eval = str(f.read(), encoding='utf-8') _info = eval(_eval) # 产品编号和产品目录 _product_num = FileTool.get_dir_name(path) sheet.write(current_row[0], 0, _product_num) sheet.write(current_row[0], 1, path) # 逐个信息项写入 for _key in _info.keys(): if _key in title.keys(): sheet.write(current_row[0], title[_key], _info[_key]) else: # 要新增列标题 _col = len(title) + 2 title[_key] = _col sheet.write(0, _col, _key) # 写入信息值 sheet.write(current_row[0], _col, _info[_key]) # 换到下一行 current_row[0] += 1 # 处理子目录 _dirs = FileTool.get_dirlist(path) for _dir in _dirs: cls._write_product_info_to_xls(_dir, sheet, title, current_row)
def _get_child_dir_list(cls, path: str, with_root: bool = True) -> list: """ 获取目录及子目录清单 (保证顺序为先子目录,再父目录) @param {str} path - 开始目录 @param {bool} with_root=True - 是否包含当前目录 @returns {list} - 文件夹清单 """ _list = [] for _dir in FileTool.get_dirlist(path): _temp_list = cls._get_child_dir_list(_dir, with_root=True) _list.extend(_temp_list) if with_root: _list.append(path) return _list
def _get_pic_file_list(cls, input_path: str) -> list: """ 获取制定目录下的所有图片文件清单 @param {str} input_path - 要处理的目录 @returns {list} - 文件清单列表 """ _list = [] # 先获取当前目录下的所有xml文件 for _file in FileTool.get_filelist(input_path, is_fullname=True): _ext = FileTool.get_file_ext(_file) if _ext.lower() in ('jpg', 'jpeg'): _list.append(_file) # 获取子目录 for _dir in FileTool.get_dirlist(input_path): _temp_list = cls._get_pic_file_list(_dir) _list.extend(_temp_list) return _list
def _get_labelimg_annotation_file_list(cls, input_path: str) -> list: """ 获取要处理的LabelImg标注文件清单 @param {str} input_path - 起始目录 @returns {list} - 返回文件清单 """ _list = [] # 先获取当前目录下的所有xml文件 for _file in FileTool.get_filelist(input_path, regex_str=r'.*\.xml$'): _pic_file = _file[0:-3] + 'jpg' if os.path.exists(_pic_file): _list.append(_file) # 获取子目录 for _dir in FileTool.get_dirlist(input_path): _temp_list = cls._get_labelimg_annotation_file_list(_dir) _list.extend(_temp_list) return _list
def labelimg_del_not_rgb_pic(cls, path: str): """ 删除位深不为RGB三通道的图片 (解决image_size must contain 3 elements[4]报错) @param {str} path - 要处理的路径 """ _path = os.path.realpath(path) # 遍历所有子目录 _sub_dirs = FileTool.get_dirlist(path=_path, is_fullpath=True) for _dir in _sub_dirs: # 递归删除子目录的信息 cls.labelimg_del_not_rgb_pic(_dir) # 检查自己目录下的图片 _files = FileTool.get_filelist(path=_path, is_fullname=False) for _file in _files: _file_ext = FileTool.get_file_ext(_file) if _file_ext == 'xml': # 标签文件不处理 continue # 打开图片判断位深 _fp = open(os.path.join(_path, _file), 'rb') _img = Image.open(_fp) if _img.mode != 'RGB': # 需要删除掉 _fp.close() _img_file = os.path.join(_path, _file) _xml_file = os.path.join( _path, FileTool.get_file_name_no_ext(_file) + '.xml') print('delete %s' % _img_file) FileTool.remove_file(_img_file) if os.path.exists(_xml_file): FileTool.remove_file(_xml_file) else: _fp.close()
def labelimg_pic_deal(cls, path: str): """ TFRecord图片兼容处理 1.删除位深不为RGB三通道的图片 (解决image_size must contain 3 elements[4]报错) 2.转换图片格式为jpg 3.检查xml文件的文件名和路径是否正确 @param {str} path - 要处理的路径 """ _path = os.path.realpath(path) # 遍历所有子目录 _sub_dirs = FileTool.get_dirlist(path=_path, is_fullpath=True) for _dir in _sub_dirs: # 递归删除子目录的信息 cls.labelimg_pic_deal(_dir) # 检查自己目录下的图片 _files = FileTool.get_filelist(path=_path, is_fullname=False) for _file in _files: _file_ext = FileTool.get_file_ext(_file) if _file_ext == 'xml': # 标签文件不处理 continue _img_file = os.path.join(_path, _file) _file_no_ext = FileTool.get_file_name_no_ext(_file) if _file_ext in ('png', 'gif'): # 转换图片格式 _fp = open(_img_file, 'rb') _img = Image.open(_fp) _rgb_im = _img.convert('RGB') _rgb_im.save(os.path.join(_path, _file_no_ext + '.jpg')) _fp.close() # 删除原文件,修改xml中的文件名 FileTool.remove_file(_img_file) _xml_file = os.path.join(_path, _file_no_ext + '.xml') if os.path.exists(_xml_file): _tree = ET.parse(os.path.join(_path, _xml_file)) _root = _tree.getroot() _root.find('filename').text = _file_no_ext + '.jpg' _root.find('path').text = os.path.join( _path, _file_no_ext + '.jpg') _tree.write(os.path.join(_path, _xml_file), encoding='utf-8', method="xml", xml_declaration=None) # 修改文件名变量 _img_file = os.path.join(_path, _file_no_ext + '.jpg') # 打开图片判断位深 _fp = open(_img_file, 'rb') _img = Image.open(_fp) if _img.mode != 'RGB': # 需要删除掉 _fp.close() _xml_file = os.path.join( _path, FileTool.get_file_name_no_ext(_file) + '.xml') print('delete %s' % _img_file) FileTool.remove_file(_img_file) if os.path.exists(_xml_file): FileTool.remove_file(_xml_file) else: _fp.close() # 检查xml文件 _xml_file = os.path.join(_path, _file_no_ext + '.xml') if os.path.exists(_xml_file): _tree = ET.parse(os.path.join(_path, _xml_file)) _root = _tree.getroot() if _root.find('filename' ).text != _file_no_ext + '.jpg' or os.path.split( _root.find('path').text)[0] != _path: _root.find('filename').text = _file_no_ext + '.jpg' _root.find('path').text = os.path.join( _path, _file_no_ext + '.jpg') _tree.write(os.path.join(_path, _xml_file), encoding='utf-8', method="xml", xml_declaration=None)