首页 > 解决方案 > 如何从数据库(sqlite Django)而不是本地文件中读取图像?

问题描述

我正在尝试从数据库中获取图像并对图像进行编码以进行人脸识别。

模型

from django.db import models

class Image(models.Model):
    name = models.CharField(max_length=500)
    imagefile = models.FileField(upload_to='images/', null=True, verbose_name="")

    def __str__(self):
        return self.name + ": " + str(self.imagefile)

读取图像

from .models import Image
import cv2

lastimage = Image.objects.last()
imagefile = lastimage.imagefile
image = cv2.imread(imagefile)
cv2.imshow('image',image)

错误 得到这个错误

异常类型:TypeError

异常值:内置操作的参数类型错误

标签: pythondjangosqliteopencvdjango-models

解决方案


这是我创建 AI Web 项目时的方法:

np.ndarray <=( cv2.imencode| cv2.imdecode) => buff <==> base64


推荐阅读