1111def is_in_button (x , y , btn_pos ):
1212 return x > btn_pos [0 ] and x < btn_pos [0 ] + btn_pos [2 ] and y > btn_pos [1 ] and y < btn_pos [1 ] + btn_pos [3 ]
1313
14+ def get_back_btn_img (width ):
15+ ret_width = int (width * 0.1 )
16+ img_back = image .load ("/maixapp/share/icon/ret.png" )
17+ w , h = (ret_width , img_back .height () * ret_width // img_back .width ())
18+ if w % 2 != 0 :
19+ w += 1
20+ if h % 2 != 0 :
21+ h += 1
22+ img_back = img_back .resize (w , h )
23+ return img_back
24+
1425def main (disp ):
1526 global curr_model
1627
@@ -28,19 +39,25 @@ def main(disp):
2839 classifier = nn .Classifier (model = models [models_keys [curr_model ]], dual_buff = False )
2940 cam = camera .Camera (detector .input_width (), detector .input_height (), detector .input_format ())
3041
42+ font_scale = 2 if cam .height () >= 480 else 1.2
43+ font_thickness = 2 if cam .height () >= 480 else 1
44+ str_size = image .string_size ("A" , scale = font_scale , thickness = font_thickness )
45+ str_w = str_size .width ()
46+ str_h = str_size .height ()
47+
3148 mode_pressed = False
3249 ts = touchscreen .TouchScreen ()
33- img_back = image . load ( "/maixapp/share/icon/ret.png" )
34- back_rect = [0 , 0 , 32 , 32 ]
35- mode_rect = [0 , cam .height () - 26 , image .string_size (models_keys [curr_model ]).width () + 6 , 30 ]
50+ img_back = get_back_btn_img ( cam . width () )
51+ back_rect = [0 , 0 , img_back . width (), img_back . height () ]
52+ mode_rect = [0 , cam .height () - int ( str_h * 2 ) , image .string_size (models_keys [curr_model ], scale = font_scale , thickness = font_thickness ).width () + 6 , int ( str_h * 2 ) ]
3653 back_rect_disp = image .resize_map_pos (cam .width (), cam .height (), disp .width (), disp .height (), image .Fit .FIT_CONTAIN , back_rect [0 ], back_rect [1 ], back_rect [2 ], back_rect [3 ])
3754 mode_rect_disp = image .resize_map_pos (cam .width (), cam .height (), disp .width (), disp .height (), image .Fit .FIT_CONTAIN , mode_rect [0 ], mode_rect [1 ], mode_rect [2 ], mode_rect [3 ])
3855
3956
4057 # for draw result info
4158 max_labels_length = 0
4259 for label in classifier .labels :
43- size = image .string_size (label )
60+ size = image .string_size (label , scale = font_scale , thickness = font_thickness )
4461 if size .width () > max_labels_length :
4562 max_labels_length = size .width ()
4663
@@ -73,18 +90,18 @@ def main(disp):
7390 for j in range (len (classifier .labels )):
7491 idx = res [j ][0 ]
7592 score = res [j ][1 ]
76- img .draw_string (0 , img_std_first .height () + idx * 16 , classifier .labels [idx ], image .COLOR_WHITE )
77- img .draw_rect (max_labels_length , int (img_std_first .height () + idx * 16 ), int (score * max_score_length ), 8 , image .COLOR_GREEN if score >= emotion_conf_th else image .COLOR_RED , - 1 )
78- img .draw_string (int (max_labels_length + score * max_score_length + 2 ), int (img_std_first .height () + idx * 16 ), f"{ score :.1f} " , image .COLOR_RED )
93+ img .draw_string (0 , img_std_first .height () + idx * str_h , classifier .labels [idx ], image .COLOR_WHITE , font_scale , font_thickness )
94+ img .draw_rect (max_labels_length , int (img_std_first .height () + idx * str_h ), int (score * max_score_length ), 8 , image .COLOR_GREEN if score >= emotion_conf_th else image .COLOR_RED , - font_thickness )
95+ img .draw_string (int (max_labels_length + score * max_score_length + 2 ), int (img_std_first .height () + idx * str_h ), f"{ score :.1f} " , image .COLOR_RED , font_scale , font_thickness )
7996 # draw on all face
8097 color = image .COLOR_GREEN if res [0 ][1 ] >= emotion_conf_th else image .COLOR_RED
8198 obj = objs [idxes [i ]]
8299 img .draw_rect (obj .x , obj .y , obj .w , obj .h , color , 1 )
83- img .draw_string (obj .x , obj .y , f"{ classifier .labels [res [0 ][0 ]]} : { res [0 ][1 ]:.1f} " , color )
100+ img .draw_string (obj .x , obj .y , f"{ classifier .labels [res [0 ][0 ]]} : { res [0 ][1 ]:.1f} " , color , font_scale , font_thickness )
84101
85102 img .draw_image (0 , 0 , img_back )
86103 img .draw_rect (mode_rect [0 ], mode_rect [1 ], mode_rect [2 ], mode_rect [3 ], image .COLOR_WHITE )
87- img .draw_string (4 , img .height () - 20 , f"{ models_keys [curr_model ]} " )
104+ img .draw_string (4 , img .height () - str_h - str_h // 2 , f"{ models_keys [curr_model ]} " , scale = font_scale , thickness = font_thickness )
88105 disp .show (img )
89106 x , y , preesed = ts .read ()
90107 if preesed :
@@ -97,8 +114,8 @@ def main(disp):
97114 curr_model = (curr_model + 1 ) % len (models_keys )
98115 msg = "switching model ..."
99116 size = image .string_size (msg , scale = 1.3 )
100- img .draw_string ((img .width () - size .width ()) // 2 , (img .height () - size .height ())// 2 , msg , image .COLOR_RED , scale = 1.3 , thickness = - 3 )
101- img .draw_string ((img .width () - size .width ()) // 2 , (img .height () - size .height ())// 2 , msg , image .COLOR_WHITE , scale = 1.3 )
117+ img .draw_string ((img .width () - size .width ()) // 2 , (img .height () - size .height ())// 2 , msg , image .COLOR_RED , font_scale , int ( font_thickness * 2 ) )
118+ img .draw_string ((img .width () - size .width ()) // 2 , (img .height () - size .height ())// 2 , msg , image .COLOR_WHITE , font_scale , font_thickness )
102119 disp .show (img )
103120 del detector
104121 del landmarks_detector
@@ -111,6 +128,7 @@ def main(disp):
111128except Exception :
112129 import traceback
113130 msg = traceback .format_exc ()
131+ print (msg )
114132 img = image .Image (disp .width (), disp .height ())
115133 img .draw_string (0 , 0 , msg , image .COLOR_WHITE )
116134 disp .show (img )
0 commit comments