02-Media-8-uvc_with_csc.py 使用硬件解码的USB摄像头(UVC)捕获视频并显示的程序
uvc_with_csc.py 程序为前篇文章《02-Media-7-uvc.py 使用软件解码的USB摄像头(UVC)捕获视频并显示的程序》的姊妹篇,区别在于:uvc_with_csc.py使用了硬件解码。
UVC.start(cvt = True) #cvt 是否将snapshot获取的图像硬件解码为NV12格式
使用3.5寸液晶显示屏的源代码:
import time, os, urandom, sys, gcfrom media.display import *
from media.media import *
from media.uvc import *from nonai2d import CSCDISPLAY_WIDTH = ALIGN_UP(800, 16)
DISPLAY_HEIGHT = 480#DISPLAY_WIDTH = ALIGN_UP(1920, 16)
#DISPLAY_HEIGHT = 1080csc = 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)
#Display.init(Display.LT9611, width = DISPLAY_WIDTH, height = DISPLAY_HEIGHT, to_ide = True)
# init media manager
MediaManager.init()while True:plugin, dev = UVC.probe()if plugin:print(f"detect USB Camera {dev}")breaktime.sleep_ms(100)for i, mode in enumerate(UVC.list_video_mode()):print(f"模式{i}: {mode.width}x{mode.height} {mode.format}@{mode.fps}fps")mode = UVC.video_mode(640, 480, UVC.FORMAT_MJPEG, 30)
#mode = UVC.video_mode(1920, 1080, UVC.FORMAT_MJPEG, 30)succ, mode = UVC.select_video_mode(mode)
print(f"select mode success: {succ}, mode: {mode}")UVC.start(cvt = True)#cvt 是否将snapshot获取的图像硬件解码为NV12格式clock = time.clock()while True:clock.tick()img = Nonewhile img is None:try:img = UVC.snapshot()except:print("drop frame")continueimg = csc.convert(img)Display.show_image(img)img.__del__()gc.collect()print(f"fps: {clock.fps()}")# deinit display
Display.deinit()
csc.destroy()
UVC.stop()
time.sleep_ms(100)
# release media buffer
MediaManager.deinit()
使用HDMI外接高清显示屏的接口示例程序:
import time, os, urandom, sys, gcfrom media.display import *
from media.media import *
from media.uvc import *from nonai2d import CSC#DISPLAY_WIDTH = ALIGN_UP(800, 16)
#DISPLAY_HEIGHT = 480DISPLAY_WIDTH = ALIGN_UP(1920, 16)
DISPLAY_HEIGHT = 1080csc = 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)
Display.init(Display.LT9611, width = DISPLAY_WIDTH, height = DISPLAY_HEIGHT, to_ide = True)
# init media manager
MediaManager.init()while True:plugin, dev = UVC.probe()if plugin:print(f"detect USB Camera {dev}")breaktime.sleep_ms(100)for i, mode in enumerate(UVC.list_video_mode()):print(f"模式{i}: {mode.width}x{mode.height} {mode.format}@{mode.fps}fps")#mode = UVC.video_mode(640, 480, UVC.FORMAT_MJPEG, 30)
mode = UVC.video_mode(1920, 1080, UVC.FORMAT_MJPEG, 30)succ, mode = UVC.select_video_mode(mode)
print(f"select mode success: {succ}, mode: {mode}")UVC.start(cvt = True)#cvt 是否将snapshot获取的图像硬件解码为NV12格式clock = time.clock()while True:clock.tick()img = Nonewhile img is None:try:img = UVC.snapshot()except:print("drop frame")continueimg = csc.convert(img)Display.show_image(img)img.__del__()gc.collect()print(f"fps: {clock.fps()}")# deinit display
Display.deinit()
csc.destroy()
UVC.stop()
time.sleep_ms(100)
# release media buffer
MediaManager.deinit()