-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkivy_main.py
154 lines (126 loc) · 6.18 KB
/
kivy_main.py
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image as KivyImage
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.uix.filechooser import FileChooserIconView
from kivy.graphics import Rectangle, Color
from kivy.uix.popup import Popup
from kivy.uix.widget import Widget
from predictor import predictor # Assuming predictor function is in predictor.py file
class StartScreen(Screen):
def __init__(self, **kwargs):
super(StartScreen, self).__init__(**kwargs)
self.background_color = [1, 1, 1, 1]
self.layout = BoxLayout(orientation='vertical')
# Add image covering the whole screen
image = KivyImage(source='first_page.jpg', size=(1, 1))
self.layout.add_widget(image)
# Add next button
next_button = Button(
text='Next',
size_hint=(None, None),
size=(200, 50),
pos_hint={'center_x': 0.5},
background_color=(1, 1, 1, 1),
color=(0, 0, 0, 1)
)
next_button.bind(on_press=self.go_to_capture_screen)
self.layout.add_widget(next_button)
self.add_widget(self.layout)
def go_to_capture_screen(self, instance):
self.manager.current = 'capture_screen'
class CaptureScreen(Screen):
def __init__(self, **kwargs):
super(CaptureScreen, self).__init__(**kwargs)
# Create a white background widget
background = Widget()
background.canvas.add(Color(1, 1, 1, 1))
background.canvas.add(Rectangle(size=self.size))
# Create a BoxLayout with vertical orientation to contain the button
self.layout = BoxLayout(orientation='vertical', padding=50, spacing=50, size_hint_y=None)
# Add the upload button
self.upload_button = Button(
text='Upload Image',
size_hint=(None, None),
size=(200, 50),
pos_hint={'center_x': 0.5},
background_color=(1, 1, 1, 1), # Light blue background color
color=(0, 0, 0, 1), # Black text color
)
# Bind the button press event
self.upload_button.bind(on_press=self.upload_image)
# Add the button to the layout
self.layout.add_widget(self.upload_button)
# Set the BoxLayout's height to match its content
self.layout.bind(minimum_height=self.layout.setter('height'))
# Position the BoxLayout in the center of the screen vertically
self.layout.pos_hint = {'center_x': 0.5, 'center_y': 0.5}
# Add the layout to the screen
self.add_widget(self.layout)
def upload_image(self, instance):
# Open the file chooser when the Upload Image button is pressed
file_chooser = FileChooserIconView(path='/')
file_chooser.bind(on_submit=self.load_uploaded_image)
popup = Popup(title='Select an Image', content=file_chooser, size_hint=(0.9, 0.9))
popup.open()
def load_uploaded_image(self, file_chooser, selected_file, _):
# Check if the selected file is a JPEG or PNG image
if selected_file and (os.path.splitext(selected_file[0])[1].lower() == '.jpeg' or \
os.path.splitext(selected_file[0])[1].lower() == '.png' or \
os.path.splitext(selected_file[0])[1].lower() == '.jpg'):
class_name, probability = predictor(selected_file[0], "class_dict.csv", "model.tflite")
App.get_running_app().show_result(selected_file[0], class_name, probability)
else:
print("Please select a JPEG, JPG or PNG image.")
class ResultScreen(Screen):
def __init__(self, image_path, class_name, probability, **kwargs):
super(ResultScreen, self).__init__(**kwargs)
self.background_color = [1, 1, 1, 1]
self.layout = BoxLayout(orientation='vertical')
# Display the captured image
image = KivyImage(source=image_path, size_hint=(1, 0.8))
self.layout.add_widget(image)
# Display the predicted class and probability
prediction_label = result_label = Label(text=f'Disease: {class_name}\nProbability: {probability}', size_hint=(1, 0.1))
self.layout.add_widget(prediction_label)
# Create a button to go back to the capture screen
# Create a button to go back to the capture screen
back_button = Button(
text='Capture New Image',
size_hint=(None, None),
size=(200, 50),
pos_hint={'center_x': 0.5},
background_color=(1, 1, 1, 1), # Light blue background color
color=(0, 0, 0, 1), # Black text color
)
back_button.bind(on_press=self.go_to_capture_screen)
self.layout.add_widget(back_button)
self.add_widget(self.layout)
def go_to_capture_screen(self, instance):
# Change to the capture screen
self.manager.current = 'capture_screen'
class SkinDiseaseDetectorApp(App):
def __init__(self, **kwargs):
super(SkinDiseaseDetectorApp, self).__init__(**kwargs)
self.screen_manager = ScreenManager()
def build(self):
self.start_screen = StartScreen(name='start_screen')
self.capture_screen = CaptureScreen(name='capture_screen')
self.screen_manager.add_widget(self.start_screen)
self.screen_manager.add_widget(self.capture_screen)
return self.screen_manager
def show_result(self, image_path, class_name, probability):
# Remove the old result screen if it exists
if 'result_screen' in self.screen_manager.screen_names:
self.screen_manager.remove_widget(self.screen_manager.get_screen('result_screen'))
# Add the new result screen
result_screen = ResultScreen(image_path=image_path, class_name=class_name, probability=probability, name='result_screen')
self.screen_manager.add_widget(result_screen)
self.screen_manager.current = 'result_screen'
if __name__ == '__main__':
SkinDiseaseDetectorApp().run()