diff --git a/2018-Python-Practice-master/main.py b/2018-Python-Practice-master/main.py index 9d8e7df..902ea6d 100644 --- a/2018-Python-Practice-master/main.py +++ b/2018-Python-Practice-master/main.py @@ -1,72 +1,78 @@ -import logging -import sys -from datetime import datetime - -import tensorflow as tf - -''' -1. import `Video` and `save_video` from the correct module of package "styler" -''' -from ... import Video -from ... import save_video - - -model_file = 'data/vg-30.pb' -model_name = 'vg-30' -logging.basicConfig( - stream=sys.stdout, - format='%(asctime)s %(levelname)s:%(message)s', - level=logging.INFO, - datefmt='%I:%M:%S') - - -def main(): - - with open(model_file, 'rb') as f: - graph_def = tf.GraphDef() - graph_def.ParseFromString(f.read()) - tf.import_graph_def(graph_def) - graph = tf.get_default_graph() - - with tf.Session(config=tf.ConfigProto( - intra_op_parallelism_threads=4)) as session: - - logging.info("Initializing graph") - session.run(tf.global_variables_initializer()) - - image = graph.get_tensor_by_name("import/%s/image_in:0" % model_name) - out = graph.get_tensor_by_name("import/%s/output:0" % model_name) - shape = image.get_shape().as_list() - - ''' - 2. set the `path` to your input - ''' - with Video(...) as v: - frames = v.read_frames(image_h=shape[1], image_w=shape[2]) - - logging.info("Processing image") - start_time = datetime.now() - - ''' - 3. Write a list comprehension to iterate through all frames, - and make it be processed by Tensorflow. - ''' - processed = [ - session.run(out, feed_dict={image: [frame]}) - ... - ] - - ''' - 4. Pass the results as a argument into function - ''' - save_video('result.mp4', - fps=30, h=shape[1], w=shape[2], - frames=...) - - logging.info("Processing took %f" % ( - (datetime.now() - start_time).total_seconds())) - logging.info("Done") - - -if __name__ == '__main__': - main() +import logging +import sys +from datetime import datetime + +import tensorflow as tf + + + +''' +1. import `Video` and `save_video` from the correct module of package "styler" +''' +from styler.video import Video +from styler.utils import save_video + + +model_file = 'data/vg-30.pb' +model_name = 'vg-30' +logging.basicConfig( + stream=sys.stdout, + format='%(asctime)s %(levelname)s:%(message)s', + level=logging.INFO, + datefmt='%I:%M:%S') + + +def main(): + + with open(model_file, 'rb') as f: + graph_def = tf.GraphDef() + graph_def.ParseFromString(f.read()) + tf.import_graph_def(graph_def) + graph = tf.get_default_graph() + + with tf.Session(config=tf.ConfigProto( + intra_op_parallelism_threads=4)) as session: + + logging.info("Initializing graph") + session.run(tf.global_variables_initializer()) + + image = graph.get_tensor_by_name("import/%s/image_in:0" % model_name) + out = graph.get_tensor_by_name("import/%s/output:0" % model_name) + shape = image.get_shape().as_list() + + + ''' + 2. set the `path` to your input + ''' + with Video('input/jaguar.mp4') as v: + frames = v.read_frames(image_h=shape[1], image_w=shape[2]) + + + logging.info("Processing image") + start_time = datetime.now() + + ''' + 3. Write a list comprehension to iterate through all frames, + and make it be processed by Tensorflow. + ''' + + processed = [ + session.run(out, feed_dict={image: [frame]}) + for frame in frames + ] + + + ''' + 4. Pass the results as a argument into function + ''' + save_video('result.mp4', + fps=30, h=shape[1], w=shape[2], + frames=processed) + + logging.info("Processing took %f" % ( + (datetime.now() - start_time).total_seconds())) + logging.info("Done") + + +if __name__ == '__main__': + main() diff --git a/2018-Python-Practice-master/result.mp4 b/2018-Python-Practice-master/result.mp4 new file mode 100644 index 0000000..6bc3902 Binary files /dev/null and b/2018-Python-Practice-master/result.mp4 differ diff --git a/2018-Python-Practice-master/styler/__pycache__/utils.cpython-35.pyc b/2018-Python-Practice-master/styler/__pycache__/utils.cpython-35.pyc new file mode 100644 index 0000000..f26a68d Binary files /dev/null and b/2018-Python-Practice-master/styler/__pycache__/utils.cpython-35.pyc differ diff --git a/2018-Python-Practice-master/styler/__pycache__/video.cpython-35.pyc b/2018-Python-Practice-master/styler/__pycache__/video.cpython-35.pyc new file mode 100644 index 0000000..36a8b2b Binary files /dev/null and b/2018-Python-Practice-master/styler/__pycache__/video.cpython-35.pyc differ diff --git a/2018-Python-Practice-master/styler/utils.py b/2018-Python-Practice-master/styler/utils.py index c4dcb5c..01db707 100644 --- a/2018-Python-Practice-master/styler/utils.py +++ b/2018-Python-Practice-master/styler/utils.py @@ -1,48 +1,48 @@ -import cv2 -import numpy as np - -MEAN_VALUES = np.array([123, 117, 104]).reshape((1,1,3)) - - -def resize(img, image_h, image_w, zoom=False): - # crop image from center - ratio = float(image_h) / image_w - height, width = int(img.shape[0]), int(img.shape[1]) - yy, xx = 0, 0 - if height > width * ratio: #too tall - yy = int(height - width * ratio) // 2 - height = int(width * ratio) - else: # too wide - xx = int(width - height / ratio) // 2 - width = int(height / ratio) - if zoom: - yy += int(height / 6) - xx += int(height / 6) - height = int(height * 2 / 3) - width = int(width * 2 / 3) - crop_img = img[yy:yy + height, xx:xx + width] - # resize - resized_img = cv2.resize(crop_img, (image_h, image_w)) - centered_img = resized_img - MEAN_VALUES - - return centered_img - - -def save_video(filepath, fps, w, h, frames): - codecs = ['WMV1', 'MJPG', 'XVID', 'PIM1'] - ''' - If you cannot write video file, you may change the used codec - ''' - used_codec = codecs[2] # change the index from codecs - fourcc = cv2.VideoWriter_fourcc(*used_codec) - out = cv2.VideoWriter(filepath, fourcc, fps, (w, h)) - for frame in frames: - f = frame[0, :, :, :] - out.write(post_process(f)) - out.release() - - -def post_process(image): - img = image + MEAN_VALUES - img = np.clip(img, 0, 255).astype('uint8') - return cv2.cvtColor(img, cv2.COLOR_RGB2BGR) +import cv2 +import numpy as np + +MEAN_VALUES = np.array([123, 117, 104]).reshape((1,1,3)) + + +def resize(img, image_h, image_w, zoom=False): + # crop image from center + ratio = float(image_h) / image_w + height, width = int(img.shape[0]), int(img.shape[1]) + yy, xx = 0, 0 + if height > width * ratio: #too tall + yy = int(height - width * ratio) // 2 + height = int(width * ratio) + else: # too wide + xx = int(width - height / ratio) // 2 + width = int(height / ratio) + if zoom: + yy += int(height / 6) + xx += int(height / 6) + height = int(height * 2 / 3) + width = int(width * 2 / 3) + crop_img = img[yy:yy + height, xx:xx + width] + # resize + resized_img = cv2.resize(crop_img, (image_h, image_w)) + centered_img = resized_img - MEAN_VALUES + + return centered_img + + +def save_video(filepath, fps, w, h, frames): + codecs = ['WMV1', 'MJPG', 'XVID', 'PIM1'] + ''' + If you cannot write video file, you may change the used codec + ''' + used_codec = codecs[2] # change the index from codecs + fourcc = cv2.VideoWriter_fourcc(*used_codec) + out = cv2.VideoWriter(filepath, fourcc, fps, (w, h)) + for frame in frames: + f = frame[0, :, :, :] + out.write(post_process(f)) + out.release() + + +def post_process(image): + img = image + MEAN_VALUES + img = np.clip(img, 0, 255).astype('uint8') + return cv2.cvtColor(img, cv2.COLOR_RGB2BGR) diff --git a/2018-Python-Practice-master/styler/video.py b/2018-Python-Practice-master/styler/video.py index deb57be..38eed19 100644 --- a/2018-Python-Practice-master/styler/video.py +++ b/2018-Python-Practice-master/styler/video.py @@ -1,36 +1,43 @@ -import cv2 - -from styler.utils import resize - - -class Video: - - def __init__(self, path): - self.path = path - self.cap = cv2.VideoCapture(self.path) - self.frames = [] - - def __enter__(self): - if not self.cap.isOpened(): - raise Exception('Cannot open video: {}'.format(self.path)) - return self - - def __len__(self): - return len(self.frames) - - def read_frames(self, image_h, image_w): - ''' - 5. - - Read video frames from `self.cap` and collect frames into list - - Apply `resize()` on each frame before add it to list - - Also assign frames to "self" object - - Return your results - ''' - frames = [] - # 5-1 /5-2 Read video and collect them - - self.frames = ... # 5-3 let object have the result - return ... # return your results - - def __exit__(self, exc_type, exc_val, exc_tb): - self.cap.release() +import cv2 + +from styler.utils import resize + + +class Video: + + def __init__(self, path): + self.path = path + self.cap = cv2.VideoCapture(self.path) + self.frames = [] + + def __enter__(self): + if not self.cap.isOpened(): + raise Exception('Cannot open video: {}'.format(self.path)) + return self + + def __len__(self): + return len(self.frames) + + def read_frames(self, image_h, image_w): + ''' + 5. + - Read video frames from `self.cap` and collect frames into list + - Apply `resize()` on each frame before add it to list + - Also assign frames to "self" object + - Return your results + ''' + frames = [] + # 5-1 /5-2 Read video and collect them + while(True): + ret, frame = self.cap.read() + + if(frame is None): + break + res = resize(frame,image_h, image_w); + frames.append(res) + + self.frames = frames # 5-3 let object have the result + return self.frames # return your results + + def __exit__(self, exc_type, exc_val, exc_tb): + self.cap.release() diff --git a/2018-Python-Practice-master/tmp.mp4 b/2018-Python-Practice-master/tmp.mp4 new file mode 100644 index 0000000..6bc3902 Binary files /dev/null and b/2018-Python-Practice-master/tmp.mp4 differ diff --git a/README.md b/README.md index be7c870..c4fa1b0 100644 --- a/README.md +++ b/README.md @@ -1,36 +1,36 @@ -# 2018-Python-Practice -The practice is provided by Hung-Jin Lin(leVirve @Github) - -Apply deep-style-transfer model onto video! -(The video can choose by your own, or you can use the sample video provided in the sample code) - -## Requirements: -- Tensorflow (CPU version) -```bash -pip install tensorflow -``` -- OpenCV - - Windows: with Python 3.5 (x64) - - `pip install opencv-python` - - Mac: - - Install through `brew` - - Linux: - - Build by your own or use other pre-build - -## Homeworks -1. Import `Video` and `save_video` from the correct module of package `styler` -2. Find and set the input video `path` in `Line#44` -3. Write a list comprehension to iterate through all frames, and make it be processed by Tensorflow. -4. Pass the results as a argument into function -5. Modify the class method `read_frames()` in `styler/video.py` - - Read video frames from `self.cap` and collect frames into list - - Apply `resize()` on each frame before add it to list - - Also assign frames to "self" object - - Return your results - -## Note: -If you can not save the output, you may try to change the codec used by changing the codec index in `styler/utils.py` Line#36. - - -## Reference: -We use the trained model and code in [deep-style-transfer](https://github.com/albertlai/deep-style-transfer). +# 2018-Python-Practice +The practice is provided by Hung-Jin Lin(leVirve @Github) + +Apply deep-style-transfer model onto video! +(The video can choose by your own, or you can use the sample video provided in the sample code) + +## Requirements: +- Tensorflow (CPU version) +```bash +pip install tensorflow +``` +- OpenCV + - Windows: with Python 3.5 (x64) + - `pip install opencv-python` + - Mac: + - Install through `brew` + - Linux: + - Build by your own or use other pre-build + +## Homeworks +1. Import `Video` and `save_video` from the correct module of package `styler` +2. Find and set the input video `path` in `Line#44` +3. Write a list comprehension to iterate through all frames, and make it be processed by Tensorflow. +4. Pass the results as a argument into function +5. Modify the class method `read_frames()` in `styler/video.py` + - Read video frames from `self.cap` and collect frames into list + - Apply `resize()` on each frame before add it to list + - Also assign frames to "self" object + - Return your results + +## Note: +If you can not save the output, you may try to change the codec used by changing the codec index in `styler/utils.py` Line#36. + + +## Reference: +We use the trained model and code in [deep-style-transfer](https://github.com/albertlai/deep-style-transfer).