Flow Chart for OCR Training

Flow Chart for OCR Training


Code:

import sys

import numpy as np
import cv2
import glob

images = glob.glob('./train_image/*.jpg')
#im = cv2.imread('sign_train.png')
#im3 = im.copy()
samples =  np.empty((0,100))
responses = []
keys = [i for i in range(48,58)]

offset = 2

for fname in images:
    im = cv2.imread(fname)
    [depth, width, height] = im.shape[::-1]
    im_def = im.copy()
    
    ########### Sign Detect New One 2.21
    sign_finding = 2.21*im[:,:,2] - im[:,:,1] - im[:,:,0]
    sign_finding = np.where(sign_finding > 60, sign_finding, 0)
    sign_finding = np.uint8(np.abs(sign_finding))

    sign_t,sign_contours,sign_hierarchy = cv2.findContours(sign_finding,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    
    max_x = 0
    max_y = 0
    min_x = width
    min_y = height
    select_area = 999999
    for cnt in sign_contours:
        if cv2.contourArea(cnt)>300:
            [x,y,w,h] = cv2.boundingRect(cnt)
            #if max_x < x:
            #    max_x = x + w
            #if min_x > x:
            #    min_x = x
            #if max_y < y:
            #    max_y = y + h
            #if min_y > y:
            #    min_y = y
            if cv2.contourArea(cnt) < select_area:
                select_area = cv2.contourArea(cnt)
                min_x = x
                min_y = y
                max_x = x + w
                max_y = y + h
            cv2.rectangle(im_def,(x,y),(x+w,y+h),(0,255,0),2)

    cv2.rectangle(im_def,(min_x,min_y),(max_x,max_y),(255,0,0),2)
    
    cv2.imshow('Sign Finding', sign_finding)
    cv2.imshow('Sign Crop', im_def)
    
    #while True:
    #    if cv2.waitKey(1) & 0xFF == ord('p'):
    #        break
    
    im = im[min_y:max_y, min_x:max_x, :]
    gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray,(5,5),0)
    thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
    #thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)

    
    
    #################      Now finding Contours         ###################
    #print(size(cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)))
    im2, contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    
    [depth, width, height] = im.shape[::-1]
    print("-----------")
    for cnt in contours:
        #if cv2.contourArea(cnt)>160 and cv2.contourArea(cnt)<500:
        if cv2.contourArea(cnt)>85:
            
            [x,y,w,h] = cv2.boundingRect(cnt)
            
            print(w)
            print(h)
            #cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
            #cv2.imshow('norm',im)
            if  w>10 and w<22 and h>20 and h<35: #28
                print(cv2.contourArea(cnt))
                print("In")
                y_start = y-offset
                y_stop = y+h+offset
                x_start = x-offset
                x_stop = x+w+offset
                if y_start < 0:
                    y_start = 0
                if y_stop > height:
                    y_stop = height
                if x_start < 0:
                    x_start = 0
                if x_stop > width:
                    x_stop = width
                cv2.rectangle(im,(x_start,y_start),(x_stop,y_stop),(0,0,255),2)
                roi = thresh[y_start:y_stop,x_start:x_stop]
                roismall = cv2.resize(roi,(10,10))
                cv2.imshow('Thresh',thresh)
                cv2.imshow('norm',im)
                key = cv2.waitKey(0)
                print("---")
                print(int(chr(key)))

                if key == 27:  # (escape to quit)
                    sys.exit()
                elif key in keys:
                    responses.append(int(chr(key)))
                    sample = roismall.reshape((1,100))
                    samples = np.append(samples,sample,0)

responses = np.array(responses,np.float32)
responses = responses.reshape((responses.size,1))
print("training complete")

np.savetxt('generalsamples_sign.data',samples)
np.savetxt('generalresponses_sign.data',responses)

Comments

Popular posts from this blog

Flow Chart for Car Tracking

Flow Chart for OCR Detection