urbanogilson / SICAR

This tool is designed for students, researchers, data scientists or anyone who would like to have access to SICAR files
https://urbanogilson.github.io/posts/sicar/
MIT License
76 stars 37 forks source link

"Permission denied" em arquivo temporário durante resolução do captcha #20

Open ivanlonel opened 12 months ago

ivanlonel commented 12 months ago

Estou recebendo o seguinte erro nessa linha quando chamo download_country, download_state etc:

Traceback (most recent call last):
  File "d:\ivanl\git\car\car.py", line 6, in <module>
    car.download_country()
  File "D:\ivanl\git\car\.venv\Lib\site-packages\SICAR\sicar.py", line 501, in download_country
    result[str(state)] = self.download_state(
                         ^^^^^^^^^^^^^^^^^^^^
  File "D:\ivanl\git\car\.venv\Lib\site-packages\SICAR\sicar.py", line 464, in download_state
    return self.download_cities(
           ^^^^^^^^^^^^^^^^^^^^^
  File "D:\ivanl\git\car\.venv\Lib\site-packages\SICAR\sicar.py", line 427, in download_cities
    result[(city, code)] = self.download_city_code(
                           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ivanl\git\car\.venv\Lib\site-packages\SICAR\sicar.py", line 361, in download_city_code
    captcha = self._driver.get_captcha(self._download_captcha())
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ivanl\git\car\.venv\Lib\site-packages\SICAR\drivers\tesseract.py", line 64, in get_captcha
    self._process_captcha(captcha),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ivanl\git\car\.venv\Lib\site-packages\SICAR\drivers\captcha.py", line 110, in _process_captcha
    cv2.cvtColor(self._png_to_jpg(captcha), cv2.COLOR_BGR2GRAY)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\ivanl\git\car\.venv\Lib\site-packages\SICAR\drivers\captcha.py", line 63, in _png_to_jpg
    captcha.save(png.name)
  File "D:\ivanl\git\car\.venv\Lib\site-packages\PIL\Image.py", line 2435, in save
    fp = builtins.open(filename, "w+b")
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\ivanl\\AppData\\Local\\Temp\\tmpoyd5af69.png'

Parece que o pillow está chamando open() no arquivo que já foi aberto pelo NamedTemporaryFile, assim como nesse caso: https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file

github-actions[bot] commented 12 months ago

Welcome! Your issue will be analyzed as soon as possible. Hopefully, we can find a solution to the problem together, please try to provide as much information as possible to help us identify and fix the bug or improve the repository.

urbanogilson commented 10 months ago

O problema persiste?

Qual é a versão do sistema operacional? Python?

ivanlonel commented 10 months ago

Bom dia.

Sim, o problema persiste na versão 0.7.0.

Windows 11 Pro, 22H2 (22621.3007)

Python 3.11.5 Saída de sys.version: 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)]

pillow 10.2.0

rmatias3 commented 2 months ago

Mesmo problema comigo. Instalei o módulo agora. Ou seja, a versão mais recente. E estou com o python 3.12.4.

Traceback (most recent call last): File "c:\Users\RSMat\OneDrive\Engenharia\W2 Engenharia\Rio Vivo\SICAR\Programação\sicar_access.py", line 12, in result = car.download_state(state=State.AL, polygon=Polygon.AREA_PROPERTY, folder='AL') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\RSMat\AppData\Local\Programs\Python\Python312\Lib\site-packages\SICAR\sicar.py", line 292, in download_state captcha = self._driver.get_captcha(self._download_captcha()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\RSMat\AppData\Local\Programs\Python\Python312\Lib\site-packages\SICAR\drivers\tesseract.py", line 64, in get_captcha self._process_captcha(captcha), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\RSMat\AppData\Local\Programs\Python\Python312\Lib\site-packages\SICAR\drivers\captcha.py", line 110, in _process_captcha cv2.cvtColor(self._png_to_jpg(captcha), cv2.COLOR_BGR2GRAY) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\RSMat\AppData\Local\Programs\Python\Python312\Lib\site-packages\SICAR\drivers\captcha.py", line 63, in _png_to_jpg captcha.save(png.name) File "C:\Users\RSMat\AppData\Local\Programs\Python\Python312\Lib\site-packages\PIL\Image.py", line 2564, in save fp = builtins.open(filename, "w+b") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PermissionError: [Errno 13] Permission denied: 'C:\Users\RSMat\AppData\Local\Temp\tmpktsks7yh.png'

brittoeduardo commented 1 month ago

Eu consegui fazendo essas seguintes alterações:

1 - Entre na biblioteca do SICAR e depois captcha.py 2 - Dentro do captcha.py localize as duas linhas

    with tempfile.NamedTemporaryFile(suffix=".png") as png:
        with tempfile.NamedTemporaryFile(suffix=".jpg") as jpg:

3 - Adicione o comando delete=False

    with tempfile.NamedTemporaryFile(suffix=".png",delete=False) as png:
        with tempfile.NamedTemporaryFile(suffix=".jpg",delete=False) as jpg:

4 - Salve o arquivo captcha.py .

Talvez resolva de forma provisória.