-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored input and output and added progressbar
- Loading branch information
1 parent
97aa6fd
commit c689387
Showing
8 changed files
with
175 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
|
||
class BaseIO(): | ||
def __init__(self, output_path): | ||
self.data = None | ||
|
||
self.output_path = output_path | ||
|
||
def set_input(self): | ||
pass | ||
|
||
def feed_data(self, data): | ||
self.data = data | ||
|
||
def pad_data(self, num_padding): | ||
for _ in range(num_padding): | ||
self.data.insert(0, self.data[0]) | ||
self.data.append(self.data[-1]) | ||
|
||
def save_frames(self): | ||
pass | ||
|
||
def close(self): | ||
pass | ||
|
||
def __getitem__(self, idx): | ||
return self.data[idx] | ||
|
||
def __len__(self): | ||
return len(self.data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from utils.io_classes.base_io import BaseIO | ||
|
||
import numpy as np | ||
import cv2 | ||
import os | ||
|
||
|
||
class ImageIO(BaseIO): | ||
|
||
def __init__(self, output_path): | ||
super(ImageIO, self).__init__(output_path) | ||
|
||
self.count = 0 | ||
|
||
def set_input(self, input_folder): | ||
images = [] | ||
for root, _, files in os.walk(input_folder): | ||
for file in sorted(files): | ||
if file.split('.')[-1].lower() in ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'tga']: | ||
images.append(os.path.join(root, file)) | ||
self.feed_data(images) | ||
|
||
def save_frames(self, frames): | ||
if not isinstance(frames, list): | ||
frames = [frames] | ||
# TODO: Re-add ability to save with original name | ||
for img in frames: | ||
cv2.imwrite(os.path.join(self.output_path, | ||
f'{(self.count):08}.png'), img) | ||
self.count += 1 | ||
|
||
def __getitem__(self, idx): | ||
return cv2.imread(self.data[idx], cv2.IMREAD_COLOR) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from utils.io_classes.base_io import BaseIO | ||
|
||
import numpy as np | ||
import cv2 | ||
import ffmpeg | ||
|
||
|
||
class VideoIO(BaseIO): | ||
|
||
def __init__(self, output_path, scale, crf=0, exp=1): | ||
super(VideoIO, self).__init__(output_path) | ||
|
||
self.crf = crf | ||
self.scale = scale | ||
self.exp = exp | ||
|
||
self.process = None | ||
|
||
def set_input(self, input_video): | ||
# Grabs video metadata information | ||
probe = ffmpeg.probe(input_video) | ||
video_stream = next( | ||
(stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None) | ||
width = int(video_stream['width']) | ||
height = int(video_stream['height']) | ||
framerate = int(video_stream['r_frame_rate'].split( | ||
'/')[0]) / int(video_stream['r_frame_rate'].split('/')[1]) | ||
vcodec = 'libx264' | ||
|
||
# Imports video to buffer | ||
out, _ = ( | ||
ffmpeg | ||
.input(input_video) | ||
.output('pipe:', format='rawvideo', pix_fmt='rgb24') | ||
.global_args('-loglevel', 'error') | ||
.run(capture_stdout=True) | ||
) | ||
# Reads video buffer into numpy array | ||
video = ( | ||
np | ||
.frombuffer(out, np.uint8) | ||
.reshape([-1, height, width, 3]) | ||
) | ||
|
||
# Convert numpy array into frame list | ||
images = [] | ||
for i in range(video.shape[0]): | ||
frame = cv2.cvtColor(video[i], cv2.COLOR_RGB2BGR) | ||
images.append(frame) | ||
|
||
self.feed_data(images) | ||
|
||
# Open output file writer | ||
self.process = ( | ||
ffmpeg | ||
.input('pipe:', r=framerate*(self.exp**2), format='rawvideo', pix_fmt='rgb24', s='{}x{}'.format(width * self.scale, height * self.scale)) | ||
.output(self.output_path, pix_fmt='yuv420p', vcodec=vcodec, r=framerate*(self.exp**2), crf=self.crf, preset='veryfast') | ||
.global_args('-loglevel', 'error') | ||
.overwrite_output() | ||
.run_async(pipe_stdin=True) | ||
) | ||
|
||
def save_frames(self, frames): | ||
if not isinstance(frames, list): | ||
frames = [frames] | ||
for img in frames: | ||
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | ||
self.process.stdin.write( | ||
img | ||
.astype(np.uint8) | ||
.tobytes() | ||
) | ||
|
||
def close(self): | ||
self.process.stdin.close() | ||
self.process.wait() | ||
|
||
def __getitem__(self, idx): | ||
return self.data[idx] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.