· 6 years ago · Sep 13, 2019, 04:40 PM
1from classifier_engine import ClassifierEngine
2from preprocessing import Preprocessing
3from extraction_engine import ImageExtraction
4
5from PIL import Image # pip install pillow
6from io import BytesIO
7import matplotlib.pyplot as plt
8
9import numpy as np
10import time
11import requests
12import os
13import sys
14import gc
15import cv2 # pip install opencv-python
16import base64
17import threading
18import warnings
19
20from flask import Flask, request, jsonify
21from flask_restful import Resource, Api
22import boto3
23import uuid
24from keras.preprocessing.image import load_img
25from keras.preprocessing.image import img_to_array
26from keras.models import load_model
27import tensorflow as tf
28
29
30
31warnings.filterwarnings("ignore")
32warnings.filterwarnings(action="ignore", category=DeprecationWarning)
33warnings.filterwarnings(action="ignore", category=FutureWarning)
34
35# Force TensorFlow to use single thread.
36session_conf = tf.ConfigProto(intra_op_parallelism_threads=1,
37 inter_op_parallelism_threads=1)
38from keras import backend as K
39K.clear_session()
40
41sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
42K.set_session(sess)
43
44
45import pytesseract
46from pytesseract import image_to_string
47
48pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
49
50app = Flask(__name__)
51
52my_brands = ["Hills", "Dark"]
53label_to_class = os.listdir(os.path.join('../project', 'dataset', 'train'))
54
55
56def load_classification_model():
57 # load the trained image classification model
58 global trained_model
59 trained_model = load_model('model.h5')
60
61
62@app.route('/api/Predict', methods=['POST'])
63def Predict():
64
65 # # In case of tensorflow_gpu, the following restricts the gpu allocation to grow
66 # # instead of claiming all the gpu hardware.
67 cfg = tf.ConfigProto()
68 cfg.gpu_options.allow_growth = True
69
70 global graph
71 with graph.as_default():
72 json_data = request.get_json()
73
74 # request parameters
75 _Id = json_data['Id']
76 _DID = json_data['DID']
77 _ActualImage_url = json_data['ActualImage']
78 _display_image_name = json_data['FileName']
79 _Key = json_data['Key']
80 _AWSKEY = json_data['AWSKEY']
81 _BUCKET = json_data['BUCKET']
82 _URL = json_data['URL']
83
84 tic = time.time() # start time
85 for file in os.listdir('../folder1'):
86 os.remove(os.path.join('../folder1', file))
87
88 ActualImage_file = getActualImage(_ActualImage_url)
89 actual_image = capturing_image(ActualImage_file.content)
90
91 upload_image_name = f"{uuid.uuid4()}.jpg"
92
93 plt.imsave(os.path.join('../folder1', _display_image_name), actual_image)
94
95 shelf_image = f"{'../folder1'}/{_display_image_name}"
96
97 # load the image
98 pic = load_img(shelf_image, target_size=(224, 224))
99 # convert to array
100 pic = img_to_array(pic)
101 pic = pic.reshape(1, 224, 224, 3)
102 # center pixel data
103 pic = pic.astype('float32')
104 pic = pic - [123.68, 116.779, 103.939]
105
106 with graph.as_default():
107 classify_image = trained_model.predict(pic)
108 gc.collect()
109
110 print("Image Classified As:", np.argmax(classify_image[0]))
111
112 if np.argmax(classify_image[0]) == 0:
113 list_brand_matches = getMatches(image_name=upload_image_name, image=actual_image,
114 _Key=_Key,
115 _AWSKEY=_AWSKEY,
116 _URL=_URL,
117 _BUCKET=_BUCKET, )
118 with open(os.path.join('../folder1', upload_image_name), "rb") as image_file:
119 annotated_image = image_file.read()
120 main_image_bytes = base64.b64encode(annotated_image)
121
122 s3 = boto3.client('s3', aws_access_key_id=_Key, aws_secret_access_key=_AWSKEY)
123 S3ProcessedImage_URL = upload(s3, _URL, _BUCKET, upload_image_name,
124 '../folder1')
125
126 response = {
127 "Status": 1,
128 "Message": "Data Process Successfully.",
129 "MessageCode": 1,
130 "StatusCode": 1,
131 "result": {
132 "Id": _Id,
133 "CategoryCode": 0, /*
134 "S3ProcessedImageBase64": '',
135 "S3ProcessedImage": S3ProcessedImage_URL,
136 "ListAIBrandMatches": list_brand_matches
137 }}
138
139 print(f"Response Time : {time.time() - tic}") # Time of computation
140
141 elif np.argmax(classify_image[0]) == 1:
142 list_brand_matches = getSecondMatch(image_name=_display_image_name, image=actual_image,
143 _Key=_Key,
144 _AWSKEY=_AWSKEY,
145 _URL=_URL,
146 _BUCKET=_BUCKET, )
147
148 response = {
149 "Status": 1,
150 "Message": "Data 2 Process Successfully.",
151 "MessageCode": 1,
152 "StatusCode": 1,
153 "result": {
154 "Id": _Id,
155 "CategoryCode": 1,
156 "S3ProcessedImageBase64": '',
157 "S3ProcessedImage": '',
158 "ListAIBrandMatches": list_brand_matches
159 }}
160
161 print(f"Data 2 Response Time : {time.time() - tic}") # Time of computation
162
163 else:
164 response = {
165 "Status": 1,
166 "Message": "Wrong Image!",
167 "MessageCode": 1,
168 "StatusCode": 1,
169 "result": {
170 "Id": _Id,
171 "CategoryCode": 2,
172 "S3ProcessedImageBase64": '',
173 "S3ProcessedImage": '',
174 "ListAIBrandMatches": []
175 }}
176 print("Unknown Image!")
177
178
179 return jsonify(response)
180
181
182def getActualImage(ActualImage_url):
183 try:
184 ActualImage = requests.get(ActualImage_url)
185 except requests.exceptions.HTTPError as errh:
186 print("Http Error:", errh)
187 sys.exit(1)
188 except requests.exceptions.ConnectionError as errc:
189 print("Error Connecting:", errc)
190 sys.exit(1)
191 except requests.exceptions.Timeout as errt:
192 print("Timeout Error:", errt)
193 sys.exit(1)
194 except requests.exceptions.RequestException as err:
195 print("OOps: Something Else", err)
196 sys.exit(1)
197
198 return ActualImage
199
200
201def capturing_image(img):
202 res_img = Image.open(BytesIO(img))
203 b, g, r = res_img.split()
204 res_img = Image.merge('RGB', (r, g, b))
205 res_img = np.array(res_img)
206 return res_img
207
208
209def getMatches(image_name, image, _Key, _AWSKEY, _URL, _BUCKET):
210
211 ListAIBrandMatches = []
212 bottle_image_name = uuid.uuid4()
213
214 s3 = boto3.client('s3', aws_access_key_id=_Key, aws_secret_access_key=_AWSKEY)
215
216 for file in os.listdir('../folder2'):
217 # print(file)
218 os.remove(os.path.join('../folder2', file))
219
220 classifier = ClassifierEngine()
221 model = classifier.load_pretrained_model('../models/new_Sep_12_7.model')
222
223 im_processing = Preprocessing()
224 extractor = ImageExtraction()
225
226 draw, image_coordinates = extractor.extract_to_folder(image, bottle_image_name, '../folder2',
227 threshold=0.49)
228 plt.imsave(os.path.join('../folder1', image_name), draw)
229
230 os.chdir('../folder2')
231 if len(os.listdir('.')) == 0:
232 return ListAIBrandMatches
233
234 image_files = os.listdir('.')
235 # print("image_files:: ", image_files)
236
237 for image_file in image_files:
238 matches = {}
239 image_idx = int(image_file.split('.')[0].split('-')[-1])
240
241 try:
242 img = Image.open(image_file)
243 img = im_processing.resize_maintain_aspect_ratio(img, '256x256')
244 img = im_processing.paste(img, '256x256')
245
246 with graph.as_default():
247 p = classifier.predict(model, img, (256, 256))
248
249 gc.collect()
250
251 except:
252 continue
253 matches['BrandId'] = None
254
255 matches["Brand"] = label_to_class[np.argmax(p[0])]
256 matches['ProductName'] = "My" if matches["Brand"] in my_brands else "Cool"
257 matches["Match"] = round(max(p[0] * 100).item(), 2)
258 matches["ImageBase64"] = ''
259 S3ProcessedImage_urls = upload(s3, _URL, _BUCKET, image_file,
260 '../folder2')
261 matches["S3URLImageBase64"] = S3ProcessedImage_urls
262 matches['GalloCompetitorID'] = 0
263 matches['LOB_CODE'] = None
264 matches['LOB_NAME'] = None
265 matches['BrandFeedbackID'] = None
266 matches['BrandFeedbackType'] = 0
267 matches['Top'] = image_coordinates[image_idx]['Top']
268 matches['Left'] = image_coordinates[image_idx]['Left']
269 matches['Width'] = image_coordinates[image_idx]['Width']
270 matches['Height'] = image_coordinates[image_idx]['Height']
271 ListAIBrandMatches.append(matches)
272
273 return ListAIBrandMatches
274
275
276def upload(s3, base_url, bucket_name, display_image_name, upload_path):
277 s3.upload_file(os.path.join(upload_path, display_image_name), bucket_name,
278 display_image_name,
279 ExtraArgs={"ACL": "public-read"})
280 S3ProcessedImage_URL = f"{base_url}/{display_image_name}"
281 return S3ProcessedImage_URL
282
283
284def getSecondMatch(image_name, image, _Key, _AWSKEY, _URL,
285 _BUCKET):
286 ListAIBrandMatches = []
287 shelf_image = f"{'../folder1'}/{image_name}"
288 extracted_texts = image_to_string(shelf_image)
289 txt = ""
290 for text in extracted_texts:
291 if txt.strip() != '' and text == "\n":
292 matches = {}
293 matches['BrandId'] = None
294 matches["Brand"] = txt.strip()
295 # matches["Description"] = extracted_texts
296 matches['ProductName'] = "My" if matches["Brand"] in my_brands else "Cool"
297 matches["Match"] = 0
298 matches["S3URLImageBase64"] = ''
299 matches['GalloCompetitorID'] = 0
300 matches['LOB_CODE'] = None
301 matches['LOB_NAME'] = None
302 matches['BrandFeedbackID'] = None
303 matches['BrandFeedbackType'] = 0
304 matches['Top'] = 0
305 matches['Left'] = 0
306 matches['Width'] = 0
307 matches['Height'] = 0
308 matches['Flavor'] = ''
309 matches['Appellation'] = ''
310 matches['LineOfBusiness'] = ''
311 matches['DollarValue'] = 0
312 matches['Color'] = ''
313 txt = ""
314 ListAIBrandMatches.append(matches)
315 else:
316 if text != "\n":
317 txt = txt + text
318
319 return ListAIBrandMatches
320
321
322if __name__ == '__main__':
323 load_classification_model()
324 graph = tf.get_default_graph()
325 app.run(host='0.0.0.0', port=5000, debug=False)