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)
# -*- 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 -*- from JoTools.txkjRes.deteRes import DeteRes a = DeteRes() b = DeteRes() a.add_obj(1, 2, 3, 4, 'a') a.add_obj(2, 3, 4, 5, 'b') b.add_obj(2, 3, 4, 5, 'c') b.add_obj(2, 3, 4, 5, 'd') c = a + b a.print_as_fzc_format() print('-' * 50) b.print_as_fzc_format() print('-' * 50) c.print_as_fzc_format()
# -*- coding: utf-8 -*- # -*- author: jokker -*- from JoTools.for_csdn.word_pic.word_pic import WordImage from PIL import Image from JoTools.operateDeteRes import OperateDeteRes from JoTools.txkjRes.deteRes import DeteRes from JoTools.utils.FileOperationUtil import FileOperationUtil xml_path = r"C:\Users\14271\Desktop\del\xml\img65.xml" a = DeteRes(xml_path) for each_obj in a: print(each_obj.crop_path)
# # 关键点检测 # # 模型的下载路径: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'))
# -*- coding: utf-8 -*- # -*- author: jokker -*- # 类型,等效于 type() ? from JoTools.txkjRes.deteRes import DeteRes a = DeteRes() print(DeteRes.__class__) print("123".__class__) print(a.__class__) print(type(DeteRes)) print(type("123")) print(type(a))
# -*- coding: utf-8 -*- # -*- author: jokker -*- from JoTools.txkjRes.deteRes import DeteRes img_path = r"C:\Users\14271\Desktop\rustDebug\test.jpg" xml_path = r"C:\Users\14271\Desktop\rustDebug\test.xml" save_path = r"C:\Users\14271\Desktop\rustDebug\res.jpg" a = DeteRes(xml_path=xml_path, assign_img_path=img_path) mask = a.deep_copy() mask.filter_by_tags(need_tag=['mask']) ljc = a.deep_copy() ljc.filter_by_tags(need_tag=['ljc']) ljc.filter_by_dete_res_mask(mask) ljc.draw_dete_res(save_path)
loc = res['result']['face_list'][i]['location'] x1, y1 = loc['left'], loc['top'] width, height = loc['width'], loc['height'] x2, y2 = x1 + width, y1 + height face_info.append([int(x1), int(y1), int(x2), int(y2)]) return face_info # OperateDeteRes.crop_imgs(img_dir, xml_dir=img_dir, save_dir=save_dir) # todo 测试正脸的图片 for img_path in FileOperationUtil.re_all_file( img_dir, lambda x: str(x).endswith(('.JPG', '.jpg'))): dete_res = DeteRes(assign_img_path=img_path) res = dete_face(img_path) print(res) for index, each_res in enumerate(res): x1, y1, x2, y2 = each_res dete_res.add_obj(x1=x1, y1=y1, x2=x2, y2=y2, tag='face', assign_id=index) save_path = os.path.join(save_dir, os.path.split(img_path)[1]) dete_res.draw_dete_res(save_path) time.sleep(3)
# -*- 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)
# -*- coding: utf-8 -*- # -*- author: jokker -*- from JoTools.txkjRes.deteRes import DeteRes img_path = r"C:\Users\14271\Desktop\rustDebug\test.jpg" xml_path = r"C:\Users\14271\Desktop\rustDebug\test.xml" save_path = r"C:\Users\14271\Desktop\rustDebug\res.jpg" a = DeteRes(xml_path=xml_path, assign_img_path=img_path) mask = a.deep_copy() mask.filter_by_tags(need_tag=['mask']) mask.print_as_fzc_format() print('-' * 100) a.print_as_fzc_format() b = a - mask print('-' * 100) b.print_as_fzc_format()
index = 0 for each_xml_path in FileOperationUtil.re_all_file(xml_dir, endswitch=['.xml']): index += 1 print(index, each_xml_path) xml_name = os.path.split(each_xml_path)[1] img_name = xml_name[:-3] + 'jpg' each_img_path = os.path.join(img_dir, img_name) if not (os.path.exists(each_xml_path) and os.path.exists(each_img_path)): print("* 没找到对应的 img 数据: {0}".format(each_img_path)) continue a = DeteRes(each_xml_path) # a.filter_by_area(50*100) if len(a) < 1: continue img_path_list.append(each_img_path) xml_path_list.append(each_xml_path) FileOperationUtil.move_file_to_folder(img_path_list, img_save_dir, is_clicp=False) FileOperationUtil.move_file_to_folder(xml_path_list, xml_save_dir, is_clicp=False)
# -*- author: jokker -*- from JoTools.txkjRes.segmentJson import SegmentJson from JoTools.txkjRes.deteRes import DeteRes from JoTools.utils.FileOperationUtil import FileOperationUtil import base64 import numpy as np from labelme import utils import labelme import cv2 from PIL import Image json_dir = r"C:\data\004_绝缘子污秽\val\json" a = SegmentJson() dete_res = DeteRes() for each_json_path in list( FileOperationUtil.re_all_file(json_dir, endswitch=['.json']))[20:]: print(each_json_path) a.parse_json_info(each_json_path, parse_img=True, parse_mask=True) dete_res.img = Image.fromarray(a.image_data) for each_obj in a.shapes: print(each_obj.box) box = each_obj.box dete_res.add_obj(box[0], box[1], box[2], box[3], tag=each_obj.label)
# -*- coding: utf-8 -*- # -*- author: jokker -*- from JoTools.txkjRes.deteRes import DeteObj, DeteRes save_path = r"C:\Users\14271\Desktop\del.xml" a = DeteRes() a.add_obj(10, 20, 30, 40, "test", 0.5) a.add_angle_obj(10, 10, 100, 100, 30, 'hehe', 0.5) a[0].hehe = 'just test' a[0].add_new = 123222121 a[0].add_123 = '111122' a[1].test_1 = 'test1' a[1].test_2 = 'test1' a[1].test_3 = 'test1' a.save_to_xml(save_path) b = a.save_to_json() a = DeteRes(json_dict=b) print(a[0].__dict__) print(a[1].__dict__) print(a[0].add_123) print(a[1].test_1)
# -*- 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) #
# -*- coding: utf-8 -*- # -*- author: jokker -*- import cv2 import numpy as np from JoTools.txkjRes.deteRes import DeteRes import matplotlib.pyplot as plt img_path = r"C:\Users\14271\Desktop\del\test\00fa186e8d4d6660b49ddef8a35a77de.jpg" xml_path = r"C:\Users\14271\Desktop\del\test\00fa186e8d4d6660b49ddef8a35a77de.xml" save_xml_path = r"C:\Users\14271\Desktop\del\test\save_002.xml" a = DeteRes(xml_path) # a.img_path = img_path img_ndarry = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), 1) img_ndarry = cv2.cvtColor(img_ndarry, cv2.COLOR_BGR2RGB) a.img_ndarry = img_ndarry for each_dete_obj in a: b = a.get_sub_img_by_dete_obj_new(each_dete_obj, RGB=True) # b = a.get_sub_img_by_dete_obj(each_dete_obj, RGB=True) plt.imshow(b) plt.show()
img_dir_list = [ r"\\192.168.3.80\数据\9eagle数据库\peiyu_06.library\images", r"\\192.168.3.80\数据\9eagle数据库\peiyu_07.library\images", 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
# -*- coding: utf-8 -*- # -*- author: jokker -*- from JoTools.txkjRes.deteRes import DeteObj, DeteRes a = DeteRes(r"C:\Users\14271\Desktop\del\del.xml") a[2].tag = 'fzc_broken' a[4].tag = 'test' a[1].conf = 0.6 # b = a.filter_by_attr("x1", 1155, update=False) # b = a.update_attr_for_all_obj("x1", 1155, update=False) # for each_dete_obj in a: # print(each_dete_obj.get_area()) b = a.filter_by_area(10650, update=False, mode='lt') # b = a.filter_by_tags(remove_tag=['Fnormal'], update=True) # b = a.filter_by_conf(0.5, update=False, mode='gt') b.print_as_fzc_format() print('-'*100) a.print_as_fzc_format()
# -*- 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)
# -*- 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)
# -*- coding: utf-8 -*- # -*- author: jokker -*- from JoTools.txkjRes.deteRes import DeteRes img_path = r"C:\Users\14271\Desktop\rustDebug\test.jpg" xml_path = r"C:\Users\14271\Desktop\rustDebug\test.xml" save_path = r"C:\Users\14271\Desktop\rustDebug\res.jpg" a = DeteRes(xml_path=xml_path, assign_img_path=img_path) mask = a.deep_copy() mask.filter_by_tags(need_tag=['mask']) ljc = a.deep_copy() ljc.filter_by_tags(need_tag=['ljc']) intersection = mask.intersection(a) union = ljc.union(mask) diff = a.difference(ljc) diff.print_as_fzc_format() print(a.issubset(ljc)) print(a.isupperset(ljc)) mask.add_obj_2(ljc.alarms[0]) ljc.print_as_fzc_format()
# -*- coding: utf-8 -*- # -*- author: jokker -*- # 返回子类列表 from JoTools.txkjRes.deteRes import DeteRes, ResBase a = DeteRes() b = DeteRes() print(ResBase.__subclasses__())
# -*- coding: utf-8 -*- # -*- author: jokker -*- # 类的静态函数、类函数、普通函数、全局变量以及一些内置的属性都是放在类__dict__里 # python 中 一些内置的数据类型是没有__dict__属性的 # 类的 __dict__ 属性 # 类的实例的 __dict__ 属性 from JoTools.txkjRes.deteRes import DeteRes a = DeteRes(r"C:\Users\14271\Desktop\del\del.xml") print(DeteRes.__dict__) print(a.__dict__) # ------------------------ 可以用于简化代码 --------------------------------- # Person1 和 Person2 能实现一样的功能 class Person1: def __init__(self,_obj): self.name = _obj['name'] self.age = _obj['age']
up_dict = { "LmObj_rust": "rust", "dpObj_rust": "rust", "kkxObj_rust": "rust", "noObj_rust": "rust", "kkxObj_miss": "K", } # up_dict = {"extra": "Fnormal", "UGuaHuan":"Fnormal"} # up_dict = {"Fnormal": "fzc", "fzc_broken":"fzc"} xml_dir = r"C:\Users\14271\Desktop\kkx_all_compare\xml_tmp" save_dir = r"C:\Users\14271\Desktop\kkx_all_compare\xml_tmp" OperateDeteRes.get_class_count(xml_dir, print_count=True) # exit() for each_xml_path in FileOperationUtil.re_all_file(xml_dir, endswitch=['.xml']): a = DeteRes(each_xml_path) a.update_tags(up_dict) # a.filter_by_tags(need_tag=["td"]) # a.do_augment(augment_parameter=[0.05,0.05,0.05,0.05]) save_path = os.path.join(save_dir, os.path.split(each_xml_path)[1]) a.save_to_xml(save_path) OperateDeteRes.get_class_count(save_dir, print_count=True)