2013-03-06 14:41:02 +08:00
|
|
|
#!/usr/bin/env python
|
2012-11-24 02:57:22 +08:00
|
|
|
|
2015-12-13 09:43:58 +08:00
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
import numpy as np
|
2017-12-11 17:55:03 +08:00
|
|
|
import cv2 as cv
|
2013-03-06 14:41:02 +08:00
|
|
|
|
|
|
|
# built-in modules
|
2012-10-17 07:18:30 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2013-03-06 14:41:02 +08:00
|
|
|
|
|
|
|
# local modules
|
2012-10-17 07:18:30 +08:00
|
|
|
import video
|
|
|
|
from common import mosaic
|
|
|
|
|
|
|
|
from digits import *
|
|
|
|
|
|
|
|
def main():
|
2013-03-15 20:55:58 +08:00
|
|
|
try:
|
2013-03-06 14:41:02 +08:00
|
|
|
src = sys.argv[1]
|
2013-03-15 20:55:58 +08:00
|
|
|
except:
|
2013-03-06 14:41:02 +08:00
|
|
|
src = 0
|
2012-10-17 07:18:30 +08:00
|
|
|
cap = video.create_capture(src)
|
|
|
|
|
|
|
|
classifier_fn = 'digits_svm.dat'
|
|
|
|
if not os.path.exists(classifier_fn):
|
2015-12-13 09:43:58 +08:00
|
|
|
print('"%s" not found, run digits.py first' % classifier_fn)
|
2012-10-17 07:18:30 +08:00
|
|
|
return
|
|
|
|
|
2016-02-05 22:46:52 +08:00
|
|
|
if True:
|
2017-12-11 17:55:03 +08:00
|
|
|
model = cv.ml.SVM_load(classifier_fn)
|
2016-02-05 22:46:52 +08:00
|
|
|
else:
|
2017-12-11 17:55:03 +08:00
|
|
|
model = cv.ml.SVM_create()
|
2016-06-14 21:01:36 +08:00
|
|
|
model.load_(classifier_fn) #Known bug: https://github.com/opencv/opencv/issues/4969
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
while True:
|
2017-08-25 00:45:14 +08:00
|
|
|
_ret, frame = cap.read()
|
2017-12-11 17:55:03 +08:00
|
|
|
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
|
2017-12-11 17:55:03 +08:00
|
|
|
bin = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY_INV, 31, 10)
|
|
|
|
bin = cv.medianBlur(bin, 3)
|
2018-09-27 12:42:21 +08:00
|
|
|
contours, heirs = cv.findContours( bin.copy(), cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)
|
2013-03-06 14:41:02 +08:00
|
|
|
try:
|
|
|
|
heirs = heirs[0]
|
|
|
|
except:
|
|
|
|
heirs = []
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
for cnt, heir in zip(contours, heirs):
|
|
|
|
_, _, _, outer_i = heir
|
|
|
|
if outer_i >= 0:
|
|
|
|
continue
|
2017-12-11 17:55:03 +08:00
|
|
|
x, y, w, h = cv.boundingRect(cnt)
|
2012-10-17 07:18:30 +08:00
|
|
|
if not (16 <= h <= 64 and w <= 1.2*h):
|
|
|
|
continue
|
|
|
|
pad = max(h-w, 0)
|
2017-11-09 07:46:35 +08:00
|
|
|
x, w = x - (pad // 2), w + pad
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0))
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
bin_roi = bin[y:,x:][:h,:w]
|
|
|
|
|
|
|
|
m = bin_roi != 0
|
|
|
|
if not 0.1 < m.mean() < 0.4:
|
|
|
|
continue
|
|
|
|
'''
|
2017-08-25 00:45:14 +08:00
|
|
|
gray_roi = gray[y:,x:][:h,:w]
|
2012-10-17 07:18:30 +08:00
|
|
|
v_in, v_out = gray_roi[m], gray_roi[~m]
|
|
|
|
if v_out.std() > 10.0:
|
|
|
|
continue
|
|
|
|
s = "%f, %f" % (abs(v_in.mean() - v_out.mean()), v_out.std())
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.putText(frame, s, (x, y), cv.FONT_HERSHEY_PLAIN, 1.0, (200, 0, 0), thickness = 1)
|
2012-10-17 07:18:30 +08:00
|
|
|
'''
|
|
|
|
|
|
|
|
s = 1.5*float(h)/SZ
|
2017-12-11 17:55:03 +08:00
|
|
|
m = cv.moments(bin_roi)
|
2012-10-17 07:18:30 +08:00
|
|
|
c1 = np.float32([m['m10'], m['m01']]) / m['m00']
|
|
|
|
c0 = np.float32([SZ/2, SZ/2])
|
|
|
|
t = c1 - s*c0
|
|
|
|
A = np.zeros((2, 3), np.float32)
|
|
|
|
A[:,:2] = np.eye(2)*s
|
|
|
|
A[:,2] = t
|
2017-12-11 17:55:03 +08:00
|
|
|
bin_norm = cv.warpAffine(bin_roi, A, (SZ, SZ), flags=cv.WARP_INVERSE_MAP | cv.INTER_LINEAR)
|
2012-10-17 07:18:30 +08:00
|
|
|
bin_norm = deskew(bin_norm)
|
|
|
|
if x+w+SZ < frame.shape[1] and y+SZ < frame.shape[0]:
|
|
|
|
frame[y:,x+w:][:SZ, :SZ] = bin_norm[...,np.newaxis]
|
|
|
|
|
|
|
|
sample = preprocess_hog([bin_norm])
|
2018-11-08 16:17:41 +08:00
|
|
|
digit = model.predict(sample)[1].ravel()
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.putText(frame, '%d'%digit, (x, y), cv.FONT_HERSHEY_PLAIN, 1.0, (200, 0, 0), thickness = 1)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.imshow('frame', frame)
|
|
|
|
cv.imshow('bin', bin)
|
|
|
|
ch = cv.waitKey(1)
|
2012-10-17 07:18:30 +08:00
|
|
|
if ch == 27:
|
|
|
|
break
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.destroyAllWindows()
|