学习记录2

该文描述了一个图像处理流程,首先通过load_image函数加载图片,然后使用letterbox_image进行尺寸调整以匹配Yolov2的416格式。接着,image_to_array_1dim函数将图像数据转换为1维数组,再进行定点量化处理,最后将处理后的数据复制到FPGA内存中进行后续计算。整个过程涉及到图像的加载、缩放、颜色通道分离、数组重塑和定点量化操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前面完成了权重数据复制,现在需要对图像数据进行复制

图像加载与处理

图像加载

def load_image():
    orig_img_path = (ORIG_IMG_PATH)
    try:
        img = PIL_Image.open(orig_img_path)
        print("Open pictrue success!")
    except IOError:
        print("fail to load image!")
    print("pictrue size:", img.size)
    print("pictrue mode:", img.mode)
    return img
frame_in=load_image()#导入图片
img_w =frame_in.size[0]#图片宽度
img_h =frame_in.size[1]#图片长度
img_out = frame_in

yolov2处理图片采用416格式

def letterbox_image(img, w, h):
    if ((float)(w / img_w)) < ((float)(h / img_h)):
        new_w = w
        new_h = (img_h * w) // img_w
    else:
        new_w = (img_w * h) // img_h
        new_h = h
    img_resized = img.resize((new_w, new_h), PIL_Image.BILINEAR)#图像缩放,安装416比例缩放

    boxed = PIL_Image.new('RGB', (w, h), (127, 127, 127))#空白图像
    box1 = (0, 0, new_w, new_h)#crop用于切割图像
    boxed.paste(img_resized.crop(box1), ((w - new_w) // 2, (h - new_h) // 2))

    return boxed

PYNQ使用的是PIL来图像处理,得到的数组格式需要进行转换。
此外,传入FPGA的图像数据并不是3维的,而是将RGB展开成1维数组

def image_to_array_1dim(img, w, h):

    img_r, img_g, img_b = img.split()

    image_array_r = np.array(img_r, dtype='float32') / 255
    image_array_g = np.array(img_g, dtype='float32') / 255
    image_array_b = np.array(img_b, dtype='float32') / 255

    image_array_r = image_array_r.reshape(w * h, )
    image_array_g = image_array_g.reshape(w * h, )
    image_array_b = image_array_b.reshape(w * h, )

    img_array_3 = [image_array_r, image_array_g, image_array_b]
    print(img_array_3)
    img_array_3 = np.array(img_array_3)
    img_array_3 = img_array_3.reshape(3 * w * h, )
    print(img_array_3)
    return img_array_3

对数据进行定点量化

def float32_int(img_array_3_416_416):
        current_in = np.array(img_array_3_416_416[::2]*(math.pow(2.0,14)),dtype='uint16')从第一个元素开始,每两个数中取一个
        next_in = np.array(img_array_3_416_416[1::2]*(math.pow(2.0,14)),dtype= 'uint16')从第二个元素开始,每两个数中取一个
        input_tmp_img = next_in*65536 + current_in
        print(img_array_3_416_416.shape)
        return input_tmp_img

最后将数据copy进FPGA

   np.copyto(img_base_buffer[0:259584], input_tmp_img)
    print("yolov2_image copy ok\n")
    end_time = time.time()
    load_image_to_memory = end_time - start_tim
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值