Monitor Your Drinking Habits! (with cv2)

Use Canny edge detection and Hough transformation to extract and count wine cork shapes of varying diameter/color/texture.

Inputs

Output

Code

# Will Forfang
# 05/11/2015

print "Loading tools...",
import cv2, pdb
import numpy as np
import os
print "Done."

if 'results' not in os.listdir('.'):
	os.mkdir('results')
	print "Generating results directory."

# Inputs
print "Opening image...",
rimg = cv2.imread('DSC_1531.JPG',0)
img = cv2.imread('DSC_1531_cropped.JPG',0)

print "Done."
print "Applying blur...",
cimg = cv2.medianBlur(rimg,5)
cimg = cv2.cvtColor(rimg,cv2.COLOR_GRAY2BGR)
print "Done."

print "Finding corks...",
corks = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,120,
                            param1=20,param2=25,minRadius=60,maxRadius=80)
corks = np.uint16(np.around(corks))
print "Found",len(corks[0,:]),"corks."

print "Labelling corks...",
for i in corks[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),10)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),6)
print "Done."

# text position:
anchorX = int(.65*len(cimg[0]))
anchorY = int(.25*len(cimg))
cv2.putText(cimg,'detected '+str(len(corks[0,:]))+' corks', (anchorX,anchorY), cv2.FONT_HERSHEY_SIMPLEX, 5, (0,240,0),10)
cv2.imwrite('results/detected_'+str(len(corks[0,:]))+'_corks.png',cimg)