Development of the ocr part of AOI
Samo Penic
2018-11-21 5460bf601a854c842342a740df0f6d36ad785bbc
commit | author | age
69abed 1 3
5460bf 2 W¬õ[Úã@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
8 cCsâ|ddkrd}n|ddkr$d}nd}tj|tjtddƒdd}tj|tjtd    d
9 ƒd d}t|d kƒ}tjd |ƒtj|jƒtj    tj
5460bf 10 ƒ\}}}t |dd„d}    tj |    dƒ\}
69abed 11 } } } || | | …|
SP 12 d
13 ||
5460bf 14 | d…f}|S)NrÚ1é#ré(éé)Ú
69abed 15 iterationséééé2z/tmp/sidblock1.pngcSs
5460bf 16 tj|ƒS)N)Úcv2Z contourArea)Úctrrrr    Ú<lambda>%sz#find_biggest_blob.<locals>.<lambda>)Úkeyééÿÿÿÿ) rÚ morphologyExÚ
69abed 17 MORPH_OPENr
5460bf 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
5460bf 23     rFT)Úzip)Úsid_nor'ÚsÚesrrr    Ú sid_compare,sr3cCsò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
5460bf 33     Úz/tmp/sid_contour1.pngcSstj|ƒdS)Nr)rr$)rrrr    rHsz%segment_by_contours.<locals>.<lambda>)rrr é€é z/tmp/sid_no_{}.pngrgào@r)r6r6r)r.rrrr r!r"r#Ú    enumerater$rÚresizeÚformatÚstrÚpredictÚreshape)r%r&Ú
SP 34 classifierr'r0r(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
5460bf 52     r4z/tmp/sidblock2.pngrrNrr5r6z/tmp/sid_no_{}.pnggào@)r6r6r) Úlenr.rrÚshapeÚintÚrangerr8r9r:r;r<) r%r&r'r=r0Zsid_lenÚ    imgHeightÚimgWidthZnumWidthr>Únumrrr    Úsegment_by_sid_len[s       &rHc 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é
5460bf 70 )rrz/tmp/sid_3rd1.pngrNrgè?r4Téÿz/tmp/sid_3rd2.pngr5r6z/tmp/sid_3no_{}.pnggào@rrr)rrJrJr)r6r6r)rrÚ MORPH_CLOSEr
SP 71 rrÚimreadÚ    template8rBÚ matchTemplateÚTM_CCOEFF_NORMEDrÚwhereÚcvtColorÚCOLOR_GRAY2BGRr/ÚappendrAr#ÚdiffrCÚarrayÚ    rectangler7r8r9r:r;r<)r%r&r'r=Z block_imageÚtemplater,r-ÚresÚlocÚcimgÚloc_filtered_xÚloc_filtered_yÚptÚaÚpointsr0r>rGrrr    Úsegment_by_7segmentsusJ      
69abed 72 
5460bf 73 ,  , (r`cCsrg}g}d|}|jƒ}t|dkƒ}tjd|ƒtj|tjtddƒdd}tj|tjtddƒdd}ttj    |d    kƒƒ}tjd
SP 74 |ƒtj|tj
75 tddƒdd}tj|tjtd d ƒƒ}ttj |d kƒƒ}tj|tj
76 td d ƒƒ}tjd |ƒt ||||ƒ}t |ƒt |ƒkst||ƒ r,|jdƒt||||ƒ}t |ƒt |ƒkrVt||||ƒ}|jdƒt||ƒshdg}|||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
5460bf 87     rJéFz/tmp/enSID0.pngrrr)rrr5z/tmp/enSID1.pngrIgà?z/tmp/enhancedSID.pngzTrying second SID algorithm.zTrying third SID algorithm.z
SP 88 Wrong SID!)r rrrrrKr
89 rrZthinrZ skeletonizer@rAr3rSr`rH)r%r=r'Zsid_warnZsid_errZimage_originalr0rrr    ÚgetSID­s2      
69abed 90 
5460bf 91  rb)rÚnumpyrZskimagerrÚ pkg_resourcesZ templatefileÚresource_filenameÚ__name__rMr
SP 92 r.r3r@rHr`rbrrrr    Ú<module>s  "8