def predict(imagepath, target_x, target_y, name, model):
if imagepath.startswith('http://') or imagepath.startswith('https://') or imagepath.startswith('ftp://'):
response = requests.get(imagepath)
img = Image.open(BytesIO(response.content))
img = img.resize((target_x, target_y))
else:
if not os.path.exists(imagepath):
raise Exception('Input image file does not exist')
img = image.load_img(imagepath, target_size=(target_x, target_y))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = processInputImage(name, x)
preds = decodePrediction(name, model.predict(x))
result = []
for p in preds[0]:
result.append({"synset": p[0], "text": p[1], "prediction": float("{0:.2f}".format((p[2] * 100)))})
return json.loads(jsonpickle.encode(result, unpicklable=False))
评论列表
文章目录