Development of the ocr part of AOI
Samo Penic
2018-11-18 2fec6b84c1ebc8ea0c257185b83266aae9f57639
commit | author | age
69abed 1 3
SP 2 j|ð[    *ã@sZddlmZddlmZddlZddlZddlZddlZdZ    ej
3 e e    ƒZ Gdd„dƒZ dS)é)Údecodeé)ÚgetSIDNz /template.pngc@sˆeZdZd#dd„Zd$dd„Zd%d    d
4 „Zd d „Zd d„Zd&dd„Zdd„Z    dd„Z
5 d'dd„Z d(dd„Z dd„Z dd„Zdd „Zd!d"„ZdS))ÚPaperNú/tmpcCsd||_||_d|_d|_|dkr(ddin||_g|_g|_d|_||_|dk    r`|j    |ƒ|j
6 ƒdS)NÚanswer_thresholdgÐ?) ÚfilenameÚ output_pathÚinvalidÚQRDataÚsettingsÚerrorsÚwarningsÚsidÚsid_classifierÚ    loadImageÚrunOcr)Úselfrrr r    ©rú8/home/samo/programiranje/python/sizif-ocr/aoi_ocr/Ocr.pyÚ__init__s
7 zPaper.__init__rcCsJtj||ƒ|_|jdkr.|jjdƒd|_dS|jjdd…\|_|_dS)NzFile could not be loaded!Tré)    Úcv2ÚimreadÚimgr Úappendr
8 ÚshapeÚ    imgHeightÚimgWidth)rrZ
9 rgbchannelrrrrs 
10  zPaper.loadImageú/tmp/debug_image.pngcCstj||jƒdS)N)rÚimwriter)rrrrrÚ    saveImage$szPaper.saveImagecCs6|jdkrdS|jƒ|jƒd}|jƒ|jƒdS)NTr)r
11 ÚdecodeQRandRotateÚ imgTresholdÚgenerateAnswerMatrixr!)rZ    skewAnglerrrr's
12 z Paper.runOcrcCsÞ|jdkrdStj|jd
13 ƒ}t|ƒ}||_t|ƒdkrR|jjdƒd|_d|_dSt|ƒdkrŠx*|D]"}|j    dks||j    dkrd||d<PqdW||_
14 |dj|_ |dj j }|dj j}||jdkrÚ||jdkrÚ|jd    ƒdS) NTérzQR code could not be found!rÚEAN13ZQRg@é´)r%r%)r
15 rÚblurrrÚlenr rÚdataÚtypeÚQRDecoder ZrectÚleftÚtoprrÚ rotateAngle)rr(ÚdZddZxposZyposrrrr"7s*
16    
17    zPaper.decodeQRandRotatec    Csdtj|jd|jdf|dƒ}tj|j||j|jftjtjdd}||_|jjdd…\|_|_dS)Nrgð?éÿ)ÚflagsZ
18 borderModeZ borderValuer)r1r1r1)    rZgetRotationMatrix2DrrZ
19 warpAffinerZ INTER_CUBICZBORDER_CONSTANTr)rÚangleZrot_matÚresultrrrr/Os
20 zPaper.rotateAnglecCs&tj|jddtjtjBƒ\|_|_dS)Né€r1)rÚ    thresholdrZ THRESH_BINARYZ THRESH_OTSUZthreshÚbwimg)rrrrr#dszPaper.imgTresholdc     Csàd|j}tjd|ƒd}d}tj|jtjƒ}x|tj|dtjddƒD]b}|d\}}}}    tj    |||f||    fd dƒtj
21 |    |||ƒ}
22 |
23 rFt |
24 ƒd    krF||
25 7}|d7}qFWytj ||ƒ} Wnd} YnXtjd
26 |ƒ| S) Nr1z/tmp/debug_1.pngrgrr'iEré
27 z/tmp/debug_2.png)rrr1) r7rr ÚcvtColorrÚCOLOR_GRAY2BGRZ HoughLinesPÚnpZpiÚlineZarctan2ÚabsZrad2deg) rÚnegZ angle_counterr3Úcimgr<Zx1Zy1Zx2Zy2Z
28 this_angleZskewrrrÚ getSkewAngleis&
29   
30  zPaper.getSkewAngleç333333ë?éÈcCs„tjtdƒ}|jddd    …\}}|jd|…dd…f}tj||tjƒ}tj||kƒ}tj    |tj
31 ƒ}    g}
32 g} t |dƒdkr€d
33 } nîtj |dƒ} xFt |ddd …ŽD]0} | d| dkr¢| j| dƒ|
34 j| dƒq¢Wt tt |
35 | ƒƒŽ\}
36 } tj|
37 ƒdk}tj|dƒ}tj|
38 ƒ}
39 tj| ƒ} | ||
40 |g}xBt |ddd …ŽD],} tj|    | | d|| d|fd dƒq>Wtjd|    ƒ||_|S)Nrréé(Tr1rz/tmp/debug_3.pngéÿÿÿÿrErErE)rr1r1)rrÚmarkerfilenamerrÚ matchTemplateÚTM_CCOEFF_NORMEDr;Úwherer9r:r)ÚminÚziprÚsortedÚdiffÚarrayÚ    rectangler ÚxMarkerLocations)rr6ZheightÚtemplateÚwÚhÚcrop_imgÚresÚlocr?Úloc_filtered_xÚloc_filtered_yZmin_yÚptÚarrrÚlocateUpMarkers‡s6  
41 
42 , zPaper.locateUpMarkerscCsžtjtdƒ}|jddd    …\}}|jdd…| d…f}tj||tjƒ}tj||kƒ}tj    |tj
43 ƒ}    g}
44 g} t |dƒdkr‚d
45 } nîtj |dƒ} xFt |ddd …ŽD]0}|d| dkr¤| j|dƒ|
46 j|dƒq¤Wt tt | |
47 ƒƒŽ\} }
48 tj| ƒdk}tj|dƒ}tj|
49 ƒ}
50 tj| ƒ} | ||
51 |g}xBt |ddd …ŽD],}tj|    ||d||d|fd dƒq@Wtjd|    ƒ|d|d|j|g|_|jS)NrrrCrDTr1rz/tmp/debug_4.pngrErErErE)rr1r1)rrrFrrrGrHr;rIr9r:r)ÚmaxrKrrLrMrNrOr rÚyMarkerLocations)rr6ÚwidthrQrRrSrTrUrVr?rWrXZmin_xZmax_xrYrZrrrÚlocateRightMarkers¬s6  
52 
53 , zPaper.locateRightMarkersc     Cs¼|jƒ|jƒd}d}d}|}||}g|_xˆ|jdD]z}g}xd|jdD]V}|j|||t||ƒ…|||t||ƒ…f}    |tj|    ƒ}
54 |j    |
55 |ƒqNW|jj    |ƒq:WdS)Nr8éé2rr)
56 r[r_Ú answerMatrixr]rPr7ÚintrZ countNonZeror) rZroixoffZroiyoffZroiwidthZ    roiheightZtotpxÚyZonelineÚxZroiZblackrrrr$Ñs"zPaper.generateAnswerMatrixcsœˆjdkrdSˆjdk    r&ˆjjddƒ}tˆjtdˆjƒtdˆjƒ…tdˆjƒtdˆjƒ…fˆj|ƒ\}}}‡fdd„|Dƒ‡fd    d„|Dƒ|S)
57 NreÚsid_maskg{®Gáz¤?gR¸…ëQ¸?gffffffæ?g®Gáz®ï?csg|]}ˆjj|ƒ‘qSr)r r)Ú.0Úe)rrrú
58 <listcomp>õsz*Paper.get_enhanced_sid.<locals>.<listcomp>csg|]}ˆjj|ƒ‘qSr)rr)rgrR)rrrriös)rr Úgetrrrcrr)rrfZesÚerrÚwarnr)rrÚget_enhanced_sidès
59 
60  zPaper.get_enhanced_sidcCsØ|jdkr*|jjdƒddddddœ}|Stj|jdƒ}|jdjdkr|t|dd…ƒt|dƒdt|dd…ƒdddœS|jd    ƒ}t|dƒt|d
61 ƒdt|d ƒt|dƒd œ}t    |ƒd krÐ|d |d<|SdS)Nz+Could not read QR or EAN code! Not an exam?)Úexam_idÚpage_noÚpaper_idÚ
62 faculty_idrÚutf8rr&érr`ú,r%r)rnrorprqéréûÿÿÿrE)
63 r r rÚbytesrr,r+rcÚsplitr))rZretvalZqrdatar*rrrÚ get_code_dataùs.
64  
65 
66 
67   zPaper.get_code_datacCs|jƒ}tj|jdƒ|d<|j|d<|j|d<t|jd|jƒt|j    d|j
68 ƒf|d<t|jd|jƒt|j    d|j
69 ƒf|d<t j |j ƒ|jdkdjƒ|d    <|d
70 dkrÌ|d dkrÌ|jƒ|d
71 <tjj|jd j|jjd ƒdjd ƒdd…ƒdƒ}tj||jƒ||d<|S)NrrZqrr rrZ up_positionZright_positionrZ
72 ans_matrixrroÚ.ú/z.pngÚoutput_filenamerErE)ryrwrr r rÚlistrPrr]rr;rNrbr ÚtolistrmÚosÚpathÚjoinr    rrxrr r)rr*r|rrrÚget_paper_ocr_datas
73 
74 " 4zPaper.get_paper_ocr_data)NNNr)r)r)r)rArB)rArB)Ú__name__Ú
75 __module__Ú __qualname__rrr!rr"r/r#r@r[r_r$rmryr‚rrrrr s
76 
77 
78 
79 
80 %
81 %!r)Z pyzbar.pyzbarrZ sid_processrrZnumpyr;rÚ pkg_resourcesZ
82 markerfileÚresource_filenamerƒrFrrrrrÚ<module>s