-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcv2_detect.py
147 lines (132 loc) · 4.73 KB
/
cv2_detect.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
145
146
147
import cv2
import cv2.cv as cv
import numpy as np
import math as m
CASCADES = {
'face': cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_alt.xml'),
# 'frontalt': cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_alt.xml'),
'profile': cv2.CascadeClassifier('haarcascades/haarcascade_profileface.xml'),
'upperbody': cv2.CascadeClassifier('haarcascades/haarcascade_upperbody.xml'),
# 'eye': cv2.CascadeClassifier('haarcascades/haarcascade_eye.xml'),
'fullbody': cv2.CascadeClassifier('haarcascades/haarcascade_fullbody.xml'),
}
CASCADE_COLOUR = {
'n_front': (0, 255, 0),
'r_front': (255, 0, 0),
'profile': (255, 0, 0),
'frontalt': (255, 255, 0),
'upperbody': (0, 0, 255),
}
def rotate90(img):
# timg = cv.CreateImage((img.height,img.width), img.depth, img.channels) # transposed image
# rotate clockwise
timg = cv2.transpose(img)
timg = cv2.flip(timg, 1)
return timg
def detect(inputimg, scaleFactor=1.2, minNeighbors=4, minSize=(20, 20),
flags=cv.CV_HAAR_SCALE_IMAGE):
rectlist = {}
hit = False
for t, img in [("n_", inputimg), ("r_", rotate90(inputimg))]:
for cascade_type, cascade in CASCADES.iteritems():
rects = cascade.detectMultiScale(img, scaleFactor=scaleFactor,
minNeighbors=minNeighbors,
minSize=minSize, flags=flags)
if len(rects) == 0:
rectlist[t+cascade_type] = []
else:
rects[:, 2:] += rects[:, :2]
hit = True
rectlist[t+cascade_type] = rects
return rectlist, hit
def explain_houghp_lines(filename, a=150, b=200, c=60, d=20, e=4):
img = cv2.imread(filename)
grey = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
cv2.imwrite(filename+"grey.jpg", grey)
edges = cv2.Canny(grey,a,b,apertureSize = 3)
cv2.imwrite(filename+"edges.jpg", edges)
lines = cv2.HoughLinesP(edges, e,np.pi/180, 300, minLineLength = c, maxLineGap = d)
if lines != []:
for x1,y1,x2,y2 in lines[0]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.imwrite(filename+'houghlines.jpg',img)
def houghp_lines(filename, a=150, b=200, c=60, d=20, e=4):
try:
img = cv2.imread(filename)
grey = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(grey,a,b,apertureSize = 3)
lines = cv2.HoughLinesP(edges,e,np.pi/180, 300, minLineLength = c, maxLineGap = d)
if lines != None:
return lines
else:
return [[]]
except cv2.error:
return [[]]
def houghp_circles(filename, a=2, b=80, c=100, d=100, e=50, f=150):
try:
img = cv2.imread(filename)
grey = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(grey, cv2.cv.CV_HOUGH_GRADIENT, a, b, None, c, d, e, f)
if circles != None:
return circles, img.shape
else:
return [], img.shape
except cv2.error:
return [], (1,1,1)
def polar_lines(lines):
polar = []
for x1,y1,x2,y2 in lines[0]:
x = x2 - x1
y = y2 - y1
XsqPlusYsq = x**2 + y**2
r = m.sqrt(XsqPlusYsq)
theta = m.atan2(y, x)
polar.append((r,theta))
return polar
def slantyness(filename, a=150, b=200, c=60, d=20, e=5):
try:
# faking a subjective measure
# takes the probabalistic hough lines, and combines them (intentionally without mathematical rigour!)
# to form an average 'gradient' for the image
lines = houghp_lines(filename, a=a, b=b, c=c, d=d, e=e)
polarlines = polar_lines(lines)
slants = [(1 - m.cos(4*line[1])) for line in polarlines]
# ave. closer to 0, predominately horiz+vert lines
# closer to 1, " slanted lines
if slants:
return sum(slants)/ float(len(slants))
else:
return 0.0
except cv2.error:
return 0.0
def aspectratio(filename, a=150, b=200, c=60, d=20, e=20):
try:
lines = houghp_lines(filename, a=a, b=b, c=c, d=d, e=20)
polarlines = polar_lines(lines)
horiz = [(r,th) for r, th in polarlines if np.abs(th) < 2*np.pi/8]
verts = [(r,th) for r, th in polarlines if np.abs(th) > 2*np.pi/8]
hbias = sum(map(lambda x: x[0], horiz))
vbias = sum(map(lambda x: x[0], verts))
if horiz:
hbias = hbias / len(horiz)
if verts:
vbias = vbias / len(verts)
return hbias, vbias
except cv2.error:
return 1.0, 1.0
def area(filename, dpi=1.0):
img = cv2.imread(filename)
h,w,depth = img.shape
return float(h*w)/(dpi**2.0)
def bubblyness(filename, a=2, b=80, c=100, d=100, e=20, f=150):
circles, shape = houghp_circles(filename, a,b,c,d,e,f)
h,w,_ = shape
midx = int(w/2)
midy = int(h/2)
if circles != []:
sizes = [x[2] for x in circles[0]]
xs = [midx - x[0] for x in circles[0]]
ys = [midy - x[1] for x in circles[0]]
return sum(sizes)/float(len(sizes)), sum(xs)/float(len(xs)), sum(ys)/float(len(ys))
else:
return 0,0,0