"返回"在功能错误之外 Keras 预测值



我正在尝试从URL获取图像。在另一个文件上显示输出。我正在使用 keras 和张量流来运行代码,但返回函数不起作用如何解决这个问题?我从另一个文件中得到结果

def get_score(file, model=weapon_model):
tmp_filename = ""
if file.find('http') != -1:
random_text =  "".join([random.choice(string.ascii_letters[:26]) for i in range(15)])
tmp_filename = "res/" + random_text + "out.jpg"
if not os.path.isdir('res/'):
os.mkdir("res/")
URL = file
c = urllib3.PoolManager()
with c.request('GET', URL, preload_content=False) as resp, open(tmp_filename, "wb") as out_file:
shutil.copyfileobj(resp, out_file)
resp.release_conn()
file = tmp_filename
m,n = 50,50
im = Image.open(file);
imrs = im.resize((m,n))
imrs=img_to_array(imrs)/255;
imrs=imrs.transpose(2,0,1);
imrs=imrs.reshape(3,m,n);
x=[];
x.append(imrs);
x=np.array(x);
predictions = weapon_model.predict(x)[:,1]
#predictions = weapon_model.predict(x)
print (predictions)
print ("Weapon score:  ", predictions[1])
return predictions


请尝试下面的缩进代码。

def get_score(file, model=weapon_model):
tmp_filename = ""
if file.find('http') != -1:
random_text =  "".join([random.choice(string.ascii_letters[:26]) for i in range(15)])
tmp_filename = "res/" + random_text + "out.jpg"
if not os.path.isdir('res/'):
os.mkdir("res/")
URL = file
c = urllib3.PoolManager()
with c.request('GET', URL, preload_content=False) as resp, open(tmp_filename, "wb") as out_file:
shutil.copyfileobj(resp, out_file)
resp.release_conn()
file = tmp_filename
m,n = 50,50
im = Image.open(file);
imrs = im.resize((m,n))
imrs=img_to_array(imrs)/255;
imrs=imrs.transpose(2,0,1);
imrs=imrs.reshape(3,m,n);
x=[];
x.append(imrs);
x=np.array(x);
predictions = weapon_model.predict(x)[:,1]
#predictions = weapon_model.predict(x)
print (predictions)
print ("Weapon score:  ", predictions[1])
return predictions

最新更新