Original image is compressed after using image.compressed_for_ide() but shouldn't

Hello.
I am taking a snapshot like usual and performing some elaboration on it, that do not compress the image.

Later on I would like to access pixels of fills using get_pixel() but it works only if I don’t call
print(fills.compressed_for_ide(quality = 100), end="")

The function compressed_for_ide() should leave the original image untouched and create a JPEG copy for the IDE to parse it, but it seems the original image is compressed after the call.

Is this a bug, or am I missing something?

Thanks!

This sounds like a bug.

Can you post a snip of the code that makes the error happen and the error message.

# Untitled - By: Danny - mar ott 4 2022
import sensor
import image
import time
import os
import gc
import pyb
from pyb import LED, Pin, UART, Servo


frameSize = sensor.QVGA # works with H7

imageW = const(320)
imageH = const(240)

faceH = const(imageH)
faceW = int(imageH*3/4)

startBtn = Pin(Pin.board.P2, mode = Pin.IN, pull = Pin.PULL_UP)
startBtnOld = startBtn.value()==0


print('init camera')
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(frameSize)
sensor.set_contrast(1)
sensor.set_gainceiling(8)
sensor.set_auto_gain(True) # must be turned off for color tracking
sensor.set_auto_whitebal(True) # must be turned off for color tracking
sensor.set_auto_exposure(True)
sensor.skip_frames(time = 2000)

edges = sensor.alloc_extra_fb(imageW, imageH, sensor.GRAYSCALE)

face_cascade = image.HaarCascade("frontalface", stages=25)

clock = time.clock()

while(True):

    clock.tick()
    img = sensor.snapshot()
    img.lens_corr(1.4, 1.2) #second number (zoom) allows to take pictures from further away from robot

    # when start button is pressed, process image and generate gcode
    startBtnValue = startBtn.value()==0

    if startBtnValue and not startBtnOld:
        #img.copy(roi = faceMod, copy_to_fb = edges)
        img.copy(copy_to_fb = edges)
        edges.gaussian(2)
        CANNY_THR = (50,60)
        edges.find_edges(image.EDGE_CANNY, threshold=CANNY_THR).negate()

        print(edges.compressed_for_ide(quality = 100), end="") #offending call
        
        # get_pixel gives OSError: Image is compressed!  if compressed_for_ide() is called
        print(edges.get_pixel(0,0)) 

        del img
        gc.collect()

    startBtnOld = startBtnValue
    print(clock.fps())

error calling get_pixel after compressed_for_ide

# Untitled - By: Danny - mar ott 4 2022
import sensor
import image
import time
import os
import gc
import pyb
from pyb import LED, Pin, UART, Servo


frameSize = sensor.QVGA # works with H7

imageW = const(320)
imageH = const(240)

faceH = const(imageH)
faceW = int(imageH*3/4)

startBtn = Pin(Pin.board.P2, mode = Pin.IN, pull = Pin.PULL_UP)
startBtnOld = startBtn.value()==0


print('init camera')
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(frameSize)
sensor.set_contrast(1)
sensor.set_gainceiling(8)
sensor.set_auto_gain(True) # must be turned off for color tracking
sensor.set_auto_whitebal(True) # must be turned off for color tracking
sensor.set_auto_exposure(True)
sensor.skip_frames(time = 2000)

edges = sensor.alloc_extra_fb(imageW, imageH, sensor.GRAYSCALE)

face_cascade = image.HaarCascade("frontalface", stages=25)

clock = time.clock()

while(True):

    clock.tick()
    img = sensor.snapshot()
    img.lens_corr(1.4, 1.2) #second number (zoom) allows to take pictures from further away from robot

    # when start button is pressed, process image and generate gcode
    startBtnValue = startBtn.value()==0

    if startBtnValue and not startBtnOld:
        #img.copy(roi = faceMod, copy_to_fb = edges)
        img.copy(copy_to_fb = edges)
        edges.gaussian(2)
        CANNY_THR = (50,60)
        edges.find_edges(image.EDGE_CANNY, threshold=CANNY_THR).negate()


       # print(edges.compressed_for_ide(quality = 100), end="") #offending call

        print(edges.get_pixel(0,0)) # get_pixel gives OSError: Image is compressed!  if compressed_for_ide() is called

        del img
        gc.collect()

    startBtnOld = startBtnValue
    print(clock.fps())

error calling get_pixel after compressed_for_ide

# Untitled - By: Danny - mar ott 4 2022
import sensor
import image
import time
import os
import gc
import pyb
from pyb import LED, Pin, UART, Servo


frameSize = sensor.QVGA # works with H7

imageW = const(320)
imageH = const(240)

faceH = const(imageH)
faceW = int(imageH*3/4)

startBtn = Pin(Pin.board.P2, mode = Pin.IN, pull = Pin.PULL_UP)
startBtnOld = startBtn.value()==0


print('init camera')
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(frameSize)
sensor.set_contrast(1)
sensor.set_gainceiling(8)
sensor.set_auto_gain(True) # must be turned off for color tracking
sensor.set_auto_whitebal(True) # must be turned off for color tracking
sensor.set_auto_exposure(True)
sensor.skip_frames(time = 2000)

edges = sensor.alloc_extra_fb(imageW, imageH, sensor.GRAYSCALE)

face_cascade = image.HaarCascade("frontalface", stages=25)

clock = time.clock()

while(True):

    clock.tick()
    img = sensor.snapshot()
    img.lens_corr(1.4, 1.2) #second number (zoom) allows to take pictures from further away from robot

    # when start button is pressed, process image and generate gcode
    startBtnValue = startBtn.value()==0

    if startBtnValue and not startBtnOld:
        #img.copy(roi = faceMod, copy_to_fb = edges)
        img.copy(copy_to_fb = edges)
        edges.gaussian(2)
        CANNY_THR = (50,60)
        edges.find_edges(image.EDGE_CANNY, threshold=CANNY_THR).negate()


       # print(edges.compressed_for_ide(quality = 100), end="") #offending call

        print(edges.get_pixel(0,0)) # get_pixel gives OSError: Image is compressed!  if compressed_for_ide() is called

        del img
        gc.collect()

    startBtnOld = startBtnValue
    print(clock.fps())

Hi, please reduce the code to just what causes the error. Not your whole script.

img = sensor.snapshot()    
img.copy(copy_to_fb = edges)
      
# offending call
print(edges.compressed_for_ide(quality = 100), end="") #offending call

# get_pixel gives OSError: Image is compressed!  if compressed_for_ide() is called

print(edges.get_pixel(0,0))

Looks like a bug, will fix.

Hello

I have a similar issue, not sure if that bug is still present or if I’m doing something wrong.
As far as I understand, the following code should work, as compressed() is supposed to leave the original img untouched:

import sensor, image

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)

while(True):

    img = sensor.snapshot()

    #img.compress()            # compress in place
    img2 = img.compressed()    # supposed to leave the original untouched

    blobs = img.find_blobs([(250,255)])
    print(blobs)
    print()

Camera is a H7 Plus with a global shutter sensor, firmware is 4.4.1.

What happens is that I receive an “OSError: Image is not mutable!” at find_blobs().
If I comment the compressed() line and uncomment the compress() line, that gives me the same error, but this time I expect that to happen.
If I comment out both, the blobs are detected without any issue.

Any idea what may be wrong here?

Thank you
Regards

Felix

Hi, I’ll release a new firmware image today which fixes this issue.

There was a bug with the method and it updated the pointer to the object accidentally.

Yes, it’s working now.
Thanks!