zoukankan      html  css  js  c++  java
  • ctd数据格式转普通坐标点

    ctd_label2pt.py

    import numpy as np
    
    import cv2
    import random
    
    import os
    
    
    def get_absolute_path(p):
        if p.startswith('~'):
            p = os.path.expanduser(p)
        return os.path.abspath(p)
    
    def read_lines(p):
        """return the text in a file in lines as a list """
        p = get_absolute_path(p)
        f = open(p,'r')
        return f.readlines()
    
    def replace_all(s, old, new, reg = False):
        if reg:
            import re
            targets = re.findall(old, s)
            for t in targets:
                s = s.replace(t, new)
        else:
            s = s.replace(old, new)
        return s
    
    def remove_all(s, sub):
        return replace_all(s, sub, '')
    
    def split(s, splitter, reg = False):
        if not reg:
            return s.split(splitter)
        import re
        return re.split(splitter, s)
    
    def get_bboxes(img, gt_path, save_path):
        h, w = img.shape[0:2]
        lines = read_lines(gt_path)
        bboxes = []
        tags = []
        for line in lines:
            line = line.strip()
            line = remove_all(line, 'xefxbbxbf')
            gt = split(line, ',')
    
            x1 = np.int(gt[0])
            y1 = np.int(gt[1])
    
            bbox = [np.int(gt[i]) for i in range(4, 32)]
            bbox = np.asarray(bbox) + ([x1 * 1.0, y1 * 1.0] * 14)
            with open(save_path,'a') as fw:
                for cnt,val in enumerate(bbox):
                    val = int(val)
                    if cnt != len(bbox)-1:
                        fw.write(str(val))
                        fw.write(",")
                    else:
                        fw.write(str(val))
                        fw.write('
    ')
    
    
    # def get_bboxes(img, gt_path, save_path):
    #     h, w = img.shape[0:2]
    #     lines = util.io.read_lines(gt_path)
    #     bboxes = []
    #     tags = []
    #     for line in lines:
    #         line = line.strip()
    #         line = util.str.remove_all(line, 'xefxbbxbf')
    #         gt = util.str.split(line, ',')
    #
    #         x1 = np.int(gt[0])
    #         y1 = np.int(gt[1])
    #
    #         bbox = [np.int(gt[i]) for i in range(4, 32)]
    #         bbox = np.asarray(bbox) + ([x1 * 1.0, y1 * 1.0] * 14)
    #         with open(save_path,'a') as fw:
    #             for cnt,val in enumerate(bbox):
    #                 val = int(val)
    #                 if cnt != len(bbox)-1:
    #                     fw.write(str(val))
    #                     fw.write(",")
    #                 else:
    #                     fw.write(str(val))
    #                     fw.write('
    ')
    
    
    path_text_image = "/media/data_1/2019_project_test/PSENet/data/CTW1500/train/text_image/"
    path_text_label_curve = "/media/data_1/2019_project_test/PSENet/data/CTW1500/train/text_label_curve/"
    path_save_txt = os.path.dirname(os.path.dirname(path_text_image)) + '/ctd2general/'
    if not os.path.exists(path_save_txt):
        os.makedirs(path_save_txt)
    
    list_img = os.listdir(path_text_image)
    cnt = 0
    for img_name in list_img:
        cnt += 1
        print("cnt=%d,img=%s"%(cnt,img_name))
        img_path = path_text_image + img_name
        img = cv2.imread(img_path)
        txt_path = path_text_label_curve + img_name.replace('.jpg','.txt')
        txt_save = path_save_txt + img_name.replace('.jpg','.txt')
        get_bboxes(img, txt_path, txt_save)
    
    
    
    
    
    
    
    
  • 相关阅读:
    (4)UIView和父子控件
    (2)第一个IOS程序
    svn本地目录结构for window
    (1)xcode基本设置和控制器等介绍
    git版本控制 for window安装和命令行使用
    linux虚拟机如何配置网卡信息(确保两台服务器通信)
    linux系统中firewalld防火墙管理工具firewallcmd(CLI命令行)
    linux系统中firewalld防火墙管理工具firewallconfig(GUI图形用户界面)
    linux系统中使用nmtui命令配置网络参数(图形用户界面)
    网卡是什么?
  • 原文地址:https://www.cnblogs.com/yanghailin/p/11265729.html
Copyright © 2011-2022 走看看