Example program for ocr yogurt

* This example demonstrates how to segment the expiration date of
* a yoghurt cap. After the segmentation, the characters are classified
* by the mlp ocr classifier using the dotprint font.
* 
* 
* Display initializations
dev_update_off ()
dev_close_window ()
read_image (Image, 'ocr/yogurt_lid_01')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_colored (12)
dev_set_line_width (2)
* 
read_ocr_class_cnn ('Universal_NoRej.occ', OCRHandle)
* Approximate the character's dot size
DotDiameter := 6
* Rough character size
CharHeight := 60
CharWidth := 50
* Number of candidates returned by the classifier
NCandidates := 5
for I := 1 to 4 by 1
    read_image (Image, 'ocr/yogurt_lid_' + I$'02')
    * 
    * Segmentation of characters on yoghurt cover
    decompose3 (Image, ImageR, ImageG, ImageB)
    * diff_of_gauss() emphasizes structures having a characteristic dimension of size
    * DotDiameter (line thickness, maximum diameter, ...)
    diff_of_gauss (ImageR, DiffOfGauss, DotDiameter / 2, 2)
    scale_image_max (DiffOfGauss, ImageScaleMax)
    * Merge dots
    gray_dilation_shape (ImageScaleMax, ImageMax, 3, 3, 'octagon')
    * Segment_characters expects black on white characters
    invert_image (ImageMax, ImageInvert)
    segment_characters (ImageInvert, ImageInvert, ImageForeground, RegionForeground, 'local_contrast_best', 'false', 'true', 'medium', CharWidth, CharHeight, 20, 40, UsedThreshold)
    closing_circle (RegionForeground, RegionClosing, 3)
    connection (RegionClosing, ConnectedRegions)
    select_shape (ConnectedRegions, SelectedRegions, 'height', 'and', 39, 65)
    * 
    * We assume that characters are aligned along a line and
    * the regions over or below this line are distractors
    area_center (SelectedRegions, Area, Row, Column)
    get_image_size (Image, Width, Height)
    tuple_histo_range (Row, 0, Height, 2 * Height / CharHeight, Histo, BinSize)
    tuple_find (Histo, max(Histo), IndMax)
    CharRow := BinSize * (IndMax[0] + 0.5)
    select_shape (SelectedRegions, Characters, 'row', 'and', CharRow - CharHeight / 2, CharRow + CharHeight / 2)
    * 
    * Read out the characters on the yoghurt cover
    sort_region (Characters, SortedRegions, 'character', 'true', 'row')
    do_ocr_word_cnn (SortedRegions, ImageForeground, OCRHandle, '\\d{4}[A-Z]', NCandidates, 3, Class, Confidence, Word, Score)
    * 
    DateString := Word{0} + Word{1} + '.' + Word{2} + Word{3} + '.'
    BatchID := Word{4}
    * 
    dev_display (Image)
    dev_display (SortedRegions)
    disp_message (WindowHandle, 'Best before: ' + DateString + '\nBatch ID   : ' + BatchID, 'window', 12, 12, 'black', 'true')
    if (I < 4)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor
clear_ocr_class_cnn (OCRHandle)

 

發佈了20 篇原創文章 · 獲贊 3 · 訪問量 5343
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章