SpiderReddy commited on
Commit
22e754c
·
verified ·
1 Parent(s): e911460

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -3,7 +3,6 @@ import cv2
3
  import gradio as gr
4
  import AnimeGANv3_src
5
  import numpy as np
6
- from moviepy.editor import VideoFileClip, ImageSequenceClip
7
 
8
  class AnimeGANv3:
9
  def __init__(self):
@@ -14,7 +13,7 @@ class AnimeGANv3:
14
  """Process a single frame with AnimeGANv3."""
15
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
16
  output = AnimeGANv3_src.Convert(frame_rgb, style_code, det_face)
17
- return output
18
 
19
  def inference(self, video_path, style, if_face=None):
20
  print(video_path, style, if_face)
@@ -33,30 +32,34 @@ class AnimeGANv3:
33
  style_code = style_codes.get(style, "U")
34
  det_face = if_face == "Yes"
35
 
36
- # Open the video
37
  cap = cv2.VideoCapture(video_path)
38
  if not cap.isOpened():
39
  raise Exception("Could not open video file")
40
 
41
  # Get video properties
42
  fps = cap.get(cv2.CAP_PROP_FPS)
 
 
43
  frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
44
- print(f"Processing {frame_count} frames at {fps} FPS")
45
 
46
- # Extract and process frames
47
- frames = []
 
 
 
 
48
  while True:
49
  ret, frame = cap.read()
50
  if not ret:
51
  break
52
  processed_frame = self.process_frame(frame, style_code, det_face)
53
- frames.append(processed_frame)
54
- cap.release()
55
 
56
- # Convert frames to video
57
- save_path = "output/out.mp4"
58
- clip = ImageSequenceClip(frames, fps=fps)
59
- clip.write_videofile(save_path, codec="libx264", audio=False)
60
 
61
  return save_path
62
  except Exception as error:
 
3
  import gradio as gr
4
  import AnimeGANv3_src
5
  import numpy as np
 
6
 
7
  class AnimeGANv3:
8
  def __init__(self):
 
13
  """Process a single frame with AnimeGANv3."""
14
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
15
  output = AnimeGANv3_src.Convert(frame_rgb, style_code, det_face)
16
+ return output[:, :, ::-1] # Convert back to BGR for OpenCV
17
 
18
  def inference(self, video_path, style, if_face=None):
19
  print(video_path, style, if_face)
 
32
  style_code = style_codes.get(style, "U")
33
  det_face = if_face == "Yes"
34
 
35
+ # Open the input video
36
  cap = cv2.VideoCapture(video_path)
37
  if not cap.isOpened():
38
  raise Exception("Could not open video file")
39
 
40
  # Get video properties
41
  fps = cap.get(cv2.CAP_PROP_FPS)
42
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
43
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
44
  frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
45
+ print(f"Processing {frame_count} frames at {fps} FPS, {width}x{height}")
46
 
47
+ # Set up video writer
48
+ save_path = "output/out.mp4"
49
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Use 'mp4v' codec for MP4
50
+ out = cv2.VideoWriter(save_path, fourcc, fps, (width, height))
51
+
52
+ # Process frames
53
  while True:
54
  ret, frame = cap.read()
55
  if not ret:
56
  break
57
  processed_frame = self.process_frame(frame, style_code, det_face)
58
+ out.write(processed_frame)
 
59
 
60
+ # Release resources
61
+ cap.release()
62
+ out.release()
 
63
 
64
  return save_path
65
  except Exception as error: