Beispiel #1
0
def main():
    # parse arguments
    args = _args()
    
    # edit config
    if args.config: _editconfig(args)
    
    if not args.quiet: print()
    if args.verbose: print('initializing configurations.... done')
    
    # download data
    if args.verbose: print('downloading data............... ', end = '')
    mldata = skdata.fetch_mldata("MNIST Original")
    if args.verbose: print('done')
    
    # separate featues and labels
    X = np.array(mldata.data, dtype='int16')
    y = np.array(mldata.target, dtype='int')
    
    # update config
    if args.verbose: print('updating configurations........ ', end = '')
    config = _updateconfig(args=args, data=X)
    if args.verbose: print('done')
    
    # preprocess featues
    if args.haar:
        X = _haar(X, load=args.load, save=args.save, verbose=args.verbose)
    elif args.hogs:
        X = _hogs(X, load=args.load, save=args.save, verbose=args.verbose)
    elif args.sift:
        X = _sift(X, load=args.load, save=args.save, verbose=args.verbose)
    elif args.surf:
        X = _surf(X, load=args.load, save=args.save, verbose=args.verbose)
    else:
        X = _fscale(X, split=True, verbose=args.verbose)
    
    # create training and cross-validation sets
    split_d = config['train_d']
    X_train = X[:split_d]
    y_train = y[:split_d]
    X_valid = X[split_d:]
    y_valid = y[split_d:]
    
    # create classifier
    if args.lr:
        classifier = lm.LogisticRegression(solver='lbfgs', multi_class='multinomial')
    elif args.sv:
        classifier = sv.LinearSVC()
    else:
        classifier = lm.LogisticRegression()
    
    # train classifier
    if args.verbose: print('training classifier............ ', end = '')
    t_beg = time.time()
    classifier.fit(X_train, y_train)
    t_end = time.time()
    if args.verbose: print('done @ %8.2f sec' %(t_end - t_beg))
    
    # save classifier
    if args.save:
        if args.verbose: print('dumping classifier............. ', end = '')
        skex.joblib.dump(classifier, config['clfr_data_path'], compress=9)
        if args.verbose: print('done')
    
    # cross-validate classifier
    if not args.quiet: print('training error................. ', end = '')
    p_train = classifier.predict(X_train)
    e_train = round(sum(y_train!=p_train) / len(y_train) * 100, 2)
    if not args.quiet: print(str(e_train) + '%')
    
    if not args.quiet: print('cross-validation error......... ', end = '')
    p_valid = classifier.predict(X_valid)
    e_valid = round(sum(y_valid!=p_valid) / len(y_valid) * 100, 2)
    if not args.quiet: print(str(e_valid) + '%')
    
    if args.plot:
        train = np.c_[y_train, X_train]
        valid = np.c_[y_valid, X_valid]
        steps = config['steps_d']
        _plot(classifier, train, valid, steps, save=args.save, verbose=args.verbose)
    
    if not args.quiet: print()
Beispiel #2
0
def main():
    # parse arguments
    args = _args()
    
    # begin time
    t_beg = time.time()
    
    # load config
    config     = _configinfo()
    preprocess = config['preprocess']
    shape_x    = config['shape_x']
    shape_y    = config['shape_y']
    clfrpath   = config['clfr_data_path']
    rootpath   = config['root_data_path']
    filepath   = args.filepath
    savename   = rootpath + os.path.split(filepath)[-1].split('.')[0]
    
    if args.verbose: print('\ninitializing configurations.... done')
    
    # load classifier
    if args.verbose: print('loading classifier............. ', end = '')
    if os.path.isfile(clfrpath):
        classifier = skex.joblib.load(clfrpath)
        if args.verbose: print('done')
    else:
        if args.verbose: print('not found\n')
        sys.exit()
    
    # load image
    if args.verbose: print('loading image.................. ', end = '')
    if os.path.isfile(filepath):
        (_, extn) = os.path.splitext(filepath)
        if extn in ['.bmp', '.jpeg', '.jpg', '.png', '.tif', '.tiff']:
            image = cv.imread(filepath)
            if args.verbose: print('done')
        else:
            if args.verbose: print('unsupported format\n')
            sys.exit()
    else:
        if args.verbose: print('not found\n')
        sys.exit()
    
    image_input = image.copy()
    
    # convert image to grayscale
    if args.verbose: print('converting colorspace.......... ', end = '')
    image_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    if args.verbose: print('done')
    
    # apply guassian blurring to remove noise
    if args.verbose: print('removing noise from image...... ', end = '')
    image_blur = cv.GaussianBlur(image_gray, (3, 3), 0, 0)
    if args.verbose: print('done')
    
    # threshold image
    if args.verbose: print('thresholding image............. ', end = '')
    (_, image_th) = cv.threshold(image_blur, 95, 255, cv.THRESH_BINARY_INV)
    if args.verbose: print('done')
    
    image_rois = image_th.copy()
    
    # find contours
    if args.verbose: print('finding contours in  image..... ', end = '')
    (_, contours, _) = cv.findContours(image_th.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
    if args.verbose: print('done')
    
    # find bounding rectangle around each contour
    bn_rects = []
    for cntr in contours:
        bn_rects.append(cv.boundingRect(cntr))
    if args.verbose: print('number of objects detected..... %d' %(len(bn_rects)))
    
    # process each bounding rectangle
    loop = 0
    for rect in bn_rects:
        if args.verbose: print('\rprocessing objects............. %d%%'
                               %(loop*100//len(bn_rects)), end = '')
        
        # attributes of bounding rectangle
        x = rect[0]
        y = rect[1]
        w = rect[2]
        h = rect[3]
        
        # ignore tiny objects assuming them as noise
        if h <= 8:
            continue
        
        # draw bounding rectangle on images
        cv.rectangle(image_rois, (x, y), (x+w, y+h), (255, 255, 255), 1)
        cv.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
        
        # extract region of interest from thresholded image using attributes of bounding rectangle
        image_roi = image_th[y:y+h, x:x+w]
        
        # pad region of interest
        if w >= h:
            length = int(1.8 * w)
        else:
            length = int(1.8 * h)
        image_pad = np.zeros((length, length), dtype='uint8')
        i = (length - h) // 2
        j = (length - w) // 2
        image_pad[i:i+h, j:j+w] = image_roi
        
        # resize padded image
        image_roi = cv.resize(image_pad, (shape_x, shape_y), interpolation=cv.INTER_AREA)
        
        # perform dilation as morphological transformation
        image_roi = cv.dilate(image_roi, (3, 3))
        
        # preprocess roi
        image_roi = image_roi.reshape(1, -1)
        
        if   preprocess == 'haar': X = _haar(image_roi, load=False)
        elif preprocess == 'hogs': X = _hogs(image_roi, load=False)
        elif preprocess == 'sift': X = _sift(image_roi, load=False)
        elif preprocess == 'surf': X = _surf(image_roi, load=False)
        else:X = _fscale(image_roi, load=True)
        
        X = X.reshape(1, -1)
        
        # predict label
        pred = classifier.predict(X)
        pred = pred[0]
        
        # put predicted label on image
        cv.putText(image, str(pred), (x, y-8), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
        
        loop = loop + 1
    
    if args.verbose: print('\rprocessing objects............. done')
    
    # save images
    if args.save:
        if args.verbose: print('saving images.................. ', end = '')
        cv.imwrite(savename+'_1_input.png' , image_input)
        cv.imwrite(savename+'_2_gray.png'  , image_gray)
        cv.imwrite(savename+'_3_blur.png'  , image_blur)
        cv.imwrite(savename+'_4_th.png'    , image_th)
        cv.imwrite(savename+'_5_rois.png'  , image_rois)
        cv.imwrite(savename+'_6_output.png', image)
        if args.verbose: print('done')
    
    # end time
    t_end = time.time()
    
    if args.verbose: print('processing time................ %.2f sec\n'
                           %(t_end - t_beg))
    
    # display image with predictions
    cv.imshow('DIGIT RECOGNIZER', image)
    
    cv.waitKey(0)
    cv.destroyAllWindows()