o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
你好,我有一个物体在六个视角拍摄的215*215尺寸的图片,背景区域为白色,总共是六张,rgb000frontleft,rgb000front。这六张图片被处理为边缘图,文件名为…
- User
你好,我有一个物体在六个视角拍摄的215*215尺寸的图片,背景区域为白色,总共是六张,rgb_000_front_left,rgb_000_front。这六张图片被处理为边缘图,文件名为rgb_000_front_simple,rgb_000_front_left_simple假设一个物体的六个视角的草图越不一样(例如草图被处理为mask,各个mask区别越大),rgb的颜色越不一样,这个物体的得分越高。请帮我写这样一个python函数。 load_sketch 和load image可以使用这样的函数
self.view_types = ['front', 'front_right', 'right', 'back', 'left', 'front_left'] self.load_sketch(os.path.join(self.root_dir, self.paths[object_name][5:-4], "rgb_%03d_%s_simple.%s" % (set_idx, cond_view, self.suffix)), bg_color, cond_alpha, return_type='pt').permute(2, 0, 1)
def load_sketch(self, img_path, bg_color, alpha, return_type='np'): # not using cv2 as may load in uint16 format # img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED) # [0, 255] # img = cv2.resize(img, self.img_wh, interpolation=cv2.INTER_CUBIC) # pil always returns uint8 img = np.array(Image.open(img_path).resize(self.img_wh)) img = img.astype(np.float32) / 255. # [0, 1] assert img.shape[-1] == 3 or img.shape[-1] == 4 # RGB or RGBA if alpha is None and img.shape[-1] == 4: alpha = img[:, :, 3:] img = img[:, :, :3] # if alpha.shape[-1] != 1: # alpha = alpha[:, :, None] # img = img[...,:3] * alpha + bg_color * (1 - alpha) if return_type == "np": pass elif return_type == "pt": img = torch.from_numpy(img) else: raise NotImplementedError return img def load_image(self, img_path, bg_color, alpha, return_type='np'): # not using cv2 as may load in uint16 format # img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED) # [0, 255] # img = cv2.resize(img, self.img_wh, interpolation=cv2.INTER_CUBIC) # pil always returns uint8 img = np.array(Image.open(img_path).resize(self.img_wh)) img = img.astype(np.float32) / 255. # [0, 1] assert img.shape[-1] == 3 or img.shape[-1] == 4 # RGB or RGBA if alpha is None and img.shape[-1] == 4: alpha = img[:, :, 3:] img = img[:, :, :3] if alpha.shape[-1] != 1: alpha = alpha[:, :, None] img = img[...,:3] * alpha + bg_color * (1 - alpha) if return_type == "np": pass elif return_type == "pt": img = torch.from_numpy(img) else: raise NotImplementedError return img - Assistant
import os import numpy as np from PIL import Image import torch def compute_object_score(object_name, root_dir, paths, img_wh=(215,215), bg_color=(1,1,1), alpha=None, set_idx=0, suffix='png'): # List of view types view_types = ['front', 'front_right', 'right', 'back', 'left', 'front_left'] images = [] for cond_view in view_types: # Construct the image path object_sub_dir = paths[object_name][5:-4] img_name = "rgb_%03d_%s_simple.%s" % (set_idx, cond_view, suffix) img_path = os.path.join(root_dir, object_sub_dir, img_name) # Load the sketch image img = load_sketch(img_path, img_wh, bg_color, alpha, return_type='np') images.append(img) # Stack images into an array of shape (6, H, W, C) images_array = np.stack(images) # shape: (6, H, W, C) # Compute the variance across the images at each pixel variance_per_pixel = np.var(images_array, axis=0) # shape: (H, W, C) # Sum over all pixels to get total variance total_variance = np.sum(variance_per_pixel) # Alternatively, compute the average variance per pixel average_variance = total_variance / (images_array.shape[1] * images_array.shape[2] * images_array.shape[3]) # Return the total variance as the score return total_variance def load_sketch(img_path, img_wh, bg_color, alpha, return_type='np'): # Load image and resize img = np.array(Image.open(img_path).resize(img_wh)) img = img.astype(np.float32) / 255.0 # Normalize to [0, 1] assert img.shape[-1] == 3 or img.shape[-1] == 4 # RGB or RGBA if alpha is None and img.shape[-1] == 4: alpha = img[:, :, 3:] img = img[:, :, :3] # If alpha exists, composite image over background if alpha is not None: if alpha.shape[-1] != 1: alpha = alpha[:, :, None] img = img[..., :3] * alpha + np.array(bg_color) * (1 - alpha) if return_type == "np": pass elif return_type == "pt": img = torch.from_numpy(img) else: raise NotImplementedError return img def compute_image_difference(img1, img2): # Compute the mean absolute difference between two images diff = np.abs(img1 - img2) mean_diff = np.mean(diff) return mean_diff