概述
python脚本如下,可以用于确认标注没是否存在问题。
import os
import glob
from natsort import natsorted
import cv2
import numpy as np
def get_file_list(folder_path: str, p_postfix: list = None, sub_dir: bool = True) -> list:
"""
获取所给文件目录里的指定后缀的文件,读取文件列表目前使用的是 os.walk 和 os.listdir ,这两个目前比 pathlib 快很多
:param filder_path: 文件夹名称
:param p_postfix: 文件后缀,如果为 [.*]将返回全部文件
:param sub_dir: 是否搜索子文件夹
:return: 获取到的指定类型的文件列表
"""
assert os.path.exists(folder_path) and os.path.isdir(folder_path)
if p_postfix is None:
p_postfix = ['.jpg']
if isinstance(p_postfix, str):
p_postfix = [p_postfix]
file_list = [x for x in glob.glob(folder_path + '/**/*.*', recursive=True) if
os.path.splitext(x)[-1] in p_postfix or '.*' in p_postfix]
return natsorted(file_list)
def get_boxes(file_path: str):
anno_file = ""
if file_path.endswith(".jpg"):
anno_file = file_path.replace("img", "gt").replace(".jpg", ".txt")
if os.path.isfile(anno_file):
# print(anno_file, "文件存在")
stature = 1
else:
# print(anno_file, "文件不存在")
stature = 0
f = open(anno_file, encoding='utf-8')
x1, y1, x2, y2, x3, y3, x4, y4 = 0, 0, 0, 0, 0, 0, 0, 0
chars = ""
box_list = []
for line in f.readlines():
# print(line)
data = line.replace('n', '')
substr = data.split(',')
locate = substr[:8]
[x1, y1, x2, y2, x3, y3, x4, y4] = [int(x) for x in locate]
chars = substr[8:]
org_loc = [x1, y1, x2, y2, x3, y3, x4, y4]
box_list.append(org_loc)
box_list = np.array(box_list)
return box_list
def draw_bbox(img_path, result, color=(255, 0, 0), thickness=2):
if isinstance(img_path, str):
img_path = cv2.imread(img_path)
# img_path = cv2.cvtColor(img_path, cv2.COLOR_BGR2RGB)
img_path = img_path.copy()
for point in result:
point = point.astype(int)
cv2.polylines(img_path, [point], True, color, thickness)
return img_path
def show_img(imgs: np.ndarray, title='img'):
color = (len(imgs.shape) == 3 and imgs.shape[-1] == 3)
imgs = np.expand_dims(imgs, axis=0)
for i, img in enumerate(imgs):
plt.figure()
plt.title('{}_{}'.format(title, i))
plt.imshow(img, cmap=None if color else 'gray')
plt.show()
def init_args():
import argparse
parser = argparse.ArgumentParser(description='')
parser.add_argument('--input_folder', default=r'F:datasetimg', type=str, help='img path for predict')
parser.add_argument('--output_folder', default='./test/output', type=str, help='img path for output')
parser.add_argument('--show', action='store_true', help='show result')
parser.add_argument('--save_resut', action='store_true', help='save box and score to txt file')
args = parser.parse_args()
return args
if __name__ == '__main__':
import pathlib
from tqdm import tqdm
import matplotlib.pyplot as plt
args = init_args()
img_folder = pathlib.Path(args.input_folder)
for img_path in tqdm(get_file_list(args.input_folder, p_postfix=['.jpg'])):
boxes_list = get_boxes(img_path)
boxes_list.resize(len(boxes_list), 4, 2)
img = draw_bbox(cv2.imread(img_path)[:, :, ::-1], boxes_list)
if args.show:
show_img(img, title=os.path.basename(img_path))
plt.show()
# 保存结果到路径
os.makedirs(args.output_folder, exist_ok=True)
img_path = pathlib.Path(img_path)
output_path = os.path.join(args.output_folder, img_path.stem + '_result.jpg')
cv2.imwrite(output_path, img[:, :, ::-1])
最后
以上就是温柔书本为你收集整理的icdar2015文本检测样本展示的全部内容,希望文章能够帮你解决icdar2015文本检测样本展示所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复