Пример #1
0
def resize_one_img_xml(save_dir, resize_ratio, img_xml):
    """将一张训练图片进行 resize"""
    # 解析读到的数据
    img_path, xml_path = img_xml
    #
    a = DeteRes(xml_path)
    #
    if (not os.path.exists(img_path)) or (not os.path.exists(xml_path)):
        return
    #
    if len(a) < 1:
        return
    #
    im = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), 1)
    im_height, im_width = im.shape[:2]
    im_height_new, im_width_new = int(im_height * resize_ratio), int(im_width * resize_ratio)
    im_new = cv2.resize(im, (im_width_new, im_height_new))
    #
    # a.height = im_height_new
    # a.width = im_width_new
    # a.img_path =
    # 将每一个 obj 进行 resize
    for each_obj in a:
        each_obj.x1 = max(1, int(each_obj.x1 * resize_ratio))
        each_obj.x2 = min(im_width_new - 1, int(each_obj.x2 * resize_ratio))
        each_obj.y1 = max(1, int(each_obj.y1 * resize_ratio))
        each_obj.y2 = min(im_height_new - 1, int(each_obj.y2 * resize_ratio))
    # 保存 img
    save_img_path = os.path.join(save_dir, 'JPEGImages', FileOperationUtil.bang_path(xml_path)[1] + '.jpg')
    cv2.imwrite(save_img_path, im_new)
    # 保存 xml
    a.img_path = save_img_path
    save_xml_path = os.path.join(save_dir, 'Annotations', FileOperationUtil.bang_path(xml_path)[1] + '.xml')
    a.save_to_xml(save_xml_path)
def main():
    xml_path = r"C:\Users\14271\Desktop\del\pillow_cv2\test_2.xml"
    img_path = r"C:\Users\14271\Desktop\del\pillow_cv2\test_2.jpg"

    a = DeteRes(xml_path)
    a.img_path = img_path

    # img_a = a.get_img_array(RGB=False)
    # img_b = a.get_img_array_new(RGB=False)

    start = time.time()
    # for each_dete_obj in a:
    #     print('-'*50)
    #     each_img_a = a.get_sub_img_by_dete_obj(each_dete_obj)
    #     each_img_b = a.get_sub_img_by_dete_obj_new(each_dete_obj)
    #     # print(each_img_a.shape)
    #     # print(each_img_b.shape)
    #     pass

    a.crop_dete_obj(r"C:\Users\14271\Desktop\del\pillow_cv2\crop")
    a.crop_dete_obj_new(r"C:\Users\14271\Desktop\del\pillow_cv2\crop")

    stop = time.time()

    print(stop - start)
Пример #3
0
    r"\\192.168.3.80\数据\9eagle数据库\peiyu_11.library\images"
]

for dir_index, img_dir in enumerate(img_dir_list):

    for index, each_json_path in enumerate(
            FileOperationUtil.re_all_file(img_dir,
                                          lambda x: str(x).endswith('.json'))):

        try:

            print(dir_index, index, each_json_path)
            b = DeteRes()
            a = EagleMetaData()
            a.load_atts_from_json(each_json_path)
            b.img_path = os.path.join(os.path.dirname(each_json_path),
                                      a.name + '.jpg')

            if not os.path.exists(b.img_path):
                continue

            if a.comments is None:
                continue

            for each_comment in a.comments:
                x1 = int(each_comment['x'])
                y1 = int(each_comment['y'])
                x2 = int(each_comment['width']) + x1
                y2 = int(each_comment['height']) + y1
                tag = str(each_comment['annotation']).split('_')[0]
                b.add_obj(x1, y1, x2, y2, tag=tag, conf=-1)
Пример #4
0
# -*- coding: utf-8  -*-
# -*- author: jokker -*-

import os
from JoTools.utils.FileOperationUtil import FileOperationUtil
from JoTools.txkjRes.deteRes import DeteRes

xml_dir = r"C:\Users\14271\Desktop\连接件训练\xml_angle"
save_dir = r"C:\Users\14271\Desktop\连接件训练\xml"
img_dir = r"E:\连接件训练数据集"

for each_xml_path in FileOperationUtil.re_all_file(xml_dir,
                                                   endswitch=['.xml']):
    a = DeteRes(xml_path=each_xml_path)
    a.img_path = os.path.join(img_dir,
                              os.path.split(each_xml_path)[1][:-4] + '.jpg')
    a.angle_obj_to_obj()
    # a.save_to_xml(os.path.join(save_dir, os.path.split(each_xml_path)[1]))
    a.draw_dete_res(
        os.path.join(r"C:\Users\14271\Desktop\连接件训练\img",
                     os.path.split(each_xml_path)[1][:-4] + '.jpg'))
# -*- coding: utf-8  -*-
# -*- author: jokker -*-

import time
from JoTools.txkjRes.deteRes import DeteRes

img_path = r"C:\Users\14271\Desktop\del\test.jpg"
xml_path = r"C:\Users\14271\Desktop\del\test.xml"

a = DeteRes(xml_path)

a.img_path = img_path

a.draw_dete_res(r"C:\Users\14271\Desktop\del\draw_res_a.jpg")

# # b = a.deep_copy()
#
# c = DeteRes(json_dict=a.save_to_json())
#
# res = c.deep_copy()
#
# res.print_as_fzc_format()
# print('-'*50)
# a.print_as_fzc_format()
#
# print(a.img.width)
# print(a.img.height)
#
# print(res.img.width)
# print(res.img.height)
#