我现在在sd卡保存了一张图片 想在想用摄像头再拍一张做对比

Viewed 305

What steps will reproduce the problem?
该问题的重现步骤是什么?
1.sdcard中已经存在保存好的YUV420SP格式文件,为开发板抓取并拍照的文件。
2.如何通过开发板读取已经保存好的文件,没有找到对应的功能函数。
3.新拍摄的照片想通过对比确认是否异常 这种需求是否需要进行模型学习。
4.canmv的ide中例程sensor目录下的snapshot_and_save文件的py文件保存在开发板后,开发板上电没有抓取并保存图片,但是ide下运行会生成抓取的照片文件。这个也不是很懂,为什么上电没有自动运行。
What is the expected output? What do you see instead
希望获得指点如何读取已有的图片文件。希望了解两张图片做比较判断差异的时候 是否需要进行模型学习。

What version of the product are you using? On what operating system?
硬件为K230。固件版本CanMV-K230_micropython_v1.0_sdk_v1.6_nncase_v2.8.3(创乐博V3.0)。
图片获取是参照canmv的ide中例程sensor目录下的snapshot_and_save文件所保存

Are there any error messages or logs?
没有错误信息。

What have you tried to resolve the issue?
尝试了很多方法,但是存储的py文件,开发板上电都没有运行并生成图片文件。

Please provide any additional information (e.g., code snippets, configuration settings, screenshots):
请提供其他相关信息(如代码片段、配置文件、截图等)。

1 Answers

可以把捕捉的图片存成bin文件,然后读取显示,代码如下:

from media.sensor import *
from media.display import *
from media.media import *
import os,sys,gc
import ulab.numpy as np
import image
import time

def read_bin(bin_path,width,height):
    with open(bin_path, 'rb') as f:
        data = f.read()
    data_ori = np.frombuffer(data, dtype=np.uint8).reshape((width,height,3))
    print(data_ori.shape)
    img=image.Image(width, height, image.RGB888, alloc=image.ALLOC_REF,data =data_ori)
    return img
    
def save_snapshot_bin(width,height,save_bin):
    try:
        sensor = Sensor(fps=30)
        # sensor reset
        sensor.reset()
        # set hmirror
        # sensor.set_hmirror(False)
        # sensor vflip
        # sensor.set_vflip(False)
    
        # set chn1 output format
        sensor.set_framesize(width = width, height = height, chn = CAM_CHN_ID_1)
        sensor.set_pixformat(Sensor.RGB888, chn = CAM_CHN_ID_1)
    
        # use hdmi as display output
        Display.init(Display.VIRT, to_ide = True)
        # init media manager
        MediaManager.init()
        # sensor start run
        sensor.run()
    
        # drop 100 frames
        for i in range(100):
            img=sensor.snapshot(chn=CAM_CHN_ID_1)
            Display.show_image(img)
    
        # snapshot and save
        img = sensor.snapshot(chn = CAM_CHN_ID_1)
    
        img_np=img.to_rgb888().to_numpy_ref()
        print(img_np.shape)
        
        with open(save_bin, "wb") as file:
            file.write(img_np.tobytes())
            print('Success!')
        file.close()
    except KeyboardInterrupt as e:
        print(f"user stop")
    except BaseException as e:
        print(f"Exception '{e}'")
    finally:
        # sensor stop run
        if isinstance(sensor, Sensor):
            sensor.stop()
        # deinit display
        Display.deinit()
        os.exitpoint(os.EXITPOINT_ENABLE_SLEEP)
        time.sleep_ms(100)
        # release media buffer
        MediaManager.deinit()
        

if __name__=="__main__":
    bin_path="/sdcard/examples/output.bin"
    width=640
    height=480
    mode="show" # "save" or "show"
    if mode=="save":
        save_snapshot_bin(width,height,bin_path)
    elif mode=="show":
        img=read_bin(bin_path,width,height)
        try:
            img.compress_for_ide()
            gc.collect()
        except Exception as e:
            sys.print_exception(e)
    else:
        pass

我尝试运行您的代码 但是报错为OSError: [Errno 2] ENOENT 网上看到是路径错误可能 于是我在sacard文件夹里添加了您上路路径的examples文件夹 (bin_path="/sdcard/examples/output.bin"),但是依旧报那个错误,我这里直接无法跑您的例程,抱歉 新手 实在是笨了。

这个是状态栏详细的一个内容
Traceback (most recent call last):
File "", line 77, in
File "", line 10, in read_bin
OSError: [Errno 2] ENOENT
MPY: soft reboot

先将mode改为'save',运行,保存后将mode改为'show',实现显示。这个程序包含两个功能,一个是从摄像头抓取图像保存bin文件,另一个是加载bin文件恢复成图片在IDE显示。