{ "cells": [ { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import os\n", "import time" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "class Object(object):\n", " pass" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def extract_images(video_path, save_path):\n", " dsize = (256, 256)\n", " video_cap = cv2.VideoCapture(video_path)\n", " success, image = video_cap.read()\n", " frame_count = 0\n", " while success:\n", " frame_save_path = os.path.join(save_path, 'img{0}.jpg'.format(str(frame_count).zfill(6)))\n", " #do pseudocoloring\n", " cv2.imwrite(frame_save_path, cv2.applyColorMap(image, cv2.COLORMAP_JET))\n", " #resize image to 256*256\n", " output = cv2.resize(image, dsize)\n", " cv2.imwrite(frame_save_path, output)\n", " success, image = video_cap.read()\n", " frame_count +=1\n", " # count frames for each video\n", " with open(os.path.join(save_path, 'n_frames'), 'w') as file:\n", " file.write(str(frame_count))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "extract_images('C:\\\\Users\\\\zxasv\\\\Pictures\\\\Camera Roll\\\\video\\\\WIN_20210611_01_17_15_Pro.mp4', 'C:\\\\Users\\\\zxasv\\\\Pictures\\\\Camera Roll\\\\fr')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def main(opt):\n", " class_folders = os.listdir(opt.video_root_directory_path)\n", " for class_folder in class_folders:\n", " class_name = '_'.join(class_folder.lower().split(' '))\n", " class_save_path = os.path.join(opt.save_root_directory_path, class_name)\n", " if not os.path.exists(class_save_path):\n", " os.makedirs(class_save_path)\n", "\n", " current_class_video_path = os.path.join(opt.video_root_directory_path, class_folder)\n", " current_video_list = os.listdir(current_class_video_path)\n", "\n", " num_video = 0\n", " for video in current_video_list:\n", " video_source_path = os.path.join(current_class_video_path, video)\n", " video_save_path = os.path.join(class_save_path, '{0}'.format((video.split('.')[0])))\n", " if not os.path.exists(video_save_path):\n", " os.makedirs(video_save_path)\n", " # Раскадровка\n", " extract_images(video_source_path, video_save_path)\n", " " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Storyboard started...\n" ] }, { "ename": "NotADirectoryError", "evalue": "[WinError 267] Неверно задано имя папки: 'C:\\\\Users\\\\zxasv\\\\Pictures\\\\Camera Roll\\\\video\\\\WIN_20210610_17_48_26_Pro.mp4'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNotADirectoryError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Storyboard started...'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mtotal_start\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mopt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Total time: '\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mround\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mtotal_start\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m/\u001b[0m \u001b[1;36m60\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m' minutes'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Storyboard ended success!'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m\u001b[0m in \u001b[0;36mmain\u001b[1;34m(opt)\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[0mcurrent_class_video_path\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mopt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvideo_root_directory_path\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mclass_folder\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mcurrent_video_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlistdir\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcurrent_class_video_path\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 11\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 12\u001b[0m \u001b[0mnum_video\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mNotADirectoryError\u001b[0m: [WinError 267] Неверно задано имя папки: 'C:\\\\Users\\\\zxasv\\\\Pictures\\\\Camera Roll\\\\video\\\\WIN_20210610_17_48_26_Pro.mp4'" ] } ], "source": [ "opt = Object()\n", "opt.video_root_directory_path = ''\n", "opt.save_root_directory_path = ''\n", "print('Storyboard started...')\n", "total_start = time.time()\n", "main(opt)\n", "print('Total time: ' + str(round((time.time() - total_start) / 60)) + ' minutes')\n", "print('Storyboard ended success!')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Storyboard started...Total time: 73 minutesStoryboard ended success!'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Storyboard started... \\\n", "Total time: 73 minutes \\\n", "Storyboard ended success!'" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Storyboard started... Total time: 58 minutes Storyboard ended success!'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Storyboard started... \\\n", "Total time: 58 minutes \\\n", "Storyboard ended success!'" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Storyboard started...Total time: 22 minutesStoryboard ended success!'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Storyboard started... \\\n", "Total time: 22 minutes \\\n", "Storyboard ended success!'" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Storyboard started... Total time: 17 minutes Storyboard ended success!'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Storyboard started... \\\n", "Total time: 17 minutes \\\n", "Storyboard ended success!'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.17" } }, "nbformat": 4, "nbformat_minor": 4 }