feat: ✨ 实现显示和摄像头部分的驱动
This commit is contained in:
parent
4cbe2b7a64
commit
d96a7dc599
66
k230/01test_cam_dis.py
Normal file
66
k230/01test_cam_dis.py
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
# main.py
|
||||||
|
# 主程序 - 调用摄像头模块和显示模块,实现实时画面显示
|
||||||
|
# 适用于庐山派 K230-CanMV 开发板
|
||||||
|
|
||||||
|
from camera_module import camera_init, camera_start, camera_snapshot, camera_stop, camera_deinit
|
||||||
|
from display_module import get_display_size, display_init, display_show, display_deinit
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# 添加/sdcard到python的搜索路径
|
||||||
|
sys.path.append("/sdcard")
|
||||||
|
|
||||||
|
|
||||||
|
# ============ 配置区域 ============
|
||||||
|
# 显示模式: "VIRT"(虚拟显示器), "LCD"(3.1寸屏幕), "HDMI"(HDMI扩展板)
|
||||||
|
DISPLAY_MODE = "LCD"
|
||||||
|
|
||||||
|
# 摄像头配置
|
||||||
|
HMIRROR = False # 水平镜像
|
||||||
|
VFLIP = False # 垂直翻转
|
||||||
|
# =================================
|
||||||
|
|
||||||
|
# 获取显示尺寸
|
||||||
|
DISPLAY_WIDTH, DISPLAY_HEIGHT = get_display_size(DISPLAY_MODE)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 初始化摄像头(分辨率与显示器匹配)
|
||||||
|
camera_init(
|
||||||
|
width=DISPLAY_WIDTH,
|
||||||
|
height=DISPLAY_HEIGHT,
|
||||||
|
hmirror=HMIRROR,
|
||||||
|
vflip=VFLIP
|
||||||
|
)
|
||||||
|
|
||||||
|
# 初始化显示器
|
||||||
|
display_init(DISPLAY_MODE)
|
||||||
|
|
||||||
|
# 启动摄像头
|
||||||
|
camera_start()
|
||||||
|
|
||||||
|
print("开始实时画面显示...")
|
||||||
|
|
||||||
|
# 主循环:捕获并显示画面
|
||||||
|
while True:
|
||||||
|
os.exitpoint()
|
||||||
|
|
||||||
|
# 捕获一帧图像
|
||||||
|
img = camera_snapshot()
|
||||||
|
|
||||||
|
# 显示图像
|
||||||
|
display_show(img)
|
||||||
|
|
||||||
|
except KeyboardInterrupt as e:
|
||||||
|
print("用户停止: ", e)
|
||||||
|
except BaseException as e:
|
||||||
|
print("异常: ", e)
|
||||||
|
finally:
|
||||||
|
# 停止摄像头
|
||||||
|
camera_stop()
|
||||||
|
# 释放显示器
|
||||||
|
display_deinit()
|
||||||
|
os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
|
||||||
|
time.sleep_ms(100)
|
||||||
|
# 释放摄像头资源
|
||||||
|
camera_deinit()
|
||||||
90
k230/camera_module.py
Normal file
90
k230/camera_module.py
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
# camera_module.py
|
||||||
|
# 摄像头图像获取模块
|
||||||
|
# 适用于庐山派 K230-CanMV 开发板
|
||||||
|
|
||||||
|
from media.sensor import *
|
||||||
|
from media.media import *
|
||||||
|
|
||||||
|
# 摄像头配置参数
|
||||||
|
sensor_id = 2
|
||||||
|
sensor = None
|
||||||
|
_is_running = False # 摄像头运行状态标志
|
||||||
|
|
||||||
|
|
||||||
|
def camera_init(width, height, chn=CAM_CHN_ID_0, hmirror=False, vflip=False):
|
||||||
|
"""
|
||||||
|
初始化摄像头
|
||||||
|
|
||||||
|
参数:
|
||||||
|
width: 输出图像宽度
|
||||||
|
height: 输出图像高度
|
||||||
|
chn: 摄像头通道ID
|
||||||
|
hmirror: 是否水平镜像
|
||||||
|
vflip: 是否垂直翻转
|
||||||
|
|
||||||
|
返回:
|
||||||
|
sensor对象
|
||||||
|
"""
|
||||||
|
global sensor
|
||||||
|
|
||||||
|
# 构造摄像头对象
|
||||||
|
sensor = Sensor(id=sensor_id)
|
||||||
|
# 重置摄像头
|
||||||
|
sensor.reset()
|
||||||
|
|
||||||
|
# 设置镜像和翻转
|
||||||
|
if hmirror:
|
||||||
|
sensor.set_hmirror(True)
|
||||||
|
if vflip:
|
||||||
|
sensor.set_vflip(True)
|
||||||
|
|
||||||
|
# 设置通道的输出尺寸
|
||||||
|
sensor.set_framesize(width=width, height=height, chn=chn)
|
||||||
|
# 设置通道的输出像素格式为RGB565
|
||||||
|
sensor.set_pixformat(Sensor.RGB565, chn=chn)
|
||||||
|
|
||||||
|
# 初始化媒体管理器
|
||||||
|
MediaManager.init()
|
||||||
|
|
||||||
|
return sensor
|
||||||
|
|
||||||
|
|
||||||
|
def camera_start():
|
||||||
|
"""启动摄像头"""
|
||||||
|
global sensor, _is_running
|
||||||
|
if sensor:
|
||||||
|
sensor.run()
|
||||||
|
_is_running = True
|
||||||
|
|
||||||
|
|
||||||
|
def camera_snapshot(chn=CAM_CHN_ID_0):
|
||||||
|
"""
|
||||||
|
捕获一帧图像
|
||||||
|
|
||||||
|
参数:
|
||||||
|
chn: 摄像头通道ID
|
||||||
|
|
||||||
|
返回:
|
||||||
|
捕获的图像对象
|
||||||
|
"""
|
||||||
|
global sensor
|
||||||
|
if sensor:
|
||||||
|
return sensor.snapshot(chn=chn)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def camera_stop():
|
||||||
|
"""停止摄像头"""
|
||||||
|
global sensor, _is_running
|
||||||
|
if _is_running and isinstance(sensor, Sensor):
|
||||||
|
sensor.stop()
|
||||||
|
_is_running = False
|
||||||
|
|
||||||
|
|
||||||
|
def camera_deinit():
|
||||||
|
"""释放摄像头资源"""
|
||||||
|
global sensor, _is_running
|
||||||
|
camera_stop()
|
||||||
|
MediaManager.deinit()
|
||||||
|
sensor = None
|
||||||
|
_is_running = False
|
||||||
81
k230/display_module.py
Normal file
81
k230/display_module.py
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
# display_module.py
|
||||||
|
# 显示模块
|
||||||
|
# 适用于庐山派 K230-CanMV 开发板
|
||||||
|
|
||||||
|
from media.display import *
|
||||||
|
|
||||||
|
# 显示模式配置
|
||||||
|
# "VIRT" - 虚拟显示器
|
||||||
|
# "LCD" - 3.1寸屏幕
|
||||||
|
# "HDMI" - HDMI扩展板
|
||||||
|
|
||||||
|
|
||||||
|
def get_display_size(mode):
|
||||||
|
"""
|
||||||
|
根据显示模式获取显示尺寸
|
||||||
|
|
||||||
|
参数:
|
||||||
|
mode: 显示模式 "VIRT", "LCD", "HDMI"
|
||||||
|
|
||||||
|
返回:
|
||||||
|
(width, height) 元组
|
||||||
|
"""
|
||||||
|
if mode == "VIRT":
|
||||||
|
# 虚拟显示器模式,宽度需要16字节对齐
|
||||||
|
width = ((1920 + 15) // 16) * 16
|
||||||
|
height = 1080
|
||||||
|
elif mode == "LCD":
|
||||||
|
# 3.1寸屏幕模式
|
||||||
|
width = 800
|
||||||
|
height = 480
|
||||||
|
elif mode == "HDMI":
|
||||||
|
# HDMI扩展板模式
|
||||||
|
width = 1920
|
||||||
|
height = 1080
|
||||||
|
else:
|
||||||
|
raise ValueError("未知的显示模式,请选择 'VIRT', 'LCD' 或 'HDMI'")
|
||||||
|
|
||||||
|
return (width, height)
|
||||||
|
|
||||||
|
|
||||||
|
def display_init(mode, width=None, height=None, to_ide=True, fps=60):
|
||||||
|
"""
|
||||||
|
初始化显示器
|
||||||
|
|
||||||
|
参数:
|
||||||
|
mode: 显示模式 "VIRT", "LCD", "HDMI"
|
||||||
|
width: 自定义宽度,None则使用默认值
|
||||||
|
height: 自定义高度,None则使用默认值
|
||||||
|
to_ide: 是否同时输出到IDE
|
||||||
|
fps: 帧率
|
||||||
|
"""
|
||||||
|
# 获取默认尺寸
|
||||||
|
default_width, default_height = get_display_size(mode)
|
||||||
|
|
||||||
|
if width is None:
|
||||||
|
width = default_width
|
||||||
|
if height is None:
|
||||||
|
height = default_height
|
||||||
|
|
||||||
|
# 根据模式初始化显示器
|
||||||
|
if mode == "VIRT":
|
||||||
|
Display.init(Display.VIRT, width=width, height=height, fps=fps)
|
||||||
|
elif mode == "LCD":
|
||||||
|
Display.init(Display.ST7701, width=width, height=height, to_ide=to_ide)
|
||||||
|
elif mode == "HDMI":
|
||||||
|
Display.init(Display.LT9611, width=width, height=height, to_ide=to_ide)
|
||||||
|
|
||||||
|
|
||||||
|
def display_show(img):
|
||||||
|
"""
|
||||||
|
显示图像
|
||||||
|
|
||||||
|
参数:
|
||||||
|
img: 要显示的图像对象
|
||||||
|
"""
|
||||||
|
Display.show_image(img)
|
||||||
|
|
||||||
|
|
||||||
|
def display_deinit():
|
||||||
|
"""释放显示器资源"""
|
||||||
|
Display.deinit()
|
||||||
Loading…
x
Reference in New Issue
Block a user