Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import zipfile
|
| 2 |
+
import os
|
| 3 |
+
import cv2
|
| 4 |
+
from gradio_client import Client, handle_file
|
| 5 |
+
import shutil
|
| 6 |
+
|
| 7 |
+
# Function to process the video and apply the color transformation to each frame
|
| 8 |
+
def process_video(input_zip_path, style_image_path, output_video_path):
|
| 9 |
+
# Initialize the Hugging Face Client for the color transformation API
|
| 10 |
+
client = Client("Abu1998/Image_Color_Transfer_Video")
|
| 11 |
+
|
| 12 |
+
# Prepare the zip file containing images
|
| 13 |
+
with zipfile.ZipFile(input_zip_path, 'r') as zip_ref:
|
| 14 |
+
zip_ref.extractall('input_images') # Extract images to 'input_images' folder
|
| 15 |
+
|
| 16 |
+
# Process each image
|
| 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 |
+
# Apply color transformation to each image
|
| 24 |
+
for image_file in image_files:
|
| 25 |
+
input_image_path = os.path.join('input_images', image_file)
|
| 26 |
+
|
| 27 |
+
# Use the style image and the current image for transformation
|
| 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(output_folder, f"transformed_{image_file}")
|
| 36 |
+
with open(transformed_image_path, 'wb') as f:
|
| 37 |
+
f.write(result[0]['data'])
|
| 38 |
+
|
| 39 |
+
# Create a video from the transformed images
|
| 40 |
+
images = [os.path.join(output_folder, f) for f in sorted(os.listdir(output_folder))]
|
| 41 |
+
frame = cv2.imread(images[0])
|
| 42 |
+
height, width, layers = frame.shape
|
| 43 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 44 |
+
video_writer = cv2.VideoWriter(output_video_path, fourcc, 30.0, (width, height))
|
| 45 |
+
|
| 46 |
+
for image in images:
|
| 47 |
+
frame = cv2.imread(image)
|
| 48 |
+
video_writer.write(frame)
|
| 49 |
+
|
| 50 |
+
video_writer.release()
|
| 51 |
+
|
| 52 |
+
# Clean up temporary files
|
| 53 |
+
shutil.rmtree('input_images')
|
| 54 |
+
shutil.rmtree('output_images')
|
| 55 |
+
|
| 56 |
+
print(f"Video saved to {output_video_path}")
|
| 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 |
+
process_video(input_zip_path, style_image_path, output_video_path)
|