-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
175 lines (154 loc) · 5.23 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Author: JuledZ
import cv2
import os
from os.path import isfile, join
import random
import string
import requests
import re
import shutil
deepai_api_key = "*INSERT-YOUR-API-KEY-HERE*"
def rand_string(length):
rand_str = "".join(random.choice(
string.ascii_lowercase
+ string.ascii_uppercase
+ string.digits)
for i in range(length))
return rand_str
def get_video_length(video_path):
cap = cv2.VideoCapture(video_path)
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
return length
def extract_frames(video_path, skip_frames=1):
_, file_name = os.path.split(video_path)
file_name_without_ext = os.path.splitext(file_name)[0]
length = get_video_length(video_path)
if length == 0:
return 0
cap = cv2.VideoCapture(video_path)
count = 0
video_id = rand_string(5)
video_filename = "".join(file_name_without_ext[:6]+video_id)
save_path = "temp/"+ video_filename
while True:
try:
fix = "".join(save_path+"/fix_frames")
raw = "".join(save_path+"/raw_frames")
os.makedirs(fix)
os.makedirs(raw)
print("Directory created for ", save_path)
break
except FileExistsError:
print("Directory " , save_path , " already exists, reseting id", end='\r')
video_id = rand_string(5)
video_filename = "".join(file_name_without_ext[:6]+video_id)
save_path = "temp/"+ video_filename
save_path = "temp/"+ video_filename +"/raw_frames/"
# test first frame
ret,frame = cap.read()
test_file_path = os.path.join(
save_path,
file_name_without_ext[:6]+\
'{}.jpg'.format(video_id, count))
cv2.imwrite(test_file_path, frame)
if os.path.isfile(test_file_path):
os.remove(test_file_path)
count = 1
while ret:
ret,frame = cap.read()
if ret and count % skip_frames == 0:
cv2.imwrite(os.path.join(
save_path,
file_name_without_ext[:6]+
'{}_{}.jpg'.format(video_id, count)
), frame)
count += 1
print("Generated {}/{} frames".format(count, length), end='\r')
else:
count += 1
else:
print("Test file cannot be saved")
return 0
return video_filename, count
cap.release()
def converge_frames(video_len, video_filename, fps):
frame_array = []
temp_path = 'temp/' + video_filename + '/fix_frames/'
files = [f for f, x in zip(os.listdir(temp_path), range(video_len)) if isfile(join(temp_path, video_filename + '_' + str(x) + '.jpg'))]
files.sort(key=lambda f: int(re. sub('\D', '', f)))
#print(files)
for i in range(len(files)):
filename = temp_path + files[i]
# reading files
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width,height)
print("Proccessed {}/{} frames of video".format(i, video_len), end='\r')
frame_array.append(img)
output_path = 'output/' + video_filename + '.mp4' # If video output error .avi may be a good option
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
for i in range(len(frame_array)):
# writing to image array
out.write(frame_array[i])
out.release()
return output_path
def colorize(video_len, video_filename):
for x in range(1, video_len):
raw_path = ''.join('temp/' + video_filename + '/raw_frames/'+video_filename+'_'+str(x)+'.jpg')
r = requests.post(
"https://api.deepai.org/api/colorizer",
files={
'image': open(raw_path, 'rb'),
},
headers={'api-key': 'b891cd87-6737-49ea-9fb1-44009b0daa78'}
)
response = r.json()
output_url = response['output_url']
download_img = requests.get(output_url)
fix_path = ''.join('temp/' + video_filename + '/fix_frames/'+video_filename+'_'+str(x)+'.jpg')
proccessed_frame = open(fix_path, "wb")
proccessed_frame.write(download_img.content)
proccessed_frame.close()
print("Colorized {}/{} frames".format(x, video_len), end='\r')
def clear_workspace(video_filename):
cur_path = "".join('temp/'+video_filename)
shutil.rmtree(cur_path)
def video_main(video):
video = str(video)
video.replace(" ", "_")
# print(video)
cur_video = cv2.VideoCapture(video)
fps = cur_video.get(cv2.CAP_PROP_FPS)
extraction_results = extract_frames(video, skip_frames=1)
video_filename = extraction_results[0]
frames_extracted = extraction_results[1]
print("\n")
colorize(frames_extracted, video_filename)
print("\n")
created_video = converge_frames(frames_extracted, video_filename, fps)
print("Cleaning workspace for ", video_filename)
#clear workspace
clear_workspace(video_filename)
print("Your video was colorized: ", created_video)
def image_main(image):
_, file_name = os.path.split(image)
file_name_without_ext = os.path.splitext(file_name)[0]
image_id = rand_string(5)
image_filename = "".join(file_name_without_ext[:6]+image_id)
print("Uploading...")
r = requests.post(
"https://api.deepai.org/api/colorizer",
files={
'image': open(image, 'rb'),
},
headers={'api-key': deepai_api_key}
)
response = r.json()
output_url = response['output_url']
download_img = requests.get(output_url)
print("Saving...")
fix_path = ''.join('output/' + image_filename + '.jpg')
proccessed_frame = open(fix_path, "wb")
proccessed_frame.write(download_img.content)
proccessed_frame.close()
return fix_path