Skip to main content

MaixPy Python3 library

Project description

MaixPy3 Logo

MaixPy3 GitHub forks GitHub stars GitHub watchers

English | 简体中文

MIT PyPI version Python issue

MaixPy3 is a Python3 toolkit based on cpython, which simplifies the development of applications on Linux AI edge devices through Python programming.

TOC

Usage

Display the camera image on the screen.

from maix import display, camera
display.show(camera.capture())

After inputting the image to the model, the result of the forward algorithm is returned.

from PIL import Image
from maix import nn

m = nn.load({
    "param": "/root/models/resnet_awnn.param",
    "bin": "/root/models/resnet_awnn.bin"
  }, opt={
    "model_type":  "awnn",
    "inputs": {
        "input0": (224, 224, 3)
    },
    "outputs": {
        "output0": (1, 1, 1000)
    },
    "first_layer_conv_no_pad": False,
    "mean": [127.5, 127.5, 127.5],
    "norm": [0.00784313725490196, 0.00784313725490196, 0.00784313725490196],
})

img = Image.open("input.jpg")
out = m.forward(img, quantize=True)
print(out.shape)
out = nn.F.softmax(out)
print(out.max(), out.argmax())

Some examples of accessing hardware peripherals.

  • GPIO
import time
from maix import gpio
PH_BASE = 224 # "PH"
gpiochip1 = gpio.chip("gpiochip1")
led = gpiochip1.get_line((PH_BASE + 14)) # "PH14"
config = gpio.line_request()
config.request_type = gpio.line_request.DIRECTION_OUTPUT
led.request(config)

while led:
    led.set_value(0)
    time.sleep(0.1)
    led.set_value(1)
    time.sleep(0.1)
  • PWM
import time
from maix import pwm

with pwm.PWM(6) as pwm6:
  pwm6.period = 1000000
  pwm6.duty_cycle = 10000
  pwm6.enable = True
  while True:
    for i in range(50, 1, -10):
      pwm6.duty_cycle = i * 10000
      time.sleep(0.1)
    for i in range(1, 50, +10):
      pwm6.duty_cycle = i * 10000
      time.sleep(0.1)
  • I2C
from maix import i2c
i2c = i2c.I2CDevice('/dev/i2c-2', 0x26)
i2c.write(0x1, b'\xAA')
print(i2c.read(0x1, 1))
  • SPI
from maix import spi
spi = spi.SpiDev()
spi.open(1, 0) # '/dev/spidev1.0'

spi.bits_per_word = 8
spi.max_speed_hz = 1
spi.mode = 0b11

import time

while True:
  time.sleep(0.1)
  to_send = [0x01, 0x02, 0x01]
  print(spi.xfer2(to_send, 800000)) # 800Khz
  • UART
from maix import serial

with serial.Serial("/dev/ttyS1",115200) as ser:
  ser.write(b"Hello Wrold !!!\n")
  ser.setDTR(True)
  ser.setRTS(True)
  tmp = ser.readline()
  print(tmp)
  ser.write(tmp)
  ser.setDTR(False)
  ser.setRTS(False)
  • EVENT
from maix import evdev
from select import select
dev = evdev.InputDevice('/dev/input/event9')

while True:
  select([dev], [], [])
  for event in dev.read():
    print(event.code, event.value)

See the documentation for more information

Jupyter

Install rpyc_ikernel kernel in jupyter notebook & lab to get an IDE that can remotely call Python code, videos, and image streaming.

Click here to view the effect usage_display_hook.ipynb. Note that jupyter runs on your computer.

Progress

Platform Status Example config Memory usage
linux x86_64 working general general unlimited
maix_v83x working maix_v83x maix_v83x 11M+
maix_r329 working maix_r329 maix_r329 unknown
RPi_XXXX maybe NULL
Windows build fail

The development progress is in no particular order.

Build

Under linux x86_64, use python3 setup.py build to complete the general package construction.

For other platforms, take the version of maix_v83x as an example, match the Python3 + cross-compilation chain of the corresponding platform(toolchain_v83x_linux_x86), and run python3.8 setup.py build maix_v83x to complete the construction of the target platform package.

Welcome to provide configurations of different platforms to MaixPy3/envs/ to adapt to the MaixPy3 environment.

If you need .whl pre-compiled package, please change build to bdist_wheel.

Develop

Tested glibc >= 27 on Ubuntu20.04 & manjaro20.03.

Each catalog function of the project.

  • docs [store some general development documents]
  • envs [store compilation configurations for different platforms]
  • examples [store examples or applications on different platforms]
  • ext_modules [store project modules that need to be compiled]
  • maix [Provide maix entry Python module]
  • tests [Provide tox common test items]
  • setup.py [MaixPy3 project compilation entry]

If you want to submit the configuration of other platforms, please refer to the following:

_maix_modules = [
    libi2c_module,
]

_maix_data_files = [
    ('/maix', get_srcs(ext_so, ['so' 'ko'])),
]

_maix_py_modules = [
    "numpy",
]

If you want to submit some Python and C modules that need to be compiled, it is recommended to use the sub-repository to import, refer to the following:

If you want to submit some useful Python tools or sample code, refer to the following:

Thanks

All this comes from the power of open source. Thanks to them, they are listed in no particular order.

The open source repositories that may be cited in the future are also grateful to them.

You are welcome to recommend the open source projects you need. If you find any missing projects, please let me know immediately.

License

Copyright (c) Sipeed Corporation. All rights reserved.

Licensed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

maixpy3-0.3.4.tar.gz (13.3 MB view hashes)

Uploaded Source

Built Distributions

maixpy3-0.3.4-cp39-cp39-manylinux2014_aarch64.whl (20.5 MB view hashes)

Uploaded CPython 3.9

maixpy3-0.3.4-cp38-cp38-linux_armv7l.whl (3.6 MB view hashes)

Uploaded CPython 3.8

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page