json文件的读取,以及更改特定标签特定标签名的对应像素值更改

25 篇文章 6 订阅
17 篇文章 12 订阅
  1. 获取所需标签名“person”所对应的文件名,csv文档
  2. 获取文档名,一个读取该文件名下的标签图片Annotations,一个读取json文件所对应的标签,每一个标签下的数值生成列表
  3. 读取列表下key所对应Annotations下的图片,另外,生成一个全零矩阵,以及新的文件夹,用来存放同文档名同图片名,并创建一个矩阵用来存放新图片的数值
  4. 如果该图片已经在该路径下存在,读取该图片的值(只修改对应位置的像素值),如果不存在,则将该矩阵保存为图片
  5. 将标签名下的数值保存为列表,依次读取列表中键值,将标签名为“person”的像素值点进行改色

ps:opencv读取的图片是bgr通道,所以,生成的矩阵的hw,以及遍历的hw,对应像素值的通道数,所对应的数值设置都要注意。

json文件:

 标签图:

 效果图:

# 批量图片的处理
import csv
import json
import csv
import os

import cv2
import pandas as pd
from PIL import Image
import numpy as np

# 图片根目录
root = 'C:\\FeigeDownload\\YouTube_vos\\train\\train\\Annotations\\'
save_root = 'C:\\FeigeDownload\\YouTube_vos\\train\\train\\mask1\\'  # 存放新图片的路径

# 读取数据
data = pd.read_csv("data1.csv")  # DataFrame结构  如果读成CSV文件,第一个数值为0的话会丢失,00001.jpg  ->  1.jpg  转为数组!!!
d_data = np.array(data)  # 转为数组

# 读取json文件
fi = 'C:\\FeigeDownload\\YouTube_vos\\train\\train\\meta.json'
filename = json.load(open(fi))
filename_dict = filename['videos']  # 文件名的键值对

# 生成文件夹
def makedir(path):
    folder = os.path.exists(path)

    if not folder:
        os.makedirs(path)
        print("...New folder...")
    else:
        print("...There is this folder!")

# 创建png文件
def Png(f_path, image_name, w, h):
    # 将该路径下的文件存入列表
    file_name_list = os.listdir(f_path)  # os.listdir()方法获取文件夹名字,返回数组
    m = image_name + '.png'
    if m in file_name_list:
        # 读取该图片的数值
        png_path = os.path.join(f_path, m)
        image_ = cv2.imread(png_path)
        # 复制该图片下的矩阵值
        image = np.zeros(image_.shape).astype(np.uint8)
        image = image_.copy()
    else:
        image = np.zeros((h, w, 3)).astype(np.uint8)  # 三通道数组 BGR模式
    return image

# 从键值对里面寻找匹配csv文档中的列表
for element in d_data:
    ele_str = str(element[0])  # 读取数组的值 '01c4cb5ffe'
    objects_ = filename_dict[ele_str]['objects']  # 字典

    # 生成文件夹
    file_path = os.path.join(save_root, ele_str)
    makedir(file_path)

    # 读取json字典中的标签  # 有的标签不是从1开始
    # objects_len = len(objects_)
    # for i in range(objects_len):
    for la_item in objects_.items():
        # 读取标签对应的图片名
        a = la_item[0]
        label_name = objects_[a]['category']

        # 获取标签名下的数字,生成列表,获取其列表
        iamge_list = []
        iamge_list = objects_[a]['frames']


        # 按照列表依次生成路径
        for b in iamge_list:  # 字符串形式
            # 读取路径对应的图片
            # b = str(j)
            image_path = os.path.join(root, ele_str, b + '.png')
            image_ = cv2.imread(image_path)  #  opencv读取BGR
            # 定义一个数组,用来对图片进行累加,生成一张图
            # image = np.array(image_).astype(np.uint8)
            image = np.array(image_)
            h = image.shape[0]
            w = image.shape[1]

            # 如果路径下该图片已经存在,则new_iamge复制该图片
            new_iamge = Png(file_path, b, w, h)

            # 将该标签的颜色进行更改
            if a == '1':  # 标签为红色 [236,95,103] 只改变特定像素点
                red = np.array([236, 95, 103], dtype=np.uint8)  # 读取的图片数据类型是uint8,int与uint8无法比较
                re_test = red[0]
                # 判断:如果标签名为'person',改成白色
                if label_name == 'person':
                    test_max = np.array([0], dtype=np.uint8)
                    for x in range(0, h):
                        for y in range(0, w):
                            la = image[x, y, 0]  # uint8
                            if test_max < la:
                                test_max = la
                            # if image[x, y, 0] == 236 and image[x, y, 1] == 95 and image[x, y, 2] == 103:
                            if image[x, y, 0] == red[2] and image[x, y, 1] == red[1] and image[x, y, 2] == red[0]:  # BGR三通道要换顺序
                                new_iamge[x, y, 0] = 255  # 三通道
                                new_iamge[x, y, 1] = 255
                                new_iamge[x, y, 2] = 255
                # 否则,改成黑色        ******原本是黑色,不需要改动*****(略过)
                # else:
                #     for x in range(0, h):
                #         for y in range(0, w):
                #             if image[x, y, 0] == 103 and image[x, y, 1] == 95 and image[x, y, 2] == 236:
                #                 new_iamge[x, y, 0] = 0
                #                 new_iamge[x, y, 1] = 0
                #                 new_iamge[x, y, 2] = 0
            elif a == '2':  # 标签为橙色  (249, 145, 87)
                if label_name == 'person':
                    for x in range(0, h):
                        for y in range(0, w):
                            if image[x, y, 0] == 87 and image[x, y, 1] == 145 and image[x, y, 2] == 249:
                                new_iamge[x, y, 0] = 255
                                new_iamge[x, y, 1] = 255
                                new_iamge[x, y, 2] = 255
                # else:
                #     for x in range(0, h):
                #         for y in range(0, w):
                #             if image[x, y, 0] == 87 and image[x, y, 1] == 145 and image[x, y, 2] == 249:
                #                 new_iamge[x, y, 0] = 0
                #                 new_iamge[x, y, 1] = 0
                #                 new_iamge[x, y, 2] = 0
            elif a == '3':  # 标签为黄色  (250, 200, 99)
                if label_name == 'person':
                    for x in range(0, h):
                        for y in range(0, w):
                            if image[x, y, 0] == 99 and image[x, y, 1] == 200 and image[x, y, 2] == 250:
                                new_iamge[x, y, 0] = 255
                                new_iamge[x, y, 1] = 255
                                new_iamge[x, y, 2] = 255
                # else:
                #     for x in range(0, h):
                #         for y in range(0, w):
                #             if image[x, y, 0] == 99 and image[x, y, 1] == 200 and image[x, y, 2] == 250:
                #                 new_iamge[x, y, 0] = 0
                #                 new_iamge[x, y, 1] = 0
                #                 new_iamge[x, y, 2] = 0
            elif a == '4':  # 标签为绿色  (153, 199, 148)
                if label_name == 'person':
                    for x in range(0, h):
                        for y in range(0, w):
                            if image[x, y, 0] == 148 and image[x, y, 1] == 199 and image[x, y, 2] == 153:
                                new_iamge[x, y, 0] = 255
                                new_iamge[x, y, 1] = 255
                                new_iamge[x, y, 2] = 255
                # else:
                #     for x in range(0, h):
                #         for y in range(0, w):
                #             if image[x, y, 0] == 148 and image[x, y, 1] == 199 and image[x, y, 2] == 153:
                #                 new_iamge[x, y, 0] = 0
                #                 new_iamge[x, y, 1] = 0
                #                 new_iamge[x, y, 2] = 0
            elif a == '5':  # 标签为天蓝色 (98, 179, 178)
                if label_name == 'person':
                    for x in range(0, h):
                        for y in range(0, w):
                            if image[x, y, 0] == 178 and image[x, y, 1] == 179 and image[x, y, 2] == 98:
                                new_iamge[x, y, 0] = 255
                                new_iamge[x, y, 1] = 255
                                new_iamge[x, y, 2] = 255
                # else:
                #     for x in range(0, h):
                #         for y in range(0, w):
                #             if image[x, y, 0] == 178 and image[x, y, 1] == 179 and image[x, y, 2] == 98:
                #                 new_iamge[x, y, 0] = 0
                #                 new_iamge[x, y, 1] = 0
                #                 new_iamge[x, y, 2] = 0
            elif a == '6':  # 标签为蓝色 (102, 153, 204)
                if label_name == 'person':
                    for x in range(0, h):
                        for y in range(0, w):
                            if image[x, y, 0] == 204 and image[x, y, 1] == 153 and image[x, y, 2] == 102:
                                new_iamge[x, y, 0] = 255
                                new_iamge[x, y, 1] = 255
                                new_iamge[x, y, 2] = 255
                # else:
                #     for x in range(0, h):
                #         for y in range(0, w):
                #             if image[x, y, 0] == 204 and image[x, y, 1] == 153 and image[x, y, 2] == 102:
                #                 new_iamge[x, y, 0] = 0
                #                 new_iamge[x, y, 1] = 0
                #                 new_iamge[x, y, 2] = 0

            # 保存图片
            png_path = os.path.join(file_path, b + '.png')
            cv2.imwrite(png_path, new_iamge)
#             print("image_change", b + '.png')
#         print("~~one_picture~~")
#     print("There have been read a file")
# print("Finished!!!")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值