[python基础]基础文本操作

1.普通文本操作


#普通文本操作
import os
file=open('/hdd2/wh/pascalraw/PASCALRAW/trainval/train.txt')    
#file=open('val.txt')    

file_list = []  
labelMat = []
for line in file.readlines():    
   #line = line + ".txt"
   #type(line)
   s = ""
   curLine = line.strip().split(" ")
   curLine = s.join(curLine) + ".jpg"
   file_list.append(curLine)
print(file_list)
path = "/hdd2/wh/pascalraw/PASCALRAW/jpg"
abs_path = os.path.abspath(path)
os.listdir(abs_path)

result = "/hdd2/wh/datasets/COCO/train2017/"
result_dir = os.path.abspath(result)


#print(result)
os.listdir(result)
#os.path.join(abs_path,"2014_004248.xml")
#print(os.path.join(abs_path,"2014_004248.xml"))
for file in file_list:
   shutil.copyfile(os.path.join(abs_path,file), result_dir)
   shutil.copy()
print("done.")
   

2.文本操作分离尾数是奇数或者偶数的文件

2.1 方法1:使用glob.glob() 分离奇数偶数图片。

这个字符串的书写和我们使用linux的shell命令相似,或者说基本一样。也就是说,只要我们按照平常使用cd命令时的参数就能够找到我们所需要的文件的路径。字符串中可以包括“”、“?”和"["、"]",其中“”表示匹配任意字符串,“?”匹配任意单个字符,[0-9]与[a-z]表示匹配0-9的单个数字与a-z的单个字符

import os
import shutil
import xml.etree.ElementTree as ET
import glob
# 复制图像到另一个文件夹
# 文件所在文件夹
file_dir = '/hdd2/wh/pascalraw/PASCALRAW/jpg/'

# 创建一个子文件存放文件
result_dir_0 = '/hdd2/wh/datasets/COCO/train2017'
result_dir_1 = '/hdd2/wh/datasets/COCO/val2017'

file_list = os.listdir(file_dir)
#print(file_list)

even_files = glob.glob(os.path.join(file_dir, "*[02468].jpg"))
#print("odd_files", odd_files)

odd_files = glob.glob(os.path.join(file_dir, "*[13579].jpg"))
#print("even_files", even_files)


for image in even_files:
        shutil.copy(os.path.join(file_dir, image), result_dir_0)
for image in even_files:
        shutil.copy(os.path.join(file_dir, image), result_dir_1)



2.2 方法2:用最无脑的方法:str.endswith(file or image , match_str)


for image in file_list:
    #print(image)
    #if image == "2014_00???[0,2,4,6,8].jpg": //不行 不能用==直接连接
    if str.endswith(image, "0.jpg") or str.endswith(image, "2.jpg") or str.endswith(image, "4.jpg") or str.endswith(image, "6.jpg") or str.endswith(image, "8.jpg"):
        if os.path.exists(file_dir):
            shutil.copy(os.path.join(file_dir, image), result_dir_0)
    if str.endswith(image, "1.jpg") or str.endswith(image, "3.jpg") or str.endswith(image, "5.jpg") or str.endswith(image, "7.jpg") or str.endswith(image, "9.jpg"):
        if os.path.exists(file_dir):
            shutil.copy(os.path.join(file_dir, image), result_dir_1)
'''
print("done!")

done

在这里插入图片描述

3. Python批量替换文件中指定字符串

# -*- coding:utf-8 -*-
__author__ = 'ShawDa'


import glob

xmls = glob.glob('xml_files/*.xml')
for one_xml in xmls:
    print(one_xml)
    f = open(one_xml, 'r+', encoding='utf-8')
    all_the_lines = f.readlines()
    f.seek(0)
    f.truncate()
    for line in all_the_lines:
        line = line.replace('dog', 'pig')
        line = line.replace('cat', 'bike')
        f.write(line)
    f.close()
#  可视化coco格式json标注中的box到图片上
import json
import shutil
import cv2
import os

def select(json_path, outpath, image_path):
    cat_class = []
    count = 0
    json_file = open(json_path)
    infos = json.load(json_file)
    images = infos["images"]
    annos = infos["annotations"]
    assert len(images) == len(images)
    for i in range(len(images)):
        im_id = images[i]["id"]
        im_path = image_path + "/" + images[i]["file_name"]
        img = cv2.imread(im_path)
        
        print(img.shape)
        for j in range(len(annos)):
            if annos[j]["image_id"] == im_id:
                cate_id = annos[j]["category_id"]
                
                
                #if cate_id == '0':
                #    cat_class.append("bicycle")
                #if cate_id == '1':
                #    cat_class.append("car")
                #if cate_id == '2':
                #    cat_class.append("person")
                
                print(cate_id)
                x, y, w, h = annos[j]["bbox"]
                x, y, w, h = int(x), int(y), int(w), int(h)
                x2, y2 = x + w, y + h
                # object_name = annos[j][""]
                img = cv2.rectangle(img, (x, y), (x2, y2), (255, 0, 0), thickness=2)
                cv2.putText(img, str(cate_id), (200, 200), cv2.FONT_HERSHEY_COMPLEX, 2.0, (100, 200, 200), 3)
                count = count + 1
                img_name = outpath + "/" + images[i]["file_name"]
                cv2.imwrite(img_name, img)
                break
        print(i)
    #print(cat_class)

if __name__ == "__main__":
    json_path = "/hdd2/wh/datasets/coco_raw/annotations/instances_train2017_jpg.json"
    out_path = "/hdd2/wh/datasets/vis/"
    if os.path.exists(out_path):
        shutil.rmtree(out_path)
    os.mkdir(out_path)
    image_path = "/hdd2/wh/datasets/coco_raw/train2017/"
    select(json_path, out_path, image_path)

4. voc2coco.py

#!/usr/bin/python

# pip install lxml

import sys
import os
import json
import xml.etree.ElementTree as ET
import glob

START_BOUNDING_BOX_ID = 1
PRE_DEFINE_CATEGORIES = None
# If necessary, pre-define category and its id
#  PRE_DEFINE_CATEGORIES = {"aeroplane": 1, "bicycle": 2, "bird": 3, "boat": 4,
#  "bottle":5, "bus": 6, "car": 7, "cat": 8, "chair": 9,
#  "cow": 10, "diningtable": 11, "dog": 12, "horse": 13,
#  "motorbike": 14, "person": 15, "pottedplant": 16,
#  "sheep": 17, "sofa": 18, "train": 19, "tvmonitor": 20}


def get(root, name):
    vars = root.findall(name)
    return vars


def get_and_check(root, name, length):
    vars = root.findall(name)
    if len(vars) == 0:
        raise ValueError("Can not find %s in %s." % (name, root.tag))
    if length > 0 and len(vars) != length:
        raise ValueError(
            "The size of %s is supposed to be %d, but is %d."
            % (name, length, len(vars))
        )
    if length == 1:
        vars = vars[0]
    return vars


def get_filename_as_int(filename):
    try:
        filename = filename.replace("\\", "/")
        filename = os.path.splitext(os.path.basename(filename))[0]
        return int(filename)
    except:
        raise ValueError("Filename %s is supposed to be an integer." % (filename))


def get_categories(xml_files):
    """Generate category name to id mapping from a list of xml files.
    
    Arguments:
        xml_files {list} -- A list of xml file paths.
    
    Returns:
        dict -- category name to id mapping.
    """
    classes_names = []
    for xml_file in xml_files:
        tree = ET.parse(xml_file)
        root = tree.getroot()
        for member in root.findall("object"):
            classes_names.append(member[0].text)
    classes_names = list(set(classes_names))
    classes_names.sort()
    return {
    
    name: (i + 1) for i, name in enumerate(classes_names)}


def convert(xml_files, json_file):
    json_dict = {
    
    "images": [], "type": "instances", "annotations": [], "categories": []}
    if PRE_DEFINE_CATEGORIES is not None:
        categories = PRE_DEFINE_CATEGORIES
    else:
        categories = get_categories(xml_files)
        print("get_categories:", categories)
    bnd_id = START_BOUNDING_BOX_ID
    for xml_file in xml_files:
        tree = ET.parse(xml_file)
        root = tree.getroot()
        path = get(root, "path")
        if len(path) == 1:
            filename = os.path.basename(path[0].text)
        elif len(path) == 0:
            filename = get_and_check(root, "filename", 1).text
        else:
            raise ValueError("%d paths found in %s" % (len(path), xml_file))
        ## The filename must be a number
        image_id = get_filename_as_int(filename)
        size = get_and_check(root, "size", 1)
        width = int(get_and_check(size, "width", 1).text)
        height = int(get_and_check(size, "height", 1).text)
        image = {
    
    
            "file_name": filename,
            "height": height,
            "width": width,
            "id": image_id,
        }
        json_dict["images"].append(image)
        ## Currently we do not support segmentation.
        #  segmented = get_and_check(root, 'segmented', 1).text
        #  assert segmented == '0'
        for obj in get(root, "object"):
            category = get_and_check(obj, "name", 1).text
            if category not in categories:
                new_id = len(categories) + 1
                categories[category] = new_id
            category_id = categories[category]
            bndbox = get_and_check(obj, "bndbox", 1)
            xmin = int(get_and_check(bndbox, "xmin", 1).text) - 1
            ymin = int(get_and_check(bndbox, "ymin", 1).text) - 1
            xmax = int(get_and_check(bndbox, "xmax", 1).text)
            ymax = int(get_and_check(bndbox, "ymax", 1).text)
            assert xmax > xmin
            assert ymax > ymin
            o_width = abs(xmax - xmin)
            o_height = abs(ymax - ymin)
            ann = {
    
    
                "area": o_width * o_height,
                "iscrowd": 0,
                "image_id": image_id,
                "bbox": [xmin, ymin, o_width, o_height],
                "category_id": category_id,
                "id": bnd_id,
                "ignore": 0,
                "segmentation": [],
            }
            json_dict["annotations"].append(ann)
            bnd_id = bnd_id + 1

    for cate, cid in categories.items():
        cat = {
    
    "supercategory": "none", "id": cid, "name": cate}
        json_dict["categories"].append(cat)

    os.makedirs(os.path.dirname(json_file), exist_ok=True)
    json_fp = open(json_file, "w")
    json_str = json.dumps(json_dict)
    json_fp.write(json_str)
    json_fp.close()


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(
        description="Convert Pascal VOC annotation to COCO format."
    )
    parser.add_argument("xml_dir", help="Directory path to xml files.", type=str, default='')
    parser.add_argument("json_file", help="Output COCO format json file.", type=str, default='')
    args = parser.parse_args()
    #xml_files = glob.glob(os.path.join(r"C:\Users\chenwei\Study\ONE\Example Segmentation\Codes\val_2", "*.xml"))
    xml_files = glob.glob(os.path.join(xml_dir, "*.xml"))

    # If you want to do train/test split, you can pass a subset of xml files to convert function.
    print("Number of xml files: {}".format(len(xml_files)))
    convert(xml_files, args.json_file)
    #convert(xml_files, r"C:\Users\chenwei\Study\ONE\Example Segmentation\Codes\train_2\val_raw.json")
    
    print("Success: {}".format(args.json_file))
    


5.check_image.py

from PIL import Image
import imghdr
import argparse
import os
# 功能:查看图片能否打开,有没有这个图片、是不是完好、没有损坏的图片
# 参数:图片路径
# 返回:True

error_image = []

def check_pic(path_pic):
    #print(os.listdir(path_pic))
    for image  in sorted(os.listdir(path_pic)):
        if not image.startswith('._', 0, 2):
            #print(image)
            #print(Image.open(image))
            try:
                img = Image.open(os.path.join(path_pic, image) )# 如果图片不存在,报错FileNotFoundError
                img.load()    # 如果图片不完整,报错OSError: image file is truncated
                
                '''
                result_type = imghdr.what(os.path.join(path_pic, image))
                print(result_type)
                if result_type == 'jpg':
                    print(image)
                    error_image.append(image)
                    print('error')
                '''
            except (FileNotFoundError):
                print('FileNotFoundError!')
                error_image.append(image)      
            except(OSError):
                print("OSError:image file is truncated!")
                error_image.append(image)
                
            print("image" + image +' is ok')  
        else:
            continue
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--input", default='/hdd2/wh/pascalraw/PASCALRAW/jpg', help='plz input your image path')
    args = parser.parse_args()
    print(args.input)
    check_pic(args.input)
    print(error_image)

#flag = check_pic(r"C:\\Users\\chenwei\\Desktop\\JPG")

#img = Image.open(os.path.join("C:\\Users\\chenwei\\Desktop\\JPG", '2014_004216.jpg') )# 如果图片不存在,报错FileNotFoundError
#print(img.load())
if __name__ == '__main__':
    main()

6. vis_bbox_and_catogaries.py

可视化bbox和标签

#  可视化coco格式json标注中的box到图片上
import json
import shutil
import cv2

def select(json_path, outpath, image_path):
    class = []
    count = 0
    json_file = open(json_path)
    infos = json.load(json_file)
    images = infos["images"]
    annos = infos["annotations"]
    assert len(images) == len(images)
    for i in range(len(images)):
        im_id = images[i]["id"]
        im_path = image_path + "/" + images[i]["file_name"]
        img = cv2.imread(im_path)
        for j in range(len(annos)):
            if annos[j]["image_id"] == im_id:
	cate_id = annos[j]["category_id"]
                if(cate_id == '0'):
                    class.append("bicycle")
                if(cate_id == '1'):
                    class.append("car")
                if(cate_id == '2'):
                    class.append("person")
                x, y, w, h = annos[j]["bbox"]
                x, y, w, h = int(x), int(y), int(w), int(h)
                x2, y2 = x + w, y + h
                # object_name = annos[j][""]
                img = cv2.rectangle(img, (x, y), (x2, y2), (255, 0, 0), thickness=2)
                cv.putText(img, class[count], (x, y + 20), cv.FONT_HERSHEY_COMPLEX, 2.0, (100, 200, 200), 3)
	count = count + 1
                img_name = outpath + "/" + images[i]["file_name"]
                cv2.imwrite(img_name, img)
                # continue
        print(i)
    print(class)

if __name__ == "__main__":
    json_path = "/hdd2/wh/datasets/coco_raw/annotations/instances_train2017_jpg.json"
    out_path = "/hdd2/wh/datasets/vis/"
    image_path = "/hdd2/wh/datasets/coco_raw/train2017/"
    select(json_path, out_path, image_path)

参考:
https://blog.csdn.net/mantoureganmian/article/details/47949101

https://blog.csdn.net/sinat_36811967/article/details/86570911?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-2.add_param_isCf&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-2.add_param_isCf

https://blog.csdn.net/weixin_44692101/article/details/108502859

https://blog.csdn.net/hehangjiang/article/details/79840638

猜你喜欢

转载自blog.csdn.net/weixin_43823854/article/details/108900185