Spaces:
Build error
Build error
parokshsaxena commited on
Commit ยท
5ca0873
1
Parent(s): d3da15b
intensity transfer instead of color
Browse filesFiles changed (2)
152 unmodified lines15315415515615715815916016124 unmodified lines186187188189190191152 unmodified lines#human_img_orig = human_img_dict["background"].convert("RGB") # ImageEditorhuman_img_orig = human_img_dict.convert("RGB") # Imageif background_img:human_img_orig = BackgroundProcessor.color_transfer(human_img_orig, background_img)"""# Derive HEIGHT & WIDTH such that width is not more than 1000. This will cater to both Shein images (4160x6240) of 3:4 AR and model standard images ( 768x1024 ) of 2:3 AR24 unmodified lineselse:human_img = human_img_orig.resize((WIDTH, HEIGHT))if is_checked:# internally openpose_model is resizing human_img to resolution 384 if not passed as input152 unmodified lines15315415515615715815924 unmodified lines184185186187188189190191192193152 unmodified lines#human_img_orig = human_img_dict["background"].convert("RGB") # ImageEditorhuman_img_orig = human_img_dict.convert("RGB") # Image"""# Derive HEIGHT & WIDTH such that width is not more than 1000. This will cater to both Shein images (4160x6240) of 3:4 AR and model standard images ( 768x1024 ) of 2:3 AR24 unmodified lineselse:human_img = human_img_orig.resize((WIDTH, HEIGHT))# Do color transfer from background image for better image harmonizationif background_img:human_img = BackgroundProcessor.intensity_transfer(human_img, background_img)if is_checked:# internally openpose_model is resizing human_img to resolution 384 if not passed as input
234 unmodified lines23523623723823924013 unmodified lines254255256257234 unmodified lines@classmethoddef color_transfer(cls, source_pil: Image, target_pil: Image) -> Image:source = ImageFormatConvertor.pil_to_cv2(source_pil)target = ImageFormatConvertor.pil_to_cv2(target_pil)source = cv2.cvtColor(source, cv2.COLOR_BGR2LAB)target = cv2.cvtColor(target, cv2.COLOR_BGR2LAB)13 unmodified linesres = cv2.cvtColor(result, cv2.COLOR_LAB2BGR)res_pil = ImageFormatConvertor.cv2_to_pil(res)return res_pil234 unmodified lines23523623723823924024124224324424513 unmodified lines259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309234 unmodified lines@classmethoddef color_transfer(cls, source_pil: Image, target_pil: Image) -> Image:source = ImageFormatConvertor.pil_to_cv2(source_pil)# Resize background imagewidth, height = source_pil.width, source_pil.heighttarget_pil = target_pil.convert("RGB")target_pil = target_pil.resize((width, height))target = ImageFormatConvertor.pil_to_cv2(target_pil)source = cv2.cvtColor(source, cv2.COLOR_BGR2LAB)target = cv2.cvtColor(target, cv2.COLOR_BGR2LAB)13 unmodified linesres = cv2.cvtColor(result, cv2.COLOR_LAB2BGR)res_pil = ImageFormatConvertor.cv2_to_pil(res)return res_pil@classmethoddef intensity_transfer(cls, source_pil: Image, target_pil: Image) -> Image:"""Transfers the intensity distribution from the target image to the source image.Parameters:source (np.ndarray): The source image (foreground) to be harmonized.target (np.ndarray): The target image (background) whose intensity distribution is to be matched.eps (float): A small value to avoid division by zero.Returns:np.ndarray: The intensity-transferred source image."""source = ImageFormatConvertor.pil_to_cv2(source_pil)# Resize background imagewidth, height = source_pil.width, source_pil.heighttarget_pil = target_pil.convert("RGB")target_pil = target_pil.resize((width, height))target = ImageFormatConvertor.pil_to_cv2(target_pil)source_lab = cv2.cvtColor(source, cv2.COLOR_BGR2LAB)target_lab = cv2.cvtColor(target, cv2.COLOR_BGR2LAB)# Compute the mean and standard deviation of the L channel (intensity) of the source and target imagessource_mean, source_std = cv2.meanStdDev(source_lab[:, :, 0])target_mean, target_std = cv2.meanStdDev(target_lab[:, :, 0])# Reshape the mean and std to (1, 1, 1) so they can be broadcast correctlysource_mean = source_mean.reshape((1, 1, 1))source_std = source_std.reshape((1, 1, 1))target_mean = target_mean.reshape((1, 1, 1))target_std = target_std.reshape((1, 1, 1))# Transfer the intensity (L channel)result_l = (source_lab[:, :, 0] - source_mean) * (target_std / source_std) + target_meanresult_l = np.clip(result_l, 0, 255).astype(np.uint8)# Combine the transferred L channel with the original A and B channelsresult_lab = np.copy(source_lab)result_lab[:, :, 0] = result_l# return cv2.cvtColor(result_lab, cv2.COLOR_LAB2BGR)res = cv2.cvtColor(result_lab, cv2.COLOR_LAB2BGR)res_pil = ImageFormatConvertor.cv2_to_pil(res)return res_pil
app.py+5-3
src/background_processor.py+52