-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlabelReader.py
179 lines (154 loc) · 4.57 KB
/
labelReader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python
from __future__ import print_function
from config import *
from utils.darknet_classify_image import *
from utils.keras_classify_image import *
from utils.azure_ocr import *
from utils.tesseract_ocr import *
import utils.logger as logger
from utils.rotate import *
from utils.lookup_database import *
import sys
from PIL import Image
import time
import os
from RotNet.correct_rotation import *
PYTHON_VERSION = sys.version_info[0]
OS_VERSION = os.name
class RobotIdentifier():
''' Programatically finds and determines if a pictures contains an asset and where it is. '''
def init_vars(self):
try:
self.DARKNET = DARKNET
self.KERAS = KERAS
self.TESSERACT = TESSERACT
self.COGNITIVE_SERVICES = COGNITIVE_SERVICES
self.COSMOS_DATABASE = COSMOS_DATABASE
self.LOCAL_DATABASE = LOCAL_DATABASE
return 0
except:
return -1
def init_classifier(self):
''' Initializes the classifier '''
try:
if self.DARKNET:
# Get a child process for speed considerations
logger.good("Initializing Darknet")
self.classifier = DarknetClassifier()
elif self.KERAS:
logger.good("Initializing Keras")
self.classifier = KerasClassifier()
if self.classifier == None or self.classifier == -1:
return -1
return 0
except:
return -1
def init_ocr(self):
''' Initializes the OCR engine '''
try:
if self.TESSERACT:
logger.good("Initializing Tesseract")
self.OCR = TesseractOCR()
elif self.COGNITIVE_SERVICES:
logger.good("Initializing Cognitive Services")
self.OCR = AzureOCR()
if self.OCR == None or self.OCR == -1:
return -1
return 0
except:
return -1
def init_database(self):
if self.LOCAL_DATABASE:
logger.good("Initializing local database")
from utils.local_database import LocalDatabase
self.database = LocalDatabase()
elif self.COSMOS_DATABASE:
logger.good("Initializing Cosmos Database")
from utils.cosmos_database import CosmosDatabase
self.database = CosmosDatabase()
else:
self.database = -1
if self.database == -1:
return -1
return 0
def init_tabComplete(self):
''' Initializes the tab completer '''
try:
if OS_VERSION == "posix":
global tabCompleter
global readline
from utils.PythonCompleter import tabCompleter
import readline
comp = tabCompleter()
# we want to treat '/' as part of a word, so override the delimiters
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.set_completer(comp.pathCompleter)
if not comp:
return -1
return 0
except:
return -1
def prompt_input(self):
''' Prompts the user for input, depending on the python version.
Return: The filename provided by the user. '''
if PYTHON_VERSION == 3:
filename = str(input(" Specify File >>> "))
elif PYTHON_VERSION == 2:
filename = str(raw_input(" Specify File >>> "))
return filename
from utils.locate_asset import locate_asset
def initialize(self):
if self.init_vars() != 0:
logger.fatal("Init vars")
if self.init_tabComplete() != 0:
logger.fatal("Init tabcomplete")
if self.init_classifier() != 0:
logger.fatal("Init Classifier")
if self.init_ocr() != 0:
logger.fatal("Init OCR")
if initialize_rotnet() != 0:
logger.fatal("Init RotNet")
if self.init_database() == -1:
logger.info("Not using Database")
def find_and_classify(self, filename):
start = time.time()
#### Classify Image ####
logger.good("Classifying Image")
coords = self.classifier.classify_image(filename)
########################
time1 = time.time()
print("Classify Time: " + str(time1-start))
#### Crop/rotate Image ####
logger.good("Locating Asset")
cropped_images = self.locate_asset(filename, self.classifier, lines=coords)
###########################
time2 = time.time()
print("Rotate Time: " + str(time2-time1))
#### Perform OCR ####
ocr_results = None
if cropped_images == []:
logger.bad("No assets found, so terminating execution")
else:
logger.good("Performing OCR")
ocr_results = self.OCR.ocr(cropped_images)
#####################
time3 = time.time()
print("OCR Time: " + str(time3-time2))
end = time.time()
logger.good("Elapsed: " + str(end-start))
#### Lookup Database ####
if self.database != -1:
products = self.database.lookup_database(ocr_results)
return products
else:
return ocr_results
#########################
def __init__(self):
''' Run RobotIdentifier! '''
self.initialize()
if __name__ == "__main__":
identifier = RobotIdentifier()
while True:
filename = identifier.prompt_input()
identifier.find_and_classify(filename)