Nick088 commited on
Commit
b0f79d3
·
verified ·
1 Parent(s): 9bdccea

Delete inference_video.py

Browse files
Files changed (1) hide show
  1. inference_video.py +0 -198
inference_video.py DELETED
@@ -1,198 +0,0 @@
1
- import cv2
2
- import numpy as np
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
10
- from RealESRGAN import RealESRGAN
11
- import torch
12
- from PIL import Image
13
- import numpy as np
14
-
15
-
16
- IMAGE_FORMATS = ('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')
17
-
18
-
19
- def inference_image(image, size):
20
- global model2
21
- global model4
22
- global model8
23
- if image is None:
24
- raise gr.Error("Image not uploaded")
25
-
26
- width, height = image.size
27
- if width >= 5000 or height >= 5000:
28
- raise gr.Error("The image is too large.")
29
-
30
- if torch.cuda.is_available():
31
- torch.cuda.empty_cache()
32
-
33
- if size == '2x':
34
- try:
35
- result = model2.predict(image.convert('RGB'))
36
- except torch.cuda.OutOfMemoryError as e:
37
- print(e)
38
- model2 = RealESRGAN(device, scale=2)
39
- model2.load_weights('weights/RealESRGAN_x2.pth', download=False)
40
- result = model2.predict(image.convert('RGB'))
41
- elif size == '4x':
42
- try:
43
- result = model4.predict(image.convert('RGB'))
44
- except torch.cuda.OutOfMemoryError as e:
45
- print(e)
46
- model4 = RealESRGAN(device, scale=4)
47
- model4.load_weights('weights/RealESRGAN_x4.pth', download=False)
48
- result = model2.predict(image.convert('RGB'))
49
- else:
50
- try:
51
- result = model8.predict(image.convert('RGB'))
52
- except torch.cuda.OutOfMemoryError as e:
53
- print(e)
54
- model8 = RealESRGAN(device, scale=8)
55
- model8.load_weights('weights/RealESRGAN_x8.pth', download=False)
56
- result = model2.predict(image.convert('RGB'))
57
-
58
- print(f"Frame of the Video size ({device}): {size} ... OK")
59
- return result
60
-
61
-
62
-
63
- # assign directory
64
- directory = 'videos' #PATH_WITH_INPUT_VIDEOS
65
- zee = 0
66
-
67
- def convert_frames_to_video(pathIn,pathOut,fps):
68
- global INPUT_DIR
69
- cap = cv2.VideoCapture(f'/{INPUT_DIR}/videos/input.mp4')
70
- fps = cap.get(cv2.CAP_PROP_FPS)
71
- frame_array = []
72
- files = [f for f in os.listdir(pathIn) if isfile(join(pathIn, f))]
73
- #for sorting the file names properly
74
- files.sort(key = lambda x: int(x[5:-4]))
75
- size2 = (0,0)
76
-
77
- for i in range(len(files)):
78
- filename=pathIn + files[i]
79
- #reading each files
80
- img = cv2.imread(filename)
81
- height, width, layers = img.shape
82
- size = (width,height)
83
- size2 = size
84
- print(filename)
85
- #inserting the frames into an image array
86
- frame_array.append(img)
87
- out = cv2.VideoWriter(pathOut,cv2.VideoWriter_fourcc(*'DIVX'), fps, size2)
88
- for i in range(len(frame_array)):
89
- # writing to a image array
90
- out.write(frame_array[i])
91
- out.release()
92
-
93
-
94
- for filename in os.listdir(directory):
95
-
96
- f = os.path.join(directory, filename)
97
- # checking if it is a file
98
- if os.path.isfile(f):
99
-
100
-
101
- print("PROCESSING :"+str(f)+"\n")
102
- # Read the video from specified path
103
-
104
- #video to frames
105
- cam = cv2.VideoCapture(str(f))
106
-
107
- try:
108
-
109
- # PATH TO STORE VIDEO FRAMES
110
- if not os.path.exists(f'/{INPUT_DIR}/upload/'):
111
- os.makedirs(f'/{INPUT_DIR}/upload/')
112
-
113
- # if not created then raise error
114
- except OSError:
115
- print ('Error: Creating directory of data')
116
-
117
- # frame
118
- currentframe = 0
119
-
120
-
121
- while(True):
122
-
123
- # reading from frame
124
- ret,frame = cam.read()
125
-
126
- if ret:
127
- # if video is still left continue creating images
128
- name = f'/{INPUT_DIR}/upload/frame' + str(currentframe) + '.jpg'
129
-
130
- # writing the extracted images
131
- cv2.imwrite(name, frame)
132
-
133
-
134
- # increasing counter so that it will
135
- # show how many frames are created
136
- currentframe += 1
137
- print(currentframe)
138
- else:
139
- #deletes all the videos you uploaded for upscaling
140
- #for f in os.listdir(video_folder):
141
- # os.remove(os.path.join(video_folder, f))
142
-
143
- break
144
-
145
- # Release all space and windows once done
146
- cam.release()
147
- cv2.destroyAllWindows()
148
-
149
- #apply super-resolution on all frames of a video
150
-
151
- # Specify the directory path
152
- all_frames_path = f"/{INPUT_DIR}/upload/"
153
-
154
- # Get a list of all files in the directory
155
- file_names = os.listdir(all_frames_path)
156
-
157
- # process the files
158
- for file_name in file_names:
159
- inference_image(f"/{INPUT_DIR}/upload/{file_name}")
160
-
161
-
162
- #convert super res frames to .avi
163
- pathIn = f'/{INPUT_DIR}/results/restored_imgs/'
164
-
165
- zee = zee+1
166
- fName = "video"+str(zee)
167
- filenameVid = f"{fName}.avi"
168
-
169
- pathOut = f"/{INPUT_DIR}/results_videos/"+filenameVid
170
-
171
- convert_frames_to_video(pathIn, pathOut, fps)
172
-
173
-
174
- #convert .avi to .mp4
175
- src = f'/{INPUT_DIR}/results_videos/'
176
- dst = f'/{INPUT_DIR}/results_mp4_videos/'
177
-
178
- for root, dirs, filenames in os.walk(src, topdown=False):
179
- #print(filenames)
180
- for filename in filenames:
181
- print('[INFO] 1',filename)
182
- try:
183
- _format = ''
184
- if ".flv" in filename.lower():
185
- _format=".flv"
186
- if ".mp4" in filename.lower():
187
- _format=".mp4"
188
- if ".avi" in filename.lower():
189
- _format=".avi"
190
- if ".mov" in filename.lower():
191
- _format=".mov"
192
-
193
- inputfile = os.path.join(root, filename)
194
- print('[INFO] 1',inputfile)
195
- outputfile = os.path.join(dst, filename.lower().replace(_format, ".mp4"))
196
- subprocess.call(['ffmpeg', '-i', inputfile, outputfile])
197
- except:
198
- print("An exception occurred")