'return ' is outside of function error keras预测值
'return ' is outside of funtion error keras predict value
我正在尝试从 URL 获取图像。在另一个 file.I 上显示输出,我正在使用 keras 和 tensorflow 来 运行 一个代码,但是 return 函数不起作用如何解决这个问题?我从另一个文件得到结果
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
我正在尝试从 URL 获取图像。在另一个 file.I 上显示输出,我正在使用 keras 和 tensorflow 来 运行 一个代码,但是 return 函数不起作用如何解决这个问题?我从另一个文件得到结果
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