-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
126 lines (101 loc) · 3.43 KB
/
Copy pathgui.py
File metadata and controls
126 lines (101 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import tkinter as tk
# from tkinter import filedialog, Label, Button, Entry, W, END, Canvas
from tkinter import *
from tkinter import filedialog
import cv2
from PIL import Image, ImageTk
import time
import tensorflow as tf
import json
import datetime
import os
import cv2
import numpy as np
import sys
DATASET_DIR = "Dataset/PlantVillage/"
TRAIN_DIR = DATASET_DIR + "/train"
VALID_DIR = DATASET_DIR + "/validation"
MODEL_DIR = "./models/"
LOG_DIR = "./logs/"
IMAGE_SIZE = (299, 299)
BATCH_SIZE = 16
FV_SIZE = 2048
def load_image(filename):
img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (IMAGE_SIZE[0], IMAGE_SIZE[1]) )
img = img /255.0
return img
with open(DATASET_DIR + '/categories.json', 'r') as f:
cat_to_name = json.load(f)
classes = list(cat_to_name.values())
print (classes)
model = tf.keras.Sequential([
tf.keras.applications.InceptionV3(input_shape=IMAGE_SIZE+(3,), include_top=False),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(rate=0.2),
tf.keras.layers.Dense(len(classes), activation='softmax')
])
latest = tf.train.latest_checkpoint(MODEL_DIR)
if latest:
print("Load weight from last")
print(latest)
model.load_weights(latest)
else:
print("No weights. Exits")
exit()
model.summary()
gui = tk.Tk()
gui.resizable(False, False)
gui.title("Disease Identification")
Label(gui, text="Image path: ").grid(row=0, sticky=W)
txtbPath = Entry(gui)
txtbPath.grid(row=0, column=1, columnspan=2, sticky=W)
Label(gui, text="True label: ").grid(row=1, sticky=W)
txtbTrueLabel = Entry(gui)
txtbTrueLabel.grid(row=1, column=1, columnspan=2, sticky=W)
Label(gui, text="Predited label: ").grid(row=2, sticky=W)
txtbLabel = Entry(gui)
txtbLabel.grid(row=2, column=1,sticky=W)
Label(gui, text="Confident: ").grid(row=3, column=0, sticky=W)
txtbScore = Entry(gui)
txtbScore.grid(row=3, column=1, sticky=W)
Label(gui, text="Result: ").grid(row=4, column=0, sticky=W)
txtbCorrect = Entry(gui)
txtbCorrect.grid(row=4, column=1, sticky=W)
def OpenFile():
path = filedialog.askopenfilename()
print (path)
txtbPath.delete(0,END)
txtbPath.insert(0,path)
cv_img = cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB)
cv2.resize(cv_img, (400, 400))
photo = ImageTk.PhotoImage(image = Image.fromarray(cv_img))
# item = canvas.create_image(0, 0, image=photo, anchor=NW)
# canvas.tag_raise(item)
lb = Label(gui, image = photo)
lb.image = photo
lb.grid(row=5, columnspan=3)
true_label = path.split('/')[-2]
print(true_label)
txtbTrueLabel.delete(0,END)
txtbTrueLabel.insert(0,true_label)
img = load_image(path)
probabilities = model.predict(np.asarray([img]))[0]
class_idx = np.argmax(probabilities)
print("probabilities: ")
print(probabilities[class_idx])
txtbScore.delete(0,END)
txtbScore.insert(0,str(probabilities[class_idx]))
print("PREDICTED: class: %s, confidence: %f" % (classes[class_idx], probabilities[class_idx]))
txtbLabel.delete(0,END)
txtbLabel.insert(0,str(classes[class_idx]))
txtbCorrect.delete(0,END)
if(txtbLabel.get() == txtbTrueLabel.get()):
txtbCorrect.insert(0,"Right Prediction")
else:
txtbCorrect.insert(0,"Wrong Prediction")
btnOpen = Button(gui, text=' Open ', fg='black', command=lambda: OpenFile(), height=1, width=7)
btnOpen.grid(row=0, column=2)
gui.mainloop()