如何删除Flask中的变量会话。

huangapple go评论76阅读模式
英文:

I always got the same result, How to remove variable session in flask

问题

您想要确保服务器不必重新启动并且GLCM数据能够根据上传的数据得出正确的结果,可能有一些问题需要解决。以下是一些可能的解决方法:

  1. 数据共享问题:确保在处理每个上传的图像时,您的特征列表(feature)是在每次请求中重新初始化的。在您的processImg函数的开头,可以添加以下行来清除旧特征数据:

    feature = []
    

    这将确保每个图像的特征都是基于该图像而不是之前的数据计算的。

  2. 模型加载问题:确保模型只加载一次,而不是在每次请求中重新加载。您已经在全局范围内加载了模型,这是正确的做法。只需确保不会在每次请求中重新加载模型。

  3. 缓存问题:如果您在应用中使用了缓存,确保它不会导致问题。有时候,缓存可能会导致结果出现问题,因为它们可能会在不同的请求之间共享数据。

  4. 数据处理流程:检查数据处理流程,确保它不会在不同请求之间共享数据。这包括GLCM数据和颜色数据。

  5. 调试输出:在代码中添加一些调试输出,以便更好地理解发生了什么。例如,在不同请求处理时打印一些关键数据,以确定问题的根本原因。

通过这些步骤,您应该能够更好地诊断和解决问题,从而确保GLCM数据能够根据上传的数据正确生成结果,而无需重新启动服务器。

英文:

I'm building an application for GLCM and RGB extraction and also make prediction. But somehow if I use different data I always get results on the previous data or first data. But not for RGB data, RGB data always manages to get results according to the uploaded data. Unless I stop the server and restart it then it should work.

here is my code in app.py

from flask import Flask, render_template, request
from keras.models import load_model
from io import BytesIO
import keras.utils as image
import numpy as np
from PIL import Image
import math
import pandas as pd
import csv
import requests
from io import BytesIO
import numpy as np
import keras.utils as image
import cv2 as cv
from data import Data
from glcm import Glcm
from sklearn.preprocessing import StandardScaler
import os

app = Flask(__name__)

model = load_model('klasifikasi_ayam_potong_berformalin3.h5', compile=False)

matriks = [
    [0,1],
    [1,0],
]

matriksIterasi = [
    [0,0,0,0],
    [0,0,0,0],
    [0,0,0,0],
    [0,0,0,0],
]

feature = []

def countFeatures(data, glcm):
    asm = glcm.asmMethod(data)
    energy = glcm.energy(asm)
    feature.append(energy)
    idm = glcm.idmMethod(data)
    feature.append(idm)
    cont = glcm.contrasMethod(data)
    feature.append(cont)
    corel = glcm.correlation(data)
    feature.append(corel)

def processImg(imgPat):

    imageObject  = Image.open(imgPat)
    cropped     = imageObject.crop((0,0,250,250))
    citra = np.array(cropped)
    citra = citra.transpose(2,0,1).reshape(3,-1)
    matriksIterasi = [[0 for i in range(256)] for j in range(256)]

    glcm = Glcm()
    data0 = glcm.forOder0(citra, matriksIterasi)
    countFeatures(data0, glcm)
    data45 = glcm.forOder45(citra, matriksIterasi)
    countFeatures(data45, glcm)
    data90 = glcm.forOder90(citra, matriksIterasi)
    countFeatures(data90, glcm)
    data135 = glcm.forOder135(citra, matriksIterasi)
    countFeatures(data135, glcm)

    for i in range(len(data0)-1):
        for j in range(len(data0[i])-1):
            data0[i][j]=(data0[i][j]+data45[i][j]+data90[i][j]+data135[i][j])/4
    
    countFeatures(data0, glcm)

    R = []
    G = []
    B = []

    cvImage = cv.imread(imgPat, cv.COLOR_RGB2BGR)
    rows, cols, _ = cvImage.shape

    color_B = 0
    color_G = 0
    color_R = 0
    color_N = 0 
    for i in range(rows):
        for j in range(cols):
            k = cvImage[i,j]
            if k[0] > k[1] and k[0] > k[2]:
                color_B = color_B + 1
                continue
            if k[1] > k[0] and k[1] > k[2]:
                color_G = color_G + 1
                continue
            if k[2] > k[0] and k[2] > k[1]:
                color_R = color_R + 1
                continue
            color_N = color_N + 1

        pix_total = rows * cols

    G.append(color_G/pix_total)
    B.append(color_B/pix_total)
    R.append(color_R/pix_total)
    print("    ")
    print(imgPat, '\n')
    df = pd.DataFrame({'Red': R, 'Green': G, 'Blue': B, 'energy0':feature[0], 'contras0':feature[1], 'homogenity0':feature[2], 'correlation0':feature[3], 'energy45':feature[4], 'contras45':feature[5], 'homogenity45':feature[6], 'correlation45':feature[7], 'energy90':feature[8], 'contras90':feature[9], 'homogenity90':feature[10], 'correlation90':feature[11], 'energy135':feature[12], 'contras135':feature[13], 'homogenity135':feature[14], 'correlation135':feature[15], 'energy_mean':feature[16], 'contras_mean':feature[17], 'homogenity_mean':feature[18], 'correlation_mean':feature[19]})
    print(df)

    sc = StandardScaler()
    xtesx = sc.fit_transform(df)
    prediction = model.predict(xtesx)
    data_pred = []
    pred = ''
    for i in prediction:
        if i > 0.5:
            data_pred.append(1)
            pred = 'Good'
        else:
             data_pred.append(0)
             pred = 'Bad'

    return pred

@app.route("/", methods=['GET', 'POST'])
def main():
	return render_template("index.html")

@app.route("/about")
def about_page():
	return render_template("about.html")

@app.route("/contact")
def contact_page():
	return render_template("contact.html")

@app.route("/submit", methods = ['POST'])

def get_output():
    if request.method == 'POST':
       
       img = request.files['my_image']
       img_path = "static/" + img.filename	
       img.save(img_path)
       
       pred = processImg(img_path)
       
    return render_template("index.html", prediction = pred, img_path = img_path)

if __name__ =='__main__':
	app.run(debug = True)

you could see the same results after Red, Green, Blue which is on energy0, contras0 and so on..

results could be found here

How do I make it not necessary to restart the server and the GLCM data gets according to the uploaded data?

答案1

得分: 0

我已成功解决了这个问题,并根据@Frayal的评论找到了解决方案。也就是说,我只需要使用clear()函数重置feature[]列表。

英文:

I have managed to fix this problem, and based on the comments from @Frayal, I found a solution. That is, I only need to reset the feature[] list with the clear() function.

huangapple
  • 本文由 发表于 2023年7月6日 19:24:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定