Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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 ?

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

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.