haiderakt commited on
Commit
854d27f
·
1 Parent(s): ab097f0

Fixed Sound for Videos

Browse files
Files changed (4) hide show
  1. __pycache__/utils.cpython-312.pyc +0 -0
  2. app.py +3 -4
  3. requirements.txt +2 -0
  4. utils.py +13 -5
__pycache__/utils.cpython-312.pyc CHANGED
Binary files a/__pycache__/utils.cpython-312.pyc and b/__pycache__/utils.cpython-312.pyc differ
 
app.py CHANGED
@@ -24,7 +24,6 @@ if mode == "Image":
24
 
25
  st.image(cv2.cvtColor(result, cv2.COLOR_BGR2RGB), caption="Blurred Image")
26
 
27
- # Save to temp file and create download button
28
  _, temp_path = tempfile.mkstemp(suffix=".jpg")
29
  cv2.imwrite(temp_path, result)
30
  with open(temp_path, "rb") as file:
@@ -49,10 +48,10 @@ elif mode == "Video":
49
 
50
  with st.spinner("Processing video..."):
51
  try:
52
- blur_faces_video(input_path, output_path, update_callback=update_progress)
53
  st.success("✅ Processing complete!")
54
- st.video(output_path)
55
- with open(output_path, "rb") as file:
56
  st.download_button("📥 Download Blurred Video", file, file_name="blurred_video.mp4", mime="video/mp4")
57
  except Exception as e:
58
  st.error(f"Something went wrong: {e}")
 
24
 
25
  st.image(cv2.cvtColor(result, cv2.COLOR_BGR2RGB), caption="Blurred Image")
26
 
 
27
  _, temp_path = tempfile.mkstemp(suffix=".jpg")
28
  cv2.imwrite(temp_path, result)
29
  with open(temp_path, "rb") as file:
 
48
 
49
  with st.spinner("Processing video..."):
50
  try:
51
+ final_output = blur_faces_video(input_path, output_path, update_callback=update_progress)
52
  st.success("✅ Processing complete!")
53
+ st.video(final_output)
54
+ with open(final_output, "rb") as file:
55
  st.download_button("📥 Download Blurred Video", file, file_name="blurred_video.mp4", mime="video/mp4")
56
  except Exception as e:
57
  st.error(f"Something went wrong: {e}")
requirements.txt CHANGED
@@ -3,3 +3,5 @@ opencv-python
3
  ultralytics
4
  numpy
5
  Pillow
 
 
 
3
  ultralytics
4
  numpy
5
  Pillow
6
+ moviepy==1.0.3
7
+ imageio-ffmpeg
utils.py CHANGED
@@ -1,7 +1,7 @@
1
  import cv2
2
  from ultralytics import YOLO
 
3
 
4
- # Load your preferred face detection model
5
  model = YOLO("yolov8-face-hf.pt")
6
 
7
  # ------------------ IMAGE FUNCTION ------------------ #
@@ -36,7 +36,8 @@ def blur_faces_video(input_path, output_path, update_callback=None):
36
  height = height if height % 2 == 0 else height - 1
37
 
38
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
39
- out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
 
40
 
41
  frame_num = 0
42
  while True:
@@ -50,7 +51,6 @@ def blur_faces_video(input_path, output_path, update_callback=None):
50
 
51
  for box in results.boxes.xyxy:
52
  x1, y1, x2, y2 = map(int, box)
53
-
54
  x1 = max(0, min(w, x1))
55
  x2 = max(0, min(w, x2))
56
  y1 = max(0, min(h, y1))
@@ -58,7 +58,6 @@ def blur_faces_video(input_path, output_path, update_callback=None):
58
 
59
  face = frame[y1:y2, x1:x2]
60
  if face.size > 0:
61
- # Stronger blur
62
  blur_size = max(55, ((x2 - x1) // 2) | 1)
63
  if blur_size % 2 == 0:
64
  blur_size += 1
@@ -72,4 +71,13 @@ def blur_faces_video(input_path, output_path, update_callback=None):
72
  update_callback(frame_num / total_frames)
73
 
74
  cap.release()
75
- out.release()
 
 
 
 
 
 
 
 
 
 
1
  import cv2
2
  from ultralytics import YOLO
3
+ from moviepy.editor import VideoFileClip
4
 
 
5
  model = YOLO("yolov8-face-hf.pt")
6
 
7
  # ------------------ IMAGE FUNCTION ------------------ #
 
36
  height = height if height % 2 == 0 else height - 1
37
 
38
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
39
+ temp_video_path = "blurred_temp.mp4"
40
+ out = cv2.VideoWriter(temp_video_path, fourcc, fps, (width, height))
41
 
42
  frame_num = 0
43
  while True:
 
51
 
52
  for box in results.boxes.xyxy:
53
  x1, y1, x2, y2 = map(int, box)
 
54
  x1 = max(0, min(w, x1))
55
  x2 = max(0, min(w, x2))
56
  y1 = max(0, min(h, y1))
 
58
 
59
  face = frame[y1:y2, x1:x2]
60
  if face.size > 0:
 
61
  blur_size = max(55, ((x2 - x1) // 2) | 1)
62
  if blur_size % 2 == 0:
63
  blur_size += 1
 
71
  update_callback(frame_num / total_frames)
72
 
73
  cap.release()
74
+ out.release()
75
+
76
+ try:
77
+ original = VideoFileClip(input_path)
78
+ processed = VideoFileClip(temp_video_path).set_audio(original.audio)
79
+ processed.write_videofile(output_path, codec="libx264", audio_codec="aac", verbose=False, logger=None)
80
+ return output_path
81
+ except Exception as e:
82
+ print("Audio merging failed:", e)
83
+ return temp_video_path