Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,87 @@
|
|
| 1 |
-
import
|
| 2 |
import os
|
|
|
|
| 3 |
import cv2
|
|
|
|
| 4 |
from gradio_client import Client, handle_file
|
| 5 |
-
import shutil
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
client = Client("Abu1998/Image_Color_Transfer_Video")
|
| 11 |
|
| 12 |
-
# Prepare the
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
image_files = [f for f in os.listdir('input_images') if f.endswith('.png') or f.endswith('.jpg')]
|
| 18 |
-
|
| 19 |
-
# Prepare output folder
|
| 20 |
-
output_folder = 'output_images'
|
| 21 |
-
os.makedirs(output_folder, exist_ok=True)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
for image_file in image_files:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
result = client.predict(
|
| 29 |
-
img1=handle_file(style_image_path),
|
| 30 |
-
img2=handle_file(input_image_path),
|
| 31 |
-
api_name="/_transfer_style"
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
# Save the transformed image
|
| 35 |
-
transformed_image_path = os.path.join(
|
| 36 |
with open(transformed_image_path, 'wb') as f:
|
| 37 |
-
f.write(result[0]['
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
# Example usage
|
| 59 |
-
input_zip_path = 'path_to_zip_of_images.zip' # Path to the zip file of images from the video
|
| 60 |
-
style_image_path = 'path_to_style_image.jpg' # Path to the style image for color transformation
|
| 61 |
-
output_video_path = 'output_video.mp4' # Path where the final video will be saved
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import zipfile
|
| 4 |
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
from gradio_client import Client, handle_file
|
|
|
|
| 7 |
|
| 8 |
+
# Downgrade numpy to version <2 if not already set in requirements
|
| 9 |
+
os.system("pip install numpy<2")
|
| 10 |
+
|
| 11 |
+
# Function to handle video processing
|
| 12 |
+
def process_video(style_image, zip_file):
|
| 13 |
+
# Extract images from the uploaded zip file
|
| 14 |
+
zip_path = zip_file.name
|
| 15 |
+
output_dir = "extracted_images"
|
| 16 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 17 |
+
|
| 18 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 19 |
+
zip_ref.extractall(output_dir)
|
| 20 |
+
|
| 21 |
+
# Get all image files from the extracted zip
|
| 22 |
+
image_files = [os.path.join(output_dir, f) for f in os.listdir(output_dir) if f.endswith(('.png', '.jpg', '.jpeg'))]
|
| 23 |
+
|
| 24 |
+
# Initialize the client for image-to-image color transfer
|
| 25 |
client = Client("Abu1998/Image_Color_Transfer_Video")
|
| 26 |
|
| 27 |
+
# Prepare the style image (color reference)
|
| 28 |
+
style_image_path = style_image.name
|
| 29 |
+
style_image_url = handle_file(style_image_path)
|
| 30 |
|
| 31 |
+
transformed_images = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Process each image and apply color transfer
|
| 34 |
for image_file in image_files:
|
| 35 |
+
image_url = handle_file(image_file)
|
| 36 |
+
result = client.predict(img1=style_image_url, img2=image_url, api_name="/_transfer_style")
|
| 37 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# Save the transformed image
|
| 39 |
+
transformed_image_path = os.path.join("transformed_images", os.path.basename(image_file))
|
| 40 |
with open(transformed_image_path, 'wb') as f:
|
| 41 |
+
f.write(result[0]['path'].read())
|
| 42 |
+
|
| 43 |
+
transformed_images.append(transformed_image_path)
|
| 44 |
+
|
| 45 |
+
# Convert transformed images back to video
|
| 46 |
+
frame_array = []
|
| 47 |
+
for image_path in transformed_images:
|
| 48 |
+
img = cv2.imread(image_path)
|
| 49 |
+
height, width, layers = img.shape
|
| 50 |
+
size = (width, height)
|
| 51 |
+
frame_array.append(img)
|
| 52 |
+
|
| 53 |
+
video_output_path = "output_video.mp4"
|
| 54 |
+
out = cv2.VideoWriter(video_output_path, cv2.VideoWriter_fourcc(*'mp4v'), 20.0, size)
|
| 55 |
+
|
| 56 |
+
for frame in frame_array:
|
| 57 |
+
out.write(frame)
|
| 58 |
+
|
| 59 |
+
out.release()
|
| 60 |
+
|
| 61 |
+
return video_output_path
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# Gradio Interface setup
|
| 65 |
+
def create_gradio_interface():
|
| 66 |
+
with gr.Blocks() as app:
|
| 67 |
+
gr.Markdown("# Image Color Transfer Video Generator")
|
| 68 |
+
|
| 69 |
+
# Input components
|
| 70 |
+
style_image = gr.Image(type="file", label="Upload Style Image (Reference Image for Color Transfer)")
|
| 71 |
+
zip_file = gr.File(label="Upload Zip File of Images")
|
| 72 |
+
|
| 73 |
+
# Output component
|
| 74 |
+
video_output = gr.Video(label="Generated Video")
|
| 75 |
+
|
| 76 |
+
# Button to trigger processing
|
| 77 |
+
process_button = gr.Button("Generate Video")
|
| 78 |
+
|
| 79 |
+
# Action for button click
|
| 80 |
+
process_button.click(fn=process_video, inputs=[style_image, zip_file], outputs=video_output)
|
| 81 |
|
| 82 |
+
app.launch(share=True)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
# Run the application
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
create_gradio_interface()
|