guardiancc commited on
Commit
b768cfd
·
verified ·
1 Parent(s): b794f67

Update engine.py

Browse files
Files changed (1) hide show
  1. engine.py +54 -1
engine.py CHANGED
@@ -67,6 +67,59 @@ class Engine:
67
 
68
  logger.info("✅ FacePoke Engine initialized successfully.")
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  async def load_frames(self, frames):
71
  uid = str(uuid.uuid4())
72
  for frame in loader(frames):
@@ -397,4 +450,4 @@ class Engine:
397
  return [buffered.getvalue(), result_image]
398
 
399
  except Exception as e:
400
- raise ValueError(f"Failed to modify image: {str(e)}")
 
67
 
68
  logger.info("✅ FacePoke Engine initialized successfully.")
69
 
70
+ async def process_video(self, video, params):
71
+ cap = cv2.VideoCapture(video_path)
72
+ video_writer = None
73
+ frames = []
74
+
75
+ while True:
76
+ ret, frame = cap.read()
77
+
78
+ if not ret:
79
+ break
80
+
81
+ if video_writer is None:
82
+ height, width, _ = frame.shape
83
+ output_file = "output_video.mp4"
84
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
85
+ video_writer = cv2.VideoWriter(output_file, fourcc, 24.0, (width, height))
86
+
87
+ frames.append(frame)
88
+
89
+ for frame in loader(frames):
90
+ image = Image.fromarray(frame)
91
+ image = image.convert('RGB')
92
+
93
+ img_rgb = np.array(image)
94
+
95
+ inference_cfg = self.live_portrait.live_portrait_wrapper.cfg
96
+ img_rgb = await asyncio.to_thread(resize_to_limit, img_rgb, inference_cfg.ref_max_shape, inference_cfg.ref_shape_n)
97
+ crop_info = await asyncio.to_thread(self.live_portrait.cropper.crop_single_image, img_rgb)
98
+ img_crop_256x256 = crop_info['img_crop_256x256']
99
+
100
+ I_s = await asyncio.to_thread(self.live_portrait.live_portrait_wrapper.prepare_source, img_crop_256x256)
101
+ x_s_info = await asyncio.to_thread(self.live_portrait.live_portrait_wrapper.get_kp_info, I_s)
102
+ f_s = await asyncio.to_thread(self.live_portrait.live_portrait_wrapper.extract_feature_3d, I_s)
103
+ x_s = await asyncio.to_thread(self.live_portrait.live_portrait_wrapper.transform_keypoint, x_s_info)
104
+
105
+ processed_data = {
106
+ 'img_rgb': img_rgb,
107
+ 'crop_info': crop_info,
108
+ 'x_s_info': x_s_info,
109
+ 'f_s': f_s,
110
+ 'x_s': x_s,
111
+ 'inference_cfg': inference_cfg
112
+ }
113
+
114
+ _, frame = await self.transform_frame(processed_data, params)
115
+ bgr_frame = cv2.cvtColor(np.array(frame), cv2.COLOR_BGR2RGB)
116
+ video_writer.write(cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2RGB))
117
+
118
+ video_writer.release()
119
+ cap.release()
120
+
121
+ return output_file
122
+
123
  async def load_frames(self, frames):
124
  uid = str(uuid.uuid4())
125
  for frame in loader(frames):
 
450
  return [buffered.getvalue(), result_image]
451
 
452
  except Exception as e:
453
+ raise ValueError(f"Failed to modify image: {str(e)}")