### K230 Bare-metal Shell Example Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/user-contribute/k230-bare-metal-embedded Demonstrates a simple command-line interface (shell) for bare-metal environments on the K230. It requires minimal setup with UART functions (`putchar`, `getchar`) and a basic parser. This example showcases interactive commands for system control and information retrieval. ```shell K230> help Available commands: help - print this help echo - print reboot - reboot the system mem_read
- read memory mem_write
- write memory tsensor - read temperature sensor cpuid - print CPUID serialboot - enter serial boot mode jump
- jump to address jumpbig
- jump to big core and run ``` -------------------------------- ### PM Module Overview and Usage Example Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/mpp/k230_canmv_pm_module_api Introduction to the PM module, its components (CPU and KPU power management), and a Python example demonstrating how to get current CPU frequency, list supported frequencies, and set a CPU frequency profile. ```APIDOC ## PM Module Overview and Usage Example ### Description The PM module, or Power Management module, is designed to optimize and manage device energy consumption. It provides power management functionalities for both the CPU and KPU (Neural Processing Unit) within the MicroPython environment. The module is accessed via `mpp.pm`, which contains `cpu` and `kpu` objects for managing their respective power states. ### Usage Example This example shows how to interact with the CPU power management features: ```python from mpp import pm # Get the current CPU frequency current_freq = pm.cpu.get_freq() # Get a list of supported CPU frequency profiles supported_freqs = pm.cpu.list_profiles() # Set the CPU frequency to a specific profile (e.g., profile index 1) pm.cpu.set_profile(1) ``` ``` -------------------------------- ### Basic Wired Network Example Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/network/eth A simple example demonstrating how to initialize the wired network interface and configure it using DHCP. ```APIDOC ## Basic Wired Network Configuration ### Description This script initializes the `network.LAN` interface, configures it to use DHCP for network settings, and then checks and prints the connection status and obtained network configuration. ### Method Python script execution ### Endpoint N/A ### Parameters None ### Request Example ```python import network def nic_config(): # Create network interface nic = network.LAN() # Set DHCP mode (automatic allocation by your router) nic.ifconfig("dhcp") # Check connection status if nic.isconnected(): print("Network configuration successful!") print("Automatically obtained configuration: ", nic.ifconfig()) else: print("Connection failed, please check network cable connection") nic_config() ``` ### Response #### Success Response (Output) - **Status messages** (string) - Prints messages indicating successful configuration or connection failure. - **Network configuration** (tuple) - If successful, prints the IP address, subnet mask, gateway, and DNS server obtained via DHCP. #### Response Example ``` Network configuration successful! Automatically obtained configuration: ('192.168.1.100', '255.255.255.0', '192.168.1.1', '192.168.1.1') ``` #### Error Response - **Error message** (string) - Prints "Connection failed, please check network cable connection" if the connection cannot be established. ``` -------------------------------- ### Example: Displaying Text Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/machine/k230_canmv_spi_lcd_module_api A comprehensive example demonstrating how to initialize the SPI_LCD, configure its parameters, and draw text strings with different colors. ```APIDOC ## Example: Displaying Text ### Description This example demonstrates how to initialize the SPI_LCD, configure its parameters, and draw text strings with different colors on the screen. ### Code ```python import time, image from machine import FPIOA, Pin, SPI, SPI_LCD fpioa = FPIOA() # Configure pins for SPI and LCD control fpioa.set_function(19, FPIOA.GPIO19) pin_cs = Pin(19, Pin.OUT, pull=Pin.PULL_NONE, drive=15) pin_cs.value(1) # Set CS high initially fpioa.set_function(20, FPIOA.GPIO20) pin_dc = Pin(20, Pin.OUT, pull=Pin.PULL_NONE, drive=15) pin_dc.value(1) # Set DC high initially fpioa.set_function(44, FPIOA.GPIO44, pu = 1) pin_rst = Pin(44, Pin.OUT, pull=Pin.PULL_UP, drive=15) # Configure SPI interface fpioa.set_function(15, fpioa.QSPI0_CLK) # CLK pin fpioa.set_function(16, fpioa.QSPI0_D0) # MOSI pin spi1 = SPI(1, baudrate=1000*1000*50, polarity=1, phase=1, bits=8) # Initialize SPI_LCD lcd = SPI_LCD(spi1, pin_dc, pin_cs, pin_rst) # Configure LCD parameters lcd.configure(320, 240, hmirror = False, vflip = True, bgr = False) print(lcd) # Initialize the image buffer img = lcd.init() print(img) # Clear the image buffer and draw strings img.clear() img.draw_string_advanced(0,0,32, "RED, 你好世界~", color = (255, 0, 0)) img.draw_string_advanced(0,40,32, "GREEN, 你好世界~", color = (0, 255, 0)) img.draw_string_advanced(0,80,32, "BLUE, 你好世界~", color = (0, 0, 255)) # Show the image on the LCD lcd.show() ``` ### Explanation This example initializes the LCD with specified pins and SPI settings, configures its resolution and orientation, and then draws three lines of text in red, green, and blue respectively. Finally, `lcd.show()` updates the display to render the text. ``` -------------------------------- ### Load KPU Model Example Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/nncase/k230_canmv_nncase_runtime_api An example demonstrating how to load a kmodel file for KPU inference using the nncase_runtime module. It initializes the kpu object and specifies the path to the kmodel file. This is a common starting point for KPU-based applications. ```python import os import ujson import nncase_runtime as nn import ulab.numpy as np import image import sys # 加载kmodel kmodel_path="/sdcard/examples/kmodel/face_detection_320.kmodel" kpu=nn.kpu() kpu.load_kmodel(kmodel_path) ``` -------------------------------- ### Example: Displaying Text on Screen Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/machine/k230_canmv_spi_lcd_module_api This example demonstrates how to initialize the SPI_LCD module, configure its parameters, and display text in different colors on the screen. It involves setting up GPIO pins for SPI communication and then using the LCD's drawing capabilities. ```python import time, image from machine import FPIOA, Pin, SPI, SPI_LCD fpioa = FPIOA() # 使用 gpio 19 接屏幕 cs脚 fpioa.set_function(19, FPIOA.GPIO19) pin_cs = Pin(19, Pin.OUT, pull=Pin.PULL_NONE, drive=15) pin_cs.value(1) # 使用 gpio 20 接屏幕 dc 脚 fpioa.set_function(20, FPIOA.GPIO20) pin_dc = Pin(20, Pin.OUT, pull=Pin.PULL_NONE, drive=15) pin_dc.value(1) # 使用 gpio 44 接屏幕 reset 脚 fpioa.set_function(44, FPIOA.GPIO44, pu = 1) pin_rst = Pin(44, Pin.OUT, pull=Pin.PULL_UP, drive=15) # spi fpioa.set_function(15, fpioa.QSPI0_CLK) # scl fpioa.set_function(16, fpioa.QSPI0_D0) # mosi spi1 = SPI(1,baudrate=1000*1000*50, polarity=1, phase=1, bits=8) lcd = SPI_LCD(spi1, pin_dc, pin_cs, pin_rst) lcd.configure(320, 240, hmirror = False, vflip = True, bgr = False) print(lcd) img = lcd.init() print(img) img.clear() img.draw_string_advanced(0,0,32, "RED, 你好世界~", color = (255, 0, 0)) img.draw_string_advanced(0,40,32, "GREEN, 你好世界~", color = (0, 255, 0)) img.draw_string_advanced(0,80,32, "BLUE, 你好世界~", color = (0, 0, 255)) lcd.show() ``` -------------------------------- ### Example RTSP Server Usage - Python Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/mpp/k230_canmv_rtsp_module_api An example demonstrating the usage of the RTSP server API to send video and audio data over network streams. Note: This example requires an SD card to run. ```python # 示例:演示如何通过 RTSP 服务器向网络流媒体发送视频和音频数据。 # 注意:运行该示例需要 SD 卡。 ``` -------------------------------- ### Example: Play MP4 File using Player Module in Python Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/mpp/k230_canmv_player_module_api Demonstrates the complete process of playing an MP4 file, including initializing the Player, loading the file, setting an event callback, starting playback, and handling playback completion. It supports different display outputs. ```python from media.player import * import os import time start_play = False def player_event(event, data): global start_play if event == K_PLAYER_EVENT_EOF: start_play = False def play_mp4_test(filename): global start_play # 使用 IDE 作为输出显示,支持任意分辨率;适用于 BPI 开发板 player = Player(Display.VIRT) # 使用 ST7701 LCD 屏幕作为输出显示,最大分辨率为 800x480 # player = Player(Display.ST7701) # 使用 HDMI 作为输出显示 # player = Player(Display.LT9611) player.load(filename) player.set_event_callback(player_event) player.start() start_play = True while start_play: time.sleep(0.1) player.stop() print("播放结束") play_mp4_test("/sdcard/examples/test.mp4") ``` -------------------------------- ### Example Program Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/mpp/k230_canmv_vdec_module_api An example program demonstrating the usage of the VDEC module for decoding video files. ```APIDOC ## Example 1: Video Decoding ### Description This example demonstrates how to decode H.264 or H.265 video files using the VDEC module and display the output on a connected screen. ### Usage ```python from media.media import * from mpp.payload_struct import * import media.vdecoder as vdecoder from media.display import * import time import os STREAM_SIZE = 40960 def vdec_test(file_name, width=1280, height=720): print("vdec_test start") vdec_chn = VENC_CHN_ID_0 vdec_width = ALIGN_UP(width, 16) vdec_height = height vdec = None vdec_payload_type = K_PT_H264 # Set display type based on available hardware display_type = Display.ST7701 # Example: ST7701 LCD screen # Determine payload type based on file extension suffix = file_name.split('.')[-1] if suffix == '264': vdec_payload_type = K_PT_H264 elif suffix == '265': vdec_payload_type = K_PT_H265 else: print("Unknown file extension") return # Initialize video decoder vdec = vdecoder.Decoder(vdec_payload_type) # Initialize display device Display.init(display_type, to_ide=True) # Initialize media manager MediaManager.init() # Create video decoder vdec.create() # Bind display layer bind_info = vdec.bind_info(width=vdec_width, height=vdec_height, chn=vdec.get_vdec_channel()) Display.bind_layer(**bind_info, layer=Display.LAYER_VIDEO1) # Start decoding vdec.start() # Read and decode video data with open(file_name, "rb") as fi: while True: os.exitpoint() data = fi.read(STREAM_SIZE) if not data: break vdec.decode(data) # Stop and destroy decoder vdec.stop() vdec.destroy() time.sleep(1) # Deinitialize display and media manager Display.deinit() MediaManager.deinit() print("vdec_test stop") if __name__ == "__main__": os.exitpoint(os.EXITPOINT_ENABLE) vdec_test("/sdcard/examples/test.264", 800, 480) # Example: Decode an H.264 file ``` ``` -------------------------------- ### Image Creation Examples - Python Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/image-recog/img-processing Demonstrates creating image objects using different parameters. Examples include creating an ARGB8888 image in MMZ memory, a YUV420 image in VB memory, and an RGB888 image using an external data reference. ```python # 在 MMZ 区域创建 ARGB8888 格式的 640x480 图像 img = image.Image(640, 480, image.ARGB8888) # 在 VB 区域创建 YUV420 格式的 640x480 图像 img = image.Image(640, 480, image.YUV420, alloc=image.ALLOC_VB, phyaddr=xxx, virtaddr=xxx, poolid=xxx) # 使用外部引用创建 RGB888 格式的 640x480 图像 img = image.Image(640, 480, image.RGB888, alloc=image.ALLOC_REF, data=buffer_obj) ``` -------------------------------- ### SPI Module Initialization and Usage Example in Python Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/machine/k230_canmv_spi_module_api This example demonstrates how to initialize the SPI module in Python, specifying parameters like baud rate, polarity, phase, and data bit width. It also shows basic operations such as writing data, performing a write-read operation, and deinitializing the SPI module. ```python from machine import SPI # Initialize SPI, clock rate 5 MHz, polarity 0, phase 0, data width 8 bits spi = SPI(id, baudrate=5000000, polarity=0, phase=0, bits=8) # Send data to the slave device spi.write(buf) # Send data and read received data into a variable simultaneously spi.write_readinto(write_buf, read_buf) # Close SPI spi.deinit() ``` -------------------------------- ### Advanced Wired Network Example with Error Handling Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/network/eth An enhanced example for wired network configuration, featuring improved error handling, connection checking with timeouts, and formatted output. ```APIDOC ## Advanced Wired Network Configuration with Error Handling ### Description This script provides a more robust approach to configuring the wired network interface. It includes functions for initialization, configuration retrieval, connection checking with a timeout, and formatted printing of network details. It also incorporates exception handling for a more resilient operation. ### Method Python script execution ### Endpoint N/A ### Parameters None ### Request Example ```python import network import time def initialize_network_interface(): """Initialize network interface and return instance""" try: nic = network.LAN() # Create wired network interface instance print("✅ Network interface initialization successful") return nic except Exception as e: print(f"❌ Interface initialization failed: {e}") return None def get_network_config(nic): """Get and return formatted network configuration""" config = nic.ifconfig() # Get configuration info (IP, subnet mask, gateway, DNS) return { "IP Address": config[0], "Subnet Mask": config[1], "Gateway": config[2], "DNS Server": config[3] } def print_network_config(config): """Print network configuration""" print("\n" + "="*40) print("📶 Current Network Configuration:") for key, value in config.items(): print(f"{key:>10}:{value}") print("="*40 + "\n") def check_connection(nic, timeout=10): """Check network connection status with timeout mechanism""" print("🔍 Checking network connection...") start_time = time.time() while time.time() - start_time < timeout: if nic.isconnected(): print("✅ Network connection is normal") return True print("⏳ Waiting for connection...") time.sleep(1) print("❌ Connection timed out") return False def nic_config(): # Initialize network interface nic = initialize_network_interface() if not nic: return try: # Set DHCP mode nic.ifconfig("dhcp") # Set to automatically obtain IP address print("🔄 DHCP automatic configuration enabled") # Check network connection if check_connection(nic): # Get and print configuration information config = get_network_config(nic) print_network_config(config) else: print("⚠️ Please check:\n1. Network cable connection\n2. Router operation\n3. DHCP service enabled") except Exception as e: print(f"❌ An exception occurred: {e}") if __name__ == "__main__": nic_config() ``` ### Response #### Success Response (Output) - **Status messages** (string) - Detailed messages about initialization, DHCP enablement, connection status, and configuration. - **Formatted Network Configuration** (string) - A clearly formatted display of IP address, subnet mask, gateway, and DNS server. #### Response Example ``` ✅ Network interface initialization successful 🔄 DHCP automatic configuration enabled 🔍 Checking network connection... ⏳ Waiting for connection... ✅ Network connection is normal ======================================== 📶 Current Network Configuration: IP Address:192.168.1.150 Subnet Mask:255.255.255.0 Gateway:192.168.1.1 DNS Server:192.168.1.1 ======================================== ``` #### Error Response - **Error messages** (string) - Indicates if initialization failed, connection timed out, or other exceptions occurred, providing guidance for troubleshooting. ``` -------------------------------- ### Video Decoding Test Example (Python) Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/mpp/k230_canmv_vdec_module_api An example demonstrating the full lifecycle of the VDEC module, including initialization, creation, binding, decoding, and cleanup. It reads video data from a file and displays it on a specified screen. Supports H.264 and H.265. ```python from media.media import * from mpp.payload_struct import * import media.vdecoder as vdecoder from media.display import * import time import os STREAM_SIZE = 40960 def vdec_test(file_name, width=1280, height=720): print("vdec_test start") vdec_chn = VENC_CHN_ID_0 vdec_width = ALIGN_UP(width, 16) vdec_height = height vdec = None vdec_payload_type = K_PT_H264 # display_type = Display.VIRT display_type = Display.ST7701 # 使用 ST7701 LCD 屏幕作为输出,最大分辨率 800*480 # display_type = Display.LT9611 # 使用 HDMI 作为输出 # 判断文件类型 suffix = file_name.split('.')[-1] if suffix == '264': vdec_payload_type = K_PT_H264 elif suffix == '265': vdec_payload_type = K_PT_H265 else: print("未知的文件扩展名") return # 实例化视频解码器 vdec = vdecoder.Decoder(vdec_payload_type) # 初始化显示设备 if display_type == Display.VIRT: Display.init(display_type, width=vdec_width, height=vdec_height, fps=30) else: Display.init(display_type, to_ide=True) # 初始化缓冲区 MediaManager.init() # 创建视频解码器 vdec.create() # 绑定显示 bind_info = vdec.bind_info(width=vdec_width, height=vdec_height, chn=vdec.get_vdec_channel()) Display.bind_layer(**bind_info, layer=Display.LAYER_VIDEO1) vdec.start() # 打开文件 with open(file_name, "rb") as fi: while True: os.exitpoint() # 读取视频数据流 data = fi.read(STREAM_SIZE) if not data: break # 解码数据流 vdec.decode(data) # 停止视频解码器 vdec.stop() # 销毁视频解码器 vdec.destroy() time.sleep(1) # 关闭显示 Display.deinit() # 释放缓冲区 MediaManager.deinit() print("vdec_test stop") if __name__ == "__main__": os.exitpoint(os.EXITPOINT_ENABLE) vdec_test("/sdcard/examples/test.264", 800, 480) # 解码 H.264/H.265 视频文件 ``` -------------------------------- ### Encoder.Start Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/mpp/k230_canmv_venc_module_api Starts the encoding process for a specified channel. ```APIDOC ## Encoder.Start ### Description Starts the encoding process. ### Method Python ### Endpoint Encoder.Start(chn) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **chn** (int) - Required - Encoding channel number. ### Request Example ```python Encoder.Start(chn=0) ``` ### Response #### Success Response (0) - **Return Value** (int) - 0 indicates success. #### Response Example ``` 0 ``` ``` -------------------------------- ### Get Line Start Y Coordinate Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/openmv/image_module_api Returns the y-coordinate component of the first vertex (p1) of the line. This value can also be accessed via index [1]. ```python line.y1() ``` -------------------------------- ### Get Line Start X Coordinate Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/openmv/image_module_api Returns the x-coordinate component of the first vertex (p1) of the line. This value can also be accessed via index [0]. ```python line.x1() ``` -------------------------------- ### Example Configuration for Hand Keypoint Detection in Python Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/ai-demo/hand-related/hand-kp-det Sets up initial parameters for the hand keypoint detection system in a main execution block. This includes defining the display mode, input image resolution, and the file paths for the hand detection and hand keypoint estimation Kmodel files. ```python if __name__=="__main__": # 显示模式,默认"hdmi",可以选择"hdmi"和"lcd" display_mode="lcd" # k230保持不变,k230d可调整为[640,360] rgb888p_size = [1920, 1080] if display_mode=="hdmi": display_size=[1920,1080] else: display_size=[800,480] # 手掌检测模型路径 hand_det_kmodel_path="/sdcard/examples/kmodel/hand_det.kmodel" # 手部关键点模型路径 hand_kp_kmodel_path="/sdcard/examples/kmodel/handkp_det.kmodel" # 其它参数 ``` -------------------------------- ### Get BarCode Corners (Python) Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/openmv/image_module_api The corners() method returns a list of tuples representing the four corners of the barcode. Each tuple is in the format (x, y) and is typically ordered clockwise starting from the top-left corner. ```python barcode.corners() ``` -------------------------------- ### Example Usage: Displaying on Different Devices Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/image-recog/display Demonstrates how to import necessary modules and initialize the display for different modes (VIRT, LCD, HDMI) with appropriate resolution settings. ```APIDOC ## Example: Displaying on Different Devices ### Description This example shows how to initialize the display for different output devices: IDE buffer (`VIRT`), a 3.1-inch screen (`LCD`), or an external HDMI screen. ### Code ```python # Import necessary modules import time, os, urandom, sys from media.display import * from media.media import * # Select display mode: can be "VIRT", "LCD", or "HDMI" DISPLAY_MODE = "VIRT" # Set display width and height based on the mode if DISPLAY_MODE == "VIRT": # Virtual display mode DISPLAY_WIDTH = ALIGN_UP(1920, 16) DISPLAY_HEIGHT = 1080 elif DISPLAY_MODE == "LCD": # 3.1-inch screen mode DISPLAY_WIDTH = 800 DISPLAY_HEIGHT = 480 elif DISPLAY_MODE == "HDMI": # HDMI expansion board mode DISPLAY_WIDTH = 1920 DISPLAY_HEIGHT = 1080 else: raise ValueError("Unknown DISPLAY_MODE, please choose 'VIRT', 'LCD', or 'HDMI'") # Further display initialization and usage would follow here... ``` ``` -------------------------------- ### Get AprilTag Corners (Python) Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/openmv/image_module_api The `corners()` method returns a list of tuples, where each tuple represents the (x, y) coordinates of one of the four corners of the AprilTag. The corners are typically ordered starting from the top-left and proceeding clockwise. ```python apriltag.corners() ``` -------------------------------- ### Play WAV File Example (Python) Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/mpp/k230_canmv_audio_module_api This Python script demonstrates how to play a WAV audio file using the pyaudio and wave libraries. It opens a WAV file, initializes an audio stream, reads the file in chunks, writes the data to the stream for playback, and then properly closes the stream and terminates the PyAudio instance. ```python import pyaudio import wave # 播放 WAV 文件 def play_wav(file_path): # 打开 WAV 文件 wf = wave.open(file_path, 'rb') # 创建 PyAudio 对象 p = pyaudio.PyAudio() # 打开流 stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True) # 读取数据并播放 data = wf.readframes(1024) while data: stream.write(data) data = wf.readframes(1024) # 关闭流 stream.stop_stream() stream.close() p.terminate() ``` -------------------------------- ### Get DataMatrix Corners (Python) Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/openmv/image_module_api The `corners()` method returns a list of tuples, where each tuple represents the (x, y) coordinates of one of the four corners of the Data Matrix. The corners are typically ordered starting from the top-left and proceeding clockwise. ```python datamatrix.corners() ``` -------------------------------- ### Get KPU Input Description Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/nncase/k230_canmv_nncase_runtime_api Retrieves descriptive information about a specific input of the KPU model, including its data type, start offset, and size. This is essential for understanding input data requirements. It requires the input index and returns a MemoryRange object. Errors trigger C++ exceptions. ```python get_input_desc(index) ``` -------------------------------- ### Example Usage of Display Module Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/mpp/k230_canmv_display_module_api Demonstrates the usage of the Display module by initializing it with specific parameters, creating and drawing on an image, displaying it, and then deinitializing the module. It includes necessary imports and error handling. ```python from media.display import * from media.media import * import os, time, image # 使用 LCD 作为显示输出 Display.init(Display.ST7701, width=800, height=480, to_ide=True) # 初始化媒体管理器 MediaManager.init() # 创建用于绘图的图像 img = image.Image(800, 480, image.RGB565) img.clear() img.draw_string_advanced(0, 0, 32, "Hello World!,你好世界!!!", color=(255, 0, 0)) Display.show_image(img) try: while True: time.sleep(1) os.exitpoint() except KeyboardInterrupt as e: print(" 用户停止:", e) except BaseException as e: print(f" 异常:{e}") Display.deinit() MediaManager.deinit() ``` -------------------------------- ### Get KPU Output Description Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/nncase/k230_canmv_nncase_runtime_api Retrieves descriptive information about a specific output of the KPU model, including its data type, start offset, and size. This is crucial for correctly interpreting the model's results. It requires the output index and returns a MemoryRange object. Failures raise C++ exceptions. ```python get_output_desc(index) ``` -------------------------------- ### Image Object Creation Examples (Python) Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/openmv/image_module_api Demonstrates creating `Image` objects with different formats and memory allocation strategies, including MMZ, VB, and using external data references. ```python # Create a 640x480 ARGB8888 image in MMZ area img = image.Image(640, 480, image.ARGB8888) # Create a 640x480 YUV420 image in VB area img = image.Image(640, 480, image.YUV420, alloc=image.ALLOC_VB, phyaddr=xxx, virtaddr=xxx, poolid=xxx) # Create a 640x480 RGB888 image using external reference img = image.Image(640, 480, image.RGB888, alloc=image.ALLOC_REF, data=buffer_obj) ``` -------------------------------- ### Initialize Camera and Display in Python Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/image-recog/code-classif This Python code initializes the camera sensor, sets its resolution and pixel format, and then initializes the display based on a configured display mode (VIRT, LCD, or HDMI). It also initializes the MediaManager and starts the sensor. This setup is crucial for capturing and displaying images for AprilTag detection. ```python # 构造一个具有默认配置的摄像头对象 sensor = Sensor(id=sensor_id) # 重置摄像头sensor sensor.reset() # 无需进行镜像翻转 # 设置水平镜像 # sensor.set_hmirror(False) # 设置垂直翻转 # sensor.set_vflip(False) # 设置通道0的输出尺寸 sensor.set_framesize(width=picture_width, height=picture_height, chn=CAM_CHN_ID_0) # 设置通道0的输出像素格式为GRAYSCALE(灰度) sensor.set_pixformat(Sensor.GRAYSCALE, chn=CAM_CHN_ID_0) #sensor.set_pixformat(Sensor.RGB565, chn=CAM_CHN_ID_0) # 根据模式初始化显示器 if DISPLAY_MODE == "VIRT": Display.init(Display.VIRT, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, fps=60) elif DISPLAY_MODE == "LCD": Display.init(Display.ST7701, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, to_ide=True) elif DISPLAY_MODE == "HDMI": Display.init(Display.LT9611, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, to_ide=True) # 初始化媒体管理器 MediaManager.init() # 启动传感器 sensor.run() ``` -------------------------------- ### Face Detection App Initialization and Configuration Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/ai-demo/face-related/face-recognition This snippet shows the initialization of the `FaceDetectionApp` class, which inherits from `AIBase`. It outlines the key parameters required for setting up the face detection model, including model path, input size, anchor data, and various thresholds. It also highlights the importance of 16-byte alignment for image dimensions for memory access efficiency. ```python class FaceDetectionApp(AIBase): def __init__(self, kmodel_path, model_input_size, anchors, confidence_threshold=0.5, nms_threshold=0.2, rgb888p_size=[224,224], display_size=[1920,1080], debug_mode=0): super().__init__(kmodel_path, model_input_size, rgb888p_size, debug_mode) ``` -------------------------------- ### Rust 裸机开发链接脚本 Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/user-contribute/k230-bare-metal-embedded 这是一个 RISC-V 架构的链接脚本 (`link.x`),用于定义裸机 Rust 程序在 K230 上的内存布局。它指定了 `.spl_mem`(固件加载区域)的起始地址和长度,以及 `.bss_mem`(用于未初始化数据)的起始地址和长度。同时,定义了程序的入口点为 `_start`,并设置了堆栈的起始指针。 ```txt MEMORY { .spl_mem : ORIGIN = 0x80300000, LENGTH = 0x80000 } MEMORY { .bss_mem : ORIGIN = 0x80380000, LENGTH = 0x20000 } OUTPUT_ARCH("riscv") ENTRY(_start) PROVIDE(__stack_start__ = ORIGIN(.bss_mem) + LENGTH(.bss_mem)); /* 省略具体 section 定义 */ ``` -------------------------------- ### Python TCP Client Socket Example Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/extmod/k230_canmv_socket_module_api This Python code demonstrates how to configure and use a TCP socket client for sending test messages. It includes steps for getting address information, creating a socket object, connecting to a server, sending data, and closing the connection. It relies on the 'socket' and 'time' modules. ```python import socket import time PORT = 60000 def client(): # 获取 IP 地址及端口号 ai = socket.getaddrinfo("10.100.228.5", PORT) # ai = socket.getaddrinfo("10.10.1.94", PORT) print("地址信息:", ai) addr = ai[0][-1] print("连接地址:", addr) # 创建 socket 对象 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) # 连接到指定地址 s.connect(addr) for i in range(10): msg = "K230 TCP 客户端发送测试 {0} \r\n".format(i) print(msg) # 发送字符串数据 print(s.write(msg)) time.sleep(0.2) # 延时 1 秒后关闭 socket time.sleep(1) s.close() print("结束") # 运行客户端程序 client() ``` -------------------------------- ### YOLOv8 Classification Example Program Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/aidemo/yolo_module_api Example program demonstrating YOLOv8 for image classification. ```APIDOC ## Example Program: Image Classification ### Description This program demonstrates how to use the YOLOv8 API for image classification. ### Code ```python from libs.YOLO import YOLOv8 import os, sys, gc import ulab.numpy as np import image # Function to read image and convert format def read_img(img_path): img_data = image.Image(img_path) img_data_rgb888 = img_data.to_rgb888() img_hwc = img_data_rgb888.to_numpy_ref() shape = img_hwc.shape img_tmp = img_hwc.reshape((shape[0] * shape[1], shape[2])) img_tmp_trans = img_tmp.transpose() img_res = img_tmp_trans.copy() img_return = img_res.reshape((shape[2], shape[0], shape[1])) return img_return, img_data_rgb888 if __name__ == "__main__": # Modify paths and parameters as needed img_path = "/data/test_images/test_apple.jpg" kmodel_path = "/data/yolo_kmodels/cls_yolov8n_224.kmodel" labels = ["apple", "banana", "orange"] confidence_threshold = 0.5 model_input_size = [224, 224] img, img_ori = read_img(img_path) rgb888p_size = [img.shape[2], img.shape[1]] # Initialize YOLOv8 instance for classification yolo = YOLOv8(task_type="classify", mode="image", kmodel_path=kmodel_path, labels=labels, rgb888p_size=rgb888p_size, model_input_size=model_input_size, conf_thresh=confidence_threshold, debug_mode=0) yolo.config_preprocess() try: res = yolo.run(img) yolo.draw_result(res, img_ori) gc.collect() except Exception as e: sys.print_exception(e) finally: yolo.deinit() ``` ``` -------------------------------- ### 生成固件二进制并创建镜像 Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/user-contribute/k230-bare-metal-embedded 此 shell 命令组合首先使用 `cargo objcopy` 将 Rust 项目(假设已配置为 release 模式)编译为原始二进制文件 `firmware.bin`,然后执行 Python 脚本 `genimage.py` 来生成最终的 TF 卡镜像文件 `firmware.img`。这是 K230 固件构建和部署的完整流程。 ```sh cargo objcopy --release -- -O binary firmware.bin && python3 genimage.py ``` -------------------------------- ### Get Camera Image and FPS from CSI1 with Python Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/image-recog/use-sensor This script captures images from a camera connected to the CSI1 interface, displays them using the IDE's frame buffer, and prints the current frames per second (FPS). It is similar to the CSI0 example but uses `sensor_id = 1`. It requires the `media.sensor`, `media.display`, and `media.media` libraries and initializes the sensor, sets frame parameters, and runs a continuous loop for image capture and display. ```python import time, os, sys import utime from media.sensor import * from media.display import * from media.media import * #用CSI1接口的摄像头 sensor_id = 1 sensor = None try: # 构造一个具有默认配置的摄像头对象 sensor = Sensor(id=sensor_id) # 重置摄像头sensor sensor.reset() # 无需进行镜像翻转 # 设置水平镜像 # sensor.set_hmirror(False) # 设置垂直翻转 # sensor.set_vflip(False) # 设置通道0的输出尺寸为1920x1080 sensor.set_framesize(Sensor.FHD, chn=CAM_CHN_ID_0) # 设置通道0的输出像素格式为RGB888 sensor.set_pixformat(Sensor.RGB888, chn=CAM_CHN_ID_0) # 使用IDE的帧缓冲区作为显示输出 Display.init(Display.VIRT, width=1920, height=1080, to_ide=True) # 初始化媒体管理器 MediaManager.init() # 启动传感器 sensor.run() #构造clock clock = utime.clock() while True: os.exitpoint() #更新当前时间(毫秒) clock.tick() # 捕获通道0的图像 img = sensor.snapshot(chn=CAM_CHN_ID_0) # 显示捕获的图像 Display.show_image(img) #打印当前fps print("fps = ", clock.fps()) except KeyboardInterrupt as e: print("用户停止: ", e) except BaseException as e: print(f"异常: {e}") finally: # 停止传感器运行 if isinstance(sensor, Sensor): sensor.stop() # 反初始化显示模块 Display.deinit() os.exitpoint(os.EXITPOINT_ENABLE_SLEEP) time.sleep_ms(100) # 释放媒体缓冲区 MediaManager.deinit() ``` -------------------------------- ### FPIOA Module API Usage Example Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/machine/k230_canmv_fpioa_module_api This Python snippet demonstrates how to instantiate the FPIOA object and use its methods to display help information, set pin functions, and retrieve pin configurations. It covers basic usage for setting a pin to GPIO and also shows how to configure additional parameters. ```python from machine import FPIOA # Instantiate FPIOA object fpioa = FPIOA() # Print configuration of all pins fpioa.help() # Print detailed configuration of a specific pin fpioa.help(0) # Print all available configuration pins for a specific function fpioa.help(FPIOA.IIC0_SDA, func=True) # Set Pin0 to GPIO0 fpioa.set_function(0, FPIOA.GPIO0) # Set Pin2 to GPIO2, also configuring other parameters fpioa.set_function(2, FPIOA.GPIO2, ie=1, oe=1, pu=0, pd=0, st=1, ds=7) # Get the pin number currently used by a specific function fpioa.get_pin_num(FPIOA.UART0_TXD) # Get the current function of a specific pin fpioa.get_pin_func(0) ``` -------------------------------- ### YOLOv8 Video Inference Example Program Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/api/aidemo/yolo_module_api Example program demonstrating YOLOv8 for video inference. ```APIDOC ## Example Program: Video Inference ### Description This program demonstrates how to use the YOLOv8 API for real-time video inference. ### Code ```python from libs.PipeLine import PipeLine, ScopedTiming from libs.YOLO import YOLOv8 import os, sys, gc import ulab.numpy as np import image if __name__ == "__main__": # Display mode: "hdmi" or "lcd" display_mode = "hdmi" rgb888p_size = [1280, 720] if display_mode == "hdmi": display_size = [1920, 1080] else: display_size = [800, 480] # Modify paths and parameters as needed kmodel_path = "/data/yolo_kmodels/cls_yolov8n_224.kmodel" labels = ["apple", "banana", "orange"] confidence_threshold = 0.8 model_input_size = [224, 224] # Initialize PipeLine pl = PipeLine(rgb888p_size=rgb888p_size, display_size=display_size, display_mode=display_mode) pl.create() # Initialize YOLOv8 instance for video processing yolo = YOLOv8(task_type="classify", mode="video", kmodel_path=kmodel_path, labels=labels, rgb888p_size=rgb888p_size, model_input_size=model_input_size, display_size=display_size, conf_thresh=confidence_threshold, debug_mode=0) yolo.config_preprocess() try: while True: os.exitpoint() with ScopedTiming("total", 1): # Process frame by frame img = pl.get_frame() res = yolo.run(img) yolo.draw_result(res, pl.osd_img) pl.show_image() gc.collect() except Exception as e: sys.print_exception(e) finally: yolo.deinit() pl.destroy() ``` ``` -------------------------------- ### Python Example Usage of FaceDetectionApp Source: https://wiki.lckfb.com/zh-hans/lushan-pi-k230/ai-demo/face-related/face-recognition Demonstrates how to initialize the PipeLine and FaceDetectionApp, configure preprocessing, and enter a loop for continuous face detection. It sets up display modes, model paths, and necessary parameters like anchors. ```python if __name__ == "__main__": # 显示模式,默认"hdmi",可以选择"hdmi"和"lcd" display_mode="lcd" # k230保持不变,k230d可调整为[640,360] rgb888p_size = [1920, 1080] if display_mode=="hdmi": display_size=[1920,1080] else: display_size=[800,480] # 设置模型路径和其他参数 kmodel_path = "/sdcard/examples/kmodel/face_detection_320.kmodel" # 其它参数 confidence_threshold = 0.5 nms_threshold = 0.2 anchor_len = 4200 det_dim = 4 anchors_path = "/sdcard/examples/utils/prior_data_320.bin" anchors = np.fromfile(anchors_path, dtype=np.float) anchors = anchors.reshape((anchor_len, det_dim)) # 初始化PipeLine,用于图像处理流程 pl = PipeLine(rgb888p_size=rgb888p_size, display_size=display_size, display_mode=display_mode) pl.create() # 创建PipeLine实例 # 初始化自定义人脸检测实例 face_det = FaceDetectionApp(kmodel_path, model_input_size=[320, 320], anchors=anchors, confidence_threshold=confidence_threshold, nms_threshold=nms_threshold, rgb888p_size=rgb888p_size, display_size=display_size, debug_mode=0) face_det.config_preprocess() # 配置预处理 try: while True: os.exitpoint() # 检查是否有退出信号 ```