<s> import numpy as np <EOL> import cv2 <EOL> import os <EOL> import sys <EOL> import video <EOL> from common import mosaic <EOL> from digits import * <EOL> def main ( ) : <EOL> try : src = sys . argv [ <NUM_LIT:1> ] <EOL> except : src = <NUM_LIT:0> <EOL> cap = video . create_capture ( src ) <EOL> classifier_fn = '<STR_LIT>' <EOL> if not os . path . exists ( classifier_fn ) : <EOL> print '<STR_LIT>' % classifier_fn <EOL> return <EOL> model = SVM ( ) <EOL> model . load ( classifier_fn ) <EOL> while True : <EOL> ret , frame = cap . read ( ) <EOL> gray = cv2 . cvtColor ( frame , cv2 . COLOR_BGR2GRAY ) <EOL> bin = cv2 . adaptiveThreshold ( gray , <NUM_LIT:255> , cv2 . ADAPTIVE_THRESH_MEAN_C , cv2 . THRESH_BINARY_INV , <NUM_LIT> , <NUM_LIT:10> ) <EOL> bin = cv2 . medianBlur ( bin , <NUM_LIT:3> ) <EOL> contours , heirs = cv2 . findContours ( bin . copy ( ) , cv2 . RETR_CCOMP , cv2 . CHAIN_APPROX_SIMPLE ) <EOL> try : heirs = heirs [ <NUM_LIT:0> ] <EOL> except : heirs = [ ] <EOL> for cnt , heir in zip ( contours , heirs ) : <EOL> _ , _ , _ , outer_i = heir <EOL> if outer_i >= <NUM_LIT:0> : <EOL> continue <EOL> x , y , w , h = cv2 . boundingRect ( cnt ) <EOL> if not ( <NUM_LIT:16> <= h <= <NUM_LIT:64> and w <= <NUM_LIT> * h ) : <EOL> continue <EOL> pad = max ( h - w , <NUM_LIT:0> ) <EOL> x , w = x - pad / <NUM_LIT:2> , w + pad <EOL> cv2 . rectangle ( frame , ( x , y ) , ( x + w , y + h ) , ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) ) <EOL> bin_roi = bin [ y : , x : ] [ : h , : w ] <EOL> gray_roi = gray [ y : , x : ] [ : h , : w ] <EOL> m = bin_roi != <NUM_LIT:0> <EOL> if not <NUM_LIT:0.1> < m . mean ( ) < <NUM_LIT> : <EOL> continue <EOL> '''<STR_LIT>''' <EOL> s = <NUM_LIT> * float ( h ) / SZ <EOL> m = cv2 . moments ( bin_roi ) <EOL> c1 = np . float32 ( [ m [ '<STR_LIT>' ] , m [ '<STR_LIT>' ] ] ) / m [ '<STR_LIT>' ] <EOL> c0 = np . float32 ( [ SZ / <NUM_LIT:2> , SZ / <NUM_LIT:2> ] ) <EOL> t = c1 - s * c0 <EOL> A = np . zeros ( ( <NUM_LIT:2> , <NUM_LIT:3> ) , np . float32 ) <EOL> A [ : , : <NUM_LIT:2> ] = np . eye ( <NUM_LIT:2> ) * s <EOL> A [ : , <NUM_LIT:2> ] = t <EOL> bin_norm = cv2 . warpAffine ( bin_roi , A , ( SZ , SZ ) , flags = cv2 . WARP_INVERSE_MAP | cv2 . INTER_LINEAR ) <EOL> bin_norm = deskew ( bin_norm ) <EOL> if x + w + SZ < frame . shape [ <NUM_LIT:1> ] and y + SZ < frame . shape [ <NUM_LIT:0> ] : <EOL> frame [ y : , x + w : ] [ : SZ , : SZ ] = bin_norm [ ... , np . newaxis ] <EOL> sample = preprocess_hog ( [ bin_norm ] ) <EOL> digit = model . predict ( sample ) [ <NUM_LIT:0> ] <EOL> cv2 . putText ( frame , '<STR_LIT>' % digit , ( x , y ) , cv2 . FONT_HERSHEY_PLAIN , <NUM_LIT:1.0> , ( <NUM_LIT:200> , <NUM_LIT:0> , <NUM_LIT:0> ) , thickness = <NUM_LIT:1> ) <EOL> cv2 . imshow ( '<STR_LIT>' , frame ) <EOL> cv2 . imshow ( '<STR_LIT>' , bin ) <EOL> ch = cv2 . waitKey ( <NUM_LIT:1> ) <EOL> if ch == <NUM_LIT> : <EOL> break <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
