Abu1998 commited on
Commit
62789ac
·
verified ·
1 Parent(s): b7d855d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -49
app.py CHANGED
@@ -1,63 +1,87 @@
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)
 
 
 
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()