Ney Mello's Blog

Timelapse with Raspberry Pi and Python

Taking Pictures

First, we need some pictures in order to create our video.
To do that, we will use Open CV and a web cam connected to Raspberry Pi.

Photo Taker

After settup the Rapsberry Pi and the camera, we should create a script to take pictures at some interval of time.

import cv2
import time

# Initiate the VideoCapture with the number of the camera
# In our case: /dev/video0 -> camera = 0
camera = cv2.VideoCapture(0)

# Get an image from the camera
# the Read function return a tuple
# First value says with the image was successfully read
# Second value is the image we want
ret,frame = camera.read()

# Lets make sure that the image is good
while ret == False:
	ret,frame = camera.read()

# Save the image into a file
filePath = "./photos/%d.png" % int(time.time())
cv2.imwrite(filePath,frame)

# Realease the camera
del(camera)

Add the script to crontab. You can decide the interval of your photos.

# To edit the contrab
crontab -e

# The script will be called every 5 minutes
*/5 * * * * /usr/bin/python /path/to/script/photoTaker.py

Video Generator

Now it is time to put all photos together in a video.

import numpy
from PIL import Image
import cv2
import os

# Define the size of the video
# Must be the same size of the photos
width,height = (640, 480)

# Define the codec
fourcc = cv2.cv.CV_FOURCC('P','I','M','1')
# Create the video pointer
# Params  (file_name,code,frames_per_second,size)
video = cv2.VideoWriter("timelapse.avi",fourcc,20,(width,height))

# List all photos 
for i in os.listdir("photos"):
	# Open the file
	im1 = Image.open("photos/"+i)
	# Convert to NumPY array
	frame = numpy.array(im1)

	# Add into the video
	video.write(frame)

# Release the video
video.release()

Result

A timelapse of a autumn day on Arlington, VA.

Downloads and more

Links to raw files:

References

Checking if a binary tree is balanced

Tree

A tree is a data structure that its visualization resembles a real tree. However, we call the top of the tree as root!

Tree

Tree

In this post, we will work with a binary tree which is a subtype of  tree data structure.  In order to be a binary tree, which node of the tree must have up to two other nodes (one left and one right).  We also can assume that which left and right site of the node are a another binary tree.

BinaryTree

BinaryTree

Balanced Binary Tree

That being said, let’s define what a balanced binary tree is.

According to Gayle McDowell on her book Cracking the Coding Interview, a balanced binary tree is a tree that, for each level, the difference between the depths of the subtrees should not be more than one.

So, basically, our function will call itself recursively for each node checking the depth of each subtree.

def isBalanced(self,root):
	# Check if root is null
	if root == None:
		return 0

	# Calculate the depth of the left subtree (recursively)
	depthLeft = self.isBalanced(root.left)

	# Calculate the depth of the right subtree (recursively)
	depthRight = self.isBalanced(root.right)

	# Return is -1 (not balanced) if -1 if found in some place
	# during the call
	if depthLeft == -1 or depthRight == -1:
		return -1

	# If the difference is greater than 1, return -1
	if (depthLeft-depthRight) > 1:
		return -1

	# Otherwise return the max depth
	return max(depthLeft+1,depthRight+1)

Node Class

In this example, we used a class to define a Node.

class node:
	# Initiate an instance of Node
	def __init__(self,value,left=None,right=None):
		self.value = value
		self.left = left
		self.right = right

	def __setitem__(self,index,value):
		self[index] = value

	def __getitem__(self,index):
		return self[index]

	# Print the instance's value
	def __str__(self):
		return str(self.value)

Downloads and more

You can find the raw file on Github.

Any comments or suggestions are welcome.

Copyright © 2025 Ney Mello's Blog

Theme by Anders NorenUp ↑