例程讲解34-image_transfer_jpg_as_the_remote_device_for_your_computer OpenMV作为受控设备传输jpg图像到电脑

# 图像传输-作为远程设备
#
# 该脚本旨在与您计算机上的 "image_transfer_jpg_as_the_controller_device.py" 对话。
#
# 这个脚本演示了如何将帧缓冲区作为jpeg图像传输到您的计算机。

import image, network, omv, rpc, sensor, struct

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

# 关闭从OpenMV Cam端到IDE的帧缓冲连接。
#
# 当以较高的质量手动压缩jpeg图像时,需要执行此操作。
# 以便如果JPEG图像太大而无法容纳OpenMV的IDE JPEG帧缓冲区,
# 则OpenMV Cam不会尝试使用回退机制将其流式传输到IDE。

omv.disable_fb(True)

# 上面的RPC库安装在您的OpenMV Cam上,并提供了多个类,
# 可通过USB或WIFI控制您的OpenMV Cam。

################################################################
# 选择你想要控制的OpenMV摄像头的接口。
################################################################

# 取消注释以下行以设置OpenMV Cam,以控制USB VCP。
#
interface = rpc.rpc_usb_vcp_slave()

# 取消下面的注释来设置你的OpenMV摄像头来控制WiFi。
#
# * ssid - 要连接的WiFi网络。
# * ssid_key - WiFi网络密码。
# * ssid_security - WiFi安全性。
# * port - 用于将流量路由到的端口。
# * mode - 常规或接入点模式。
# * static_ip - 如果不是None,则为一个元组(IP地址,子网掩码,网关,DNS地址)。
#
# interface = rpc.rpc_wifi_slave(ssid="",
#                                ssid_key="",
#                                ssid_security=network.WINC.WPA_PSK,
#                                port=0x1DBA,
#                                mode=network.WINC.MODE_STA,
#                                static_ip=None)

################################################################
# 回调
################################################################

# 调用时设置像素格式和帧大小,拍摄快照,然后返回帧缓冲区jpg大小以存储图像。
#
# data是一个pixformat字符串和framesize字符串。
def jpeg_image_snapshot(data):
    pixformat, framesize = bytes(data).decode().split(",")
    sensor.set_pixformat(eval(pixformat))
    sensor.set_framesize(eval(framesize))
    img = sensor.snapshot().compress(quality=90)
    return struct.pack("<I", img.size())

def jpeg_image_read_cb():
    interface.put_bytes(sensor.get_fb().bytearray(), 5000) # 超时

# 给定偏移量和大小,从帧缓冲区读取数据。
# 如果数据为空,则在RPC调用完成后安排传输。
#
# data是4字节大小和4字节偏移量。
def jpeg_image_read(data):
    if not len(data):
        interface.schedule_callback(jpeg_image_read_cb)
        return bytes()
    else:
        offset, size = struct.unpack("<II", data)
        return memoryview(sensor.get_fb().bytearray())[offset:offset+size]

# 添加回调。

interface.register_callback(jpeg_image_snapshot)
interface.register_callback(jpeg_image_read)

# 一旦所有的回调都被添加,我们就可以开始处理远程事件。
# loop()不返回。

interface.loop()

results matching ""

    No results matching ""