Abu1998 commited on
Commit
65367ab
·
verified ·
1 Parent(s): b71cb15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -63
app.py CHANGED
@@ -1,81 +1,40 @@
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
- # Function to handle video processing
9
- def process_video(style_image, zip_file):
10
- # Extract images from the uploaded zip file
11
- zip_path = zip_file.name
12
- output_dir = "/tmp/extracted_images" # Use a temporary directory for Hugging Face Spaces
13
- os.makedirs(output_dir, exist_ok=True)
14
-
15
- with zipfile.ZipFile(zip_path, 'r') as zip_ref:
16
- zip_ref.extractall(output_dir)
17
-
18
- # Get all image files from the extracted zip
19
- image_files = [os.path.join(output_dir, f) for f in os.listdir(output_dir) if f.endswith(('.png', '.jpg', '.jpeg'))]
20
-
21
- # Initialize the client for image-to-image color transfer
22
- client = Client("Abu1998/Image_Color_Transfer_Video")
23
-
24
- # Prepare the style image (color reference)
25
- style_image_path = style_image.name
26
- style_image_url = handle_file(style_image_path)
27
-
28
- transformed_images = []
29
-
30
- # Process each image and apply color transfer
31
- for image_file in image_files:
32
- image_url = handle_file(image_file)
33
- result = client.predict(img1=style_image_url, img2=image_url, api_name="/_transfer_style")
34
 
35
- # Save the transformed image in the temporary directory
36
- transformed_image_path = os.path.join("/tmp/transformed_images", os.path.basename(image_file))
37
- os.makedirs("/tmp/transformed_images", exist_ok=True)
38
- with open(transformed_image_path, 'wb') as f:
39
- f.write(result[0]['path'].read())
40
-
41
- transformed_images.append(transformed_image_path)
42
-
43
- # Convert transformed images back to video
44
- frame_array = []
45
- for image_path in transformed_images:
46
- img = cv2.imread(image_path)
47
- height, width, layers = img.shape
48
- size = (width, height)
49
- frame_array.append(img)
50
-
51
- video_output_path = "/tmp/output_video.mp4"
52
- out = cv2.VideoWriter(video_output_path, cv2.VideoWriter_fourcc(*'mp4v'), 20.0, size)
53
-
54
- for frame in frame_array:
55
- out.write(frame)
56
-
57
- out.release()
58
 
59
- return video_output_path
 
60
 
61
 
62
  # Gradio Interface setup
63
  def create_gradio_interface():
64
  with gr.Blocks() as app:
65
- gr.Markdown("# Image Color Transfer Video Generator")
66
 
67
- # Input components
68
- style_image = gr.File(type="file", label="Upload Style Image (Reference Image for Color Transfer)") # Updated to gr.File
69
- zip_file = gr.File(label="Upload Zip File of Images")
70
 
71
- # Output component
72
- video_output = gr.Video(label="Generated Video")
73
 
74
- # Button to trigger processing
75
- process_button = gr.Button("Generate Video")
76
 
77
  # Action for button click
78
- process_button.click(fn=process_video, inputs=[style_image, zip_file], outputs=video_output)
79
 
80
  app.launch(share=True)
81
 
 
1
  import gradio as gr
 
 
 
 
2
  from gradio_client import Client, handle_file
3
 
4
+ # Function to handle video-to-image extraction and return the extracted frames as a ZIP
5
+ def extract_frames_from_video(video_file):
6
+ # Initialize the client for Video-To-Image API
7
+ client = Client("Abu1998/Video-To-Image")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ # Upload the video file to the API
10
+ video_url = handle_file(video_file.name)
11
+
12
+ # Send the video file to the API for processing
13
+ result = client.predict(
14
+ video_path={"video": video_url},
15
+ api_name="/predict"
16
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # The result should contain the link to the ZIP file with extracted frames
19
+ return result['filepath']
20
 
21
 
22
  # Gradio Interface setup
23
  def create_gradio_interface():
24
  with gr.Blocks() as app:
25
+ gr.Markdown("# Video to Image Frame Extractor")
26
 
27
+ # Input component for video upload
28
+ video_file = gr.File(type="file", label="Upload Video")
 
29
 
30
+ # Output component for the extracted frames (ZIP file)
31
+ zip_output = gr.File(label="Download Extracted Frames as ZIP")
32
 
33
+ # Button to trigger video-to-image extraction
34
+ process_button = gr.Button("Extract Frames")
35
 
36
  # Action for button click
37
+ process_button.click(fn=extract_frames_from_video, inputs=video_file, outputs=zip_output)
38
 
39
  app.launch(share=True)
40