Development of the ocr part of AOI
Samo Penic
2018-11-24 1b70cab35ec6399d047bb717257c1d2bc579d592
commit | author | age
69abed 1 3
1b70ca 2 Í÷[ ã@stddlZddlZddlmZmZddlZdZeje    eƒZ
69abed 3 dd„Z dd„Z dd    „Z d
SP 4 d „Zd d „Zdd„Zdd„ZdS)éN)Ú
5 morphologyÚ img_as_ubytez/template-8.pngcCstj||ftjƒS)z8
6     Function greates square kernel of size x and y
7     )ÚnpZonesÚuint8)ÚxÚy©rú@/home/samo/programiranje/python/sizif-ocr/aoi_ocr/sid_process.pyÚkernel sr
1b70ca 8 cCsâ|ddkrd}n|ddkr$d}nd}tj|tjtddƒdd}tj|tjtd    d
SP 9 ƒdd}t|dkƒ}tjd |ƒtj|jƒtj    tj
10 ƒ\}}}t |d d „d}    tj |    dƒ\}
69abed 11 } } } || | | …|
SP 12 d
13 ||
1b70ca 14 | d…f}|S)NrÚ1é-ré2éé)Ú
SP 15 iterationsééz/tmp/sidblock1.pngcSs
16 tj|ƒS)N)Úcv2Z contourArea)Úctrrrr    Ú<lambda>%sz#find_biggest_blob.<locals>.<lambda>)Úkeyéééÿÿÿÿ) rÚ morphologyExÚ
69abed 17 MORPH_OPENr
1b70ca 18 Ú MORPH_DILATErÚimwriteÚ findContoursÚcopyÚ RETR_EXTERNALÚCHAIN_APPROX_SIMPLEÚsortedÚ boundingRect)ÚimageÚoriginal_imageÚsid_maskZ    move_leftZimage2Zimage1Úim2ÚctrsÚhierÚ sorted_ctrsrrÚwÚhrrr    Úfind_biggest_blobs"    (r-cCs0x*t||ƒD]\}}|dkr ||kr dSq WdS)zÎ
69abed 19     Function compares student id number with student id mask if the recognised number is valid according to the mask
SP 20     :param sid_no:
21     :param sid_mask:
22     :return: True if they match, else False
1b70ca 23     rFT)Úzip)Úsid_nor&ÚsÚesrrr    Ú sid_compare,sr2cCsòd}t|||ƒ}tjd|ƒtj|jƒtjtjƒ\}}}t|dd„d}x¤t|ƒD]˜\}    }
69abed 24 tj    |
SP 25 ƒ\} } } }| |dkr‚|d}qR|| | |…| | | …f}t
26 |dkƒ}tj |dƒ}tjd
27 j |    ƒ|ƒ|t |j|jd dƒd ƒd ƒ}qRW|S)zæ
28     First algorithm. it segments numerals with contours. It works with numbers where individual numerals does not touch.
29     :param image:
30     :param original_image:
31     :param classifier:
32     :return: student id as a string
1b70ca 33     Úz/tmp/sid_contour1.pngcSstj|ƒdS)Nr)rr#)rrrr    rHsz%segment_by_contours.<locals>.<lambda>)rrr é€é z/tmp/sid_no_{}.pngrgào@r)r5r5r)r-rrrrr r!r"Ú    enumerater#rÚresizeÚformatÚstrÚpredictÚreshape)r$r%Ú
SP 34 classifierr&r/r'r(r)r*Úirrrr+r,Úroirrr    Úsegment_by_contours9s"         &r?c CsÆd}t|ƒ}t|||ƒ}tjd|ƒ|jdd…\}}t||ƒ}x~td|ƒD]p}    |dd…|    ||    d|…f}
69abed 35 t|
SP 36 dkƒ}
37 tj|
38 d ƒ}
39 tjd    j    |    ƒ|
40 ƒ|t
41 |j |
42 j dd ƒd
43 ƒdƒ}qNW|S) aR
44     Third algorithm. It trys to get biggest "blob" in the image and then it cuts it into individual numbers by force.
45     It has some problems with finding individual numbers, so some tweaking must be done!
46
47     :param image:
48     :param original_image:
49     :param sid_mask:
50     :param classifier:
51     :return: student id as a string
1b70ca 52     r3z/tmp/sidblock2.pngrrNrr4r5z/tmp/sid_no_{}.pnggào@)r5r5r) Úlenr-rrÚshapeÚintÚrangerr7r8r9r:r;) r$r%r&r<r/Zsid_lenÚ    imgHeightÚimgWidthZnumWidthr=Únumrrr    Úsegment_by_sid_len[s       &rGc CsHtj|tjtddƒdd}t|dkƒ}tjd|ƒtjtdƒ}|jddd…\}}tj    ||tj
69abed 53 ƒ}t j |d    kƒ}    tj |tjƒ}
SP 54 g} g} xNt|    ddd…ŽD]8} | dd| ddf} | j| dƒ| j| dƒqšWt| ƒdkræd
55 Sttt| | ƒƒŽ\} } t j| ƒt|dƒk}t j|d ƒ}t j| ƒ} t j| ƒ} | || |g}xBt|ddd…ŽD],} tj|
56 | | d|| d|fddƒqVWtjd |
57 ƒd
58 }x¬tt|ddd…ŽƒD]’\}} || d| d|…| d| d|…f}t|dkƒ}ytj|dƒ}Wn
59 d
60 Stjdj|ƒ|ƒ|t|j|jddƒdƒdƒ}q®W|S)aI
61     Second attempt. It dilates the image to get all 7 segments wisible as 8888888 then it does pattern matching of 8 with
62     pattern image. It works if the scaned gray level is high enough.
63
64     :param image:
65     :param original_image:
66     :param sid_mask:
67     :param classifier:
68     :return: student id number as a string
69     ré
1b70ca 70 )rr z/tmp/sid_3rd1.pngrNrgè?r3Téÿz/tmp/sid_3rd2.pngr4r5z/tmp/sid_3no_{}.pnggào@rrr)rrIrIr)r5r5r)rrÚ MORPH_CLOSEr
SP 71 rrÚimreadÚ    template8rAÚ matchTemplateÚTM_CCOEFF_NORMEDrÚwhereÚcvtColorÚCOLOR_GRAY2BGRr.Úappendr@r"ÚdiffrBÚarrayÚ    rectangler6r7r8r9r:r;)r$r%r&r<Z block_imageÚtemplater+r,ÚresÚlocÚcimgÚloc_filtered_xÚloc_filtered_yÚptÚaÚpointsr/r=rFrrr    Úsegment_by_7segmentsusJ      
69abed 72 
1b70ca 73 ,  , (r_cCsŒg}g}d|}|jƒ}t|dkƒ}tjd|ƒtj|tjtddƒdd}tj|tjtddƒdd}tj|tjtddƒdd}ttj    |d    kƒƒ}tjd
5460bf 74 |ƒtj|tj
1b70ca 75 tdd ƒdd}tj|tjtd d ƒƒ}ttj |d kƒƒ}tj|tj
SP 76 td d ƒƒ}tjd|ƒt ||||ƒ}t |ƒt |ƒks.t||ƒ rF|jdƒt||||ƒ}t |ƒt |ƒkrpt||||ƒ}|jdƒt||ƒs‚dg}|||fS)aÔ
69abed 77     Tries different approaches on image to get student id number. Firstly clears image of noise and then skeletonizes
SP 78     numbers and thickens it until it gets normalized image. It sends it to the segmentation and recognition functions.
79
80     Tweak both MORPH_OPEN lines....
81
82     :param image:
83     :param classifier:
84     :param sid_mask:
85     :return: (student_id, error, warning) student id as a string, list of errors and list of warnings during the recognition
86
1b70ca 87     rIéFz/tmp/enSID0.pngr)rrrér4z/tmp/enSID1.pngrrHgà?z/tmp/enhancedSID.pngzTrying second SID algorithm.zTrying third SID algorithm.z
SP 88 Wrong SID!)rrrrrrr
89 rJrZthinrZ skeletonizer?r@r2rRr_rG)r$r<r&Zsid_warnZsid_errZimage_originalr/rrr    ÚgetSID­s4      
69abed 90 
1b70ca 91  rb)rÚnumpyrZskimagerrÚ pkg_resourcesZ templatefileÚresource_filenameÚ__name__rLr
SP 92 r-r2r?rGr_rbrrrr    Ú<module>s  "8