Spaces:
Runtime error
Runtime error
Update app.py
Browse files12335456789108911121314111213141516171819202115161718192021222324252627282930317 unmodified lines394041354243443839454647424348495051525354555657585960616263646546476667496869707111 unmodified lines8384856768698687888990919293942 unmodified lines9798998384100101102103import gradio as grfrom PIL import Image, ImageEnhanceimport numpy as npimport torchimport torchvision.transforms as transformsimport torch.nn.functional as Ffrom torchvision import transformsfrom torchvision.models import resnet34from torchvision.models.segmentation import deeplabv3_resnet50import numpy as npimport cv2
# Load a pre-trained ResNet modelmodel = resnet34(pretrained=True)# Load a pre-trained ResNet model for remasteringresnet_model = resnet34(pretrained=True)resnet_model.eval()
# Define the upscaling functiondef upscale_image(image, upscale_factor=2, sharpness=1.0, contrast=1.0, brightness=1.0): # Resize the image width, height = image.size new_size = (int(width * upscale_factor), int(height * upscale_factor)) upscaled_image = image.resize(new_size, Image.BICUBIC) # Apply sharpness, contrast, and brightness adjustments upscaled_image = ImageEnhance.Sharpness(upscaled_image).enhance(sharpness) upscaled_image = ImageEnhance.Contrast(upscaled_image).enhance(contrast) upscaled_image = ImageEnhance.Brightness(upscaled_image).enhance(brightness)# Load a pre-trained DeepLab model for segmentation (optional for advanced remastering)deeplab_model = deeplabv3_resnet50(pretrained=True)deeplab_model.eval()
# Define the upscaling function using super-resolution techniquesdef upscale_image(image, upscale_factor=2): # Convert the image to a tensor and upscale it using a neural network preprocess = transforms.Compose([ transforms.ToTensor(), transforms.Lambda(lambda x: x.unsqueeze(0)) ]) img_tensor = preprocess(image) upscaled_tensor = F.interpolate(img_tensor, scale_factor=upscale_factor, mode='bicubic', align_corners=False) upscaled_image = transforms.ToPILImage()(upscaled_tensor.squeeze()) return upscaled_image
7 unmodified lines enhancer = ImageEnhance.Sharpness(image) image = enhancer.enhance(sharpness)
# For HDR simulation and tone mapping, we're using simple brightness adjustments # Apply a simulated HDR effect using tone mapping enhancer = ImageEnhance.Brightness(image) image = enhancer.enhance(hdr_intensity)
# Simulate color grading by adjusting contrast enhancer = ImageEnhance.Contrast(image) image = enhancer.enhance(color_grading)
return image # Optional: Use segmentation to remaster specific regions input_tensor = transforms.ToTensor()(image).unsqueeze(0) with torch.no_grad(): output = deeplab_model(input_tensor)['out'][0] output_predictions = output.argmax(0) # Process each segmented region (e.g., sky, water) differently (optional) # Example: Apply a slight blur to the sky region to create a dreamy effect mask = output_predictions.byte().cpu().numpy() segmented_image = np.array(image) segmented_image[mask == 15] = cv2.GaussianBlur(segmented_image[mask == 15], (5, 5), 0) final_image = Image.fromarray(segmented_image) return final_image
# Process function for Gradiodef process_image(image, upscale=False, upscale_factor=2, sharpness=1.0, contrast=1.0, brightness=1.0, remaster=False, color_range=1.0, hdr_intensity=1.0, tone_mapping=1.0, color_grading=1.0):def process_image(image, upscale=False, upscale_factor=2, remaster=False, color_range=1.0, sharpness=1.0, hdr_intensity=1.0, tone_mapping=1.0, color_grading=1.0): if upscale: image = upscale_image(image, upscale_factor, sharpness, contrast, brightness) image = upscale_image(image, upscale_factor) if remaster: image = remaster_image(image, color_range, sharpness, hdr_intensity, tone_mapping, color_grading)11 unmodified lines gr.Markdown("### Upscaling Options") upscale_checkbox = gr.Checkbox(label="Apply Upscaling") upscale_factor = gr.Slider(1, 8, value=2, label="Upscale Factor") sharpness = gr.Slider(0.5, 2.0, value=1.0, label="Sharpness") contrast = gr.Slider(0.5, 2.0, value=1.0, label="Contrast") brightness = gr.Slider(0.5, 2.0, value=1.0, label="Brightness") with gr.Group(): gr.Markdown("### Remastering Options") remaster_checkbox = gr.Checkbox(label="Apply Remastering") color_range = gr.Slider(0.5, 2.0, value=1.0, label="Dynamic Color Range") sharpness = gr.Slider(0.5, 2.0, value=1.0, label="Sharpness") hdr_intensity = gr.Slider(0.5, 2.0, value=1.0, label="HDR Intensity") tone_mapping = gr.Slider(0.5, 2.0, value=1.0, label="Tone Mapping") color_grading = gr.Slider(0.5, 2.0, value=1.0, label="Color Grading")2 unmodified lines process_button.click( process_image, inputs=[image_input, upscale_checkbox, upscale_factor, sharpness, contrast, brightness, remaster_checkbox, color_range, hdr_intensity, tone_mapping, color_grading], inputs=[image_input, upscale_checkbox, upscale_factor, remaster_checkbox, color_range, sharpness, hdr_intensity, tone_mapping, color_grading], outputs=image_output )
app.py+44-28