Merged the trunk r8547:8574, r8587
This commit is contained in:
@@ -3,6 +3,8 @@ LOCAL_PATH := $(call my-dir)
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
OPENCV_CAMERA_MODULES:=off
|
||||
OPENCV_INSTALL_MODULES:=on
|
||||
#OPENCV_LIB_TYPE:=SHARED <- this is default
|
||||
|
||||
include ../includeOpenCV.mk
|
||||
ifeq ("$(wildcard $(OPENCV_MK_PATH))","")
|
||||
|
@@ -44,8 +44,8 @@ int main( int argc, char** argv )
|
||||
int histSize[] = { h_bins, s_bins };
|
||||
|
||||
// hue varies from 0 to 256, saturation from 0 to 180
|
||||
float h_ranges[] = { 0, 256 };
|
||||
float s_ranges[] = { 0, 180 };
|
||||
float s_ranges[] = { 0, 256 };
|
||||
float h_ranges[] = { 0, 180 };
|
||||
|
||||
const float* ranges[] = { h_ranges, s_ranges };
|
||||
|
||||
|
@@ -2,6 +2,7 @@ import numpy as np
|
||||
import cv2
|
||||
import os
|
||||
from contextlib import contextmanager
|
||||
import itertools as it
|
||||
|
||||
image_extensions = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.tiff', '.pbm', '.pgm', '.ppm']
|
||||
|
||||
@@ -170,3 +171,22 @@ class RectSelector:
|
||||
return
|
||||
x0, y0, x1, y1 = self.drag_rect
|
||||
cv2.rectangle(vis, (x0, y0), (x1, y1), (0, 255, 0), 2)
|
||||
|
||||
|
||||
def grouper(n, iterable, fillvalue=None):
|
||||
'''grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx'''
|
||||
args = [iter(iterable)] * n
|
||||
return it.izip_longest(fillvalue=fillvalue, *args)
|
||||
|
||||
def mosaic(w, imgs):
|
||||
'''Make a grid from images.
|
||||
|
||||
w -- number of grid columns
|
||||
imgs -- images (must have same size and format)
|
||||
'''
|
||||
imgs = iter(imgs)
|
||||
img0 = imgs.next()
|
||||
pad = np.zeros_like(img0)
|
||||
imgs = it.chain([img0], imgs)
|
||||
rows = grouper(w, imgs, pad)
|
||||
return np.vstack(map(np.hstack, rows))
|
||||
|
BIN
samples/python2/digits.png
Normal file
BIN
samples/python2/digits.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 704 KiB |
78
samples/python2/digits.py
Normal file
78
samples/python2/digits.py
Normal file
@@ -0,0 +1,78 @@
|
||||
'''
|
||||
Neural network digit recognition sample.
|
||||
Usage:
|
||||
digits.py
|
||||
|
||||
Sample loads a dataset of handwritten digits from 'digits.png'.
|
||||
Then it trains a neural network classifier on it and evaluates
|
||||
its classification accuracy.
|
||||
'''
|
||||
|
||||
import numpy as np
|
||||
import cv2
|
||||
from common import mosaic
|
||||
|
||||
def unroll_responses(responses, class_n):
|
||||
'''[1, 0, 2, ...] -> [[0, 1, 0], [1, 0, 0], [0, 0, 1], ...]'''
|
||||
sample_n = len(responses)
|
||||
new_responses = np.zeros((sample_n, class_n), np.float32)
|
||||
new_responses[np.arange(sample_n), responses] = 1
|
||||
return new_responses
|
||||
|
||||
|
||||
SZ = 20 # size of each digit is SZ x SZ
|
||||
CLASS_N = 10
|
||||
digits_img = cv2.imread('digits.png', 0)
|
||||
|
||||
# prepare dataset
|
||||
h, w = digits_img.shape
|
||||
digits = [np.hsplit(row, w/SZ) for row in np.vsplit(digits_img, h/SZ)]
|
||||
digits = np.float32(digits).reshape(-1, SZ*SZ)
|
||||
N = len(digits)
|
||||
labels = np.repeat(np.arange(CLASS_N), N/CLASS_N)
|
||||
|
||||
# split it onto train and test subsets
|
||||
shuffle = np.random.permutation(N)
|
||||
train_n = int(0.9*N)
|
||||
digits_train, digits_test = np.split(digits[shuffle], [train_n])
|
||||
labels_train, labels_test = np.split(labels[shuffle], [train_n])
|
||||
|
||||
# train model
|
||||
model = cv2.ANN_MLP()
|
||||
layer_sizes = np.int32([SZ*SZ, 25, CLASS_N])
|
||||
model.create(layer_sizes)
|
||||
params = dict( term_crit = (cv2.TERM_CRITERIA_COUNT, 100, 0.01),
|
||||
train_method = cv2.ANN_MLP_TRAIN_PARAMS_BACKPROP,
|
||||
bp_dw_scale = 0.001,
|
||||
bp_moment_scale = 0.0 )
|
||||
print 'training...'
|
||||
labels_train_unrolled = unroll_responses(labels_train, CLASS_N)
|
||||
model.train(digits_train, labels_train_unrolled, None, params=params)
|
||||
model.save('dig_nn.dat')
|
||||
model.load('dig_nn.dat')
|
||||
|
||||
def evaluate(model, samples, labels):
|
||||
'''Evaluates classifier preformance on a given labeled samples set.'''
|
||||
ret, resp = model.predict(samples)
|
||||
resp = resp.argmax(-1)
|
||||
error_mask = (resp == labels)
|
||||
accuracy = error_mask.mean()
|
||||
return accuracy, error_mask
|
||||
|
||||
# evaluate model
|
||||
train_accuracy, _ = evaluate(model, digits_train, labels_train)
|
||||
print 'train accuracy: ', train_accuracy
|
||||
test_accuracy, test_error_mask = evaluate(model, digits_test, labels_test)
|
||||
print 'test accuracy: ', test_accuracy
|
||||
|
||||
# visualize test results
|
||||
vis = []
|
||||
for img, flag in zip(digits_test, test_error_mask):
|
||||
img = np.uint8(img).reshape(SZ, SZ)
|
||||
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
||||
if not flag:
|
||||
img[...,:2] = 0
|
||||
vis.append(img)
|
||||
vis = mosaic(25, vis)
|
||||
cv2.imshow('test', vis)
|
||||
cv2.waitKey()
|
Reference in New Issue
Block a user