Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,17 +5,27 @@ import tempfile
|
|
| 5 |
import subprocess
|
| 6 |
import os
|
| 7 |
|
|
|
|
|
|
|
| 8 |
def extract_preview(video):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
temp_dir = tempfile.mkdtemp()
|
| 10 |
frame_path = os.path.join(temp_dir, "preview.png")
|
| 11 |
|
| 12 |
-
subprocess.run(
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
return frame_path
|
| 21 |
|
|
@@ -26,49 +36,64 @@ def save_selection(evt: gr.SelectData):
|
|
| 26 |
|
| 27 |
|
| 28 |
def process_video(video, box):
|
|
|
|
|
|
|
| 29 |
if box is None:
|
| 30 |
raise gr.Error("Aucune zone sélectionnée")
|
| 31 |
|
|
|
|
| 32 |
x1, y1, x2, y2 = box
|
| 33 |
|
| 34 |
temp_dir = tempfile.mkdtemp()
|
| 35 |
frames_dir = os.path.join(temp_dir, "frames")
|
| 36 |
os.makedirs(frames_dir, exist_ok=True)
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
for f in sorted(os.listdir(frames_dir)):
|
| 45 |
path = os.path.join(frames_dir, f)
|
| 46 |
img = cv2.imread(path)
|
| 47 |
|
| 48 |
mask = np.zeros(img.shape[:2], dtype=np.uint8)
|
| 49 |
mask[y1:y2, x1:x2] = 255
|
| 50 |
-
mask = cv2.dilate(mask, np.ones((3,3), np.uint8), 1)
|
| 51 |
|
| 52 |
result = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)
|
| 53 |
cv2.imwrite(path, result)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
return output
|
| 66 |
|
|
|
|
| 67 |
|
| 68 |
with gr.Blocks() as demo:
|
| 69 |
-
gr.Markdown("## 🎥
|
| 70 |
|
| 71 |
-
video = gr.Video(label="
|
| 72 |
preview = gr.Image(label="Sélectionne la zone à supprimer", interactive=True)
|
| 73 |
|
| 74 |
selection_state = gr.State(None)
|
|
|
|
| 5 |
import subprocess
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# --------- UTILITAIRES ---------
|
| 9 |
+
|
| 10 |
def extract_preview(video):
|
| 11 |
+
if video is None:
|
| 12 |
+
raise gr.Error("Aucune vidéo fournie")
|
| 13 |
+
|
| 14 |
+
video_path = video["name"]
|
| 15 |
+
|
| 16 |
temp_dir = tempfile.mkdtemp()
|
| 17 |
frame_path = os.path.join(temp_dir, "preview.png")
|
| 18 |
|
| 19 |
+
subprocess.run(
|
| 20 |
+
[
|
| 21 |
+
"ffmpeg", "-y",
|
| 22 |
+
"-i", video_path,
|
| 23 |
+
"-vf", "select=eq(n\\,10)",
|
| 24 |
+
"-vframes", "1",
|
| 25 |
+
frame_path
|
| 26 |
+
],
|
| 27 |
+
check=True
|
| 28 |
+
)
|
| 29 |
|
| 30 |
return frame_path
|
| 31 |
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
def process_video(video, box):
|
| 39 |
+
if video is None:
|
| 40 |
+
raise gr.Error("Aucune vidéo")
|
| 41 |
if box is None:
|
| 42 |
raise gr.Error("Aucune zone sélectionnée")
|
| 43 |
|
| 44 |
+
video_path = video["name"]
|
| 45 |
x1, y1, x2, y2 = box
|
| 46 |
|
| 47 |
temp_dir = tempfile.mkdtemp()
|
| 48 |
frames_dir = os.path.join(temp_dir, "frames")
|
| 49 |
os.makedirs(frames_dir, exist_ok=True)
|
| 50 |
|
| 51 |
+
# Extraire les frames
|
| 52 |
+
subprocess.run(
|
| 53 |
+
[
|
| 54 |
+
"ffmpeg",
|
| 55 |
+
"-i", video_path,
|
| 56 |
+
"-vf", "fps=10,scale=1280:-1",
|
| 57 |
+
f"{frames_dir}/frame_%04d.png"
|
| 58 |
+
],
|
| 59 |
+
check=True
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Inpainting sur chaque frame
|
| 63 |
for f in sorted(os.listdir(frames_dir)):
|
| 64 |
path = os.path.join(frames_dir, f)
|
| 65 |
img = cv2.imread(path)
|
| 66 |
|
| 67 |
mask = np.zeros(img.shape[:2], dtype=np.uint8)
|
| 68 |
mask[y1:y2, x1:x2] = 255
|
| 69 |
+
mask = cv2.dilate(mask, np.ones((3, 3), np.uint8), 1)
|
| 70 |
|
| 71 |
result = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)
|
| 72 |
cv2.imwrite(path, result)
|
| 73 |
|
| 74 |
+
# Réassembler la vidéo
|
| 75 |
+
output_path = os.path.join(temp_dir, "output.mp4")
|
| 76 |
+
subprocess.run(
|
| 77 |
+
[
|
| 78 |
+
"ffmpeg", "-y",
|
| 79 |
+
"-framerate", "10",
|
| 80 |
+
"-i", f"{frames_dir}/frame_%04d.png",
|
| 81 |
+
"-c:v", "libx264",
|
| 82 |
+
"-pix_fmt", "yuv420p",
|
| 83 |
+
output_path
|
| 84 |
+
],
|
| 85 |
+
check=True
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
return output_path
|
| 89 |
|
|
|
|
| 90 |
|
| 91 |
+
# --------- INTERFACE ---------
|
| 92 |
|
| 93 |
with gr.Blocks() as demo:
|
| 94 |
+
gr.Markdown("## 🎥 Supprimer un texte ou sous-titre d’une vidéo")
|
| 95 |
|
| 96 |
+
video = gr.Video(label="Uploader une vidéo (≤ 60s)")
|
| 97 |
preview = gr.Image(label="Sélectionne la zone à supprimer", interactive=True)
|
| 98 |
|
| 99 |
selection_state = gr.State(None)
|