0

I wanted to create an image classification app in python as a university project, but I get the error you can see on the pic below:

Error message: Error_image

I have 2 python files a teach.py, which teaches the pictures to the algorithm and saves it and a main.py which loads the saved model and a gui to uplad and predict the images, but when I debug I get the error don't know why neither do I know how to fix it...

You can see main.py below:

main.py:

import keras
import numpy as np
import gradio as gr
import pathlib
import os
import PIL
import tkinter as tk
import warnings
import h5py
import zipfile
import tensorflow as tf
import joblib
import globals
import matplotlib.pyplot as plt
from keras.applications import vgg16
from keras.applications.vgg16 import decode_predictions
from keras.utils import img_to_array
from tkinter import *
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.vgg16 import preprocess_input
from keras.models import Sequential
from keras.layers import Dense,Flatten,Dropout
from sklearn import model_selection, datasets
from sklearn.tree import DecisionTreeClassifier
from pathlib import Path
from PIL import ImageTk, Image
from tkinter import filedialog
from tensorflow import keras
from keras import layers
from keras.models import Sequential

warnings.filterwarnings('ignore')

data_dir='birbs/birbs'

class_names = os.listdir(data_dir)

loaded_model = tf.keras.models.load_model("saved_model.h5")

def load_img():
    global img, image_data
    for img_display in frame.winfo_children():
        img_display.destroy()

    image_data = filedialog.askopenfilename(initialdir="/", title="Choose an image",
                                       filetypes=(("all files", "*.*"), ("png files", "*.png")))
    basewidth = 150 # Processing image for dysplaying
    img = Image.open(image_data)
    wpercent = (basewidth / float(img.size[0]))
    hsize = int((float(img.size[1]) * float(wpercent)))
    img = img.resize((basewidth, hsize), Image.ANTIALIAS)
    img = ImageTk.PhotoImage(img)
    file_name = image_data.split('/')
    panel = tk.Label(frame, text= str(file_name[len(file_name)-1]).upper()).pack()
    panel_image = tk.Label(frame, image=img).pack()

def classify():
    original = Image.open(image_data)
    original = original.resize((224, 224), Image.ANTIALIAS)
    numpy_image = img_to_array(original)
    image_batch = np.expand_dims(numpy_image, axis=0)
    processed_image = vgg16.preprocess_input(image_batch.copy())
    predictions = vgg_model.predict(processed_image)
    label = decode_predictions(predictions)
    table = tk.Label(frame, text="Top image class predictions and confidences").pack()
    for i in range(0, len(label[0])):
         result = tk.Label(frame, text= str(label[0][i][1]).upper() + ': ' + str(round(float(label[0][i][2])*100, 3)) + '%').pack()

root = tk.Tk()
root.title('Portable Image Classifier')
root.iconbitmap('class.ico')
root.resizable(False, False)
tit = tk.Label(root, text="Portable Image Classifier", padx=25, pady=6, font=("", 12)).pack()
canvas = tk.Canvas(root, height=500, width=500, bg='grey')
canvas.pack()
frame = tk.Frame(root, bg='white')
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
chose_image = tk.Button(root, text='Choose Image',
                        padx=35, pady=10,
                        fg="white", bg="grey", command=load_img)
chose_image.pack(side=tk.LEFT)
class_image = tk.Button(root, text='Classify Image',
                        padx=35, pady=10,
                        fg="white", bg="grey", command=classify)
class_image.pack(side=tk.RIGHT)
vgg_model = vgg16.VGG16(weights='imagenet')
root.mainloop()
ncxop
  • 33
  • 4
NokedliH
  • 1
  • 1
  • 2
    Please try to reduce the code down to a [mcve]. The error is happening within the first few lines of executable code, do don't need all the other code that never executes. – Bryan Oakley Dec 03 '22 at 20:32
  • Loot at [this](https://stackoverflow.com/a/63324528/11106801). Also the problem is in the `root.iconbitmap('class.ico')` line so most of the code isn't needed to reproduce the error – TheLizzard Dec 03 '22 at 20:36
  • Thanks for the help root.iconbitmap('class.ico') was the problem when I commented it out and ran the program it worked...also sorry for copying the whole file i just wasnt sure where the error occured – NokedliH Dec 03 '22 at 21:26
  • Does the file `class.ico` exist? – acw1668 Dec 04 '22 at 00:36

1 Answers1

0

You need iconphoto

root = tk.Tk()
root.title('Portable Image Classifier')
img = tk.PhotoImage(file='class.ico')
root.iconphoto(False, img):
:
:
:
root.mainloop()

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19