SpiderReddy commited on
Commit
8016ac3
·
verified ·
1 Parent(s): 04b3484

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -21
app.py CHANGED
@@ -3,11 +3,16 @@ import cv2
3
  import gradio as gr
4
  import AnimeGANv3_src
5
  import numpy as np
 
 
 
 
6
 
7
  class AnimeGANv3:
8
  def __init__(self):
9
- # Ensure the output directory exists
10
  os.makedirs('output', exist_ok=True)
 
11
 
12
  def process_frame(self, frame, style_code, det_face):
13
  """Process a single frame with AnimeGANv3."""
@@ -16,7 +21,7 @@ class AnimeGANv3:
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)
20
  try:
21
  # Map style names to codes
22
  style_codes = {
@@ -32,38 +37,45 @@ class AnimeGANv3:
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:
66
- print('Error:', error)
67
  return None
68
 
69
  # Create an instance of the AnimeGANv3 class
@@ -89,8 +101,8 @@ iface = gr.Interface(
89
  'AnimeGANv3_PortraitSketch',
90
  'AnimeGANv3_JP_face v1.0',
91
  'AnimeGANv3_Kpop v2.0',
92
- ], label='AnimeGANv3 Style', value='AnimeGANv3_Arcane'), # Changed 'default' to 'value'
93
- gr.Radio(choices=["Yes", "No"], label='Extract face', value="No"), # Also updated here
94
  ],
95
  outputs=[
96
  gr.Video(label="Output Video")
 
3
  import gradio as gr
4
  import AnimeGANv3_src
5
  import numpy as np
6
+ import logging
7
+
8
+ # Set up logging
9
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
10
 
11
  class AnimeGANv3:
12
  def __init__(self):
13
+ # Ensure directories exist
14
  os.makedirs('output', exist_ok=True)
15
+ os.makedirs('frames', exist_ok=True)
16
 
17
  def process_frame(self, frame, style_code, det_face):
18
  """Process a single frame with AnimeGANv3."""
 
21
  return output[:, :, ::-1] # Convert back to BGR for OpenCV
22
 
23
  def inference(self, video_path, style, if_face=None):
24
+ logging.info(f"Starting inference: video={video_path}, style={style}, face_detection={if_face}")
25
  try:
26
  # Map style names to codes
27
  style_codes = {
 
37
  style_code = style_codes.get(style, "U")
38
  det_face = if_face == "Yes"
39
 
40
+ # Open the input video and extract frames
41
  cap = cv2.VideoCapture(video_path)
42
  if not cap.isOpened():
43
  raise Exception("Could not open video file")
44
 
 
45
  fps = cap.get(cv2.CAP_PROP_FPS)
 
 
46
  frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
47
+ frames = []
 
 
 
 
 
48
 
49
+ while cap.isOpened():
 
50
  ret, frame = cap.read()
51
  if not ret:
52
  break
53
+ frames.append(frame)
 
54
 
 
55
  cap.release()
56
+ logging.info(f"Extracted {frame_count} frames at {fps} FPS to process")
57
+
58
+ # Process each frame and save as PNG with logging
59
+ for idx, frame in enumerate(frames):
60
+ stylized_frame = self.process_frame(frame, style_code, det_face)
61
+ png_filename = f'frames/frame_{idx:04d}.png'
62
+ cv2.imwrite(png_filename, stylized_frame)
63
+ logging.info(f"Processed and saved frame {idx + 1}/{frame_count} as {png_filename}")
64
+
65
+ logging.info("All frames processed and saved as PNGs")
66
+
67
+ # Combine PNGs into video using ffmpeg
68
+ save_path = "output/out.mp4"
69
+ os.system(f"ffmpeg -framerate {fps} -i frames/frame_%04d.png -c:v libx264 -pix_fmt yuv420p {save_path} -y")
70
+
71
+ # Check if the video was created
72
+ if not os.path.exists(save_path):
73
+ raise Exception("Failed to create output video with ffmpeg")
74
 
75
+ logging.info(f"Video created: {save_path}")
76
  return save_path
77
  except Exception as error:
78
+ logging.error(f"Error: {str(error)}")
79
  return None
80
 
81
  # Create an instance of the AnimeGANv3 class
 
101
  'AnimeGANv3_PortraitSketch',
102
  'AnimeGANv3_JP_face v1.0',
103
  'AnimeGANv3_Kpop v2.0',
104
+ ], label='AnimeGANv3 Style', value='AnimeGANv3_Arcane'),
105
+ gr.Radio(choices=["Yes", "No"], label='Extract face', value="No"),
106
  ],
107
  outputs=[
108
  gr.Video(label="Output Video")