python合并归一化txt内容

python合并归一化txt内容,第1张

#liufangtao  第一步处理
import glob
import re
import os
from pathlib import Path
import cv2

pathimg = r'\outputhead_jyz_4.18\results'
dir1 = r'\jyzps424\outputhead_jyz_4.18'
img_list = os.listdir(pathimg)
for name in img_list:
    oriname=name.rstrip('.jpg')
    img_file = os.path.join(pathimg, oriname + '.jpg')
    im = cv2.imread(img_file)
    print(name)
    

    path=Path(img_file)
    sep = ''
    path = path.with_suffix(sep)
    dirs = glob.glob(dir1 +'/*.txt')

    matches = [re.search(rf"%s{sep}" % path.stem, d) for d in dirs]

    for index, value in enumerate(matches):
        if value:
            pl = dirs[index]
            # print(pl)
            f=open(pl).read()
            with open(pathimg+"\"+path.stem+'.txt', 'a+',encoding='utf-8') as f1:
                f1.write(f+'\n')
            # log = open(pathimg+"\"+path.stem+'.txt', 'a+')
            # log.write(f)
    # try:
    #     txt_file = os.path.join(pathimg, oriname + '.txt')
    #     file_data = ""
    #     with open(txt_file, 'r',encoding='utf-8') as f1:
    #         lines = f1.readlines()
    #         print(lines)
            
    #         for line in lines:
                
    #             h, w = im.shape[:2]
    #             xmin, ymin, xmax, ymax = int(float(line.split(' ')[1])), int(float(line.split(' ')[2])), int(float(line.split(' ')[3])), int(float(line.split(' ')[4]))
    #             x1 = (xmin + xmax) / 2 / w
    #             x2 = (ymin + ymax) / 2 / h
    #             y1 = (xmax - xmin) / w
    #             y2 = (ymax - ymin) / h
    #             print(x1, y1, x2, y2)
    #             line = line.replace(str(line.split(' ')[1]), str(round(x1, 7)))
    #             line = line.replace(str(line.split(' ')[2]), str(round(x2, 7)))
    #             line = line.replace(str(line.split(' ')[3]), str(round(y1, 7)))
    #             line = line.replace(str(line.split(' ')[4]), str(round(y2, 7)))
               
    #             # line.split(' ')[2] = y1
    #             # line.split(' ')[3] = x2
    #             # line.split(' ')[4] = y2
    #             file_data += line

    #             print(line.split(' ')[1], line.split(' ')[2], line.split(' ')[3], line.split(' ')[4])
    #             with open(txt_file, 'w+',encoding='utf-8') as w:
    #                 w.write(''.join(file_data))
    # except FileNotFoundError:
    #     pass

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/792750.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存