""" # File : xml2.py # Time :2024-04-04 12:16 # Author :FEANGYANG # version :python 3.7 # Contact :1071082183@qq.com # Description: """ import glob import os import cv2 import numpy as np def read_xml(file): # actual parsing in_file = open(file) tree = ET.parse(in_file) root = tree.getroot() bboxes_list = [] for obj in root.iter('object'): current = list() name = obj.find('name').text xmlbox = obj.find('bndbox') xn = xmlbox.find('xmin').text xx1 = xmlbox.find('xmax').text yn = xmlbox.find('ymin').text yx1 = xmlbox.find('ymax').text bboxes_list.append([xn, yn, xx1, yx1]) return bboxes_list def get_xmls_path_list(xmls_path): xmls_path_list = [] for path in os.listdir(xmls_path): xml_path = '/'.join([xmls_path, path]) xmls_path_list.append(xml_path) # bboxes_list = read_xml(xml_path) return xmls_path_list def get_images_path_list(images_path): images_path_list = [] for path in os.listdir(images_path): image_path = '/'.join([images_path, path]) images_path_list.append(image_path) return images_path_list def test_xml2images_path(images_path, xmls_path_list): test_images_path_list = [] test_xmls_path_list = [] tmp_images_path = os.listdir(images_path) if len(tmp_images_path) > 3: suffex = os.path.splitext(tmp_images_path[2])[-1] else: raise ValueError('图片数量太少') for xml_path in xmls_path_list: xml_name = os.path.split(xml_path)[-1] image_path = '/'.join([images_path, os.path.splitext(xml_name)[0]+suffex]) if os.path.exists(image_path): test_xmls_path_list.append(xml_path) test_images_path_list.append(image_path) return test_images_path_list, test_xmls_path_list # def small_images(image_path, pixes,pic_target = './result/'): # if not os.path.exists(pic_target): # 判断是否存在文件夹如果不存在则创建为文件夹 # os.makedirs(pic_target) # picture = cv2.imread(image_path) # name = os.path.split(image_path)[-1] # # (width, length, depth) = picture.shape # w, h, _ = picture.shape # # _w = w % pixes # # _h = h % pixes # # cut_width = 64 # cut_length = 64 # # # 预处理生成0矩阵 # pic = np.zeros((cut_width, cut_length, depth)) # # 计算可以划分的横纵的个数 # num_width = int(width / cut_width) # num_length = int(length / cut_length) # # for循环迭代生成 # lines_list = [] # for i in range(0, num_width): # for j in range(0, num_length): # pic = picture[i * cut_width: (i + 1) * cut_width, j * cut_length: (j + 1) * cut_length, :] # result_path = pic_target + os.path.splitext(name)[0] +'_{}_{}.jpg'.format(i + 1, j + 1) # cv2.imwrite(result_path, pic) def small_images(image_path, pixes,pic_target = './result/'): if not os.path.exists(pic_target): # 判断是否存在文件夹如果不存在则创建为文件夹 os.makedirs(pic_target) picture = cv2.imread(image_path) name = os.path.split(image_path)[-1] (length, width, depth) = picture.shape w, h, _ = picture.shape # _w = w % pixes # _h = h % pixes cut_width = 128 cut_length = 128 # 预处理生成0矩阵 # pic = np.zeros((cut_width, cut_length, depth)) # 计算可以划分的横纵的个数 num_width = int(width / cut_width) num_length = int(length / cut_length) print(num_width, num_length) pixes += num_width * num_length print(pixes) # for循环迭代生成 lines_list = [] for i in range(0, num_width+1): lines_list.append([[i * cut_width, 0], [i * cut_width, w]]) for j in range(0, num_length+1): lines_list.append([(0, j * cut_length), (h, j * cut_length)]) point_color = (0, 0, 255) # BGR thickness = 1 lineType = 8 for line in lines_list: cv2.line(picture, tuple(line[0]), tuple(line[1]), point_color, thickness, lineType) result_path = pic_target + os.path.splitext(name)[0] + '.jpg' cv2.imwrite(result_path, picture) return pixes # def small_images(image_path, pixes, pic_target = './result/'): # if not os.path.exists(pic_target): # 判断是否存在文件夹如果不存在则创建为文件夹 # os.makedirs(pic_target) # crop_w = pixes # 裁剪图像宽度 # crop_h = pixes # img = cv2.imread(image_path) # name = os.path.split(image_path)[-1] # old_size = img.shape[0:2] # 原图尺寸 # # print(old_size[0],type(old_size[0])) # ###### # if old_size[0] % crop_h != 0 and old_size[1] % crop_w != 0: # # h_num = int(old_size[0] / crop_h) + 1 # 取整后加1 # w_num = int(old_size[1] / crop_w) + 1 # # new_height = (h_num) * crop_h # 小图像尺寸整倍数的大图像 # new_width = (w_num) * crop_w # # print(new_height,new_width) # # # # pad_h = new_height - old_size[0] # 计算自动需要填充的像素数目(图像的高这一维度上) # pad_w = new_width - old_size[1] # 计算需要填充的像素数目(图像的宽这一维度上) # # print(pad_w,pad_h) # # # top, bottom = pad_h // 2, pad_h - (pad_h // 2) # # left, right = pad_w // 2, pad_w - (pad_w // 2) # top, bottom = 0, pad_h # left, right = 0, pad_w # # print(top, bottom, left, right) # img_new = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, None, (0, 0, 0)) # # else: # h_num = int(old_size[0] / crop_h) # w_num = int(old_size[1] / crop_w) # img_new = cv2.imread(image_path) # # for i in range(h_num): # for j in range(w_num): # # print(i,j) # x = int(i * crop_h) # 高度上裁剪图像个数 # y = int(j * crop_w) # # print(x,y) # img_crop = img_new[x: x + crop_h, y: y + crop_w] # print(x, y, x + crop_h, y + crop_w) # # print(z) # saveName = os.path.splitext(name)[0] + '-' + str(i) + '-' + str(j) + ".png" # 小图像名称,内含小图像的顺序 # cv2.imwrite(pic_target + saveName, img_crop) if __name__ == '__main__': images_path1 = './data/day_time_wildfire/images' xmls_path1 = './data/day_time_wildfire/annotations/xmls' images_path2 = './data/FlameVision/Detection/images' xmls_path2 = './data/FlameVision/Detection/annotations' images_path = images_path2 xmls_path = xmls_path2 xmls_path_list = get_xmls_path_list(xmls_path) images_path_list = get_images_path_list(images_path) # test_images_path_list, test_xmls_path_list = test_xml2images_path(images_path, xmls_path_list) sum = 0 for image_path in images_path_list: sum = small_images(image_path, sum) pass