Nick088 commited on
Commit
0ba76d8
·
verified ·
1 Parent(s): 452954e

Update infer.py

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