{ "cells": [ { "cell_type": "code", "execution_count": 13, "metadata": { "tags": [], "ExecuteTime": { "end_time": "2024-04-02T19:20:43.810656Z", "start_time": "2024-04-02T19:20:43.798646Z" } }, "outputs": [], "source": [ "import cv2\n", "import os\n", "import time\n", "import shutil\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "tags": [], "ExecuteTime": { "end_time": "2024-04-02T19:20:44.354065Z", "start_time": "2024-04-02T19:20:44.350075Z" } }, "outputs": [], "source": [ "class Object(object):\n", " pass" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "tags": [], "ExecuteTime": { "end_time": "2024-04-02T19:20:44.905729Z", "start_time": "2024-04-02T19:20:44.886725Z" } }, "outputs": [], "source": [ "def extract_images(video_path, save_path):\n", " dsize = (512, 512)\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 (512, 512)\n", " # output = cv2.resize(image, dsize)\n", " cv2.imwrite(frame_save_path, image)\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": 16, "metadata": { "tags": [], "ExecuteTime": { "end_time": "2024-04-02T19:20:45.440615Z", "start_time": "2024-04-02T19:20:45.423612Z" } }, "outputs": [], "source": [ "def split_to_train_test(root_directory_path, frames = 'frames_1'):\n", " frames_path = os.path.join(root_directory_path, frames)\n", " print(frames_path)\n", " current_list = os.listdir(frames_path)\n", " train_frame_count = 0\n", " test_frame_count = 0\n", "\n", " train_save_path = os.path.join(root_directory_path, 'train')\n", " if not os.path.exists(train_save_path):\n", " os.makedirs(train_save_path)\n", "\n", " test_save_path = os.path.join(root_directory_path, 'test')\n", " if not os.path.exists(test_save_path):\n", " os.makedirs(test_save_path)\n", "\n", " for cur_frames in current_list:\n", " cur_frames_path = os.path.join(frames_path, cur_frames)\n", " n_frames = 0\n", " with open(os.path.join(cur_frames_path, 'n_frames'), 'r') as file:\n", " n_frames = int(file.readline())\n", "\n", " test = round(n_frames / 3)\n", " train = n_frames - test\n", "\n", " for i in range(train):\n", " frame_path = os.path.join(cur_frames_path, 'img{0}.jpg'.format(str(i).zfill(6)))\n", " frame_save_path = os.path.join(train_save_path, 'img{0}.jpg'.format(str(train_frame_count).zfill(6)))\n", " shutil.copyfile(frame_path, frame_save_path)\n", " train_frame_count +=1\n", "\n", " for i in range(train, n_frames):\n", " frame_path = os.path.join(cur_frames_path, 'img{0}.jpg'.format(str(i).zfill(6)))\n", " frame_save_path = os.path.join(test_save_path, 'img{0}.jpg'.format(str(test_frame_count).zfill(6)))\n", " shutil.copyfile(frame_path, frame_save_path)\n", " test_frame_count +=1\n", "\n", " with open(os.path.join(train_save_path, 'n_frames'), 'w') as file:\n", " file.write(str(train_frame_count)) \n", "\n", " with open(os.path.join(test_save_path, 'n_frames'), 'w') as file:\n", " file.write(str(test_frame_count)) " ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "tags": [], "ExecuteTime": { "end_time": "2024-04-02T19:20:47.327649Z", "start_time": "2024-04-02T19:20:47.307015Z" } }, "outputs": [], "source": [ "def main(opt):\n", "\n", " current_video_list = os.listdir(opt.video_root_directory_path)\n", " num_video = 0\n", " for video in current_video_list:\n", " video_source_path = os.path.join(opt.video_root_directory_path, video)\n", " video_save_path = os.path.join(opt.save_root_directory_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", " # деление на тренировочную и тестовую выборки\n", " split_to_train_test(opt.root_directory_path)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "tags": [], "ExecuteTime": { "end_time": "2024-04-02T19:29:45.945320Z", "start_time": "2024-04-02T19:20:51.982552Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Storyboard started...\n", "D:\\current\\lepidicid\\dataset\\frames_1\n", "D:\\current\\lepidicid\\dataset\\frames_1\n", "Total time: 9 minutes\n", "Storyboard ended success!\n" ] } ], "source": [ "opt = Object()\n", "opt.video_root_directory_path = r'D:\\current\\lepidicid\\dataset\\video_1'\n", "opt.save_root_directory_path = r'D:\\current\\lepidicid\\dataset\\frames_1'\n", "opt.root_directory_path = r'D:\\current\\lepidicid\\dataset'\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": { "ExecuteTime": { "start_time": "2024-04-02T19:20:40.621444Z" } }, "outputs": [], "source": [ "'Storyboard started... \\\n", "Total time: 73 minutes \\\n", "Storyboard ended success!'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2024-04-02T19:20:40.601378Z" } }, "outputs": [], "source": [ "'Storyboard started... \\\n", "Total time: 58 minutes \\\n", "Storyboard ended success!'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2024-04-02T19:20:40.626462Z" } }, "outputs": [], "source": [ "'Storyboard started... \\\n", "Total time: 22 minutes \\\n", "Storyboard ended success!'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2024-04-02T19:20:40.629443Z" } }, "outputs": [], "source": [ "'Storyboard started... \\\n", "Total time: 17 minutes \\\n", "Storyboard ended success!'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "start_time": "2024-04-02T19:20:40.630442Z" } }, "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.18" } }, "nbformat": 4, "nbformat_minor": 4 }