where's waldo? with python and opencv

10
Where's Waldo? with Python and OpenCV Anna Schneider @windupanna SF PyLadies Lightning Talk 25 June 2013

Upload: anna-schneider

Post on 12-Jul-2015

1.645 views

Category:

Technology


10 download

TRANSCRIPT

Page 1: Where's Waldo? with Python and OpenCV

Where's Waldo?with Python and OpenCV

Anna Schneider@windupanna

SF PyLadies Lightning Talk25 June 2013

Page 2: Where's Waldo? with Python and OpenCV

OpenCV: computer vision in C++

video processing, face recognition, and more!

http://opencv.org/http://sourceforge.net/projects/opencvlibrary/

>>> import cv2

Page 3: Where's Waldo? with Python and OpenCV

Let's get started

>>> pyladies = cv2.imread('pyladies.png')

>>> cv2.imshow("pyladies", # title pyladies) # image

Page 4: Where's Waldo? with Python and OpenCV

Images are NumPy arrays!

>>> pyladies.shape(638, 1499, 3)# (height, width, n_colors)

but with weird conventions

>>> pyladies[300,300]array([ 57, 2, 237], dtype=uint8)# (blue, green, red)

Page 5: Where's Waldo? with Python and OpenCV

How would you solve it?>>> waldo = cv2.imread('waldo_face.png')>>> scene = cv2.imread('waldo_scene.png')

Page 6: Where's Waldo? with Python and OpenCV

Matchmaker, matchmaker

>>> pyplot.imshow(scores)

>>> scores = cv2.matchTemplate(scene,# big waldo, # small

cv2.TM_CCORR_NORMED) # method

Page 7: Where's Waldo? with Python and OpenCV

Let the best score win

>>> minmaxs = cv2.minMaxLoc(scores)>>> min, max, min_pos, max_pos = minmaxs>>> print max, max_pos0.999999880791 (979, 842)# best score upper-left corner

Page 8: Where's Waldo? with Python and OpenCV

Layer it on

>>> cv2.rectangle(scene, # image corner1, # upper-left

corner2, # lower-right (0,255,0), # (r,g,b) 5) # pixels

>>> corner1 = max_pos>>> corner2 = (corner1[0]+waldo.shape[1], corner1[1]+waldo.shape[0])

>>> cv2.imshow("There's Waldo!", scene)

Page 9: Where's Waldo? with Python and OpenCV

There's Waldo!