'return'在函数错误keras预测值之外[关闭]

问题描述 投票:-1回答:1

我正在尝试从URL提取图像。在另一个文件上显示输出。我正在使用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





python tensorflow keras deep-learning caffe
1个回答
0
投票

请尝试以下缩进代码。

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
© www.soinside.com 2019 - 2024. All rights reserved.