Skip to content

Commit

Permalink
generate videos
Browse files Browse the repository at this point in the history
  • Loading branch information
chanwutk committed Nov 3, 2023
1 parent d1a7738 commit b228563
Show file tree
Hide file tree
Showing 3 changed files with 15,794 additions and 0 deletions.
253 changes: 253 additions & 0 deletions evaluation/ablation/longvideo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 21,
"id": "7f78c96d-1a45-4ced-8c65-d7c775992139",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import os\n",
"import json\n",
"import pickle\n",
"from tqdm import tqdm"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7267f20a-3b2f-4173-b613-ae36a0cd2505",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 9,
"id": "b2a726fa-f53a-4f86-bd1a-102f8371f6e9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/data/chanwutk/projects/spatialyze-ablation\n"
]
}
],
"source": [
"\n",
"def is_notebook() -> bool:\n",
" try:\n",
" shell = get_ipython().__class__.__name__\n",
" if shell == 'ZMQInteractiveShell':\n",
" # Jupyter notebook or qtconsole\n",
" return True\n",
" elif shell == 'TerminalInteractiveShell':\n",
" # Terminal running IPython\n",
" return False\n",
" else:\n",
" # Other type (?)\n",
" return False\n",
" except NameError:\n",
" # Probably standard Python interpreter\n",
" return False\n",
"\n",
"\n",
"if is_notebook():\n",
" %cd ../..\n",
" from tqdm.notebook import tqdm\n",
" from nbutils.report_progress import report_progress\n",
"else:\n",
" from tqdm import tqdm\n",
" from evaluation.ablation.nbutils.report_progress import report_progress"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "fa64ff96-ad9a-482f-8c17-8db1ffce043f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"/data/apperception-data/processed/nuscenes/full-dataset-v1.0/Trainval\n"
]
}
],
"source": [
"NUSCENES_PROCESSED_DATA = \"NUSCENES_PROCESSED_DATA\"\n",
"print(NUSCENES_PROCESSED_DATA in os.environ)\n",
"print(os.environ['NUSCENES_PROCESSED_DATA'])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "ebc0dbe2-8236-4ed9-a048-3d64d456a094",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'scene-0001-CAM_FRONT'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"DATA_DIR = os.environ[NUSCENES_PROCESSED_DATA]\n",
"# with open(os.path.join(DATA_DIR, \"videos\", \"frames.pkl\"), \"rb\") as f:\n",
"# videos = pickle.load(f)\n",
"with open(os.path.join(DATA_DIR, 'videos', 'videos.json'), 'r') as f:\n",
" videos = json.load(f)\n",
"type(videos)\n",
"videos[0]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "fc2bbf71-3aa0-4d2a-a9b5-a6d27097a043",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0523 0778 467\n"
]
}
],
"source": [
"\n",
"with open('./data/evaluation/video-samples/boston-seaport.txt', 'r') as f:\n",
" sampled_scenes = f.read().split('\\n')\n",
"print(sampled_scenes[0], sampled_scenes[-1], len(sampled_scenes))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "c8a069a6-0bdc-4bc4-afc6-b96132306775",
"metadata": {},
"outputs": [],
"source": [
"with open(os.path.join(DATA_DIR, 'videos', 'boston-seaport-scene-' + sampled_scenes[0] + '-CAM_FRONT.pkl'), 'rb') as f:\n",
" video = pickle.load(f)\n",
"video_filename = video['filename']"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "03af58b4-db6d-40d4-b3ab-b9429d73056d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'boston-seaport-scene-0523-CAM_FRONT.mp4'"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"video_filename"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "38f3f36b-e381-4de7-81f9-fdc70810c271",
"metadata": {},
"outputs": [],
"source": [
"frames = []\n",
"cap = cv2.VideoCapture(os.path.join(DATA_DIR, 'videos', video_filename))\n",
"while cap.isOpened():\n",
" ret, frame = cap.read()\n",
" if not ret:\n",
" break\n",
" frames.append(frame)\n",
"cap.release()\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "5f1b5b82-9ee4-459e-86f3-1fc863ae9502",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 2%|█▊ | 23/1000 [01:08<48:42, 2.99s/it]\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[28], line 6\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m tqdm(\u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m1000\u001b[39m)):\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m frame \u001b[38;5;129;01min\u001b[39;00m frames:\n\u001b[0;32m----> 6\u001b[0m \u001b[43mresult\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwrite\u001b[49m\u001b[43m(\u001b[49m\u001b[43mframe\u001b[49m\u001b[43m)\u001b[49m \n\u001b[1;32m 7\u001b[0m result\u001b[38;5;241m.\u001b[39mrelease() \n\u001b[1;32m 8\u001b[0m cv2\u001b[38;5;241m.\u001b[39mdestroyAllWindows()\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"result = cv2.VideoWriter('./loop-video.avi', \n",
" cv2.VideoWriter_fourcc(*'MJPG'), \n",
" 12, (1600, 900)) \n",
"for i in tqdm(range(2000)):\n",
" for frame in frames:\n",
" result.write(frame) \n",
"result.release() \n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9e4c5eb-7121-41a0-88ea-a742666695e6",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit b228563

Please sign in to comment.