Skip to content

Commit af614f6

Browse files
committed
Open-CV-Implementations
1 parent e9787e3 commit af614f6

File tree

2 files changed

+190
-0
lines changed

2 files changed

+190
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ This is a repository of code files which can be used to perform some basic tasks
6969
* [Face and Eye Detection](https://github.com/altruistcoder/Open-CV-Implementations/blob/master/face_and_eye_detection.ipynb):
7070

7171
Python Code to detect faces and eyes present in a video using OpenCV.
72+
73+
* [Taking Timelapse](https://github.com/altruistcoder/Open-CV-Implementations/blob/master/timelapse.ipynb):
74+
75+
Python Code to take a Timelapse Video using OpenCV.

timelapse.ipynb

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import os\n",
10+
"import numpy as np\n",
11+
"import cv2\n",
12+
"import time\n",
13+
"import datetime\n",
14+
"import glob"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 2,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):\n",
24+
" dim = None\n",
25+
" (h, w) = image.shape[:2]\n",
26+
" if width is None and height is None:\n",
27+
" return image\n",
28+
" if width is None:\n",
29+
" r = height / float(h)\n",
30+
" dim = (int(w * r), height)\n",
31+
" else:\n",
32+
" r = width / float(w)\n",
33+
" dim = (width, int(h * r))\n",
34+
"\n",
35+
" resized_image = cv2.resize(image, dim, interpolation = inter)\n",
36+
" return resized_image\n",
37+
"\n",
38+
"\n",
39+
"class MyVideoConf(object):\n",
40+
" STD_DIMENSIONS = {\n",
41+
" \"360p\": (480, 360),\n",
42+
" \"480p\": (640, 480),\n",
43+
" \"720p\": (1280, 720),\n",
44+
" \"1080p\": (1920, 1080),\n",
45+
" \"4k\": (3840, 2160),\n",
46+
" }\n",
47+
" # Types of Codes: http://www.fourcc.org/codecs.php\n",
48+
" VIDEO_TYPE = {\n",
49+
" 'avi': cv2.VideoWriter_fourcc(*'XVID'),\n",
50+
" 'mp4': cv2.VideoWriter_fourcc(*'XVID'),\n",
51+
" }\n",
52+
" width = 640\n",
53+
" height = 480\n",
54+
" dims = (640, 480)\n",
55+
" capture = None\n",
56+
" video_type = None\n",
57+
" def __init__(self, capture, filepath, res=\"480p\", *args, **kwargs):\n",
58+
" self.capture = capture\n",
59+
" self.filepath = filepath\n",
60+
" self.width, self.height = self.get_dims(res=res)\n",
61+
" self.video_type = self.get_video_type()\n",
62+
" \n",
63+
" def change_res(self, width, height):\n",
64+
" self.capture.set(3, width)\n",
65+
" self.capture.set(4, height)\n",
66+
"\n",
67+
" def get_dims(self, res='480p'):\n",
68+
" width, height = self.STD_DIMENSIONS['480p']\n",
69+
" if res in self.STD_DIMENSIONS:\n",
70+
" width, height = self.STD_DIMENSIONS[res]\n",
71+
" self.change_res(width, height)\n",
72+
" self.dims = (width, height)\n",
73+
" return width, height\n",
74+
"\n",
75+
" def get_video_type(self):\n",
76+
" filename, ext = os.path.splitext(self.filepath)\n",
77+
" if ext in self.VIDEO_TYPE:\n",
78+
" return self.VIDEO_TYPE[ext]\n",
79+
" return self.VIDEO_TYPE['avi']"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": 4,
85+
"metadata": {},
86+
"outputs": [],
87+
"source": [
88+
"cap = cv2.VideoCapture(0)\n",
89+
"\n",
90+
"frames_per_seconds = 20\n",
91+
"save_path = 'timelapse.avi'\n",
92+
"config = MyVideoConf(cap, filepath=save_path, res='720p')\n",
93+
"out = cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims)\n",
94+
"\n",
95+
"seconds_duration = 50\n",
96+
"seconds_between_shots = 5\n",
97+
"timelapse_img_dir = \"images_timelapse/\"\n",
98+
"\n",
99+
"if not os.path.exists(timelapse_img_dir):\n",
100+
" os.mkdir(timelapse_img_dir)\n",
101+
"\n",
102+
"now = datetime.datetime.now()\n",
103+
"finish_time = now + datetime.timedelta(seconds=seconds_duration)\n",
104+
"\n",
105+
"i = 0\n",
106+
"\n",
107+
"while datetime.datetime.now() < finish_time:\n",
108+
" ret, frame = cap.read()\n",
109+
" filename = \"{0}/{1}.jpg\".format(timelapse_img_dir, i)\n",
110+
" i += 1\n",
111+
" cv2.imwrite(filename, frame)\n",
112+
" cv2.imshow('frame', frame)\n",
113+
" time.sleep(seconds_between_shots)\n",
114+
" if cv2.waitKey(20) & 0xFF == ord('q'):\n",
115+
" break\n",
116+
"\n",
117+
"clear_images = True\n",
118+
"\n",
119+
"def images_to_video(out, image_dir, clear_images=True):\n",
120+
" image_list = glob.glob(\"{0}/*.jpg\".format(image_dir))\n",
121+
" sorted_images = sorted(image_list, key=os.path.getmtime)\n",
122+
" for file in sorted_images:\n",
123+
" image_frame = cv2.imread(file)\n",
124+
" out.write(image_frame)\n",
125+
" if clear_images:\n",
126+
" # Remove stored timelapse images\n",
127+
" for file in image_list:\n",
128+
" os.remove(file)\n",
129+
"\n",
130+
"images_to_video(out, timelapse_img_dir, clear_images=True)\n",
131+
"\n",
132+
"cap.release()\n",
133+
"cv2.destroyAllWindows()"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": null,
139+
"metadata": {},
140+
"outputs": [],
141+
"source": []
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": null,
146+
"metadata": {},
147+
"outputs": [],
148+
"source": []
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": null,
153+
"metadata": {},
154+
"outputs": [],
155+
"source": []
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": null,
160+
"metadata": {},
161+
"outputs": [],
162+
"source": []
163+
}
164+
],
165+
"metadata": {
166+
"kernelspec": {
167+
"display_name": "Python 3",
168+
"language": "python",
169+
"name": "python3"
170+
},
171+
"language_info": {
172+
"codemirror_mode": {
173+
"name": "ipython",
174+
"version": 3
175+
},
176+
"file_extension": ".py",
177+
"mimetype": "text/x-python",
178+
"name": "python",
179+
"nbconvert_exporter": "python",
180+
"pygments_lexer": "ipython3",
181+
"version": "3.5.2"
182+
}
183+
},
184+
"nbformat": 4,
185+
"nbformat_minor": 2
186+
}

0 commit comments

Comments
 (0)