shukdevdatta123 commited on
Commit
3a66796
·
verified ·
1 Parent(s): 1e31d99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -13,12 +13,13 @@ app.prepare(ctx_id=0, det_size=(640, 640))
13
  # Load the face swapper model
14
  swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=False, download_zip=False)
15
 
16
- def swap_faces_in_video(image, video):
17
  """
18
  Swaps faces from a source image with faces detected in a video and returns the path to the output video file.
19
 
20
  image: Source image (as an array)
21
  video: Path to the input video file
 
22
  """
23
  source_faces = app.get(image)
24
 
@@ -35,6 +36,7 @@ def swap_faces_in_video(image, video):
35
  cap = cv2.VideoCapture(video)
36
 
37
  # Get video properties for output
 
38
  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
39
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
40
  fps = cap.get(cv2.CAP_PROP_FPS)
@@ -43,7 +45,7 @@ def swap_faces_in_video(image, video):
43
  fourcc = cv2.VideoWriter_fourcc(*'XVID')
44
  out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
45
 
46
- while True:
47
  ret, frame = cap.read()
48
  if not ret:
49
  break # Exit if the video is finished
@@ -61,6 +63,9 @@ def swap_faces_in_video(image, video):
61
  # Write the result frame to the output video
62
  out.write(result_frame)
63
 
 
 
 
64
  # Release resources
65
  cap.release()
66
  out.release()
@@ -87,9 +92,10 @@ if st.button("Swap Faces"):
87
  tmp_video.write(video_file.read())
88
  tmp_video_path = tmp_video.name
89
 
90
- # Show a spinner while processing
91
  with st.spinner("Processing video..."):
92
- output_video_path = swap_faces_in_video(source_image, tmp_video_path)
 
93
 
94
  if output_video_path:
95
  st.success("Face swapping completed!")
 
13
  # Load the face swapper model
14
  swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=False, download_zip=False)
15
 
16
+ def swap_faces_in_video(image, video, progress):
17
  """
18
  Swaps faces from a source image with faces detected in a video and returns the path to the output video file.
19
 
20
  image: Source image (as an array)
21
  video: Path to the input video file
22
+ progress: Streamlit progress object
23
  """
24
  source_faces = app.get(image)
25
 
 
36
  cap = cv2.VideoCapture(video)
37
 
38
  # Get video properties for output
39
+ frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
40
  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
41
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
42
  fps = cap.get(cv2.CAP_PROP_FPS)
 
45
  fourcc = cv2.VideoWriter_fourcc(*'XVID')
46
  out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
47
 
48
+ for i in range(frame_count):
49
  ret, frame = cap.read()
50
  if not ret:
51
  break # Exit if the video is finished
 
63
  # Write the result frame to the output video
64
  out.write(result_frame)
65
 
66
+ # Update progress bar
67
+ progress.progress((i + 1) / frame_count)
68
+
69
  # Release resources
70
  cap.release()
71
  out.release()
 
92
  tmp_video.write(video_file.read())
93
  tmp_video_path = tmp_video.name
94
 
95
+ # Show a spinner and a progress bar while processing
96
  with st.spinner("Processing video..."):
97
+ progress_bar = st.progress(0)
98
+ output_video_path = swap_faces_in_video(source_image, tmp_video_path, progress_bar)
99
 
100
  if output_video_path:
101
  st.success("Face swapping completed!")