### Example: Displaying Text Setup Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/machine/K230_CanMV_SPI_LCD_Module_API.md This code snippet demonstrates the initial setup for displaying text on the screen, including pin configuration for SPI communication and control signals. ```python import time, image from machine import FPIOA, Pin, SPI, SPI_LCD fpioa = FPIOA() fpioa.set_function(19, FPIOA.GPIO19) pin_cs = Pin(19, Pin.OUT, pull=Pin.PULL_NONE, drive=15) pin_cs.value(1) fpioa.set_function(20, FPIOA.GPIO20) pin_dc = Pin(20, Pin.OUT, pull=Pin.PULL_NONE, drive=15) pin_dc.value(1) fpioa.set_function(44, FPIOA.GPIO44, pu=1) pin_rst = Pin(44, Pin.OUT, pull=Pin.PULL_UP, drive=15) ``` -------------------------------- ### Face Detection Example Program Setup Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/aidemo/AIBase_Module_API_Manual.md Imports necessary libraries for AI Demo application development, including AIBase, Ai2d, and media-related modules. This forms the basis for creating specific AI task implementations. ```python from libs.PipeLine import PipeLine, ScopedTiming from libs.AIBase import AIBase from libs.AI2D import Ai2d import os, sys, gc, time, random, utime import ujson from media.media import * from time import * import nncase_runtime as nn import ulab.numpy as np import image import aidemo ``` -------------------------------- ### TCP Client Example Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/extmod/K230_CanMV_socket_API_Manual.md This example demonstrates how to configure and run a TCP client using the socket library. It includes steps for getting address information, creating a socket, connecting to a server, sending messages, and closing the connection. ```python # Configure TCP/UDP socket debugging tool import socket import time PORT = 60000 def client(): # Get IP address and port number ai = socket.getaddrinfo("10.100.228.5", PORT) # ai = socket.getaddrinfo("10.10.1.94", PORT) print("Address info:", ai) addr = ai[0][-1] print("Connecting to address:", addr) # Create socket object s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) # Connect to the specified address s.connect(addr) for i in range(10): msg = "K230 TCP client test message {0} \r\n".format(i) print(msg) # Send string data print(s.write(msg)) time.sleep(0.2) # Close the socket after a 1-second delay time.sleep(1) s.close() print("End") # Run the client program client() ``` -------------------------------- ### PM Module Usage Example Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/mpp/K230_CanMV_PM_Module_API_Manual.md Demonstrates how to interact with the PM module to manage CPU power settings, including getting current frequency, listing supported profiles, and setting a specific profile. ```APIDOC ## PM Module Usage Example ### Description This example shows how to use the `mpp.pm` object to control CPU power management features. ### Code ```python from mpp import pm # Get the current CPU frequency current_freq = pm.cpu.get_freq() # Get the list of supported CPU frequency profiles supported_freqs = pm.cpu.list_profiles() # Set the CPU frequency to a specified profile (e.g., profile with index 1) pm.cpu.set_profile(1) ``` ``` -------------------------------- ### Initialize and Start RTSP Server Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/mpp/K230_CanMV_RTSP_Module_API_Manual.md This snippet demonstrates how to initialize and start the RTSP server. It configures the server with a session name, port, video type, and audio enablement. The server is then started, and a streaming thread is initiated. ```python from media.vencoder import * from media.sensor import * from media.media import * import time, os import _thread import multimedia as mm from time import * class RtspServer: def __init__(self, session_name="test", port=8554, video_type=mm.multi_media_type.media_h264, enable_audio=False): self.session_name = session_name # Session name self.video_type = video_type # Video encoding type (H264/H265) self.enable_audio = enable_audio # Whether to enable audio self.port = port # RTSP server port number self.rtspserver = mm.rtsp_server() # Instantiate RTSP server self.venc_chn = VENC_CHN_ID_0 # Video encoding channel self.start_stream = False # Whether to start streaming thread self.runthread_over = False # Whether streaming thread has ended def start(self): # Initialize streaming self._init_stream() self.rtspserver.rtspserver_init(self.port) # Create session self.rtspserver.rtspserver_createsession(self.session_name, self.video_type, self.enable_audio) # Start RTSP server self.rtspserver.rtspserver_start() self._start_stream() # Start streaming thread self.start_stream = True _thread.start_new_thread(self._do_rtsp_stream, ()) def stop(self): if not self.start_stream: return # Wait for streaming thread to exit self.start_stream = False while not self.runthread_over: sleep(0.1) self.runthread_over = False # Stop streaming self._stop_stream() self.rtspserver.rtspserver_stop() self.rtspserver.rtspserver_deinit() def get_rtsp_url(self): return self.rtspserver.rtspserver_getrtspurl(self.session_name) def _init_stream(self): width = 1280 height = 720 width = ALIGN_UP(width, 16) # Initialize sensor self.sensor = Sensor() self.sensor.reset() self.sensor.set_framesize(width=width, height=height, alignment=12) self.sensor.set_pixformat(Sensor.YUV420SP) # Instantiate video encoder self.encoder = Encoder() self.encoder.SetOutBufs(self.venc_chn, 8, width, height) # Bind camera and venc self.link = MediaManager.link(self.sensor.bind_info()['src'], (VIDEO_ENCODE_MOD_ID, VENC_DEV_ID, self.venc_chn)) # Initialize media manager MediaManager.init() # Create encoder chnAttr = ChnAttrStr(self.encoder.PAYLOAD_TYPE_H264, self.encoder.H264_PROFILE_MAIN, width, height) self.encoder.Create(self.venc_chn, chnAttr) def _start_stream(self): # Start encoding self.encoder.Start(self.venc_chn) # Start camera self.sensor.run() def _stop_stream(self): # Stop camera self.sensor.stop() # Unbind camera and venc del self.link # Stop encoding self.encoder.Stop(self.venc_chn) self.encoder.Destroy(self.venc_chn) # Clean buffer MediaManager.deinit() def _do_rtsp_stream(self): try: streamData = StreamData() while self.start_stream: os.exitpoint() # Get a frame of stream self.encoder.GetStream(self.venc_chn, streamData) # Streaming for pack_idx in range(0, streamData.pack_cnt): stream_data = bytes(uctypes.bytearray_at(streamData.data[pack_idx], streamData.data_size[pack_idx])) self.rtspserver.rtspserver_sendvideodata(self.session_name, stream_data, streamData.data_size[pack_idx], 1000) #print("stream size: ", streamData.data_size[pack_idx], "stream type: ", streamData.stream_type[pack_idx]) # Release a frame of stream self.encoder.ReleaseStream(self.venc_chn, streamData) except BaseException as e: print(f"Exception {e}") finally: self.runthread_over = True # Stop rtsp server self.stop() self.runthread_over = True if __name__ == "__main__": os.exitpoint(os.EXITPOINT_ENABLE) # Create rtsp server object rtspserver = RtspServer() # Start rtsp server rtspserver.start() # Print rtsp url print("rtsp server start:", rtspserver.get_rtsp_url()) # Stream for 60s sleep(60) # Stop rtsp server rtspserver.stop() print("done") ``` -------------------------------- ### Best Practice Example: Initialize CSC and Display Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/mpp/nonai2d_csc.md Demonstrates initializing a hardware Color Space Converter and setting up the LCD for display output. This example includes necessary imports and display configurations. ```python import time, os, urandom, sys from media.display import * from media.media import * from media.uvc import * from nonai2d import CSC DISPLAY_WIDTH = ALIGN_UP(800, 16) DISPLAY_HEIGHT = 480 # hardware Color Space Converter csc = CSC(0, CSC.PIXEL_FORMAT_RGB_565) # use lcd as display output Display.init(Display.ST7701, width = DISPLAY_WIDTH, height = DISPLAY_HEIGHT, to_ide = True) ``` -------------------------------- ### Main Execution Block Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/media/audio.md Entry point for the audio example script. It initializes the exit point, prints a start message, calls the audio_recorder function to record and play a 15-second audio file, and prints a completion message. ```python if __name__ == "__main__": os.exitpoint(os.EXITPOINT_ENABLE) print("Audio example starts") # record_audio('/sdcard/examples/test.wav', 15) # Record WAV file # play_audio('/sdcard/examples/test.wav') # Play WAV file # loop_audio(15) # Capture and output audio audio_recorder('/sdcard/examples/test.wav', 15) # Record a 15-second audio file, save it and play it print("Audio example completed") ``` -------------------------------- ### Install Repo Tool (Shell) Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/userguide/how_to_build.md Installs the 'repo' tool, which is used to manage multiple Git repositories for the K230 SDK. It downloads the tool and adds it to the user's PATH. ```shell mkdir -p ~/.bin curl https://storage.googleapis.com/git-repo-downloads/repo > ~/.bin/repo chmod a+rx ~/.bin/repo echo 'export PATH="${HOME}/.bin:${PATH}"' >> ~/.bashrc source ~/.bashrc ``` -------------------------------- ### Complete HTTP Server Example Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/network/http_server.md This example sets up a Wi-Fi connection, creates a socket, binds it to a port, and listens for incoming HTTP requests. It serves a simple "Hello" message and increments a counter for each request. The server will exit after the first request. ```python import socket import network import time, os CONTENT = b""" HTTP/1.0 200 OK Hello #%d from k230 canmv MicroPython! """ def network_use_wlan(is_wlan=True): if is_wlan: sta = network.WLAN(0) sta.connect("TEST", "12345678") print(sta.status()) while sta.ifconfig()[0] == '0.0.0.0': os.exitpoint() print(sta.ifconfig()) ip = sta.ifconfig()[0] return ip else: a = network.LAN() if not a.active(): raise RuntimeError("LAN interface is not active.") a.ifconfig("dhcp") print(a.ifconfig()) ip = a.ifconfig()[0] return ip def main(micropython_optimize=True): ip = network_use_wlan(True) s = socket.socket() ai = socket.getaddrinfo("0.0.0.0", 8081) addr = ai[0][-1] s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(addr) s.listen(5) print("Listening, connect your browser to http://%s:8081/" % (ip)) counter = 0 while True: res = s.accept() client_sock = res[0] client_addr = res[1] print("Client address:", client_addr) client_sock.setblocking(True) client_stream = client_sock if micropython_optimize else client_sock.makefile("rwb") while True: h = client_stream.read() if h is None: continue print(h) if h.endswith(b'\r\n\r\n'): break os.exitpoint() client_stream.write(CONTENT % counter) client_stream.close() counter += 1 time.sleep(2) if counter > 0: print("http server exit!") s.close() break main() ``` -------------------------------- ### Example Program: RTSP Streaming Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/mpp/K230_CanMV_RTSP_Module_API_Manual.md This example demonstrates sending video and audio data over RTSP. It requires an SD card to run. ```python # Example: Demonstrates how to send video and audio data to network streaming via RTSP server. # Note: Running this example requires an SD card. ``` -------------------------------- ### Image Creation Examples Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/openmv/image.md Examples demonstrating the creation of image objects with different formats and memory allocation strategies. ```python # Create a 640x480 image in ARGB8888 format in the MMZ area img = image.Image(640, 480, image.ARGB8888) # Create a 640x480 image in YUV420 format in the VB area img = image.Image(640, 480, image.YUV420, alloc=image.ALLOC_VB, phyaddr=xxx, virtaddr=xxx, poolid=xxx) # Create a 640x480 image in RGB888 format using external reference img = image.Image(640, 480, image.RGB888, alloc=image.ALLOC_REF, data=buffer_obj) ``` -------------------------------- ### Install Model Conversion Libraries (Windows) Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/ai/YOLO_Battle.md Install necessary libraries for model conversion on Windows. This includes installing dotnet-7, nncase via pip, and nncase-kpu offline using a downloaded .whl file. ```shell # Windows platform: Please install dotnet - 7 and add it to the environment variables. nncase can be installed online using pip, but the nncase - kpu library needs to be installed offline. Download nncase_kpu-2.*-py2.py3-none-win_amd64.whl from https://github.com/kendryte/nncase/releases. # Enter the corresponding Python environment and use pip to install in the download directory of nncase_kpu-2.*-py2.py3-none-win_amd64.whl pip install nncase_kpu-2.*-py2.py3-none-win_amd64.whl ``` -------------------------------- ### Instantiate and Read TOUCH Device Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/machine/K230_CanMV_TOUCH_API_Manual.md Instantiate a TOUCH device and read its data. This example shows basic usage for getting touch point coordinates and event information. ```python from machine import TOUCH # Instantiate TOUCH device 0 tp = TOUCH(0) # Get TOUCH data p = tp.read() print(p) # Print touch point coordinates # print(p[0].x) # print(p[0].y) # print(p[0].event) ``` -------------------------------- ### Install NNCase and Dependencies for Model Conversion (Windows) Source: https://github.com/kendryte/k230_canmv_docs/blob/main/zh/example/ai/YOLO大作战.md Install necessary libraries for model conversion on Windows. Requires .NET 7 SDK and offline installation of nncase-kpu. ```shell # Enter the corresponding python environment, and use pip to install in the nncase_kpu-2.*-py2.py3-none-win_amd64.whl download directory pip install nncase_kpu-2.*-py2.py3-none-win_amd64.whl ``` -------------------------------- ### Example Output: Multi-Touch Points Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/peripheral/touch.md Example output format for multiple touch points, where each TOUCH_INFO object represents a distinct touch. ```text (, ) ``` -------------------------------- ### Install Libraries for Model Conversion Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/ai/YOLO_Battle.md Install necessary libraries in the training environment to convert the trained model to a kmodel format. ```shell ``` -------------------------------- ### G711 Encoding/Decoding Example Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/media/audio.md This example requires an SD card to run. It showcases the G711 encoding and decoding capabilities. ```python # G711 Encoding/Decoding Example # # Note: An SD card is required to run this example. # ``` -------------------------------- ### Start RTSP Server Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/mpp/K230_CanMV_RTSP_Module_API_Manual.md Starts the RTSP server, making it ready to accept client connections and stream data. ```python rtspserver_start() ``` -------------------------------- ### multimedia.rtspserver_start Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/mpp/K230_CanMV_RTSP_Module_API_Manual.md Starts the RTSP server, enabling streaming. ```APIDOC ## multimedia.rtspserver_start ### Description Start the RTSP server. ### Syntax ```python rtspserver_start() ``` ### Parameters None ### Return Value None ### Example ```python rtspserver_start() ``` ``` -------------------------------- ### Install Model Conversion Libraries (Linux) Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/ai/YOLO_Battle.md Install necessary libraries for model conversion on Linux, including dotnet-sdk-7.0, nncase, and nncase-kpu. Ensure pip is upgraded. ```shell # Linux platform: nncase and nncase - kpu can be installed online, and dotnet - 7 needs to be installed for nncase - 2.x sudo apt-get install -y dotnet-sdk-7.0 pip install --upgrade pip pip install nncase==2.9.0 pip install nncase-kpu==2.9.0 ``` -------------------------------- ### Player.start Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/mpp/K230_CanMV_Player_Module_API_Manual.md Starts playing audio and video content. ```APIDOC ## Player.start ### Description Starts playing audio and video content. ### Syntax ```python player = Player() player.start() ``` ### Parameters None ### Return Value - **None** () - ``` -------------------------------- ### Configure and Start Access Point (AP) Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/network/wlan.md Configures and starts a wireless access point. Sets the SSID and password for the AP. Use `ap.info()` to view AP details and `ap.status()` to check its operational state. ```python import network def ap_test(): ap=network.WLAN(network.AP_IF) # Configure and create an ap ap.config(ssid='k230_ap_wjx', key='12345678') # View ap information print(ap.info()) # Check the status of the ap print(ap.status()) ap_test() ``` -------------------------------- ### Structure Description Examples Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/extmod/K230_CanMV_uctypes_API_Manual.md Examples demonstrating how to use the uctypes module to parse data structures, access memory with pointers, and interact with hardware registers. ```APIDOC ## Structure Description ### Examples ```python import uctypes # Example 1: Parsing part of an ELF file header ELF_HEADER = { "EI_MAG": (0x0 | uctypes.ARRAY, 4 | uctypes.UINT8), "EI_DATA": 0x5 | uctypes.UINT8, "e_machine": 0x12 | uctypes.UINT16, } # Assuming "f" is an ELF file opened in binary mode buf = f.read(uctypes.sizeof(ELF_HEADER, uctypes.LITTLE_ENDIAN)) header = uctypes.struct(uctypes.addressof(buf), ELF_HEADER, uctypes.LITTLE_ENDIAN) assert header.EI_MAG == b"\x7fELF" assert header.EI_DATA == 1, "Oops, endianness error. Try using uctypes.BIG_ENDIAN." print("machine:", hex(header.e_machine)) # Example 2: Data structure in memory (with pointers) COORD = { "x": 0 | uctypes.FLOAT32, "y": 4 | uctypes.FLOAT32, } STRUCT1 = { "data1": 0 | uctypes.UINT8, "data2": 4 | uctypes.UINT32, "ptr": (8 | uctypes.PTR, COORD), } # Assuming you have a structure at address "addr" struct1 = uctypes.struct(addr, STRUCT1, uctypes.NATIVE) print("x:", struct1.ptr[0].x) # Example 3: Accessing CPU registers, STM32F4xx WWDG registers WWDG_LAYOUT = { "WWDG_CR": (0, { "WDGA": 7 << uctypes.BF_POS | 1 << uctypes.BF_LEN | uctypes.BFUINT32, "T": 0 << uctypes.BF_POS | 7 << uctypes.BF_LEN | uctypes.BFUINT32, }), "WWDG_CFR": (4, { "EWI": 9 << uctypes.BF_POS | 1 << uctypes.BF_LEN | uctypes.BFUINT32, "WDGTB": 7 << uctypes.BF_POS | 2 << uctypes.BF_LEN | uctypes.BFUINT32, "W": 0 << uctypes.BF_POS | 7 << uctypes.BF_LEN | uctypes.BFUINT32, }), } WWDG = uctypes.struct(0x40002c00, WWDG_LAYOUT) WWDG.WWDG_CFR.WDGTB = 0b10 WWDG.WWDG_CR.WDGA = 1 print("Current counter:", WWDG.WWDG_CR.T) ``` ``` -------------------------------- ### AprilTags Detection Example Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/omv/apriltages.md This example demonstrates how to set up the camera to capture grayscale images and use `image.find_apriltags` to recognize AprilTags. It includes configuration for sensor, display, and media manager, along with a loop for continuous detection and drawing. ```python # AprilTags Example # This example demonstrates the powerful capabilities of CanMV in detecting April Tags. import time import math import os import gc import sys from media.sensor import * from media.display import * from media.media import * # Define the width and height of the detection image DETECT_WIDTH = 320 DETECT_HEIGHT = 240 # Define available tag families tag_FAMILIES = 0 tag_FAMILIES |= image.TAG16H5 # 4x4 square tag tag_FAMILIES |= image.TAG25H7 # 5x7 square tag tag_FAMILIES |= image.TAG25H9 # 5x9 square tag tag_FAMILIES |= image.TAG36H10 # 6x10 square tag tag_FAMILIES |= image.TAG36H11 # 6x11 square tag (default) TAG_FAMILIES |= image.ARTOOLKIT # ARToolKit tag # Function: Get the name of the tag family def family_name(tag): family_dict = { image.TAG16H5: "TAG16H5", image.TAG25H7: "TAG25H7", image.TAG25H9: "TAG25H9", image.TAG36H10: "TAG36H10", image.TAG36H11: "TAG36H11", image.ARTOOLKIT: "ARTOOLKIT", } return family_dict.get(tag.family(), "Unknown Tag Family") sensor = None try: # Construct Sensor object with default configuration sensor = Sensor(width=DETECT_WIDTH, height=DETECT_HEIGHT) # Reset sensor sensor.reset() # Set output size and format sensor.set_framesize(width=DETECT_WIDTH, height=DETECT_HEIGHT) sensor.set_pixformat(Sensor.GRAYSCALE) # Initialize display Display.init(Display.VIRT, width=DETECT_WIDTH, height=DETECT_HEIGHT, fps=100) # Initialize media manager MediaManager.init() # Start sensor sensor.run() fps = time.clock() while True: fps.tick() # Check if should exit os.exitpoint() img = sensor.snapshot() for tag in img.find_apriltags(families=TAG_FAMILIES): # Draw rectangle and center cross for recognized tags img.draw_rectangle([v for v in tag.rect()], color=(255, 0, 0)) img.draw_cross(tag.cx(), tag.cy(), color=(0, 255, 0)) print_args = (family_name(tag), tag.id(), (180 * tag.rotation()) / math.pi) print("Tag Family %s, Tag ID %d, Rotation %f (degrees)" % print_args) # Draw results on screen Display.show_image(img) gc.collect() except KeyboardInterrupt: print("User stopped") except BaseException as e: print(f"Exception '{e}'") finally: # Stop sensor if isinstance(sensor, Sensor): sensor.stop() # Destroy display Display.deinit() os.exitpoint(os.EXITPOINT_ENABLE_SLEEP) time.sleep_ms(100) # Release media buffer MediaManager.deinit() ``` -------------------------------- ### Get Line Start Y-Coordinate Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/openmv/image.md Returns the y-coordinate of the first vertex (p1) of the line segment. ```python line.y1() ``` -------------------------------- ### Get Line Start X-Coordinate Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/openmv/image.md Returns the x-coordinate of the first vertex (p1) of the line segment. ```python line.x1() ``` -------------------------------- ### Ai2d Instance Setup Pseudocode Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/ai/AI_Demo_User_Manual.md Provides pseudocode for initializing and using multiple Ai2d instances. This setup requires importing various libraries for pipeline management, AI base classes, utilities, media, and runtime operations. ```python from libs.PipeLine import PipeLine from libs.AIBase import AIBase from libs.AI2D import Ai2d from libs.Utils import * import os from media.media import * import nncase_runtime as nn import ulab.numpy as np import image import gc import sys ``` -------------------------------- ### Media Buffer Configuration and Management Example Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/mpp/K230_CanMV_Media_Module_API_Manual.md Demonstrates how to configure media buffers, initialize the MediaManager, acquire a buffer, and deinitialize the manager. Ensure buffer configuration precedes MediaManager initialization. ```python from media.media import * config = k_vb_config() config.max_pool_cnt = 1 config.comm_pool[0].blk_size = 1024 config.comm_pool[0].blk_cnt = 1 config.comm_pool[0].mode = VB_REMAP_MODE_NOCACHE ret = MediaManager._config(config) if not ret: raise RuntimeError("Buffer configuration failed.") MediaManager.init() buffer = MediaManager.Buffer.get(1024) print(buffer) buffer.__del__() MediaManager.deinit() ``` -------------------------------- ### Get Output Description Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/nncase/K230_CanMV_nncase_runtime_API_Manual.md Fetches the description of an output tensor at a specified index. Returns MemoryRange object containing dtype, start, and size. ```python get_output_desc(index) ``` -------------------------------- ### Main Execution Flow for LVGL Example Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/media/lvgl.md Sets up the exit point, initializes display and LVGL, runs the user GUI, and enters a loop to handle LVGL tasks. Includes error handling and deinitialization. ```python def main(): os.exitpoint(os.EXITPOINT_ENABLE) try: display_init() lvgl_init() user_gui_init() while True: time.sleep_ms(lv.task_handler()) except BaseException as e: print(f"Exception {e}") lvgl_deinit() display_deinit() gc.collect() if __name__ == "__main__": main() ``` -------------------------------- ### Decoder.start Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/mpp/K230_CanMV_VDEC_Module_API_Manual.md Starts the decoder and initiates the video decoding process. ```APIDOC ## Decoder.start ### Description Starts the decoder and begins the decoding process. ### Syntax ```python Decoder.start() ``` ### Parameters None ### Return Values #### Success Response (200) - **None** (None) - Indicates the decoder has started, no specific return value. ``` -------------------------------- ### Get Input Description Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/nncase/K230_CanMV_nncase_runtime_API_Manual.md Fetches the description of an input tensor at a specified index. Returns MemoryRange object containing dtype, start, and size. ```python get_input_desc(index) ``` -------------------------------- ### Get Rectangle Corners Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/openmv/image.md Returns a list of tuples, where each tuple represents the (x, y) coordinates of the four corners of the rectangle. The corners are typically ordered clockwise starting from the top-left. ```python rect.corners() ``` -------------------------------- ### Initialize and Use Media Manager, UVC, and CSC Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/mpp/nonai2d_csc.md This snippet shows the complete process of initializing the media manager, probing for a USB camera, selecting a video mode, capturing frames, converting them using CSC, displaying them, and finally deinitializing the components. ```python import time import csc # init media manager MediaManager.init() while True: plugin, dev = UVC.probe() if plugin: print(f"detect USB Camera {dev}") break time.sleep_ms(100) mode = UVC.video_mode(640, 480, UVC.FORMAT_MJPEG, 30) succ, mode = UVC.select_video_mode(mode) print(f"select mode success: {succ}, mode: {mode}") UVC.start(cvt = True) clock = time.clock() while True: clock.tick() img = UVC.snapshot() if img is not None: img = csc.convert(img) Display.show_image(img) print(clock.fps()) # deinit display Display.deinit() csc.destroy() UVC.stop() time.sleep_ms(100) # release media buffer MediaManager.deinit() ``` -------------------------------- ### QR Code Recognition Example Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/omv/qrcodes.md This example demonstrates how to set up the camera for grayscale image output and use `image.find_qrcodes` to recognize QR codes. It includes initialization for sensor, display, and media manager, along with a loop to capture, process, and display images. Ensure the display initialization matches your connected screen. ```python # QR Code Example import time import os import gc import sys from media.sensor import * from media.display import * from media.media import * DETECT_WIDTH = 640 DETECT_HEIGHT = 480 sensor = None try: # Construct Sensor object with default configuration sensor = Sensor(width=DETECT_WIDTH, height=DETECT_HEIGHT) # Reset sensor sensor.reset() # Set horizontal mirror # sensor.set_hmirror(False) # Set vertical flip # sensor.set_vflip(False) # Set output size sensor.set_framesize(width=DETECT_WIDTH, height=DETECT_HEIGHT) # Set output format sensor.set_pixformat(Sensor.GRAYSCALE) # Initialize display. If the selected screen cannot be lit, refer to the K230_CanMV_Display module API manual in the API documentation for configuration # Use HDMI output, set to VGA # Display.init(Display.LT9611, width=640, height=480, to_ide=True) # Use HDMI output, set to 1080P # Display.init(Display.LT9611, width=1920, height=1080, to_ide=True) # Use LCD output # Display.init(Display.ST7701, to_ide=True) # Use IDE output Display.init(Display.VIRT, width=DETECT_WIDTH, height=DETECT_HEIGHT, fps=100) # Initialize media manager MediaManager.init() # Start sensor sensor.run() fps = time.clock() while True: fps.tick() # Check if should exit os.exitpoint() img = sensor.snapshot() for code in img.find_qrcodes(): rect = code.rect() img.draw_rectangle([v for v in rect], color=(255, 0, 0), thickness=5) img.draw_string_advanced(rect[0], rect[1], 32, code.payload()) print(code) # Draw the result on the screen Display.show_image(img) gc.collect() # print(fps.fps()) except KeyboardInterrupt as e: print(f"user stop") except BaseException as e: print(f"Exception '{e}'") finally: # Stop sensor if isinstance(sensor, Sensor): sensor.stop() # Deinitialize display Display.deinit() os.exitpoint(os.EXITPOINT_ENABLE_SLEEP) time.sleep_ms(100) # Release media buffer MediaManager.deinit() ``` -------------------------------- ### Get QRCode Corners Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/openmv/image.md Returns a list of tuples, where each tuple represents the (x, y) coordinates of the four corners of the QR code. The corners are typically ordered clockwise starting from the top-left. ```python qrcode.corners() ``` -------------------------------- ### Keyword Spotting Imports Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/ai/AI_Demo_User_Manual.md Imports required for the keyword spotting demo, including audio, media, AI runtime, and utility modules. This setup is specific to the 'XiaonanXiaonan' keyword spotting example. ```python from libs.Utils import ScopedTiming from libs.AIBase import AIBase from libs.AI2D import Ai2d from media.pyaudio import * # Audio module from media.media import * # Software abstraction module, mainly encapsulates media data link and media buffer import media.wave as wave # wav audio processing module import nncase_runtime as nn # nncase runtime module, encapsulates kpu (kmodel inference) and ai2d (image pre-processing acceleration) operations import ulab.numpy as np # Similar to python numpy operations, but with some different interfaces import aidemo # aidemo module, encapsulates ai demo related pre-processing, post-processing, etc. import time # Time statistics import struct # Byte character conversion module import gc # Garbage collection module import os,sys # Operating system interface module ``` -------------------------------- ### Main Execution Block with Configuration Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/quick_start.md Sets up display mode, model path, and other parameters for face detection. It loads anchor data from a binary file. ```python if __name__ == "__main__": # Display mode, default "hdmi", can choose "hdmi" or "lcd" display_mode="hdmi" if display_mode=="hdmi": display_size=[1920,1080] else: display_size=[800,480] # Set model path and other parameters kmodel_path = "/sdcard/examples/kmodel/face_detection_320.kmodel" # Other parameters 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) ``` -------------------------------- ### Initialize Display and Camera Source: https://github.com/kendryte/k230_canmv_docs/blob/main/zh/ai_dev_doc.md Sets up the display, media manager, and waits for a USB camera to be detected. Configures the video mode for the camera. ```python if __name__ == "__main__": # Align display width to 16 bytes for hardware requirement DISPLAY_WIDTH = ALIGN_UP(800, 16) DISPLAY_HEIGHT = 480 # Create CSC instance for pixel format conversion (e.g., to RGB888) csc = CSC(0, CSC.PIXEL_FORMAT_RGB_888) # Initialize LCD display (ST7701) and enable IDE display Display.init(Display.ST7701, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT, to_ide=True) # Initialize media manager to manage frame buffers and UVC stream MediaManager.init() # Wait for USB camera to be detected while True: plugin, dev = UVC.probe() if plugin: print(f"detect USB Camera {dev}") break time.sleep_ms(100) # Select and configure UVC video mode: 640x480 @ 30 FPS, MJPEG format mode = UVC.video_mode(640, 480, UVC.FORMAT_MJPEG, 30) succ, mode = UVC.select_video_mode(mode) print(f"select mode success: {succ}, mode: {mode}") # Define input image from USB camera (sensor side) rgb888p_size = [640, 480] ``` -------------------------------- ### NeoPixel Initialization and Basic Usage Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/machine/K230_CanMV_LED_API_Manual.md Demonstrates how to initialize the NeoPixel object, set individual pixel colors, and update the LED strip. ```APIDOC ## NeoPixel Initialization and Basic Usage ### Description This example shows how to initialize a NeoPixel strip, set colors for specific pixels, and then write those changes to the strip. It also demonstrates how to fill the strip with a color and turn off all LEDs. ### Constructor ```python neopixel.NeoPixel(pin, n, bpp=3, timing=1) ``` **Parameters** * `pin`: The GPIO pin used to control the WS2812 LED strip. Must be a `machine.Pin` object. * `n`: Number of LEDs. * `bpp`: Bytes per pixel. `3` for RGB, `4` for RGBW. Default is `3`. * `timing`: Protocol timing configuration. Use `1` for WS2812 (default), or `0` for WS2811/800kHz compatibility mode. ### Pixel Access ```python np[i] = (r, g, b) color = np[i] ``` **Description** Access or set the color of the `i`-th pixel using a tuple in the format `(r, g, b)` or `(r, g, b, w)` depending on `bpp`. ### `write()` Method ```python np.write() ``` **Description** Send the current color buffer to the LED strip. This method must be called for any changes to take effect. ### `fill()` Method ```python np.fill((r, g, b)) ``` **Description** Set all pixels in the LED strip to the same color. The color format is the same as in `__setitem__`. ### `__len__()` Method ```python length = len(np) ``` **Description** Returns the number of pixels in the current NeoPixel object. ### Example Code ```python from machine import Pin import neopixel import time # Define the NeoPixel data pin and number of LEDs NEO_PIN = 42 NUM_PIXELS = 8 # Initialize the NeoPixel object np = neopixel.NeoPixel(Pin(NEO_PIN), NUM_PIXELS) # Set the first LED to red, second to green, third to blue np[0] = (255, 0, 0) np[1] = (0, 255, 0) np[2] = (0, 0, 255) # Write the data (must call write() to update the LEDs) np.write() # Turn off all LEDs np.fill((0, 0, 0)) np.write() ``` ``` -------------------------------- ### Install Python Dependencies for Model Conversion Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/ai/YOLO_Battle.md Install required Python libraries for model conversion, including ONNX and ONNX Runtime. Ensure nncase and its kpu library are installed, with the latter potentially requiring offline installation. ```shell pip install nncase_kpu-2.*-py2.py3-none-win_amd64.whl ``` ```shell pip install onnx ``` ```shell pip install onnxruntime ``` ```shell pip install onnxsim ``` -------------------------------- ### Install NNCase for Model Conversion Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/ai/YOLO_Battle.md Install nncase and nncase-kpu on a Linux platform for model conversion. Note that nncase-2.x requires dotnet-7 to be installed. These tools are necessary for the training environment. ```shell # On the Linux platform: nncase and nncase-kpu can be installed online. For nncase-2.x, dotnet-7 needs to be installed. sudo apt-get install -y dotnet-sdk-7.0 pip install --upgrade pip pip install nncase==2.9.0 pip install nncase-kpu==2.9.0 ``` -------------------------------- ### FPIOA Pin Configuration and Query Example Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/peripheral/fpioa.md Demonstrates instantiating the FPIOA object, printing help information for all pins, specific pins, and functions, setting pin functions to GPIO, and retrieving pin numbers and function configurations. ```python from machine import FPIOA # Instantiate FPIOA object fpioa = FPIOA() # Print the configuration status of all pins fpioa.help() # Print the detailed configuration of a specific pin (Pin0) fpioa.help(0) # Print all available pins corresponding to a specific function (e.g., IIC0_SDA) fpioa.help(FPIOA.IIC0_SDA, func=True) # Set Pin0 to GPIO0 fpioa.set_function(0, FPIOA.GPIO0) # Set Pin2 to GPIO2 and configure other options simultaneously fpioa.set_function(2, FPIOA.GPIO2, ie=1, oe=1, pu=0, pd=0, st=1, sl=0, ds=7) # Get the current pin number for a specific function (e.g., UART0_TXD) fpioa.get_pin_num(FPIOA.UART0_TXD) # Get the current function configuration of a specific pin (Pin0) fpioa.get_pin_func(0) ``` -------------------------------- ### Install Python Dependencies for Model Conversion Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/example/ai/YOLO_Battle.md Installs essential Python packages for model conversion, including nncase, onnx, onnxruntime, and onnxsim. Ensure pip is up-to-date before installation. ```shell sudo apt-get install -y dotnet-sdk-7.0 pip install --upgrade pip pip install nncase==2.9.0 pip install nncase-kpu==2.9.0 pip install onnx pip install onnxruntime pip install onnxsim ``` -------------------------------- ### Initialize and Run Display and Sensor Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/mpp/K230_CanMV_Display_Module_API_Manual.md This snippet demonstrates the complete process of initializing the display and media manager, configuring and binding the display layer using sensor information, starting the sensor, and entering a loop to keep the application running. It includes a try-except-finally block for graceful shutdown of the sensor and display upon user interruption. ```python bind_info = sensor.bind_info() # Configure and bind display layer Display.config_layer(rect=(0, 0, 800, 480), pix_format=bind_info[2], layer=Display.LAYER_VIDEO1) Display.bind_layer(src=bind_info[0], rect=bind_info[1], pix_format=bind_info[2], layer=Display.LAYER_VIDEO1) # Initialize display Display.init(Display.ST7701, width=800, height=480) MediaManager.init() # Start sensor sensor.run() try: while True: time.sleep(1) except KeyboardInterrupt: print("User stopped") finally: sensor.stop() Display.deinit() MediaManager.deinit() ``` -------------------------------- ### Install NNCase and Dependencies for Windows Source: https://github.com/kendryte/k230_canmv_docs/blob/main/zh/example/ai/YOLO大作战.md Installs nncase via pip and nncase-kpu by downloading a specific wheel file for Windows. Ensure .NET 7 is installed and added to your environment variables. ```shell pip install nncase_kpu-2.*-py2.py3-none-win_amd64.whl ``` -------------------------------- ### Enter Bootloader Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/machine/K230_CanMV_machine_API_Manual.md Initiates the boot loader mode. This function takes no parameters and returns nothing. ```python machine.bootloader() ``` -------------------------------- ### Start Audio Stream Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/mpp/K230_CanMV_Audio_Module_API_Manual.md Starts an audio stream that has been previously opened or created. ```python start_stream() ``` -------------------------------- ### Create MP4 Container Instance (kd_mp4_create) Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/mpp/K230_CanMV_MP4_Module_API_Manual.md Creates an MP4 container instance and initializes its configuration. The handle is an output parameter for the MP4 instance. ```c handle = k_u64_ptr() ret = kd_mp4_create(handle, mp4_cfg) ``` -------------------------------- ### Create Directory with `uos.mkdir` Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/stdlib/os.md Create a new directory at the specified path using `uos.mkdir(path)`. Ensure that the parent directory exists and that you have the necessary permissions. ```python uos.mkdir(path) ``` -------------------------------- ### AI2D Resize Preprocessing Example Source: https://github.com/kendryte/k230_canmv_docs/blob/main/en/api/aidemo/Ai2d_Module_API_Manual.md Demonstrates how to configure and run the AI2D module for resize preprocessing. Ensure PipeLine and Ai2d are initialized, and the build method is called with appropriate input and output shapes. The input format for Shift function must be Raw16. ```python from libs.PipeLine import PipeLine, ScopedTiming from libs.AI2D import Ai2d from media.media import * import nncase_runtime as nn import gc import sys, os if __name__ == "__main__": # Display mode, default is "hdmi", can choose "hdmi" or "lcd" display_mode = "hdmi" if display_mode == "hdmi": display_size = [1920, 1080] else: display_size = [800, 480] # Initialize PipeLine for image processing workflow pl = PipeLine(rgb888p_size=[512, 512], display_size=display_size, display_mode=display_mode) pl.create() # Create PipeLine instance my_ai2d = Ai2d(debug_mode=0) # Initialize Ai2d instance # Configure resize preprocessing method my_ai2d.resize(nn.interp_method.tf_bilinear, nn.interp_mode.half_pixel) # Construct preprocessing process my_ai2d.build([1, 3, 512, 512], [1, 3, 640, 640]) try: while True: os.exitpoint() # Check for exit signal with ScopedTiming("total", 1): img = pl.get_frame() # Get current frame data print(img.shape) # Original image shape is [1, 3, 512, 512] ai2d_output_tensor = my_ai2d.run(img) # Execute resize preprocessing ai2d_output_np = ai2d_output_tensor.to_numpy() # Type conversion print(ai2d_output_np.shape) # Preprocessed shape is [1, 3, 640, 640] gc.collect() # Garbage collection except Exception as e: sys.print_exception(e) # Print exception information finally: pl.destroy() # Destroy PipeLine instance ```