Fabrice-TIERCELIN commited on
Commit
2350566
·
verified ·
1 Parent(s): 8f1a636

Delete infer.py

Browse files
Files changed (1) hide show
  1. infer.py +0 -94
infer.py DELETED
@@ -1,94 +0,0 @@
1
- from PIL import Image
2
- import cv2 as cv
3
- import torch
4
- from RealESRGAN import RealESRGAN
5
- import tempfile
6
- import numpy as np
7
- import tqdm
8
- import ffmpeg
9
-
10
- try:
11
- import spaces
12
- except:
13
- class spaces():
14
- def GPU(*args, **kwargs):
15
- def decorator(function):
16
- return lambda *dummy_args, **dummy_kwargs: function(*dummy_args, **dummy_kwargs)
17
- return decorator
18
-
19
-
20
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
21
-
22
- @spaces.GPU(duration=60)
23
- def infer_image(img: Image.Image, size_modifier: int ) -> Image.Image:
24
- if img is None:
25
- raise Exception("Image not uploaded")
26
-
27
- width, height = img.size
28
-
29
- if width >= 5000 or height >= 5000:
30
- raise Exception("The image is too large.")
31
-
32
- model = RealESRGAN(device, scale=size_modifier)
33
- model.load_weights(f'weights/RealESRGAN_x{size_modifier}.pth', download=False)
34
-
35
- result = model.predict(img.convert('RGB'))
36
- print(f"Image size ({device}): {size_modifier} ... OK")
37
- return result
38
-
39
- @spaces.GPU(duration=60)
40
- def infer_video(video_filepath: str, size_modifier: int) -> str:
41
- model = RealESRGAN(device, scale=size_modifier)
42
- model.load_weights(f'weights/RealESRGAN_x{size_modifier}.pth', download=False)
43
-
44
- cap = cv.VideoCapture(video_filepath)
45
-
46
- tmpfile = tempfile.NamedTemporaryFile(suffix='.mp4', delete=False)
47
- vid_output = tmpfile.name
48
- tmpfile.close()
49
-
50
- # Check if the input video has an audio stream
51
- probe = ffmpeg.probe(video_filepath)
52
- has_audio = any(stream['codec_type'] == 'audio' for stream in probe['streams'])
53
-
54
- if has_audio:
55
- # Extract audio from the input video
56
- audio_file = video_filepath.replace(".mp4", ".wav")
57
- ffmpeg.input(video_filepath).output(audio_file, format='wav', ac=1).run(overwrite_output=True)
58
-
59
- vid_writer = cv.VideoWriter(
60
- vid_output,
61
- fourcc=cv.VideoWriter.fourcc(*'mp4v'),
62
- fps=cap.get(cv.CAP_PROP_FPS),
63
- frameSize=(int(cap.get(cv.CAP_PROP_FRAME_WIDTH)) * size_modifier, int(cap.get(cv.CAP_PROP_FRAME_HEIGHT)) * size_modifier)
64
- )
65
-
66
- n_frames = int(cap.get(cv.CAP_PROP_FRAME_COUNT))
67
-
68
- for _ in tqdm.tqdm(range(n_frames)):
69
- ret, frame = cap.read()
70
- if not ret:
71
- break
72
-
73
- frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
74
- frame = Image.fromarray(frame)
75
-
76
- upscaled_frame = model.predict(frame.convert('RGB'))
77
-
78
- upscaled_frame = np.array(upscaled_frame)
79
- upscaled_frame = cv.cvtColor(upscaled_frame, cv.COLOR_RGB2BGR)
80
-
81
- vid_writer.write(upscaled_frame)
82
-
83
- vid_writer.release()
84
-
85
- if has_audio:
86
- # Re-encode the video with the modified audio
87
- ffmpeg.input(vid_output).output(video_filepath.replace(".mp4", "_upscaled.mp4"), vcodec='libx264', acodec='aac', audio_bitrate='320k').run(overwrite_output=True)
88
-
89
- # Replace the original audio with the upscaled audio
90
- ffmpeg.input(audio_file).output(video_filepath.replace(".mp4", "_upscaled.mp4"), acodec='aac', audio_bitrate='320k').run(overwrite_output=True)
91
-
92
- print(f"Video file : {video_filepath}")
93
-
94
- return vid_output.replace(".mp4", "_upscaled.mp4") if has_audio else vid_output