video_to_image_sequence.ipynb 2.95 KB
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import random\n",
    "import cv2\n",
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "action = 'fight'\n",
    "base_dir = '/mnt/c/Users/hyerilee/Downloads/{}'.format(action)\n",
    "file_path = {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "for (root ,dirs, files) in os.walk(base_dir):\n",
    "    if len(dirs) == 0 and len(files) != 0:\n",
    "        if root not in file_path:\n",
    "            file_path[root] = files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getFrame(sec, saveFilePath, count):\n",
    "    vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)\n",
    "    hasFrames,img = vidcap.read()\n",
    "    if hasFrames:\n",
    "        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
    "        im_pil = Image.fromarray(img) # wsl에서 cv2.imwirte 안돼서 하는 작업. cv2 to pillow\n",
    "        if os.path.exists(saveFilePath) and count==1: #동일한 시작지점을 또 capture하는 경우 skip\n",
    "            return False\n",
    "        if not os.path.exists(saveFilePath):\n",
    "            os.makedirs(saveFilePath)\n",
    "        im_pil.save(saveFilePath+'/'+'{:03d}'.format(count)+\".jpg\")\n",
    "#         cv2.imwrite(saveFilePath+'{:03d}'.format(count)+\".jpg\", image)     # save frame as JPG file\n",
    "    return hasFrames\n",
    "\n",
    "starts = [i for i in range(180,291)] #가능시작지점\n",
    "for (file_dir, file_list) in file_path.items():\n",
    "    for video_file in file_list:\n",
    "        video_path = file_dir + '/' + video_file\n",
    "        vidcap = cv2.VideoCapture(video_path)\n",
    "        for i in range(0,10):\n",
    "            sec = random.choice(starts)\n",
    "            start_sec = sec\n",
    "            frameRate = 0.5 # it will capture image in each 1 second\n",
    "            cnt = 1\n",
    "            success = getFrame(sec, video_path[:-4]+'_start'+str(start_sec)+'s', cnt)\n",
    "            while success:\n",
    "                cnt += 1\n",
    "                sec += frameRate\n",
    "                success = getFrame(sec, video_path[:-4]+'_start'+str(start_sec)+'s', cnt)\n",
    "                if cnt >= 20:\n",
    "                    break"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}