box 提取

发布于:2024-12-18 ⋅ 阅读:(169) ⋅ 点赞:(0)

box 提取

import json
import os
import shutil

import cv2
import numpy as np
import pypinyin

class Aaa():
    pass

def pinyin(word):
    s = ''
    for i in pypinyin.pinyin(word, style=pypinyin.NORMAL):
        s += ''.join(i)
    return s
if __name__ == '__main__':

    self=Aaa()
    base_dir=r'E:\data\dao\20241120_2-dao_daoge\20241120_2'
    base_dir=r'E:\data\dao\dao_daoge'
    save_dir=r'E:\data\dao\20241120_2-dao_daoge\crop_daoge2'
    os.makedirs(save_dir,exist_ok=True)

    label_none=r'E:\data\dao\20241120_2-dao_daoge\daog_none'
    if 0:
        img_files = ['%s/%s' % (i[0].replace("\\", "/"), j) for i in os.walk(base_dir) for j in i[-1] if j.lower().endswith(('jpg', 'png', 'jpeg'))]

    self.json_list_o = ['%s/%s' % (i[0].replace("\\", "/"), j) for i in os.walk(base_dir) for j in i[-1] if j.endswith(('.json', '.xpng'))]
    self.image_list = []  # [json_path.replace(".json",".jpg") for json_path in self.json_list]
    self.point_list = []
    self.json_list = []
    bad_count = 0
    for json_path in self.json_list_o:
        if not os.path.exists(json_path):
            self.point_list.append([])
            continue
        with open(json_path, 'r', encoding='utf-8') as f:
            datas = json.load(f)
        img_width = datas['imageWidth']
        img_height = datas['imageHeight']
        image_name = datas['imagePath']
        img_path = os.path.join(os.path.dirname(json_path), image_name)
        img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), 1)  # bgr
        l_dao=None
        l_daoge=None
        for shape in datas['shapes']:
            if shape['label']=="dao":
                l_dao=shape
            elif shape['label']=="daoge":
                l_daoge=shape
        if l_dao is None or l_daoge is None:
            shutil.move(json_path,label_none)
            shutil.move(img_path,label_none)
            print('none',json_path)
            continue
        points_dao = l_dao['points']
        x1,y1 = int(points_dao[0][0]), int(points_dao[0][1])
        x2,y2 = int(points_dao[1][0]), int(points_dao[1][1])
        x1,x2 = min(x1, x2),max(x1, x2)
        y1,y2 = min(y1, y2),max(y1, y2)

        width_deta = (x2 - x1) * 0.12
        height_deta = (y2 - y1) * 0.12
        # 计算调整后的x1和y1,确保不小于0
        x1_box = max(0, x1 - width_deta)
        y1_box = max(0, y1 - height_deta)

        x2_box = min(img_width, x2 + width_deta)
        y2_box = min(img_height, y2 + height_deta)

        img_dao = img[int(y1_box):int(y2_box), int(x1_box):int(x2_box)]
        points_daoge = l_daoge['points']
        x1, y1 = int(points_daoge[0][0]), int(points_daoge[0][1])
        x2, y2 = int(points_daoge[1][0]), int(points_daoge[1][1])
        x1 = min(x1, x2)-x1_box
        x2 = max(x1, x2)-x1_box

        y1 = min(y1, y2)-y1_box
        y2 = max(y1, y2)-y1_box

        l_daoge['points']=[[x1,y1],[x2,y2]]
        if x2 - x1 < 0:
            bad_count += 1
            print('bad_count',json_path)
            exit(12)
            continue
        if 0:
            start_point = (x1, 0)
            end_point = (x1, height - 1)  # 修改

            start_point_1 = (x2, 0)
            end_point_1 = (x2, height - 1)

            # 将起点和终点组成的线段作为标签 point
            point = (start_point, end_point, start_point_1, end_point_1)

        # self.point_list.append(point)
        # self.json_list.append(json_path)
        # self.image_list.append(json_path.replace(".json", ".jpg"))
        json_name=pinyin(os.path.basename(json_path))
        json_path_new=os.path.join(save_dir,json_name)

        image_name = pinyin(image_name)
        datas['imagePath']=image_name
        jpg_path = os.path.join(save_dir, image_name)
        cv2.imwrite(jpg_path, img_dao)
        with open(json_path_new, 'w') as file:
            json.dump(datas, file, indent=4)


网站公告

今日签到

点亮在社区的每一天
去签到