Development of the ocr part of AOI
Samo Penic
2018-11-21 5460bf601a854c842342a740df0f6d36ad785bbc
commit | author | age
69abed 1 3
5460bf 2 í¬õ[N+ã@sZddlmZddlmZddlZddlZddlZddlZdZ    ej
SP 3 e e    ƒZ Gdd„dƒZ dS)é)Údecodeé)ÚgetSIDNz/template-sq.pngc@sˆeZdZd#dd„Zd$dd„Zd%d    d
69abed 4 „Zd d „Zd d„Zd&dd„Zdd„Z    dd„Z
SP 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 
5460bf 10  zPaper.loadImageú/tmp/debug_image.pngcCstj||jƒdS)N)rÚimwriter)rrrrrÚ    saveImage$szPaper.saveImagecCsD|jdkrdS|jƒ|jƒtjd|jƒd}|jƒ|jƒdS)NTz/tmp/debug_threshold.pngr)r
SP 11 ÚdecodeQRandRotateÚ imgTresholdrr ÚbwimgÚgenerateAnswerMatrixr!)rZ    skewAnglerrrr's
12 z Paper.runOcrcCsÞ|jdkrdStj|jd
69abed 13 ƒ}t|ƒ}||_t|ƒdkrR|jjdƒd|_d|_dSt|ƒdkrŠx*|D]"}|j    dks||j    dkrd||d<PqdW||_
5460bf 14 |dj|_ |dj j }|dj j}||jdkrÚ||jdkrÚ|jd    ƒdS) NTérzQR code could not be found!rÚEAN13ZQRg@é´)r&r&)r
SP 15 rÚblurrrÚlenr rÚdataÚtypeÚQRDecoder ZrectÚleftÚtoprrÚ rotateAngle)rr)ÚdZddZxposZyposrrrr"8s*
69abed 16    
SP 17    zPaper.decodeQRandRotatec    Csdtj|jd|jdf|dƒ}tj|j||j|jftjtjdd}||_|jjdd…\|_|_dS)Nrgð?éÿ)ÚflagsZ
5460bf 18 borderModeZ borderValuer)r2r2r2)    rZgetRotationMatrix2DrrZ
SP 19 warpAffinerZ INTER_CUBICZBORDER_CONSTANTr)rÚangleZrot_matÚresultrrrr0Ps
20 zPaper.rotateAnglecCs&tj|jddtjtjBƒ\|_|_dS)Né€r2)rÚ    thresholdrZ THRESH_BINARYZ THRESH_OTSUZthreshr$)rrrrr#dszPaper.imgTresholdc     Csàd|j}tjd|ƒd}d}tj|jtjƒ}x|tj|dtjddƒD]b}|d\}}}}    tj    |||f||    fd dƒtj
69abed 21 |    |||ƒ}
SP 22 |
23 rFt |
24 ƒd    krF||
25 7}|d7}qFWytj ||ƒ} Wnd} YnXtjd
5460bf 26 |ƒ| S) Nr2z/tmp/debug_1.pngrgrr(iEré
SP 27 z/tmp/debug_2.png)rrr2) r$rr ÚcvtColorrÚCOLOR_GRAY2BGRZ HoughLinesPÚnpZpiÚlineZarctan2ÚabsZrad2deg) rÚnegZ angle_counterr4Úcimgr<Zx1Zy1Zx2Zy2Z
28 this_angleZskewrrrÚ getSkewAnglejs&
69abed 29   
SP 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| ƒ} | ||
5460bf 40 |g}xBt |ddd …ŽD],} tj|    | | d|| d|fd dƒq>Wtjd|    ƒ||_|S)Nrréé(Tr2rz/tmp/debug_3.pngéÿÿÿÿrErErE)rr2r2)rrÚmarkerfilenamerr$Ú matchTemplateÚTM_CCOEFF_NORMEDr;Úwherer9r:r*ÚminÚziprÚsortedÚdiffÚarrayÚ    rectangler ÚxMarkerLocations)rr7ZheightÚtemplateÚwÚhÚcrop_imgÚresÚlocr?Úloc_filtered_xÚloc_filtered_yZmin_yÚptÚarrrÚlocateUpMarkersˆs6  
69abed 41 
5460bf 42 , zPaper.locateUpMarkerscCsÜtjtdƒ}|jddd
SP 43 …\}}|jdd…| d…f}tjd|ƒtj||tjƒ}tj    ||kƒ}tj
44 |tj ƒ}    g}
45 g} t |dƒdkrd } ntj |dƒ} xFt|ddd …ŽD]0}|d| dkr²| j|dƒ|
46 j|dƒq²Wyttt| |
69abed 47 ƒƒŽ\} }
5460bf 48 Wn*tjddgƒtjddgƒg|_|jStj| ƒdk}tj|dƒ}tj|
69abed 49 ƒ}
5460bf 50 tj| ƒ} | ||
SP 51 |g}xBt|ddd …ŽD],}tj|    ||d||d|fddƒq~Wtjd    |    ƒ|d|d|j|g|_|jS)Nrrz/tmp/debug_right.pngrCrrDTr2z/tmp/debug_4.pngrErErErE)rr2r2)rrrFrr$r rGrHr;rIr9r:r*ÚmaxrKrrLrNÚyMarkerLocationsrMrOr)rr7ÚwidthrQrRrSrTrUrVr?rWrXZmin_xZmax_xrYrZrrrÚlocateRightMarkers­s@   
69abed 52 
SP 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)
5460bf 56 r[r_Ú answerMatrixr]rPr$Ú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)
SP 57 NreÚsid_maskg{®Gáz¤?gR¸…ëQ¸?gÍÌÌÌÌÌä?gffffffî?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
69abed 59 
5460bf 60  zPaper.get_enhanced_sidcCsÖ|jdkr*|jjdƒddddddœ}|Stj|jdƒ}|jdjdkr|t|dd…ƒt|dƒdt|dd…ƒdddœS|jd    ƒ}t|dƒt|d
SP 61 ƒt|d ƒt|dƒddœ}t    |ƒd krÎ|d |d <|SdS)Nz+Could not read QR or EAN code! Not an exam?)Zexam_idÚpage_noZpaper_idZ
62 faculty_idrÚutf8rr'érr`ú,r&réréûÿÿÿrE)
63 r r rÚbytesrr-r,rcÚsplitr*)rZretvalZqrdatar+rrrÚ get_code_dataÿs0
69abed 64  
SP 65 
5460bf 66 
SP 67 
68 
69   zPaper.get_code_datacCs|jƒ}tj|jdƒ|d<|j|d<|j|d<t|jd|jƒt|jd|j    ƒf|d<t|j
70 d|jƒt|j
71 d|j    ƒf|d<t j |j ƒ|jd    kdjƒ|d
72 <|d dkrÌ|d dkrÌ|jƒ|d <tjj|jd j|jjdƒdjd ƒdd…ƒdƒ}tj||jƒ||d<|S)NroZqrr rrrZ up_positionZright_positionrZ
73 ans_matrixrrnÚ.ú/z.pngÚoutput_filenamerErE)rvrtrr r rÚlistrPrrr]r;rNrbr ÚtolistrmÚosÚpathÚjoinr    rrurr r)rr+ryrrrÚget_paper_ocr_data!s
69abed 74 
SP 75 " 4zPaper.get_paper_ocr_data)NNNr)r)r)r)rArB)rArB)Ú__name__Ú
5460bf 76 __module__Ú __qualname__rrr!rr"r0r#r@r[r_r%rmrvrrrrrr s
69abed 77 
SP 78 
5460bf 79 
SP 80 
69abed 81 %
5460bf 82 *"r)Z pyzbar.pyzbarrZ sid_processrrZnumpyr;r|Ú pkg_resourcesZ
SP 83 markerfileÚresource_filenamer€rFrrrrrÚ<module>s