def predict(url, mod, synsets):
req = urllib2.urlopen(url)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
cv2_img = cv2.imdecode(arr, -1)
img = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2RGB)
if img is None:
return None
img = cv2.resize(img, (224, 224))
img = np.swapaxes(img, 0, 2)
img = np.swapaxes(img, 1, 2)
img = img[np.newaxis, :]
mod.forward(Batch([mx.nd.array(img)]))
prob = mod.get_outputs()[0].asnumpy()
prob = np.squeeze(prob)
a = np.argsort(prob)[::-1]
out = ''
for i in a[0:5]:
out += 'probability=%f, class=%s' %(prob[i], synsets[i])
out += "\n"
return out
评论列表
文章目录