import numpy as np import cv2 from PIL import Image, ImageDraw from skimage.measure import label, regionprops def remove_small(binary_mask, min_area=10000): binary_mask = binary_mask.astype(np.uint8) label_image = label(binary_mask) regions = regionprops(label_image) small_white_regions = [region for region in regions if region.area < min_area] for region in small_white_regions: min_row, min_col, max_row, max_col = region.bbox binary_mask[min_row:max_row, min_col:max_col] = 0 return binary_mask def get_img_agnostic_upper_rectangle(im_parse, pose_data, offset_top, offset_bottom, offset_left, offset_right): foot = pose_data[18:24] faces = pose_data[24:92] hands1 = pose_data[92:113] hands2 = pose_data[113:] body = pose_data[:18] parse_array = np.array(im_parse) parse_upper_all = ((parse_array == 4).astype(np.float32) + (parse_array == 7).astype(np.float32) + (parse_array == 14).astype(np.float32) + (parse_array == 15).astype(np.float32) ) parse_upper = ((parse_array == 4).astype(np.float32) + (parse_array == 7).astype(np.float32) ) parse_head = ((parse_array == 3).astype(np.float32) + (parse_array == 1).astype(np.float32) + (parse_array == 11).astype(np.float32)) parse_fixed = (parse_array == 16).astype(np.float32) agnostic = Image.new(mode='L',size=(parse_array.shape[1], parse_array.shape[0]), color=0) img_black = Image.new(mode='L',size=(im_parse.shape[1], im_parse.shape[0]),color=0) gray_img = Image.new('L', size=(im_parse.shape[1], im_parse.shape[0]), color=128) agnostic_draw = ImageDraw.Draw(agnostic) parse_upper = np.uint8(parse_upper*255) parse_upper_all = np.uint8(parse_upper_all*255) contours_all, _ = cv2.findContours(parse_upper_all, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours, _ = cv2.findContours(parse_upper, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cloth_exist = False try: # Initialize variables to store the extreme points min_x, min_y = float('inf'), float('inf') max_x, max_y = float('-inf'), float('-inf') for contour in contours: area = cv2.contourArea(contour) if area >= 100: x, y, w, h = cv2.boundingRect(contour) min_x = min(min_x, x) min_y = min(min_y, y) max_x = max(max_x, x + w) max_y = max(max_y, y + h) cloth_exist = True _x1, _y1, _x2, _y2 = min_x, min_y, max_x, max_y except: cloth_exist = False x1 = [body[i, 0] for i in [2, 3, 4] if body[i, 0] != 0]+\ [hands2[i, 0] for i in [5,9,13] if hands2[i, 0] != 0]+\ [hands1[i, 0] for i in [5,9,13] if hands1[i, 0] != 0] if x1: x1 = np.min(x1) else: x1 = 0 y1 = [body[i, 1] for i in [2, 5] if body[i, 1] != 0]+\ [hands2[i, 1] for i in [5,9,13] if hands2[i, 1] != 0]+\ [hands1[i, 1] for i in [5,9,13] if hands1[i, 1] != 0] if y1: y1 = np.min(y1) else: y1 = 0 x2 = [body[i, 0] for i in [5, 6, 7] if body[i, 0] != 0]+\ [hands1[i, 0] for i in [5,9,13] if hands1[i, 0] != 0]+\ [hands2[i, 0] for i in [5,9,13] if hands2[i, 0] != 0] if x2: x2 = np.max(x2) else: x2 = parse_array.shape[1] y2 = [body[i, 1] for i in [8, 11] if body[i, 1] != 0]+\ [hands2[i, 1] for i in [5,9,13] if hands2[i, 1] != 0]+\ [hands1[i, 1] for i in [5,9,13] if hands1[i, 1] != 0] if y2: y2 = np.max(y2) else: y2 = parse_array.shape[0] pad_y1 = 20 pad_y2 = 10 pad_x1 = pad_x2 = 25 if cloth_exist: x1 = min(x1, _x1) x2 = max(x2, _x2) y1 = min(y1, _y1) if y2>_y2: pad_y2 = 0 y2 = max(y2, _y2) y_face = [faces[i, 1] for i in [5, 11] if faces[i, 1] != 0] if y_face: y_face = np.mean(y_face) if y_face