work on obj_detect.py
added square_size cmd line param for calibrate.py added _coverage.py script to compute cv2 api coverage by samples (just for interest, may be removed later)
This commit is contained in:
parent
99eb377143
commit
32825893bd
21
samples/python2/_coverage.py
Normal file
21
samples/python2/_coverage.py
Normal file
@ -0,0 +1,21 @@
|
||||
from glob import glob
|
||||
import cv2
|
||||
import re
|
||||
|
||||
cv2_callable = set(['cv2.'+name for name in dir(cv2) if callable( getattr(cv2, name) )])
|
||||
|
||||
found = set()
|
||||
for fn in glob('*.py'):
|
||||
print ' --- ', fn
|
||||
code = open(fn).read()
|
||||
found |= set(re.findall('cv2?\.\w+', code))
|
||||
|
||||
cv2_used = found & cv2_callable
|
||||
|
||||
r = 1.0 * len(cv2_used) / len(cv2_callable)
|
||||
print '\ncv2 api coverage: %d / %d (%.1f%%)' % ( len(cv2_used), len(cv2_callable), r*100 )
|
||||
|
||||
print '\nold (cv) symbols:'
|
||||
for s in found:
|
||||
if s.startswith('cv.'):
|
||||
print s
|
@ -4,7 +4,7 @@ import os
|
||||
from common import splitfn
|
||||
|
||||
USAGE = '''
|
||||
USAGE: calib.py [--save <filename>] [--debug <output path>] [<image mask>]
|
||||
USAGE: calib.py [--save <filename>] [--debug <output path>] [--square_size] [<image mask>]
|
||||
'''
|
||||
|
||||
|
||||
@ -13,16 +13,18 @@ if __name__ == '__main__':
|
||||
import sys, getopt
|
||||
from glob import glob
|
||||
|
||||
args, img_mask = getopt.getopt(sys.argv[1:], '', ['save=', 'debug='])
|
||||
args, img_mask = getopt.getopt(sys.argv[1:], '', ['save=', 'debug=', 'square_size='])
|
||||
args = dict(args)
|
||||
try: img_mask = img_mask[0]
|
||||
except: img_mask = '../cpp/left*.jpg'
|
||||
img_names = glob(img_mask)
|
||||
debug_dir = args.get('--debug')
|
||||
square_size = float(args.get('--square_size', 1.0))
|
||||
|
||||
pattern_size = (9, 6)
|
||||
pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 )
|
||||
pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
|
||||
pattern_points *= square_size
|
||||
|
||||
obj_points = []
|
||||
img_points = []
|
||||
@ -53,6 +55,6 @@ if __name__ == '__main__':
|
||||
img_n = len(img_points)
|
||||
rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, (w, h), camera_matrix, dist_coefs)
|
||||
print "RMS:", rms
|
||||
print "camera matrix: ", camera_matrix
|
||||
print "distortion coefficients: ", dist_coefs
|
||||
print "camera matrix:\n", camera_matrix
|
||||
print "distortion coefficients: ", dist_coefs.ravel()
|
||||
|
||||
|
@ -18,7 +18,7 @@ hist_scale = 10
|
||||
def set_scale(val):
|
||||
global hist_scale
|
||||
hist_scale = val
|
||||
cv.CreateTrackbar('scale', 'hist', hist_scale, 32, set_scale)
|
||||
cv2.createTrackbar('scale', 'hist', hist_scale, 32, set_scale)
|
||||
|
||||
try: fn = sys.argv[1]
|
||||
except: fn = 'synth:bg=../cpp/baboon.jpg:class=chess:noise=0.05'
|
||||
|
@ -2,6 +2,8 @@ import numpy as np
|
||||
import cv2
|
||||
import os
|
||||
|
||||
image_extensions = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.tiff', '.pbm', '.pgm', '.ppm']
|
||||
|
||||
def splitfn(fn):
|
||||
path, fn = os.path.split(fn)
|
||||
name, ext = os.path.splitext(fn)
|
||||
|
@ -1,6 +1,5 @@
|
||||
import numpy as np
|
||||
import cv2, cv
|
||||
import common
|
||||
|
||||
def detect(img, cascade):
|
||||
rects = cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
||||
@ -44,7 +43,7 @@ def process_image(fn, cascade, extract_faces=True):
|
||||
|
||||
faces = []
|
||||
if extract_faces:
|
||||
path, name, ext = common.splitfn(fn)
|
||||
path, name, ext = splitfn(fn)
|
||||
face_sz = 256
|
||||
for i, r in enumerate(rects):
|
||||
p1, p2, u = r.reshape(3, 2)
|
||||
@ -56,8 +55,6 @@ def process_image(fn, cascade, extract_faces=True):
|
||||
face = cv2.warpAffine(img, M, (face_sz, face_sz), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_AREA)
|
||||
faces.append(face)
|
||||
|
||||
cv2.imwrite('out/%s_%02d.bmp' % (name, i), face)
|
||||
|
||||
return small, rects, faces
|
||||
|
||||
|
||||
@ -66,23 +63,32 @@ if __name__ == '__main__':
|
||||
import sys
|
||||
import getopt
|
||||
from glob import glob
|
||||
from common import splitfn, image_extensions
|
||||
|
||||
args, img_mask = getopt.getopt(sys.argv[1:], '', ['cascade='])
|
||||
args, img_args = getopt.getopt(sys.argv[1:], '', ['cascade=', 'outdir='])
|
||||
args = dict(args)
|
||||
# "../../data/haarcascades/haarcascade_frontalface_default.xml" #haarcascade_frontalface_default
|
||||
cascade_fn = args.get('--cascade', "../../data/haarcascades/haarcascade_frontalface_alt.xml")
|
||||
outdir = args.get('--outdir')
|
||||
|
||||
img_list = []
|
||||
if len(img_args) == 0:
|
||||
img_list = ['../cpp/lena.jpg']
|
||||
else:
|
||||
for mask in img_args:
|
||||
img_list.extend(glob(mask))
|
||||
img_list = [fn for fn in img_list if splitfn(fn)[-1].lower() in image_extensions]
|
||||
|
||||
cascade = cv2.CascadeClassifier(cascade_fn)
|
||||
|
||||
mask = 'D:/Dropbox/Photos/2011-06-12 aero/img_08[2-9]*.jpg'
|
||||
for fn in glob(mask):
|
||||
print fn
|
||||
for i, fn in enumerate(img_list):
|
||||
print '%d / %d %s' % (i+1, len(img_list), fn),
|
||||
vis, rects, faces = process_image(fn, cascade)
|
||||
if len(faces) > 0 and outdir is not None:
|
||||
path, name, ext = splitfn(fn)
|
||||
cv2.imwrite('%s/%s_all.bmp' % (outdir, name), vis)
|
||||
for face_i, face in enumerate(faces):
|
||||
cv2.imwrite('%s/%s_obj%02d.bmp' % (outdir, name, face_i), face)
|
||||
print ' - %d object(s) found' % len(faces)
|
||||
cv2.imshow('img', vis)
|
||||
cv2.waitKey(100)
|
||||
|
||||
|
||||
#vis, rects = process_image('test.jpg', cascade)
|
||||
#print rects
|
||||
#cv2.imshow('img', vis)
|
||||
cv2.waitKey(50)
|
||||
cv2.waitKey()
|
||||
|
Loading…
x
Reference in New Issue
Block a user