0

I am doing a question of enhancing contrast of an image. Please have a look on the following piece of code

# this is a test cell
@exit_after(4)
def testContrastSharpening():
    # load an image
    i0 = loadImage('Data/bokehCircular.jpg', 100)

    # run student code
    s0 = contrastSharpen(i0, 3, 0.5)

    # collect test data
    rTest = s0[:, int(i0.shape[1]/2), 0]
    gTest = s0[:, int(i0.shape[1]/4), 1]
    bTest = s0[int(i0.shape[0]/5), :, 2]

    # load and compare to reference result
    l0 = load('Data/t0.npz')

    return allclose(rTest, l0['r']) and allclose(gTest, l0['g']) and allclose(bTest, l0['b'])

try:
    assert(testContrastSharpening())
    print('Contrast sharpening seems to work!')
except:
    raise Exception("Contrast sharpening is not working... please try again...")

i0 = loadImage('Data/bokehCircular.jpg', 100) This line of code is calling pre written ffunction to load an image, that function is given below

def loadImage(path, scale):
    image = Image.open(path, mode = 'r')
    image = image.resize((int(image.width * scale / 100), int(image.height * scale / 100)), resample=Image.LANCZOS)
    image = array(image.getdata()).reshape(image.size[1], image.size[0], 3)
    image = image.astype(float)
    image = image / max(image)

    return image

when I run the code, i get error on the following line image = image.astype(float) The error is given below

KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-8-bf381c328e33> in <module>
     20 try:
---> 21     assert(testContrastSharpening())
     22     print('Contrast sharpening seems to work!')

<ipython-input-1-4c3172e491d3> in inner(*args, **kwargs)
     44             try:
---> 45                 result = fn(*args, **kwargs)
     46             finally:

<ipython-input-8-bf381c328e33> in testContrastSharpening()
      4     # load an image
----> 5     i0 = loadImage('Data/bokehCircular.jpg', 100)
      6 

<ipython-input-3-545d6e0c0f38> in loadImage(path, scale)
      5     image = array(image.getdata()).reshape(image.size[1], image.size[0], 3)
----> 6     image = image.astype(float)
      7     image = image / max(image)

KeyboardInterrupt:

I have written code for enhancing the contrast of an image and I have tested it separately by loading ana image through same LoadImage function, but In the above case, LoadImage function isn't working properly Please help, if someone can , thanks

5
  • The error is a KeyboardInterrupt error which means you did stop the script on purpose? Commented Mar 19, 2020 at 8:20
  • i did not touch anything, i didn't stop the script on purpose, it gives same error everytime Commented Mar 19, 2020 at 8:21
  • What is this decorator @exit_after(4) meant to do? Wouldn't it stop after 4 secs? Maybe try to rise that? Commented Mar 19, 2020 at 8:24
  • unfortunately this is a test cell and i am unable to edit it , may be i have to create another ipynb Commented Mar 19, 2020 at 8:29
  • exit after is also a function that is being called Commented Mar 19, 2020 at 8:30

1 Answer 1

1

I think this is because of your decorator @exit_after(4). Indeed, using @exit_after(4) after 4 seconds, your test cell will be stopped by a KeyboardInterrupt error.

From the source code of @exit_after()

def exit_after(s):
   '''
   use as decorator to exit process if 
   function takes longer than s seconds
   '''

There are two solutions:

  • Modify the test: Make your test more tolerant and raise that number? e.g. @exit_after(10) to see if this works.
  • Modify your script: Make your functions faster so that they work given the 4 seconds constraint. You could start by making the fonctions almost empty, and check that it works.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.