-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcovid19_ai_diagnoser_ui.py
More file actions
137 lines (107 loc) · 6.19 KB
/
covid19_ai_diagnoser_ui.py
File metadata and controls
137 lines (107 loc) · 6.19 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
127
128
129
130
131
132
133
134
135
136
137
#Author: Jordan Bennett
#Note: Simple ui to use Ai based coronavirus2019/covid19 diagnosis tool
import covid19_ai_diagnoser
from tkinter import Frame, Tk, BOTH, Label, Menu, filedialog, messagebox, Text
from PIL import Image, ImageTk
import os
import codecs
screenWidth = "1560"
screenHeight = "840"
windowTitle = "Smart/Ai Coronavirus 2019 (Covid19) Diagnosis Tool"
import cv2
class Window(Frame):
_PRIOR_IMAGE = None
#establish variable to keep track of images added to Frame, for purpose of preventing stacking @ new image additions
#by using destroy() on each old image instance @ addition
#Added by Jordan Bennett, based on suggestion by Andrei Marinescu, who suggested that xray images should not stack as new ones are loaded. (https://www.facebook.com/mvandrei)
DIAGNOSIS_RESULT = ""
DIAGNOSIS_RESULT_FIELD = None
#Jordan_note: Added to facilitate output window data
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack(fill=BOTH, expand=1)
load = Image.open("covid19_ai_diagnoser_default__.jpg")
render = ImageTk.PhotoImage(load)
img = Label(self, image=render)
img.image = render
img.place(x=(int(screenWidth)/2)-load.width/2, y=((int(screenHeight)/2))-load.height/2-80)
self._PRIOR_IMAGE = img #setup prior image instance
self.DIAGNOSIS_RESULT_FIELD = Text(self, width=int(screenWidth), height=13)
self.DIAGNOSIS_RESULT_FIELD.pack ( )
self.DIAGNOSIS_RESULT_FIELD.place(x=0, y=int(screenHeight)-200)
def addDiagnosisResult (self, value):
self.DIAGNOSIS_RESULT_FIELD.delete("1.0","end") #clear diagnostic result text element
self.DIAGNOSIS_RESULT = "" #clear diagnostic result string variable
self.DIAGNOSIS_RESULT_FIELD.insert(1.0, value) #add new value
root = Tk()
app = Window(root)
############
#screen window size, window title
root.wm_title(windowTitle)
root.geometry(screenWidth + "x" + screenHeight)
############
#menu bar
menubar = Menu(root)
# Adding a cascade to the menu bar:
filemenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Files", menu=filemenu)
CONSTANT_DIAGNOSIS_IMAGE_SPAN = 480
# Defining function to trigger file browser
def loadRegularPneumoniaImageFromDialog():
currdir = os.getcwd()
image_file = filedialog.askopenfile(mode ='r', parent=root, initialdir=currdir, title='Please select an Xray Image of suspected regular pneumonia case:')
root.wm_title(windowTitle + " : " + image_file.name)
loadRegularPneumoniaImageFromName(image_file.name)
def loadRegularPneumoniaImageFromName(filename):
app._PRIOR_IMAGE.destroy() #destroy old image
load = Image.open(filename)
load = load.resize((CONSTANT_DIAGNOSIS_IMAGE_SPAN, CONSTANT_DIAGNOSIS_IMAGE_SPAN),Image.ANTIALIAS) #Resized "load" image to constant size on screen. However, neural network still runs on on original image scale from filename.
render = ImageTk.PhotoImage(load)
img = Label(image=render)
img.image = render
img.place(x=(int(screenWidth)/2)-CONSTANT_DIAGNOSIS_IMAGE_SPAN/2, y=((int(screenHeight)/2))-CONSTANT_DIAGNOSIS_IMAGE_SPAN/2-80)
app.DIAGNOSIS_RESULT += "**Non-Covid19 Mode Result**\n" + filename+"\n\n"
app.DIAGNOSIS_RESULT += covid19_ai_diagnoser.doOnlineInference_regularPneumonia (filename)
print(app.DIAGNOSIS_RESULT)
app._PRIOR_IMAGE = img #set latest instance of old image
app.addDiagnosisResult(app.DIAGNOSIS_RESULT)
enableDiagnosisResultColouring ( )
def loadCovid19ImageFromDialog():
currdir = os.getcwd()
image_file = filedialog.askopenfile(mode ='r', parent=root, initialdir=currdir, title='Please select an Xray Image of suspected coronavirus2019 case:')
root.wm_title(windowTitle + " : " + image_file.name)
loadCovid19ImageFromName(image_file.name)
def loadCovid19ImageFromName(filename):
app._PRIOR_IMAGE.destroy() #destroy old image
load = Image.open(filename)
load = load.resize((CONSTANT_DIAGNOSIS_IMAGE_SPAN, CONSTANT_DIAGNOSIS_IMAGE_SPAN),Image.ANTIALIAS) #Resized "load" image to constant size on screen. However, neural network still runs on on original image scale from filename.
render = ImageTk.PhotoImage(load)
img = Label(image=render)
img.image = render
img.place(x=(int(screenWidth)/2)-CONSTANT_DIAGNOSIS_IMAGE_SPAN/2, y=((int(screenHeight)/2))-CONSTANT_DIAGNOSIS_IMAGE_SPAN/2-80)
app.DIAGNOSIS_RESULT += "**Covid19 Mode Result**\n" + filename+"\n\n"
app.DIAGNOSIS_RESULT += covid19_ai_diagnoser.doOnlineInference_covid19Pneumonia (filename)
print(app.DIAGNOSIS_RESULT)
app._PRIOR_IMAGE = img #set latest instance of old image
app.addDiagnosisResult(app.DIAGNOSIS_RESULT)
enableDiagnosisResultColouring ( )
# Adding a load image button to the cascade menu "File"
filemenu.add_command(label="Load image to test for pneumonia", command=loadRegularPneumoniaImageFromDialog)
filemenu.add_command(label="Load image to test for covid-19", command=loadCovid19ImageFromDialog)
def colourDiagnosisMessageText ( diagnosisContent, startIndexText, endIndexText ):
#If pneumonia or covid19 is detected
if ( covid19_ai_diagnoser.DIAGNOSIS_MESSAGES[0] in diagnosisContent or covid19_ai_diagnoser.DIAGNOSIS_MESSAGES[1] in diagnosisContent ):
app.DIAGNOSIS_RESULT_FIELD.tag_add("DIAGNOSIS_RESULT_MESSAGE", startIndexText, endIndexText)
app.DIAGNOSIS_RESULT_FIELD.tag_configure("DIAGNOSIS_RESULT_MESSAGE", background="red", foreground ="white")
#If normal lungs state is detected
if ( covid19_ai_diagnoser.DIAGNOSIS_MESSAGES[2] in diagnosisContent ):
app.DIAGNOSIS_RESULT_FIELD.tag_add("DIAGNOSIS_RESULT_MESSAGE", startIndexText, endIndexText)
app.DIAGNOSIS_RESULT_FIELD.tag_configure("DIAGNOSIS_RESULT_MESSAGE", background="green", foreground ="white")
def enableDiagnosisResultColouring ( ):
diagnosisResultFieldContent = app.DIAGNOSIS_RESULT_FIELD.get("1.0","end")
colourDiagnosisMessageText ( diagnosisResultFieldContent, "4.0", "4.21" )
############
#root cycle
root.config(menu=menubar)
root.mainloop()