forked from stipa01/passion_fruit_mxnet_mobile_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
119 lines (94 loc) · 3.95 KB
/
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
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivymd.uix.filemanager import MDFileManager
from kivymd.utils.fitimage import FitImage
from detect import DetectFruitHealth
from kivy.properties import BooleanProperty, ObjectProperty
from kivymd.toast import toast
from kivy.clock import Clock
from kivy.utils import platform
from kivymd.uix.dialog import MDDialog
PRIMARY_EXTERNAL_STORAGE = '/'
if platform == "android":
from android.permissions import request_permissions, Permission
request_permissions([Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE])
from android.storage import primary_external_storage_path
PRIMARY_EXTERNAL_STORAGE = primary_external_storage_path()
class CustomLayout(BoxLayout):
detect = BooleanProperty(defaultvalue=False) # Image loaded and program is ready to infer
detecting = BooleanProperty(defaultvalue=False) # Program is in inference process
dialog = ObjectProperty(defaultvalue=None)
def __init__(self, **kwargs):
super(CustomLayout, self).__init__(**kwargs)
Window.bind(on_keyboard=self.events)
self.manager_open = False
self.current_image = 'placeholder.png'
self.image_model_content = None
self.file_manager = MDFileManager(
exit_manager=self.exit_manager,
select_path=self.select_path,
preview=True,
)
self.detection_model = DetectFruitHealth()
def file_manager_open(self):
self.file_manager.show(PRIMARY_EXTERNAL_STORAGE) # output manager to the screen
self.manager_open = True
def select_path(self, path):
"""It will be called when you click on the file name
or the catalog selection button.
:type path: str;
:param path: path to the selected directory or file;
"""
self.exit_manager()
self.detect = True
self.ids.inference_img.source = path
self.current_image = path
if PRIMARY_EXTERNAL_STORAGE != '/':
path = path.replace(PRIMARY_EXTERNAL_STORAGE, '')
self.ids.img_info.text = path
def exit_manager(self, *args):
"""Called when the user reaches the root of the directory tree."""
self.manager_open = False
self.file_manager.close()
def events(self, instance, keyboard, keycode, text, modifiers):
"""Called when buttons are pressed on the mobile device."""
if keyboard in (1001, 27):
if self.manager_open:
self.file_manager.back()
return True
def infer(self, img_path):
if img_path == 'placeholder.png':
toast("Please first select an image!")
return
self.detecting = True
self.detection_model.run(img_path=img_path)
self.detect = False
Clock.schedule_interval(callback=self.check_inference_result, timeout=.2)
def check_inference_result(self, dt):
if self.detection_model.output_image_path is not None:
self.ids.inference_img.source = self.detection_model.output_image_path
self.current_image = self.detection_model.output_image_path
self.detecting = False
self.detection_model.output_image_path = None
return False
def open_dialog(self):
if not self.dialog:
self.image_model_content = FitImage(source=self.current_image, size_hint_y=None, height="248dp")
self.dialog = MDDialog(
title="Image Modal View",
type="custom",
content_cls=self.image_model_content,
)
else:
self.image_model_content.source = self.current_image
self.dialog.open()
class MainApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def build(self):
kv = Builder.load_file('main.kv')
return kv
if __name__ == '__main__':
MainApp().run()