-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocess.cv.py
174 lines (137 loc) · 5.03 KB
/
preprocess.cv.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
import numpy as np
import cv2
from azure.storage import QueueService
from azure.storage import BlobService
from azure.storage import TableService
import azure
import urllib
# from os import environ
from base64 import b64decode
import itertools
from time import sleep
from math import floor
def blobToOpenCV(blob):
arr = np.asarray(bytearray(blob), dtype=np.uint8)
img = cv2.imdecode(arr, -1)
return img
# read image from URL to CV format
def getImgURL( imgURL ):
req = urllib.urlopen( imgURL )
arr = np.asarray( bytearray(req.read()), dtype=np.uint8 )
img = cv2.imdecode(arr, -1) # 'load it as it is'
return img
def makeThumbnail( image, width ) :
## Normalise to square based on lower dimension
imgH = image.shape[0]
imgW = image.shape[1]
if imgH < imgW:
diff = imgW-imgH
if diff%2 == 0:
cut = (diff)/2
crop_img = image[:, cut:imgW-cut]
else:
cut = (diff)/2.0
cut = floor(cut)
crop_img = image[:, cut+1:imgW-cut]
elif imgH > imgW:
diff = imgH-imgW
if diff%2 == 0:
cut = (diff)/2
crop_img = image[cut:imgH-cut, :]
else:
cut = (diff)/2
cut = floor(cut)
crop_img = image[cut+1:imgH-cut, :]
elif imgH==imgW:
# Nothing to do
crop_img = image
# we need to keep in mind aspect ratio
dim = ( width, width )
# perform the actual resizing of the image
res = cv2.resize(crop_img, dim, interpolation = cv2.INTER_AREA) # None, fx=2, fy=2
return res
# Analyse the overall colour of image
def getCharacteristics( image ):
# range_hist = [0, 100, -100, 100, -100, 100]
# hist_1 = cv2.calcHist([image], [0, 1, 2], None, [20, 20, 20], range_hist)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h = hsv[:,:,0]
s = hsv[:,:,1]
v = hsv[:,:,2]
hs = list(itertools.chain.from_iterable(h.tolist()))
ss = list(itertools.chain.from_iterable(s.tolist()))
vs = list(itertools.chain.from_iterable(v.tolist()))
counts = np.bincount(hs)
hw = np.argmax(counts)
counts = np.bincount(ss)
sw = np.argmax(counts)
counts = np.bincount(vs)
vw = np.argmax(counts)
return (int(hw), int(sw), int(vw))
blob_container = 'smallimages'
imagesQueue = 'smallimagesqueue'
imageWidth = 50
tableName = 'photos'
tablePartitionKey = 'allphotos'
# Get queue credentials
# accountName = environ["AZURE_STORAGE_ACCOUNT"]
with open ("ASA.key", "r") as myfile:
accountName=myfile.read().replace('\n', '')
# accountKey = environ["AZURE_STORAGE_ACCESS_KEY"]
with open ("ASK.key", "r") as myfile:
accountKey=myfile.read().replace('\n', '')
# Create blob service
blob_service = BlobService( account_name=accountName, account_key=accountKey )
blob_service.create_container( blob_container )
# Open queue with given credentials
queue_service = QueueService( account_name=accountName, account_key=accountKey )
# Open table service
table_service = TableService( account_name=accountName, account_key=accountKey )
# Repeat
while(True):
# get images form *imagesQueue* - it is invoked by CRON
messages = queue_service.get_messages( imagesQueue )
if len(messages) == 0:
sleep(15)
for message in messages:
# get image: image ID
imgBlobName = b64decode( message.message_text )
print( imgBlobName )
tableRowKey = imgBlobName
try:
blob = blob_service.get_blob(blob_container, imgBlobName)
except azure.WindowsAzureMissingResourceError:
#queue_service.delete_message( imagesQueue, message.message_id, message.pop_receipt )
continue
image = blobToOpenCV(blob) # image = getImgURL( imgURL )
# ADDED2 #####
if image is None:
print "GIF attempt in pre-process"
queue_service.delete_message( imagesQueue, message.message_id, message.pop_receipt )
table_service.delete_entity( tableName, tablePartitionKey, tableRowKey)
blob_service.delete_blob( blob_container, imgBlobName)
continue
# process image
image_tn = makeThumbnail( image, imageWidth )
(hw, sw, vw) = getCharacteristics( image )
# put thumbnail to bloob: add suffix _tn
result ,blob_tn = cv2.imencode( '.jpg', image_tn )
# Override
tnID = imgBlobName
# if imgBlobName[-4] == '.' :
# tnID = imgBlobName[:-4] + "_tn" + imgBlobName[-4:]
# else :
# tnID = imgBlobName[:-5] + "_tn" + imgBlobName[-5:]
blob_service.put_block_blob_from_bytes( blob_container, tnID, str(bytearray(blob_tn.flatten().tolist())) )
# {'PartitionKey': 'allPhotos', 'RowKey': 'imageName', 'thumbnail' : 'thumbnailName',
# 'userId' : ?, 'local' : ?, 'hue' : 200, 'saturation' : 200, 'value' : 200}
## query for image in table to ensure existence
currentTask = table_service.get_entity( tableName, tablePartitionKey, tableRowKey)
## send the quantities to table: save thumbnail ID & save image characteristics
# currentTask.thumbnail = tnID
currentTask.hue = hw
currentTask.saturation = sw
currentTask.value = vw
table_service.update_entity( tableName, tablePartitionKey, tableRowKey, currentTask)
# dequeue image
queue_service.delete_message( imagesQueue, message.message_id, message.pop_receipt )