Search code examples
pythontensorflowkerasdeep-learningcaffe

'return ' is outside of funtion error keras predict value


I am trying to fetch image from URL. Show a output on another file.I am using keras and tensorflow to run a code but return function is not working how to solve this ? I got results from another file

    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






Solution

  • Please try below indentation code.

    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