How to read image from database(sqlite Django) not local file?
OpenCV => 3.4, Operating System / Platform => Ubuntu18.4, Compiler => Pycharm2018, Django => 2.1.2
I am trying to get an image from a database and encode the image for face recognition.
Model
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)
Read Image
from .models import Image
import cv2
lastimage = Image.objects.last()
imagefile = lastimage.imagefile
image = cv2.imread(imagefile)
cv2.imshow('image',image)
Error Get this error
Exception Type: TypeError
Exception Value: bad argument type for built-in operation
failure to read docs in the 1st place.
it expects a filename as 1st arg, so, what do you have ?
(don't excpect US to know about django, or web dev in general, this is about opencv, and computer-vision !)
but again - what is
imagefile
, exactly, here ?please be more concise here.
In the database, there are two table name and imagefile.
imagefile = lastimage.imagefile
here I want to get the image file from the database.try with
imdecode()
, notimread()
then.(imagefile should be a numpy array, so you maybe need to convert it)
(also watch out, if it might be base64 encoded. if so, you need to decode that first)