Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,8 @@ import tempfile
|
|
| 5 |
import subprocess
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
def extract_preview(video):
|
| 10 |
temp_dir = tempfile.mkdtemp()
|
| 11 |
frame_path = os.path.join(temp_dir, "preview.png")
|
|
@@ -21,33 +22,37 @@ def extract_preview(video):
|
|
| 21 |
return frame_path
|
| 22 |
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
x1, y1, x2, y2 =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
temp_dir = tempfile.mkdtemp()
|
| 28 |
frames_dir = os.path.join(temp_dir, "frames")
|
| 29 |
os.makedirs(frames_dir, exist_ok=True)
|
| 30 |
|
| 31 |
-
# Extraire les frames (FPS réduit)
|
| 32 |
subprocess.run([
|
| 33 |
"ffmpeg", "-i", video,
|
| 34 |
"-vf", "fps=10,scale=1280:-1",
|
| 35 |
f"{frames_dir}/frame_%04d.png"
|
| 36 |
], check=True)
|
| 37 |
|
| 38 |
-
# Inpainting
|
| 39 |
for f in sorted(os.listdir(frames_dir)):
|
| 40 |
path = os.path.join(frames_dir, f)
|
| 41 |
img = cv2.imread(path)
|
| 42 |
|
| 43 |
mask = np.zeros(img.shape[:2], dtype=np.uint8)
|
| 44 |
-
mask[
|
| 45 |
mask = cv2.dilate(mask, np.ones((3,3), np.uint8), 1)
|
| 46 |
|
| 47 |
result = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)
|
| 48 |
cv2.imwrite(path, result)
|
| 49 |
|
| 50 |
-
# Réassembler la vidéo
|
| 51 |
output = os.path.join(temp_dir, "output.mp4")
|
| 52 |
subprocess.run([
|
| 53 |
"ffmpeg", "-y",
|
|
@@ -62,26 +67,20 @@ def process_video(video, selection):
|
|
| 62 |
|
| 63 |
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
-
gr.Markdown("## 🎥
|
| 66 |
|
| 67 |
video = gr.Video(label="Upload vidéo (≤ 60s)")
|
| 68 |
-
preview = gr.Image(label="
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
btn_preview = gr.Button("Afficher une image
|
| 71 |
btn_process = gr.Button("Supprimer le texte")
|
| 72 |
|
| 73 |
output = gr.Video(label="Vidéo finale")
|
| 74 |
|
| 75 |
-
btn_preview.click(
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
outputs=preview
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
btn_process.click(
|
| 82 |
-
process_video,
|
| 83 |
-
inputs=[video, preview.select],
|
| 84 |
-
outputs=output
|
| 85 |
-
)
|
| 86 |
|
| 87 |
demo.launch()
|
|
|
|
| 5 |
import subprocess
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
selection_box = [0, 0, 0, 0]
|
| 9 |
+
|
| 10 |
def extract_preview(video):
|
| 11 |
temp_dir = tempfile.mkdtemp()
|
| 12 |
frame_path = os.path.join(temp_dir, "preview.png")
|
|
|
|
| 22 |
return frame_path
|
| 23 |
|
| 24 |
|
| 25 |
+
def on_select(evt: gr.SelectData):
|
| 26 |
+
global selection_box
|
| 27 |
+
(x1, y1), (x2, y2) = evt.index
|
| 28 |
+
selection_box = [x1, y1, x2, y2]
|
| 29 |
+
return f"Zone sélectionnée : {selection_box}"
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def process_video(video):
|
| 33 |
+
x1, y1, x2, y2 = selection_box
|
| 34 |
+
|
| 35 |
temp_dir = tempfile.mkdtemp()
|
| 36 |
frames_dir = os.path.join(temp_dir, "frames")
|
| 37 |
os.makedirs(frames_dir, exist_ok=True)
|
| 38 |
|
|
|
|
| 39 |
subprocess.run([
|
| 40 |
"ffmpeg", "-i", video,
|
| 41 |
"-vf", "fps=10,scale=1280:-1",
|
| 42 |
f"{frames_dir}/frame_%04d.png"
|
| 43 |
], check=True)
|
| 44 |
|
|
|
|
| 45 |
for f in sorted(os.listdir(frames_dir)):
|
| 46 |
path = os.path.join(frames_dir, f)
|
| 47 |
img = cv2.imread(path)
|
| 48 |
|
| 49 |
mask = np.zeros(img.shape[:2], dtype=np.uint8)
|
| 50 |
+
mask[y1:y2, x1:x2] = 255
|
| 51 |
mask = cv2.dilate(mask, np.ones((3,3), np.uint8), 1)
|
| 52 |
|
| 53 |
result = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA)
|
| 54 |
cv2.imwrite(path, result)
|
| 55 |
|
|
|
|
| 56 |
output = os.path.join(temp_dir, "output.mp4")
|
| 57 |
subprocess.run([
|
| 58 |
"ffmpeg", "-y",
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
+
gr.Markdown("## 🎥 Suppression de texte sur vidéo")
|
| 71 |
|
| 72 |
video = gr.Video(label="Upload vidéo (≤ 60s)")
|
| 73 |
+
preview = gr.Image(label="Clique-glisse pour sélectionner la zone", interactive=True)
|
| 74 |
+
|
| 75 |
+
status = gr.Textbox(label="Statut", interactive=False)
|
| 76 |
|
| 77 |
+
btn_preview = gr.Button("Afficher une image")
|
| 78 |
btn_process = gr.Button("Supprimer le texte")
|
| 79 |
|
| 80 |
output = gr.Video(label="Vidéo finale")
|
| 81 |
|
| 82 |
+
btn_preview.click(extract_preview, video, preview)
|
| 83 |
+
preview.select(on_select, None, status)
|
| 84 |
+
btn_process.click(process_video, video, output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
demo.launch()
|