How to read image from database(sqlite Django) not local file?

asked 2018-11-02 15:00:02 -0500

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

edit retag flag offensive close merge delete

Comments

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.

berak gravatar imageberak ( 2018-11-02 18:22:57 -0500 )edit

In the database, there are two table name and imagefile. imagefile = lastimage.imagefile here I want to get the image file from the database.

Md.Rakibul Islam gravatar imageMd.Rakibul Islam ( 2018-11-02 23:39:29 -0500 )edit

try with imdecode(), not imread() 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)

berak gravatar imageberak ( 2018-11-03 02:03:24 -0500 )edit