Spaces:
Build error
Build error
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445import gradio as grfrom color_matcher import ColorMatcherfrom color_matcher.normalizer import Normalizerimport numpy as npimport cv2from PIL import Image# Function to apply color correctiondef color_match(source_img, reference_img):# Convert PIL images to OpenCV format (numpy arrays)img_src = np.array(source_img)img_ref = np.array(reference_img)# Ensure images are in RGB format (3 channels)if img_src.shape[2] == 4:img_src = cv2.cvtColor(img_src, cv2.COLOR_RGBA2RGB)if img_ref.shape[2] == 4:img_ref = cv2.cvtColor(img_ref, cv2.COLOR_RGBA2RGB)# Apply color matchingcm = ColorMatcher()img_res = cm.transfer(src=img_src, ref=img_ref, method='mkl')# Normalize the resultimg_res = Normalizer(img_res).uint8_norm()# Convert back to PIL for displaying in Gradioimg_res_pil = Image.fromarray(img_res)return img_res_pil# Gradio Interfacedef gradio_interface():# Define input and output componentsinputs = [gr.Image(type="pil", label="Source Image"),gr.Image(type="pil", label="Reference Image")]outputs = gr.Image(type="pil", label="Resulting Image")# Launch Gradio appgr.Interface(fn=color_match, inputs=inputs, outputs=outputs, title="Color Matching Tool").launch()# Run the Gradio Interfaceif __name__ == "__main__":1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import gradio as grfrom PIL import Image, ImageEnhanceimport numpy as npimport cv2from lang_sam import LangSAMfrom color_matcher import ColorMatcherfrom color_matcher.normalizer import Normalizer# Load the LangSAM modelmodel = LangSAM() # Use the default model or specify custom checkpoint: LangSAM("<model_type>", "<path/to/checkpoint>")# Function to apply color matching based on reference imagedef apply_color_matching(source_img_np, ref_img_np):# Initialize ColorMatchercm = ColorMatcher()# Apply color matchingimg_res = cm.transfer(src=source_img_np, ref=ref_img_np, method='mkl')# Normalize the resultimg_res = Normalizer(img_res).uint8_norm()return img_res# Function to extract sky and apply color matching using a reference imagedef extract_and_color_match_sky(image_pil, reference_image_pil, text_prompt="sky"):# Use LangSAM to predict the mask for the skymasks, boxes, phrases, logits = model.predict(image_pil, text_prompt)# Convert the mask to a binary format and create a mask imagesky_mask = masks[0].astype(np.uint8) * 255# Convert PIL image to numpy array for processingimg_np = np.array(image_pil)# Convert sky mask to 3-channel format to blend with the original imagesky_mask_3ch = cv2.merge([sky_mask, sky_mask, sky_mask])# Extract the sky regionsky_region = cv2.bitwise_and(img_np, sky_mask_3ch)# Convert the reference image to a numpy arrayref_img_np = np.array(reference_image_pil)# Apply color matching using the reference image to the extracted sky regionsky_region_color_matched = apply_color_matching(sky_region, ref_img_np)# Combine the color-matched sky region back into the original imageresult_img_np = np.where(sky_mask_3ch > 0, sky_region_color_matched, img_np)# Convert the result back to PIL Image for final outputresult_img_pil = Image.fromarray(result_img_np)return result_img_pil# Gradio Interfacedef gradio_interface():# Gradio function to be called on inputdef process_image(source_img, ref_img):# Extract sky and apply color matching using reference imageresult_img_pil = extract_and_color_match_sky(source_img, ref_img)return result_img_pil# Define Gradio input componentsinputs = [gr.Image(type="pil", label="Source Image"),gr.Image(type="pil", label="Reference Image") # Second input for reference image]# Define Gradio output componentoutputs = gr.Image(type="pil", label="Resulting Image")# Launch Gradio appgr.Interface(fn=process_image, inputs=inputs, outputs=outputs, title="Sky Extraction and Color Matching").launch()# Run the Gradio Interfaceif __name__ == "__main__":
app.py+54-22