import seaborn as sns
import metapack as mp
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display
from dtcv import get_image
%matplotlib inline
sns.set_context('notebook')
mp.jupyter.init()
#pkg = mp.jupyter.open_package()
#pkg = mp.jupyter.open_source_package()
pkg = mp.open_package('http://library.metatab.org/sandiegodata.org-downtown_cv-5.zip')
pkg
display(pkg.resource('counts'))
counts = pkg.resource('counts').dataframe()
# This may take a few minutes; it will download about 330 images and save them to the /tmp directory
counts['image'] = counts.image_url.apply(get_image)
counts.head()
counts['count'] = pd.to_numeric(counts['count'])
def crop(row):
"""Crop the handwritten mark, and hopefully the shape around it, from the image"""
x, y, r = row.cx, row.cy, row.r
r = int(r*1.0)
return row.image[y-r:y+r, x-r:x+r ]
plt.imshow(crop(counts.iloc[60]))
numbers = []
fig, axes = plt.subplots(nrows=20, ncols=6, figsize=(20,40))
for ax, (name, row) in zip(axes.flat, counts.iterrows()):
n = crop(row)
numbers.append(n)
ax.imshow(n)
import cv2
def draw_lines(img, lines):
for r,theta in lines[0]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*r
y0 = b*r
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(img,(x1,y1), (x2,y2), (0,255,0),2)
img = numbers[1].copy()
# Apply edge detection method on the image
edges = cv2.Canny(tr,50,150,apertureSize = 3)
# This returns an array of r and theta values
lines = cv2.HoughLines(edges,1,np.pi/180, 10)
draw_lines(img, lines)
plt.imshow(img)
numbers = []
def invert(img):
# Convert the img to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_, tr = cv2.threshold (gray, 70, 255, cv2.THRESH_BINARY_INV);
return tr
for name, row in counts.iterrows():
numbers.append(invert(crop(row)))
plt.imshow(numbers[1])