'How to save a browsed image into a variable to be called in another function
This is my GUI code which has 3 buttons : 1.Browse an Image, 2.Load and Display the chosen Image 3.Run the Image through my prediction model and print a prediction statement
import io
import os
import PySimpleGUI as sg
import cv2
import tensorflow as tf
from tensorflow import keras
from PIL import Image
from keras.preprocessing.image
import load_img
CATEGORIES = ["1st degree burn","2nd degree burn"]
file_types = [("JPEG (*.jpg)", "*.jpg"),
("All files (*.*)", "*.*")]
sg.theme('TealMono')
def prepare(filepath):
IMG_SIZE = 50
img_array = cv2.imread(filepath)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
def main():
layout = [
[sg.Image(key="-IMAGE-")],
[
sg.Text(("Burn Me Not"),size=(20,5),font=(50,20)),
sg.Text("Image File"),
sg.Input(size=(55, 1), key="-FILE-"),
sg.FileBrowse(size=(25,3), file_types=file_types),
sg.Button(("Load Image"),size=(25,3)),
sg.Button(("run model"),size=(25,3)),
sg.Button(("Exit"),size=(25,3)),
sg.Listbox(values=("",""), size=(30,5))
],
]
window = sg.Window('Test', layout).Finalize()
window.Maximize()
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
if event == "Load Image":
filename = values["-FILE-"]
if os.path.exists(filename):
image = Image.open(values["-FILE-"])
image.thumbnail((400,400))
bio = io.BytesIO()
image.save(bio, format="PNG")
window["-IMAGE-"].update(data=bio.getvalue())
if event == "run model":
model = tf.keras.models.load_model("2variable.model")
#img = load_image("image")
prediction = model.predict([prepare("two_degree_test.jpg")])
print(CATEGORIES[int(prediction[0][0])])
window.close()
if __name__ == "__main__":
main()
However, the button does nothing as if the image that I previously loaded does not save over functions/buttons
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
