你好,请帮我在这个代码的基础上修改出一个,遍历每个uid文件夹下的6个视角的图片"rgb%03d%s.%s" % (setidx, condview,…
- User
你好,请帮我在这个代码的基础上修改出一个,遍历每个uid文件夹下的6个视角的图片"rgb_%03d_%s.%s" % (set_idx, cond_view, suffix),如果六个视角中有一个是视角的图片是只有黑色和白色两种颜色,就将这个uid加入到"only_black_and_white"json文件中。 import os import numpy as np from PIL import Image import json
def compute_object_score(object_dir, 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' ]
codeimages = [] for cond_view in view_types: # Construct the image path img_name = "rgb_%03d_%s.%s" % (set_idx, cond_view, suffix) img_path = os.path.join(object_dir, img_name) rgb_img = load_sketch(img_path, img_wh, bg_color, alpha, return_type='np') images.append(rgb_img) images.append(sketch_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_variancedef 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
code - Assistant