コード例 #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)
コード例 #2
0
ファイル: test_dlib.py プロジェクト: newjokker/JoUtil
# # 关键点检测
# # 模型的下载路径:http://dlib.net/files/
# predictor = dlib.shape_predictor(r'C:\Users\14271\Desktop\del\shape_predictor_68_face_landmarks.dat')
#
# for det in dets:
#     shape = predictor(img, det)
#     print(shape.parts())
#
#     # 人脸对齐
#     my_img = dlib.get_face_chip(img, shape, size=150)
#
#     plt.imshow(my_img)
#     plt.show()
#

img_dir = r"/home/ldq/20220112_img_from_iphone/img"
save_dir = r"/home/ldq/20220112_img_from_iphone/xml"

detector = dlib.get_frontal_face_detector()
for each_img_path in FileOperationUtil.re_all_file(img_dir, endswitch=['.jpg', '.JPG', '.png', '.PNG']):
    print(each_img_path)
    each_dete_res = DeteRes(assign_img_path=each_img_path)
    img = cv2.imread(each_img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    dets = detector(img, 1)
    for each_shape in dets:
        each_dete_res.add_obj(x1=int(each_shape.left()), y1=int(each_shape.top()), x2=int(each_shape.right()), y2=int(each_shape.bottom()), tag='face')
    each_dete_res.save_to_xml(os.path.join(save_dir, FileOperationUtil.bang_path(each_img_path)[1] + '.xml'))


コード例 #3
0
# -*- coding: utf-8  -*-
# -*- author: jokker -*-

import random
import os
import shutil
import cv2
import PIL.Image as Image
from JoTools.operateDeteRes import OperateDeteRes
from JoTools.txkjRes.deteRes import DeteRes, DeteObj
from JoTools.utils.FileOperationUtil import FileOperationUtil
from JoTools.utils.RandomUtil import RandomUtil

xml_dir = r"/home/ldq/tj_dete/merge"
save_dir = r"/home/ldq/tj_dete/merge_new"

OperateDeteRes.get_class_count(xml_dir, print_count=True)

for each_xml_path in FileOperationUtil.re_all_file(xml_dir,
                                                   endswitch=['.xml']):
    #
    a = DeteRes(each_xml_path)
    a.filter_by_tags(need_tag=["2"])
    save_path = os.path.join(save_dir, os.path.split(each_xml_path)[1])

    if len(a) > 0:
        a.save_to_xml(save_path)

OperateDeteRes.get_class_count(save_dir, print_count=True)
コード例 #4
0
                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)

            if b.has_tag('040303021') or b.has_tag('040303022'):
                # 存在防振锤,需要进行复制文件
                each_xml_name = os.path.split(b.file_name)[1][:-3] + 'xml'
                each_save_img_name = os.path.split(b.file_name)[1][:-3] + 'jpg'
                each_xml_path = os.path.join(save_dir, each_xml_name)
                each_save_img_path = os.path.join(save_dir, each_save_img_name)

                # print(each_xml_path)
                # print(each_save_img_path)

                b.filter_by_tags(need_tag=[
                    '040303021', '040303022', '040303000', '040303011',
                    '040303031', '040303041'
                ])
                b.save_to_xml(each_xml_path)
                shutil.copy(b.img_path, each_save_img_path)

            # b.crop_and_save(r"C:\Users\14271\Desktop\del\crop", split_by_tag=True, include_tag_list=assign_code_list, augment_parameter=[0.1,0.1,0.1,0.1])

        except Exception as e:
            print(e)

    # print(a.name)
    # print(a.comments)
コード例 #5
0
ファイル: format_xml.py プロジェクト: newjokker/JoUtil
# -*- coding: utf-8  -*-
# -*- author: jokker -*-

import sys
import argparse
from JoTools.txkjRes.deteRes import DeteRes,DeteObj
from JoTools.utils.FileOperationUtil import FileOperationUtil



xml_dir = r"C:\Users\14271\Desktop\test\xml"


for each_xml_path in FileOperationUtil.re_all_file(xml_dir, endswitch=['.xml']):

    a = DeteRes(each_xml_path)
    a.angle_obj_to_obj()
    a.save_to_xml(each_xml_path)






コード例 #6
0
ファイル: augment_dete_obj.py プロジェクト: newjokker/JoUtil
# -*- coding: utf-8  -*-
# -*- author: jokker -*-

from JoTools.txkjRes.deteRes import DeteRes, DeteObj
from JoTools.utils.FileOperationUtil import FileOperationUtil

xml_dir = r"C:\data\fzc_优化相关资料\dataset_fzc\999_wait_for_train\武汉电科院_2021_04\xml_new_0.05"

for i in FileOperationUtil.re_all_file(xml_dir, endswitch=['.xml']):
    a = DeteRes(i)
    a.do_augment([0.05, 0.05, 0.05, 0.05], is_relative=True)
    a.save_to_xml(i)