Skip to content

Commit 30903fc

Browse files
committed
* [maixcam2] check if the app has sufficient enough to run the large model
1 parent d36307f commit 30903fc

File tree

4 files changed

+129
-7
lines changed

4 files changed

+129
-7
lines changed

projects/app_chat/main.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ def __init__(self):
8888
self.__show_load_info('loading touchscreen..')
8989
self.ts = touchscreen.TouchScreen()
9090

91+
self.check_memory()
92+
9193
self.exit_img = image.load('./assets/exit.jpg')
9294
# self.__show_load_info('loading key..')
9395
# self.key_obj = key.Key(self.on_key)
@@ -138,7 +140,35 @@ def __init__(self):
138140
self.llm_last_msg = ""
139141

140142
self.page_text = PagedText()
141-
143+
144+
def check_memory(self):
145+
from maix import sys
146+
ok = False
147+
font = "sourcehansans"
148+
mem_info = sys.memory_info()
149+
if "hw_total" in mem_info:
150+
hw_total = mem_info.get("hw_total", 0)
151+
print(f"hw_total: {hw_total}({hw_total/1024/1024/1024}G)")
152+
if hw_total < 4 * 1024 * 1024 * 1024: # is not 4g version, try release more memory
153+
ok = False
154+
else:
155+
ok = True
156+
if ok == False:
157+
img = image.Image(self.disp_w, self.disp_h, bg=image.COLOR_BLACK)
158+
err_title_msg = "Ops!!!"
159+
err_msg = "You need the 4GB version of the board to run this application."
160+
err_exit_msg = "Tap anywhere on the screen to exit."
161+
img.draw_string(0, 0, err_title_msg, image.COLOR_WHITE, 1, font=font)
162+
img.draw_string(0, 20, err_msg, image.COLOR_WHITE, 1, font=font)
163+
img.draw_string(0, 200, err_exit_msg, image.COLOR_WHITE, 0.6, font=font)
164+
self.disp.show(img)
165+
while not app.need_exit():
166+
ts_data = self.ts.read()
167+
if ts_data[2]:
168+
app.set_exit_flag(True)
169+
time.sleep_ms(100)
170+
exit(0)
171+
142172
def _whisper_thread_handle(self, path):
143173
self.whisper_results = self.whisper.transcribe(path)
144174

projects/app_speech/main.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ def __init__(self):
1616
self.disp = display.Display()
1717
self.disp_w = self.disp.width()
1818
self.disp_h = self.disp.height()
19-
2019
self.ts = touchscreen.TouchScreen()
20+
self.check_memory()
21+
2122
self.asr = nn.Whisper(model="/root/models/whisper-base/whisper-base.mud", language=self.language)
2223
self.asr_result:None|str = None
2324
self.asr_thread:None|threading.Thread = None
@@ -53,6 +54,34 @@ def __init__(self):
5354

5455
self.status = AppStatus.IDLE
5556

57+
def check_memory(self):
58+
from maix import sys
59+
ok = False
60+
font = "sourcehansans"
61+
mem_info = sys.memory_info()
62+
if "hw_total" in mem_info:
63+
hw_total = mem_info.get("hw_total", 0)
64+
print(f"hw_total: {hw_total}({hw_total/1024/1024/1024}G)")
65+
if hw_total < 4 * 1024 * 1024 * 1024: # is not 4g version, try release more memory
66+
ok = False
67+
else:
68+
ok = True
69+
if ok == False:
70+
img = image.Image(self.disp_w, self.disp_h, bg=image.COLOR_BLACK)
71+
err_title_msg = "Ops!!!"
72+
err_msg = "You need the 4GB version of the board to run this application."
73+
err_exit_msg = "Tap anywhere on the screen to exit."
74+
img.draw_string(0, 0, err_title_msg, image.COLOR_WHITE, 1, font=font)
75+
img.draw_string(0, 20, err_msg, image.COLOR_WHITE, 1, font=font)
76+
img.draw_string(0, 200, err_exit_msg, image.COLOR_WHITE, 0.6, font=font)
77+
self.disp.show(img)
78+
while not app.need_exit():
79+
ts_data = self.ts.read()
80+
if ts_data[2]:
81+
app.set_exit_flag(True)
82+
time.sleep_ms(100)
83+
exit(0)
84+
5685
def run_asr(self, pcm:bytes):
5786
self.asr_thread_exit = False
5887
self.asr_thread = threading.Thread(target=self.asr_thread_handle, args=[pcm], daemon=True)

projects/app_vlm/main.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def add_text(self, text):
2323
current_page.append(("", 0, 0)) # 初始化第一行
2424

2525
page_height_used = sum(line[2] for line in current_page)
26-
26+
2727
for ch in text:
2828
if ch == '\n' or ch == '\r':
2929
continue
@@ -32,7 +32,7 @@ def add_text(self, text):
3232
size = image.string_size(new_line_text)
3333
ch_w = size[0]
3434
ch_h = size[1]
35-
35+
3636
# 尝试放到当前行
3737
if ch_w <= self.page_width:
3838
# 更新行
@@ -76,7 +76,7 @@ def draw_last_page_on(self, img:image.Image, color: image.Color = image.COLOR_WH
7676

7777
height = 0
7878
for line_text, _, line_height in current_page:
79-
img.draw_string(0, height, line_text, color, wrap_space=0)
79+
img.draw_string(0, height, line_text, color, wrap_space=0)
8080
height += line_height
8181

8282
class App:
@@ -97,6 +97,8 @@ def __init__(self):
9797
self.ts = touchscreen.TouchScreen()
9898
self.cam = camera.Camera(640, 360)
9999

100+
self.check_memory()
101+
100102
self.exit_img = image.load('./assets/exit.jpg')
101103
self.ai_isp = bool(int(app.get_sys_config_kv("npu", "ai_isp", "1")))
102104
if self.ai_isp is True:
@@ -143,6 +145,34 @@ def __init__(self):
143145
self.page_text = PagedText(self.disp_w, self.disp_h - self.cam.height())
144146
self.sta = self.Status.IDLE
145147

148+
def check_memory(self):
149+
from maix import sys
150+
ok = False
151+
font = "sourcehansans"
152+
mem_info = sys.memory_info()
153+
if "hw_total" in mem_info:
154+
hw_total = mem_info.get("hw_total", 0)
155+
print(f"hw_total: {hw_total}({hw_total/1024/1024/1024}G)")
156+
if hw_total < 4 * 1024 * 1024 * 1024: # is not 4g version, try release more memory
157+
ok = False
158+
else:
159+
ok = True
160+
if ok == False:
161+
img = image.Image(self.disp_w, self.disp_h, bg=image.COLOR_BLACK)
162+
err_title_msg = "Ops!!!"
163+
err_msg = "You need the 4GB version of the board to run this application."
164+
err_exit_msg = "Tap anywhere on the screen to exit."
165+
img.draw_string(0, 0, err_title_msg, image.COLOR_WHITE, 1, font=font)
166+
img.draw_string(0, 20, err_msg, image.COLOR_WHITE, 1, font=font)
167+
img.draw_string(0, 200, err_exit_msg, image.COLOR_WHITE, 0.6, font=font)
168+
self.disp.show(img)
169+
while not app.need_exit():
170+
ts_data = self.ts.read()
171+
if ts_data[2]:
172+
app.set_exit_flag(True)
173+
time.sleep_ms(100)
174+
exit(0)
175+
146176
def get_vl_model(self):
147177
model_list = ["internvl", "qwen3-vl"]
148178
model_list_num = len(model_list)
@@ -318,7 +348,7 @@ def run(self):
318348
self.sta = self.Status.IDLE
319349

320350
self.show_ui()
321-
351+
322352
if self.vlm:
323353
self.vlm.cancel()
324354
time.sleep_ms(500) # Make sure the VLM has exited.
@@ -330,7 +360,7 @@ def run(self):
330360
self.disp = None
331361

332362
app.set_sys_config_kv("npu", "ai_isp", "1" if self.ai_isp else "0")
333-
363+
334364
if __name__ == '__main__':
335365
appication = App()
336366
appication.run()

projects/app_yoloworld/main.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,17 @@ def transcribe_pcm(self, pcm):
3939

4040
class APP:
4141
def __init__(self, disp):
42+
image.load_font("sourcehansans", "/maixapp/share/font/SourceHanSansCN-Regular.otf", size = 20)
4243
model = "/root/models/yolo-world_1_class.mud"
4344
feature = "/root/models/yolo-world_1_class_person.bin"
4445
labels = "/root/models/yolo-world_1_class_person.txt"
4546
self.model_valid = os.path.exists(model) and os.path.exists(feature) and os.path.exists(labels)
4647
self.disp = disp
48+
self.disp_w = self.disp.width()
49+
self.disp_h = self.disp.height()
4750
self.ts = touchscreen.TouchScreen()
51+
52+
self.check_memory()
4853
if self.model_valid:
4954
self._init_model(model, feature, labels)
5055
else:
@@ -54,6 +59,34 @@ def __init__(self, disp):
5459
image.load_font("sourcehansans", "/maixapp/share/font/SourceHanSansCN-Regular.otf", size = 24)
5560
# image.set_default_font("sourcehansans")
5661

62+
def check_memory(self):
63+
from maix import sys
64+
ok = False
65+
font = "sourcehansans"
66+
mem_info = sys.memory_info()
67+
if "hw_total" in mem_info:
68+
hw_total = mem_info.get("hw_total", 0)
69+
print(f"hw_total: {hw_total}({hw_total/1024/1024/1024}G)")
70+
if hw_total < 4 * 1024 * 1024 * 1024: # is not 4g version, try release more memory
71+
ok = False
72+
else:
73+
ok = True
74+
if ok == False:
75+
img = image.Image(self.disp_w, self.disp_h, bg=image.COLOR_BLACK)
76+
err_title_msg = "Ops!!!"
77+
err_msg = "You need the 4GB version of the board to run this application."
78+
err_exit_msg = "Tap anywhere on the screen to exit."
79+
img.draw_string(0, 0, err_title_msg, image.COLOR_WHITE, 1, font=font)
80+
img.draw_string(0, 20, err_msg, image.COLOR_WHITE, 1, font=font)
81+
img.draw_string(0, 200, err_exit_msg, image.COLOR_WHITE, 0.6, font=font)
82+
self.disp.show(img)
83+
while not app.need_exit():
84+
ts_data = self.ts.read()
85+
if ts_data[2]:
86+
app.set_exit_flag(True)
87+
time.sleep_ms(100)
88+
exit(0)
89+
5790
def _init_model(self, model, feature, labels):
5891
self._destroy_model()
5992
self.detector = nn.YOLOWorld(model, feature, labels, dual_buff=True)

0 commit comments

Comments
 (0)