Commit 26094d5e authored by Michael Murtaugh's avatar Michael Murtaugh

new generalized getfeatures and summarize_json

parent 8c52c034
#!/usr/bin/env python
from __future__ import print_function
import cv2, os, json, sys
from argparse import ArgumentParser
from analyze_image import analyze_image
p = ArgumentParser("")
p.add_argument("--cascades", default="./haarcascades", help="location of the cascade XML files, default: ./haarcascades")
p.add_argument("--settings", default="settings.json", help="settings file, default settings.json")
p.add_argument("input", nargs="*", default=[])
args = p.parse_args()
with open (args.settings) as f:
settings = json.load(f)
cascades_path = os.path.expanduser(args.cascades)
def init_features (f):
if type(f) == list:
return [init_features(x) for x in f]
f.get("cascade")
f['_cascade'] = cv2.CascadeClassifier(os.path.join(cascades_path, f["cascade"]))
if 'args' not in f:
f['args'] = {}
return f
def ensure_list (f):
if type(f) == list:
return f
else:
return [f]
def process_features (grayimg, features):
ret = {}
for x in features:
roi = grayimg
ox, oy = 0, 0
for f in ensure_list(x):
indent = ""
if ox > 0:
indent = " "
print ("{0}process_feature {1}".format(indent, f['name']), file=sys.stderr)
if f['name'] not in ret:
rects = ret[f['name']] = []
else:
rects = ret[f['name']]
fcount = 0
for (x,y,w,h) in f['_cascade'].detectMultiScale(roi, **f['args']):
rects.append([int(ox+x), int(oy+y), int(w), int(h)])
fcount += 1
# narrow for (evt) next cascaded feature...
ox, oy = x, y
roi = roi[y:y+h, x:x+w]
if fcount == 0:
break # don't cascade to the next feature (ie no eyes if no faces)...
return ret
output = []
inputs = args.input
numinput = len(inputs)
if (numinput == 0):
# attempt to read filenames from stdin
inputs = sys.stdin.read().splitlines()
inputs = [x.strip() for x in inputs if x.strip()]
numinput = len(inputs)
features = init_features(settings['features'])
for i, path in enumerate(inputs):
print ("{0}/{1} {2}".format(i, numinput, path), file=sys.stderr)
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
item = process_features(gray, features)
item['path'] = path
output.append(item)
print (json.dumps(output, indent=2))
......@@ -17,10 +17,13 @@ print ("Combining {0} sources, {1} items total".format(len(args.input), len(od))
facecount = 0
eyecount = 0
counts = {}
for x in od:
if 'faces' in x:
facecount += len(x['faces'])
if 'eyes' in x:
eyecount += len(x['eyes'])
for key in x.keys():
if key != "path":
if key not in counts:
counts[key] = 0
counts[key] += len(x[key])
print (", ".join(["{0} {1}".format(counts[name], name) for name in sorted(counts.keys()) ] ))
print ("{0} faces, {1} eyes".format(facecount, eyecount))
{"port":10987,"ipaddress":"192.168.0.21"}
{
"port":10987,
"ipaddress":"192.168.0.21",
"features": [
[
{"name": "faces", "cascade": "haarcascade_frontalface_default.xml", "color": "255,255,0", "width": 2},
{"name": "eyes", "cascade": "haarcascade_eye.xml", "color": "255,255,0", "width": 2}
],
{"name": "bodies", "cascade": "haarcascade_fullbody.xml", "color": "255,255,0", "width": 2}
]
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment