-
Notifications
You must be signed in to change notification settings - Fork 0
/
findpills.py
144 lines (111 loc) · 3.7 KB
/
findpills.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import cv2
import numpy as np
import json
from matplotlib import pyplot as plt
def kMeansColor(img, numColors):
Z = img.reshape((-1,3))
# convert to np.float32
Z = np.float32(Z)
# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = numColors
ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
# Now convert back into uint8, and make original image
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))
return center,res2
def getLeftMask(contour):
mincord = contour[0]
for coord in contour:
if coord[0][0] < mincord[0][0]:
mincord = coord
return mincord
def convertResults(results):
days = ["SunM","SunN","MonM","MonN","TueM","TueN","WedM","WedN","ThuM","ThuN","FriM","FriN","SatM","SatN"]
output = {}
output["error"] = False
for i in xrange(14):
output[days[i]] = results[i]
return json.dumps(output)
def answer(img):
img =cv2.resize(img, (0,0), fx=0.1, fy=0.1)
######
colors, segmented = kMeansColor(img,4)
#####
blue = np.array([95, 42, 10])
red = np.array([17,8,98])
cos_sim = lambda a,b: np.dot(a, b)/(np.linalg.norm(a)*np.linalg.norm(b))
bluedist = float('inf')
blueseg = None
colors = [np.array(color) for color in colors]
for color in colors:
currdistance = np.linalg.norm(color-blue)
if currdistance < bluedist:
bluedist = currdistance
blueseg = color
reddist = float('inf')
redseg = None
for color in colors:
currdistance = np.linalg.norm(color-red)
if currdistance < reddist:
reddist = currdistance
redseg = color
white1set = False
for color in colors:
if not np.array_equal(color,redseg) and not np.array_equal(color,blueseg) and not white1set:
white1 = color
white1set = True
elif not np.array_equal(color,redseg) and not np.array_equal(color,blueseg):
white2 = color
######
walls = cv2.inRange(segmented, blueseg,blueseg)
##
kernel = np.ones((7,7),np.uint8)
edges = cv2.dilate(walls,kernel,iterations = 1)
#######
kernel = np.ones((7,7),np.uint8)
edgesthin = cv2.erode(edges,kernel,iterations = 1)
###########
imgcontours = edgesthin.copy()
_, contours,h = cv2.findContours(imgcontours,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
parentfreqs= {}
for contour in h[0]:
parent = contour[-1]
parentfreqs[parent] = parentfreqs[parent] + 1 if parent in parentfreqs else 1
par = -1
for key in parentfreqs:
if parentfreqs[key] == 14:
par = key
break
if par == -1:
output = {}
output["error"] = True
return json.dumps(output)
contours = [contours[i] for i in xrange(len(contours)) if h[0][i][-1]==par]
#######
contours.sort(key = lambda contour: getLeftMask(contour)[0][0])
for x in xrange(7):
if getLeftMask(contours[2*x])[0][1] > getLeftMask(contours[2*x+1])[0][1]:
contours[2*x],contours[2*x+1]= contours[2*x+1],contours[2*x]
##########
imgcontours = img.copy()
masks = [None]*14
for x in xrange(14):
masks[x] = np.zeros(edgesthin.shape, np.uint8)
for i in xrange(14):
cv2.drawContours(masks[i],contours,i,(255),-1)
########
results = []
for x in xrange(14):
cell = cv2.bitwise_and(segmented,segmented,mask=masks[x])
whitecell1 = cv2.inRange(cell,white1,white1)
cell = cv2.inRange(cell,white2,white2)
cell = cv2.bitwise_or(cell,whitecell1,mask=masks[x])
whitesize = cv2.countNonZero(cell)
cellsize = cv2.countNonZero(masks[x])
results.append(whitesize*1.0/cellsize>0.04)
return convertResults(results)
file_path = 'original3.png'
img = cv2.imread(file_path)
print answer(img)