Exemplo n.º 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)
Exemplo n.º 3
0
# -*- 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']
Exemplo n.º 4
0
# # 关键点检测
# # 模型的下载路径: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'))


Exemplo n.º 5
0
# -*- 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()
Exemplo n.º 6
0
# -*- 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))
Exemplo n.º 7
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)
Exemplo n.º 8
0
# -*- 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()
Exemplo n.º 9
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'))
Exemplo n.º 10
0
# -*- 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()
Exemplo n.º 11
0
        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)
Exemplo n.º 12
0
# -*- 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)
Exemplo n.º 13
0
# -*- 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)
Exemplo n.º 14
0
# -*- 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)