goktugyildirim / RealTimeMedicalMaskDetection

Real Time Medical Mask Detection using OpenCV and PyTorch
1 stars 0 forks source link

ValueError: tile cannot extend outside image #1

Open jordigar opened 4 years ago

jordigar commented 4 years ago

When the face goes to the image limits it fails:

Traceback (most recent call last): File "\PyCharm Community Edition 2019.1.2\plugins\python-ce\helpers\pydev\pydevd.py", line 1438, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "\PyCharm Community Edition 2019.1.2\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/PycharmProjects/deep/deteccio_mascara.py", line 80, in pil_image = Image.fromarray(current_face, mode="RGB") # numpy to pil image because of torch model File "\AppData\Local\Continuum\anaconda3\envs\deep37\lib\site-packages\PIL\Image.py", line 2701, in fromarray return frombuffer(mode, size, obj, "raw", rawmode, 0, 1) File "\AppData\Local\Continuum\anaconda3\envs\deep37\lib\site-packages\PIL\Image.py", line 2641, in frombuffer return frombytes(mode, size, data, decoder_name, args) File "\AppData\Local\Continuum\anaconda3\envs\deep37\lib\site-packages\PIL\Image.py", line 2581, in frombytes im.frombytes(data, decoder_name, args) File "\AppData\Local\Continuum\anaconda3\envs\deep37\lib\site-packages\PIL\Image.py", line 768, in frombytes d.setimage(self.im) ValueError: tile cannot extend outside image

goktugyildirim commented 4 years ago

Yeah because code tries to draw a rectangle around the face. You can try to cancel it.

jordigar commented 4 years ago

I have deleted the code for drawing, but I have de same error:

File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\plugins\python-ce\helpers\pydev\pydevd.py", line 1438, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.2\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/PycharmProjects/deep/deteccio_mascara.py", line 81, in pil_image = Image.fromarray(current_face, mode="RGB") # numpy to pil image because of torch model File "f\AppData\Local\Continuum\anaconda3\envs\deep37\lib\site-packages\PIL\Image.py", line 2701, in fromarray return frombuffer(mode, size, obj, "raw", rawmode, 0, 1) File "\AppData\Local\Continuum\anaconda3\envs\deep37\lib\site-packages\PIL\Image.py", line 2641, in frombuffer return frombytes(mode, size, data, decoder_name, args) File "\AppData\Local\Continuum\anaconda3\envs\deep37\lib\site-packages\PIL\Image.py", line 2581, in frombytes im.frombytes(data, decoder_name, args) File "\AppData\Local\Continuum\anaconda3\envs\deep37\lib\site-packages\PIL\Image.py", line 768, in frombytes d.setimage(self.im) ValueError: tile cannot extend outside image

goktugyildirim commented 4 years ago

I think you can view the outputs of face detection model which is rectangle pixel values of the face. You can add new conditions like that if the returned boundaries are out of the camera regions, don't do anything. Also I added extra pixels around the face because classification model can not classify the face properly due to detected face does not cover the all face properly. You can focus this point. If face is not in the middle of the camera region, added extra pixels cause this issue.

If this will not work, I will view my codes in my free time, but also I have realized that this issue before I uploaded my codes into GitHub.

jordigar commented 4 years ago

I check the limits before assign current_face and now it works fine:

        if startX - extra_pixel < 0 or startY - extra_pixel < 0:
            break

Thanks a lot for your code!

goktugyildirim commented 4 years ago

You're welcome :)