Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Aug 21, 2024
1 parent 61a88d2 commit bfdd87d
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 14 deletions.
18 changes: 18 additions & 0 deletions examples/basic/demo_class_1.py
Original file line number Diff line number Diff line change
@@ -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()

33 changes: 33 additions & 0 deletions examples/basic/demo_class_2.py
Original file line number Diff line number Diff line change
@@ -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)

34 changes: 34 additions & 0 deletions examples/basic/demo_class_3.py
Original file line number Diff line number Diff line change
@@ -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()

6 changes: 6 additions & 0 deletions examples/basic/demo_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

def add(x, y):
return x + y

print(add(1, 2))

28 changes: 28 additions & 0 deletions examples/basic/demo_str_bytes.py
Original file line number Diff line number Diff line change
@@ -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))

51 changes: 51 additions & 0 deletions examples/basic/demo_time.py
Original file line number Diff line number Diff line change
@@ -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}")
22 changes: 15 additions & 7 deletions examples/hello_maix.py
Original file line number Diff line number Diff line change
@@ -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)

22 changes: 15 additions & 7 deletions examples/vision/camera/camera_capture.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit bfdd87d

Please sign in to comment.