Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from models.TCVC import TCVC_Model # Assurez-vous que le chemin est correct après avoir cloné le repo
|
4 |
+
import cv2
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Charger le modèle pré-entraîné
|
8 |
+
def load_model():
|
9 |
+
model = TCVC_Model()
|
10 |
+
model.load_state_dict(torch.load("path_to_pretrained_model.pth")) # Charge le modèle pré-entraîné
|
11 |
+
model.eval() # Mode évaluation
|
12 |
+
return model
|
13 |
+
|
14 |
+
# Fonction de colorisation de la vidéo
|
15 |
+
def colorize_video(video_path):
|
16 |
+
model = load_model()
|
17 |
+
|
18 |
+
# Lire la vidéo
|
19 |
+
cap = cv2.VideoCapture(video_path)
|
20 |
+
frame_width = int(cap.get(3))
|
21 |
+
frame_height = int(cap.get(4))
|
22 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
23 |
+
|
24 |
+
output_path = "colorized_output.mp4"
|
25 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
|
26 |
+
|
27 |
+
while(cap.isOpened()):
|
28 |
+
ret, frame = cap.read()
|
29 |
+
if not ret:
|
30 |
+
break
|
31 |
+
# Convertir l'image en noir et blanc en couleur
|
32 |
+
color_frame = model.colorize_frame(frame)
|
33 |
+
out.write(color_frame)
|
34 |
+
|
35 |
+
cap.release()
|
36 |
+
out.release()
|
37 |
+
return output_path
|
38 |
+
|
39 |
+
# Interface Gradio
|
40 |
+
def process_video(video):
|
41 |
+
colorized_video = colorize_video(video.name)
|
42 |
+
return colorized_video
|
43 |
+
|
44 |
+
# Créer l'interface Gradio
|
45 |
+
interface = gr.Interface(fn=process_video,
|
46 |
+
inputs=gr.Video(label="Télécharger une vidéo en noir et blanc"),
|
47 |
+
outputs=gr.Video(label="Vidéo colorisée"),
|
48 |
+
title="Colorisation vidéo",
|
49 |
+
description="Téléchargez une vidéo en noir et blanc et obtenez une version colorisée.")
|
50 |
+
|
51 |
+
# Lancer l'interface
|
52 |
+
if __name__ == "__main__":
|
53 |
+
interface.launch()
|