ImportError: cannot import name 'is_directory' - parsing

I've been trying to read pdf pages as an image, for extraction purposes.
I found that layoutparser serves this purpose by identifying blocks of text. However, when I try to Create a Detectron2-based Layout Detection Model, I encounter the following:
import layoutparser as lp
model = lp.Detectron2LayoutModel( config_path ='lp://PubLayNet/mask_rcnn_X_101_32x8d_FPN_3x/config',
extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.65],
label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"})
Error: [enter image description here][1]
ImportError Traceback (most recent
call last)
<ipython-input-16-eab7187a31c8> in <module>()
1 import layoutparser as lp
----> 2 model = lp.Detectron2LayoutModel( config_path ='lp://PubLayNet/mask_rcnn_X_101_32x8d_FPN_3x/config',
3 extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.65],
4 label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"})
5 layout = model.detect(image) # You need to load the image somewhere else, e.g., image = cv2.imread(...)
31 frames
/usr/local/lib/python3.7/dist-packages/PIL/ImageFont.py in <module>()
35 from . import Image
36 from ._deprecate import deprecate
---> 37 from ._util import is_directory, is_path
38
39
ImportError: cannot import name 'is_directory' from 'PIL._util' (/usr/local/lib/python3.7/dist-packages/PIL/_util.py)

I tried with pillow version <=6.2.2. It worked for me.

You might have installed a higher version of pillow. Try the following command to downgrade the pillow package. You should install pillow version less than or equal to 6.2.2.
pip install --upgrade pillow==6.2.2

You can simply install detectron2 from github :
!pip install 'git+https://github.com/facebookresearch/detectron2.git#v0.4#egg=detectron2'

Related

How to set up Raspberry Pi Buster and Intel NCS2 and OpenVINO with OpenCV trackers

Is there a definitive set of instructions to implement OpenCV trackers with OpenVINO and the now-obsolete NCS2 on a RPi 4b - Buster?
My understanding that the last OpenVINO to support the NCS2 was v2020.3.
I attempted to cross-compile using:
https://github.com/opencv/opencv/wiki/Intel-OpenVINO-backend#raspbian-buster
After installing opencv/opencv-contrib 4.5.5 from source:
$ python3
Python 3.7.3 (default, Oct 31 2022, 14:04:00)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.5.5'
>>> tracker = cv2.TrackerCSRT_create()
>>>
However, in a test.py script I have:
...
import cv2
net = cv2.dnn.readNetFromCaffe(_weights, _model)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)
...
detections = net.forward()
I get the error relating to DNN_TARGET_MYRIAD:
cv2.error: OpenCV(4.5.5) /home/pi/opencv/modules/dnn/src/dnn.cpp:1414: error: (-215:Assertion failed) preferableBackend != DNN_BACKEND_OPENCV || preferableTarget == DNN_TARGET_CPU || preferableTarget == DNN_TARGET_OPENCL || preferableTarget == DNN_TARGET_OPENCL_FP16 in function 'setUpNet'
I then used this to install OpenVINO:
https://docs.openvino.ai/latest/openvino_docs_install_guides_installing_openvino_raspbian.html
but using this version of OpenVINO (as the last to support the NCS2):
https://storage.openvinotoolkit.org/repositories/openvino/packages/2020.3/l_openvino_toolkit_runtime...
I exported the paths to the new post cross-compiled opencv_install directory:
$ export PYTHONPATH=/home/pi/Desktop/opencv_install/lib/python2.7/dist-packages/:$PYTHONPATH
$ export PYTHONPATH=/home/pi/Desktop/opencv_install/lib/python3.7/site-packages/:$PYTHONPATH
$ export LD_LIBRARY_PATH=/home/pi/Desktop/opencv_install/lib/:$LD_LIBRARY_PATH
I set up the NCS2 with no errors :
$ sudo usermod -a -G users "$(whoami)"
$ sh /opt/intel/openvino_2020.3/install_dependencies/install_NCS_udev_rules.sh
then:
$ source /opt/intel/openvino_2020.3/bin/setupvars.sh
and then checked:
$ python3
Python 3.7.3 (default, Oct 31 2022, 14:04:00)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.3.0-openvino-2020.3.0'
>>> tracker = cv2.TrackerCSRT_create()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'cv2' has no attribute 'TrackerCSRT_create'
>>>
If I open a new terminal and $ source /opt/intel/openvino_2020.3/bin/setupvars.sh
then run a test.py script:
...
import cv2
net = cv2.dnn.readNetFromCaffe(_weights, _model)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)
...
detections = net.forward()
...
I get a segmentation fault error.
So far I have not edited any of the setup scripts.
Thanks for any help! I'd like to put this NCS2 to work.
Generally, if you are able to run some OpenVINO demo with NCS2 after following this installation guide, then you should be able to use that OpenCV functionality (ensured that you had installed the correct OpenCV).
It's recommended to use the recent OpenVINO and OpenCV version.
As indicated in this OpenVINO System Requirements, the current recommended OpenCV version is 4.5.

Import error geopandas, fiona

I have an error with import geopandas, fiona.
When I'm trying to import geopandas
import geopandas as gpd
It returns me
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-8-77a0d94ee2c6> in <module>()
5 #ret = add(1,3)
6 #print(ret)
----> 7 import geopandas as gpd
~\Anaconda3\lib\site-packages\geopandas\__init__.py in <module>()
2 from geopandas.geodataframe import GeoDataFrame
3
----> 4 from geopandas.io.file import read_file
5 from geopandas.io.sql import read_postgis
6 from geopandas.tools import sjoin
~\Anaconda3\lib\site-packages\geopandas\io\file.py in <module>()
1 import os
2
----> 3 import fiona
4 import numpy as np
5 import six
~\Anaconda3\lib\site-packages\fiona\__init__.py in <module>()
67 from six import string_types
68
---> 69 from fiona.collection import Collection, BytesCollection, vsi_path
70 from fiona._drivers import driver_count, GDALEnv
71 from fiona.drvsupport import supported_drivers
~\Anaconda3\lib\site-packages\fiona\collection.py in <module>()
7
8 from fiona import compat
----> 9 from fiona.ogrext import Iterator, ItemsIterator, KeysIterator
10 from fiona.ogrext import Session, WritingSession
11 from fiona.ogrext import (
ImportError: DLL load failed: The specified module could not be found.
I used "conda install -c conda-forge geopandas" and found that geopandas is installed on C:\Users\Kim\Anaconda3(by "conda list" in anaconda prompt ). But when I typed
import sys
'geopandas' in sys.modules
It has returned me "False"
I thought reinstalling anaconda could help me but it wasn't.
Is anyone has solved this problem?
FYI, I'm using windows 10 64bit
I had same problem and following commands help me.
First of all I added conda channels (last channel has highest priority).
conda config --add channels conda-forge
conda config --add channels anaconda
Then try to create new environment using conda.
conda create -n geoPython3 python=3.6 geopandas=0.4.0 gdal=2.2.4
Let me know if it helps.

How install utils on colab?

How manually install util on google colab?
!pip install web.py==0.40.dev0
import utils
Error
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-13-a4c82eaa1619> in <module>()
20 import torch.backends.cudnn
21 get_ipython().system('pip install web.py==0.40.dev0')
---> 22 from Utils import *
23 from PIL import Image
24 from torch import nn
ModuleNotFoundError: No module named 'Utils'
---------------------------------------------------------------------------
How to correct this mistake correctly?
try change to !pip install python-utils but
Installing collected packages: python-utils
**Successfully installed python-utils-2.3.0**
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-e6796b5aa862> in <module>()
20 import torch.backends.cudnn
21 get_ipython().system('pip install python-utils')
---> 22 import utils
23 from PIL import Image
24 from torch import nn
ModuleNotFoundError: **No module named 'utils'**
please try this if this may help in your code
!install pip
!pip install utils
import utils

How to overcome import error with opencv in ipython

I believe I finally installed OpenCV using the following command.
conda install -c anaconda opencv 
it seems I can load opencv in python3
however, when I try to import this in ipython or jupyter, I get the following:
In [1]: import cv2
ImportError Traceback (most recent call last)
<ipython-input-1-72fbbcfe2587> in <module>()
----> 1 import cv2
ImportError: dlopen(/Users/mycomputer/anaconda/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so, 2): Library not loaded: #rpath/libintl.8.dylib
Referenced from: /Users/mycomputer/anaconda/lib/libharfbuzz.0.dylib
Reason: image not found
By run this command:pip install opencv-python, I fixed this issue.

Graphviz not working using conda or pip [duplicate]

This question already has answers here:
anaconda - graphviz - can't import after installation
(14 answers)
Closed 3 years ago.
I cannot get graphviz to install and import properly, either using conda install or pip install. I am using jupyter on anaconda.
conda install -c anaconda graphviz
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-0e78fd6401bd> in <module>()
----> 1 import graphviz
2 dot_data = tree.export_graphviz(clf, out_file=None)
3 graph = graphviz.Source(dot_data)
4 graph.render("iris")
ModuleNotFoundError: No module named 'graphviz'
When I install using pip I can successfully import but when I run the below code, I get a very long error message that ends with the following when running graph.render:
from sklearn.datasets import load_iris
from sklearn import tree
import graphviz
iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)
dot_data = tree.export_graphviz(clf, out_file=None)
graph = graphviz.Source(dot_data)
graph.render("iris")
Below is the end of the end of a long error message:
ExecutableNotFound: failed to execute ['dot', '-Tpdf', '-O', 'iris'], make sure the Graphviz executables are on your systems' PATH
I have the directory where the graphviz folders are in PATH.
Anyone have an idea what I am doing wrong?
http://www.graphviz.org/Download_windows.php
Install windows package from: http://www.graphviz.org/Download_windows.php
Install python graphviz package
Add C:\Program Files (x86)\Graphviz2.38\bin to User path
Add C:\Program Files (x86)\Graphviz2.38\bin\dot.exe to System Path
RESTART COMPUTER
Worked for me!
If you're running Linux, you may have to run this command in addition to the Conda command (I had to do this):
sudo apt-get install graphviz
I was getting the exact same error in Pycharm and this solved it.

Resources