diff --git a/examples/basic/demo_class_1.py b/examples/basic/demo_class_1.py new file mode 100644 index 00000000..dabdf00e --- /dev/null +++ b/examples/basic/demo_class_1.py @@ -0,0 +1,18 @@ + +class Rectangle: + def __init__(self, x, y, w, h) -> None: + print("init") + self.x = x + self.y = y + self.w = w + self.h = h + + def print_info(self): + print(f"{self.x} {self.y} {self.w} {self.h}") + + +rect1 = Rectangle(10, 10, 20, 40) +rect2 = Rectangle(0, 0, 100, 200) +rect1.print_info() +rect2.print_info() + diff --git a/examples/basic/demo_class_2.py b/examples/basic/demo_class_2.py new file mode 100644 index 00000000..059e1581 --- /dev/null +++ b/examples/basic/demo_class_2.py @@ -0,0 +1,33 @@ + +class A: + static_v1 = 1 + def __init__(self) -> None: + print("init") + self.a = 10 + + def hello(self): + print("hello", self.a) + self.a += 1 + + +obj_1 = A() +obj_2 = A() +print("obj1.static_v1:", obj_1.static_v1) +print("obj_2.static_v1:", obj_2.static_v1) +print("A.static_v1:", A.static_v1) +print("obj1.a:", obj_1.a) +print("obj2.a:",obj_2.a) +obj_2.hello() +print("") + +obj_1.static_v1 = 2 +print("obj1.static_v1:", obj_1.static_v1) +print("obj_2.static_v1:", obj_2.static_v1) +print("A.static_v1:", A.static_v1) +print("") + +A.static_v1 = 2 +print("obj1.static_v1:", obj_1.static_v1) +print("obj_2.static_v1:", obj_2.static_v1) +print("A.static_v1:", A.static_v1) + diff --git a/examples/basic/demo_class_3.py b/examples/basic/demo_class_3.py new file mode 100644 index 00000000..12327e62 --- /dev/null +++ b/examples/basic/demo_class_3.py @@ -0,0 +1,34 @@ + +class Rectangle: + def __init__(self, x, y, w, h) -> None: + print("init") + self.x = x + self.y = y + self.w = w + self.h = h + + def print_info(self): + print(f"{self.x} {self.y} {self.w} {self.h}") + +class Result(Rectangle): + def __init__(self, x, y, w, h, id): + super(Result, self).__init__(x, y, w, h) + self.id = id + + def print_info(self): + print(f"[{self.id}] {self.x} {self.y} {self.w} {self.h}") + +class RectangleColor(Rectangle): + def __init__(self, x, y, w, h, color : int): + super(RectangleColor, self).__init__(x, y, w, h) + self.color = color + + def print_info(self): + print(f"[0x{self.color:06X}] {self.x} {self.y} {self.w} {self.h}") + + +obj1 = Result(10, 10, 20, 40, "person") +obj2 = RectangleColor(0, 0, 100, 200, 0xff00ff) +obj1.print_info() +obj2.print_info() + diff --git a/examples/basic/demo_function.py b/examples/basic/demo_function.py new file mode 100644 index 00000000..447f582c --- /dev/null +++ b/examples/basic/demo_function.py @@ -0,0 +1,6 @@ + +def add(x, y): + return x + y + +print(add(1, 2)) + diff --git a/examples/basic/demo_str_bytes.py b/examples/basic/demo_str_bytes.py new file mode 100644 index 00000000..c8d138ce --- /dev/null +++ b/examples/basic/demo_str_bytes.py @@ -0,0 +1,28 @@ + +a = "hello" +b = b'hello' +print(type(a), type(b)) +print("") + +print(a == b.decode("utf-8")) +print(a.encode("utf-8") == b) +print("") + +a = "abc123\x00\x01\x02\x03" +b = b"abc123\x00\x01\x02\x03" +print("a:", a) +print("b:", b) +print("a[0]:", a[0], type(a[0])) +print("b[0]:", b[0], type(b[0])) +print("") + +a = 0x61 # 97, b'a' +print(f"hex: 0x{a:x}") +print(f"decimalism: {a}") +print(f"ASCII(str): {chr(a)}") +print("") + + +a = [97, 98, 99, 49, 50, 51, 0, 1, 2, 3] +print(bytes(a)) + diff --git a/examples/basic/demo_time.py b/examples/basic/demo_time.py new file mode 100644 index 00000000..570299df --- /dev/null +++ b/examples/basic/demo_time.py @@ -0,0 +1,51 @@ +import time as time_sys # We can use Python integrated time module. +from maix import time # And maix also have time module, have some functions python time module don't have. +from maix import app + +# get current system time. +# If system time change(like NTP), there value will be change also, +# so take care, these value may have large change after bootup's NTP sync time from network. +# If you want to calculate time interval please use ticks fufnctions. +print("timestamp s:", time.time()) +print("timestamp ms:", time.time_ms()) +print("timestamp us:", time.time_us()) +print("time:", time.localtime().strftime("%Y-%m-%d %H:%M:%S")) +print("timezone:", time.timezone()) +print("") + +# set timezone +# timezones = time.list_timezones() # Get all supportted timezones +# locale = "Asia/Shanghai" +# locale = "Etc/UTC" +# time.timezone(locale) +# print("Set timezone to:", time.timezone()) +print("") + +print("time since bootup") +print("ticks s:", time.ticks_s()) +print("ticks ms:", time.ticks_ms()) +print("ticks us:", time.ticks_us()) +print("now sleep 500 ms") +t = time.ticks_ms() +time.sleep_ms(500) # sleep_us sleep_ms sleep_s sleep +print(f"sleep {time.ticks_ms() - t} ms done") +print("") + +# You can also use python integrated time module +print("timestamp s:", time_sys.time()) + +# FPS +t = time.ticks_s() +while (not app.need_exit()) and time.ticks_diff(t) < 5: + time.sleep_ms(50) + time.sleep_ms(50) + fps = time.fps() + print(f"fps 1: {fps}") + +t = time.ticks_s() +while (not app.need_exit()) and time.ticks_diff(t) < 5: + time.sleep_ms(50) + time.fps_start() + time.sleep_ms(50) + fps = time.fps() + print(f"fps 2: {fps}") diff --git a/examples/hello_maix.py b/examples/hello_maix.py index 3d734189..9c295c0d 100644 --- a/examples/hello_maix.py +++ b/examples/hello_maix.py @@ -1,11 +1,19 @@ -from maix import image, camera, display, app, time +from maix import camera, display, app, time -cam = camera.Camera(512, 320) # Manually set resolution, default is too large +cam = camera.Camera(512, 320) # Manually set resolution + # | 手动设置分辨率 disp = display.Display() # MaixCAM default is 522x368 + # | MaixCAM 默认是 522x368 -while 1: - t = time.ticks_ms() - img = cam.read() - disp.show(img) - print(f"time: {time.ticks_ms() - t}ms, fps: {1000 / (time.ticks_ms() - t)}") +while not app.need_exit(): + # time.fps_start() # Manually set fps calculation start point, comment here mean last time fps() call is start + # | 动设置帧率(FPS)计算开始点,这里注释了表示上一次 fps 函数即是开始 + img = cam.read() # Get one frame from camera, img is maix.image.Image type object + # | 从摄像头获取一帧图像,img 是 maix.image.Image 类型的对象 + disp.show(img) # Show image to screen + # | 将图像显示到屏幕 + fps = time.fps() # Calculate FPS between last time fps() call and this time call. + # | 计算两次 fps 函数调用之间的帧率 + print(f"time: {1000/fps:.02f}ms, fps: {fps:.02f}") # print FPS in console + # | 在终端打印帧率(FPS) diff --git a/examples/vision/camera/camera_capture.py b/examples/vision/camera/camera_capture.py index 56d80f68..9c295c0d 100644 --- a/examples/vision/camera/camera_capture.py +++ b/examples/vision/camera/camera_capture.py @@ -1,11 +1,19 @@ -from maix import camera, display, time, app +from maix import camera, display, app, time -cam = camera.Camera(512, 320) # Manually set resolution, default is too large -disp = display.Display() # MaixCAM default is 552x368 +cam = camera.Camera(512, 320) # Manually set resolution + # | 手动设置分辨率 +disp = display.Display() # MaixCAM default is 522x368 + # | MaixCAM 默认是 522x368 while not app.need_exit(): - t = time.ticks_ms() - img = cam.read() # Max FPS is determined by the camera hardware and driver settings - disp.show(img) - print(f"time: {time.ticks_ms() - t}ms, fps: {1000 / (time.ticks_ms() - t)}") + # time.fps_start() # Manually set fps calculation start point, comment here mean last time fps() call is start + # | 动设置帧率(FPS)计算开始点,这里注释了表示上一次 fps 函数即是开始 + img = cam.read() # Get one frame from camera, img is maix.image.Image type object + # | 从摄像头获取一帧图像,img 是 maix.image.Image 类型的对象 + disp.show(img) # Show image to screen + # | 将图像显示到屏幕 + fps = time.fps() # Calculate FPS between last time fps() call and this time call. + # | 计算两次 fps 函数调用之间的帧率 + print(f"time: {1000/fps:.02f}ms, fps: {fps:.02f}") # print FPS in console + # | 在终端打印帧率(FPS)