Problem reading an image inside a temporary file on raspberry pi 2.

asked 2016-02-19 08:58:36 -0500

tifa gravatar image

updated 2016-02-19 11:10:15 -0500

I've been looking all night to find a solution or at least understant where the problem is comming from so here i am.

I'm runnning a Flask server linked to my Apache with wsgi. I am recovering a picture from a user webcam as base 64 string (recovered from URI) and writing it on a temporary file. I am doing the same for a profile picture (like Facebook) of the user on another temporary file.

    # Decode base64 to image data
    imgdataCam = stringBase64Cam.decode('base64')
    imgdataPro = stringBase64Pro.decode('base64')

    # Create temporary files
    tempFile1 = tempfile.NamedTemporaryFile()
    tempFile2 = tempfile.NamedTemporaryFile()

    # Write images data in files
    tmpImg1=open(tempFile1.name,'wb')
    tmpImg1.write(imgdataCam)
    tmpImg1.close()
    tmpImg2=open(tempFile2.name,'wb')
    tmpImg2.write(imgdataPro)
    tmpImg2.close()

Later, i'm trying to read those files (temporary files still not closed so they still exist) as

    img1 = cv2.imread(tempFile1.name)

But it looks like nothing appens, i made some prints everywhere and it looks like "cv2.imread()" is stoping my program without any error message. I also tried to read those images with PIL like that:

    pilImg1 = Image.open(tempFile1.name).convert('RGB') 
    ocvImg1 = np.array(pilImg1)
    img1 = ocvImg1[:, :, ::-1].copy()

With PIL i'm able to read the file but i get the same problem as imread with

    greyImg = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)

Later on my program.

Everything is running very well on my laptop but once i uploaded it on my raspberry pi, i get thoses problems. Could someone help me understanding where it is coming from please ?

It is also working well when i'm running my flask serveur with "python __init__.py" on this raspberry.

edit retag flag offensive close merge delete

Comments

I Finally managed to find where the problem was comming from.

I was not able to read the file on my raspberry as it was using wsgi and apache and had no permissions to access those files. I fixed my isues by adding the following line to my apache vHost configuration file (/etc/apache2/sites-available/myapplication.conf):

WSGIDaemonProcess fbApp user=www-data group=www-data threads=2

I still don't understand why Opencv didn't return any errors. Hope it'll help someone with the same problem.

tifa gravatar imagetifa ( 2016-02-20 03:37:33 -0500 )edit