import numpy as np
import cv2
import digits
import os
import video
from common import mosaic



def main():
    cap = video.create_capture()

    classifier_fn = 'digits_svm.dat'
    if not os.path.exists(classifier_fn):
        print '"%s" not found, run digits.py first' % classifier_fn
        return 
    
    model = digits.SVM()
    model.load('digits_svm.dat')

    SZ = 20

    while True:
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        bin = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 31, 10)
        bin = cv2.medianBlur(bin, 3)
        contours, _ = cv2.findContours( bin.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

        for cnt in contours:
            x, y, w, h = cv2.boundingRect(cnt)
            if h < 16 or h > 60 or 1.2*h < w:
                continue
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0))
            sub = bin[y:,x:][:h,:w]
            #sub = ~cv2.equalizeHist(sub)
            #_, sub_bin = cv2.threshold(sub, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)

            s = float(h)/SZ
            m = cv2.moments(sub)
            m00 = m['m00']
            if m00/255 < 0.1*w*h or m00/255 > 0.9*w*h:
                continue

            c1 = np.float32([m['m10'], m['m01']]) / 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
            sub1 = cv2.warpAffine(sub, A, (SZ, SZ), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR)
            sub1 = digits.deskew(sub1)
            if x+w+SZ < frame.shape[1] and y+SZ < frame.shape[0]:
                frame[y:,x+w:][:SZ, :SZ] = sub1[...,np.newaxis]
                
            sample = np.float32(sub1).reshape(1,SZ*SZ) / 255.0
            digit = model.predict(sample)[0]

            cv2.putText(frame, '%d'%digit, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (200, 0, 0), thickness = 1)


        cv2.imshow('frame', frame)
        cv2.imshow('bin', bin)
        if cv2.waitKey(1) == 27:
            break

if __name__ == '__main__':
    main()

@@ -668,17 +668,21 @@
 ntours, 
-_
+heirs
  = cv2.f
@@ -719,12 +719,12 @@
 ETR_
-LIST
+TREE
 , cv
@@ -747,26 +747,24 @@
 IMPLE)%0D%0A
-%0D%0A
         
 for cnt 
@@ -759,18 +759,37 @@
     
-for cnt in
+rects = map(cv2.boundingRect,
  con
@@ -793,17 +793,17 @@
 contours
-:
+)
 %0D%0A      
@@ -808,46 +808,125 @@
     
-    x, y, w, h = cv2.boundingRect
+valid_flags = %5B 16 %3C= h %3C= 64  and w %3C= 1.2*h  for x, y, w, h in rects%5D%0D%0A%0D%0A        for i, cnt in enumerate
 (c
+o
 nt
-)
+ours):
 %0D%0A  
@@ -942,64 +942,212 @@
  if 
-h %3C 16 or h %3E 60 or 1.2*h %3C w:%0D%0A                continue
+not valid_flags%5Bi%5D:%0D%0A                continue%0D%0A            _, _, _, outer_i = heirs%5B0, i%5D%0D%0A            if outer_i %3E=0 and valid_flags%5Bouter_i%5D:%0D%0A                continue%0D%0A            x, y, w, h = rects%5Bi%5D
 %0D%0A  

discard digit's internal contours in digits_video.py