Nick088 commited on
Commit
76c4015
·
verified ·
1 Parent(s): 019ce84

Update inference_video.py

Browse files
Files changed (1) hide show
  1. inference_video.py +42 -15
inference_video.py CHANGED
@@ -3,9 +3,7 @@ import numpy as np
3
  import glob
4
  from os.path import isfile, join
5
  import subprocess
6
- from IPython.display import clear_output
7
  import os
8
- from google.colab import files
9
  import shutil
10
  from io import BytesIO
11
  import io
@@ -13,18 +11,47 @@ import io
13
  IMAGE_FORMATS = ('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')
14
 
15
 
16
- model_scale = "2" #@param ["2", "4", "8"] {allow-input: false}
17
-
18
- model = RealESRGAN(device, scale=int(model_scale))
19
- model.load_weights(f'weights/RealESRGAN_x{model_scale}.pth', download=False)
20
-
21
-
22
- def process_input(filename):
23
- result_image_path = os.path.join('results/restored_imgs', os.path.basename(filename))
24
- image = Image.open(filename).convert('RGB')
25
- sr_image = model.predict(np.array(image))
26
- sr_image.save(result_image_path)
27
- print(f'Finished! Frame of the Video saved to {result_image_path}')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
 
30
  # assign directory
@@ -120,7 +147,7 @@ for filename in os.listdir(directory):
120
 
121
  # process the files
122
  for file_name in file_names:
123
- process_input(f"upload/{file_name}")
124
 
125
 
126
  #convert super res frames to .avi
 
3
  import glob
4
  from os.path import isfile, join
5
  import subprocess
 
6
  import os
 
7
  import shutil
8
  from io import BytesIO
9
  import io
 
11
  IMAGE_FORMATS = ('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')
12
 
13
 
14
+ def inference_image(image, size):
15
+ global model2
16
+ global model4
17
+ global model8
18
+ if image is None:
19
+ raise gr.Error("Image not uploaded")
20
+
21
+ width, height = image.size
22
+ if width >= 5000 or height >= 5000:
23
+ raise gr.Error("The image is too large.")
24
+
25
+ if torch.cuda.is_available():
26
+ torch.cuda.empty_cache()
27
+
28
+ if size == '2x':
29
+ try:
30
+ result = model2.predict(image.convert('RGB'))
31
+ except torch.cuda.OutOfMemoryError as e:
32
+ print(e)
33
+ model2 = RealESRGAN(device, scale=2)
34
+ model2.load_weights('weights/RealESRGAN_x2.pth', download=False)
35
+ result = model2.predict(image.convert('RGB'))
36
+ elif size == '4x':
37
+ try:
38
+ result = model4.predict(image.convert('RGB'))
39
+ except torch.cuda.OutOfMemoryError as e:
40
+ print(e)
41
+ model4 = RealESRGAN(device, scale=4)
42
+ model4.load_weights('weights/RealESRGAN_x4.pth', download=False)
43
+ result = model2.predict(image.convert('RGB'))
44
+ else:
45
+ try:
46
+ result = model8.predict(image.convert('RGB'))
47
+ except torch.cuda.OutOfMemoryError as e:
48
+ print(e)
49
+ model8 = RealESRGAN(device, scale=8)
50
+ model8.load_weights('weights/RealESRGAN_x8.pth', download=False)
51
+ result = model2.predict(image.convert('RGB'))
52
+
53
+ print(f"Image size ({device}): {size} ... OK")
54
+ return result
55
 
56
 
57
  # assign directory
 
147
 
148
  # process the files
149
  for file_name in file_names:
150
+ inference_image(f"upload/{file_name}")
151
 
152
 
153
  #convert super res frames to .avi