1212"""
1313
1414from PIL import Image
15- from preprocessing import ThresholdingManager
16- from preprocessing import BlurManager
17- from preprocessing import ColorManager
18- from preprocessing import SimplificationManager
19- from processing import FaceDetector
20- from processing import BarCodeManager
21- from processing import TextManager
15+ from prototype . preprocessing . thresholding_manager import ThresholdingManager
16+ from prototype . preprocessing . blur_manager import BlurManager
17+ from prototype . preprocessing . color_manager import ColorManager
18+ from prototype . preprocessing . simplification_manager import SimplificationManager
19+ from prototype . processing . face_manager import FaceDetector
20+ from prototype . processing . barcode_manager import BarCodeManager
21+ from prototype . processing . text_manager import TextManager
2222
2323import pytesseract
2424import cv2
2525import os
26- import json
2726
2827# Constants path to trained data for Shape Predictor.
2928SHAPE_PREDICTOR_PATH = "{base_path}/trained_data/shape_predictor_face_landmarks.dat" .format (
3029 base_path = os .path .abspath (os .path .dirname (__file__ )))
3130
31+ DESKTOP = os .path .join (os .path .join (os .path .expanduser ('~' )), 'Desktop' )
32+
3233
3334class TextExtractor :
34- def extract (img , thresh = "adaptive" , blurr = "median" , clr = "red" , rm = False , knl = [7 ]):
35+ def extract (self , img , thresh = "adaptive" , blurr = "median" , clr = "red" , rm = False , knl = [9 ]):
3536
36- image = cv2 . imread ( img )
37+ image = img
3738
3839 simplification_manager = SimplificationManager ()
3940 barcode_manager = BarCodeManager ()
4041 color_manager = ColorManager ()
4142 face_detector = FaceDetector (SHAPE_PREDICTOR_PATH )
4243 image = simplification_manager .perspectiveTransformation (image )
43- cv2 .imwrite (" output/3-warped .png" , image )
44-
44+ cv2 .imwrite (DESKTOP + "/ output/3.png" , image )
45+ data = {}
4546 barcode_data_found , barcode_scan_data , image = barcode_manager .get_barcode_info (image )
4647 if barcode_data_found :
4748 data = {
48- 'ID_number ' : barcode_scan_data .decode ('utf-8' ),
49+ 'identity_number ' : barcode_scan_data .decode ('utf-8' ),
4950 }
50- card_data = json .dumps (data )
51- print (card_data )
5251
53- if rm is True :
52+ if rm :
5453 image = face_detector .blur_face (image )
55- cv2 .imwrite (" output/4-faceRemvoal .png" , image )
54+ cv2 .imwrite (DESKTOP + "/ output/4.png" , image )
5655
57- if clr is not None :
58- if clr == "blackhat" :
59- image = color_manager .blackHat (image )
60- elif clr == "tophat" :
61- image = color_manager .topHat (image )
62- else :
63- image = color_manager .extractChannel (image , clr )
64- cv2 .imwrite ("output/5-colour_extract.png" , image )
65-
66- cv2 .imwrite ("output/colour_extract.png" , image )
67- if knl is not None :
68- blur_kernel = knl
69- else :
56+ if knl :
7057 if blurr == "median" :
71- blur_kernel = [3 ]
58+ blur_kernel = [9 ]
7259 else :
73- blur_kernel = [(3 , 3 )]
74-
75- image = cv2 .cvtColor (image , cv2 .COLOR_BGR2GRAY )
76- cv2 .imwrite ("output/6-gray.png" , image )
60+ blur_kernel = [(9 , 9 )]
61+ else :
62+ blur_kernel = knl
7763
7864 if blurr is not None :
7965 blur_manager = BlurManager ()
@@ -83,8 +69,20 @@ def extract(img, thresh="adaptive", blurr="median", clr="red", rm=False, knl=[7]
8369 image = blur_manager .gaussianBlur (image , blur_kernel = blur_kernel )
8470 elif blurr == "median" :
8571 image = blur_manager .medianBlur (image , blur_kernel = blur_kernel )
72+ cv2 .imwrite (DESKTOP + "/output/5.png" , image )
73+
74+ if clr is not None :
75+ if clr == "blackhat" :
76+ image = color_manager .blackHat (image )
77+ elif clr == "tophat" :
78+ image = color_manager .topHat (image )
79+ else :
80+ image = color_manager .extractChannel (image , clr )
81+ cv2 .imwrite (DESKTOP + "/output/6.png" , image )
82+
83+ image = cv2 .cvtColor (image , cv2 .COLOR_BGR2GRAY )
84+ cv2 .imwrite (DESKTOP + "/output/7.png" , image )
8685
87- cv2 .imwrite ("output/7-blur.png" , image )
8886 if thresh is not None :
8987 thresh_manager = ThresholdingManager ()
9088 if thresh == "adaptive" :
@@ -95,7 +93,7 @@ def extract(img, thresh="adaptive", blurr="median", clr="red", rm=False, knl=[7]
9593 filename = "{}.png" .format (os .getpid ())
9694 cv2 .imwrite (filename , image )
9795
98- cv2 .imwrite (" output/8-Extraction .png" , image )
96+ cv2 .imwrite (DESKTOP + "/ output/8.png" , image )
9997 text = pytesseract .image_to_string (Image .open (filename ))
10098 os .remove (filename )
10199
@@ -104,6 +102,6 @@ def extract(img, thresh="adaptive", blurr="median", clr="red", rm=False, knl=[7]
104102 print (text , "\n ------------------------------------------------------" )
105103 clean_text = text_manager .clean_up (text )
106104 print (clean_text , "\n -----------------------------------------------" )
107- id_details = text_manager .dictify (clean_text )
105+ id_details = text_manager .dictify (clean_text , data )
108106 print (id_details )
109107 return id_details
0 commit comments