imdb-despoiler Chrome Extension

Meaghan recently suffered from a spoiler on IMDB; she looked up an actor on a TV show and found that they were only on for a few more episodes. It was quite an ordeal. She looked around for a Chrome extension to hide this information and couldn’t find one, so she asked me to make one. Turned out to be easier than I expected, so it’s available on the Chrome Web Store.

This extension hides:

  • The number of episodes and timeframe an actor was in a show on the show’s general page
  • The number of episodes and timeframe an actor was in a show on the show’s cast detail page
  • The episode information for each show on an actor’s detail page

Let me know if you think of a new feature and I’ll try to add it. Feedback is welcome!

Getting Webcam Images with Python and OpenCV 2 (For Real This Time)

Alright, so my post Getting Webcam Images with Python and OpenCV 2 was wrong! I did not fully understand how to read the OpenCV API documentation and instead of demonstrating how to capture an image with OpenCV 2, I just demonstrated a way to capture an image with OpenCV 1.x in a way different from the example I first followed. Here is how one would actually use the newer, OO, consolidated OpenCV 2 Python API.

import cv2

# Camera 0 is the integrated web cam on my netbook
camera_port = 0

#Number of frames to throw away while the camera adjusts to light levels
ramp_frames = 30

# Now we can initialize the camera capture object with the cv2.VideoCapture class.
# All it needs is the index to a camera port.
camera = cv2.VideoCapture(camera_port)

# Captures a single image from the camera and returns it in PIL format
def get_image():
 # read is the easiest way to get a full image out of a VideoCapture object.
 retval, im = camera.read()
 return im

# Ramp the camera - these frames will be discarded and are only used to allow v4l2
# to adjust light levels, if necessary
for i in xrange(ramp_frames):
 temp = get_image()
print("Taking image...")
# Take the actual image we want to keep
camera_capture = get_image()
file = "/home/codeplasma/test_image.png"
# A nice feature of the imwrite method is that it will automatically choose the
# correct format based on the file extension you provide. Convenient!
cv2.imwrite(file, camera_capture)

# You'll want to release the camera, otherwise you won't be able to create a new
# capture object until your script exits
del(camera)

Getting Webcam Images with Python and OpenCV 2

UPDATE: This is wrong! The example in this post still uses the older 1.x OpenCV classes, but a little differently than Joseph Perla’s example. I will update soon with a real example of OpenCV 2.x camera usage in the next day or two.

I have a need to capture images programmatically for a personal project I’m working on. I’m using Python, and the best way to grab frames from a webcam with Python seems to be OpenCV. It looks pretty amazing, way more than what I would need to just capture images, but it definitely seems like a great library to get to know, so why not start here?

I first began working with this excellent guide by Joseph Perla. I managed to grab some frames and thought it was going great. Then, after a reboot, the opencv library vanished from my system. No matter what I did, I was unable to “import opencv.” Installing the binaries from the package manager didn’t help, compiling from source didn’t provide that interface, and several hours of Googling didn’t help either. Still have no idea how that happened!

Anyways,  Mr. Perla’s example uses a now deprecated Python interface, so I ended up just rewriting my little bit of code using the newer 2.1+ documentation and the current, unified cv.py interface.

To run this you just need Python (I’m using 2.7), OpenCV (python-opencv using apt-get on Ubuntu), and some sort of webcam. Hope this helps someone else who wants to get started taking images with their camera using Python! A big thanks goes to Joseph Perla for getting me started. As with everything on this site, feel free to use it however you would like.


import sys
import cv
import time

# Camera 0 is the integrated web cam on my netbook
camera_port = 0
# FPS to use when ramping the camera
fps = 30
# Number of frames to take during camera ramp
ramp_frames = 30

# Now we can set up the camera with the CaptureFromCAM() function. All it needs is
# the index to a camera port. The 'camera' variable will be a cv.capture object
camera = cv.CaptureFromCAM(camera_port)

# Captures a single image from the camera and returns it in IplImage format
def get_image():
    # QueryFrame is the easiest way to get a full image out of a capture object
    im = cv.QueryFrame(camera)
    return im

# I have found that my camera needs to capture a few frames to adjust to the
# ambient light levels. Just call this once at the beginning of each capture
# session. Probably could use tweaking or even complete omission depending on
# your camera
def ramp_camera(fps, num_frames):
    print("Ramping camera at " + str(fps) + " FPS for "
             + str(num_frames) + " frames...\n")
    while num_frames > 0:
    # Don't need to actually save these images
    im = get_image()
    num_frames = num_frames - 1
    time.sleep(1/fps)
    return

# Ramp the camera, capture an image, and save it as a PNG file
ramp_camera(30, 30)
print("Taking image...")
camera_capture = get_image()
file = "/home/codeplasma/test_image.png"
# A nice feature of the SaveImage method is that it will automatically choose the
# correct format based on the file extension you provide. Convenient!
cv.SaveImage(file, camera_capture)

# You'll want to release the camera, otherwise you won't be able to create a new
# capture object until your script exits
del(camera)

Of course, as always, feedback and questions are welcome!