Python: Can't import cv2 - python

I have a simple Python program that tries to import cv2, but fails.
File "/home/userJB/HelloWorld.py", line 4, in <module>
import cv2
File "/home/userJB/srcMisc/Python-2.7.13/INSTALL/lib/python2.7/site-packages/cv2/__init__.py", line 7, in <module>
from . import cv2
ImportError: cannot import name cv2
Where should I start looking to resolve this? I have installed a local version on python on this machine. I have also installed openCV on this machine. I might not have fully installed openCV correctly. Do I need to do anything else to build/install a cv2 module?

Related

OSError when trying to use a C-based library in Python with ctypes (Windows)

I'm currently trying to use a library written in C with my Python code but I didn't have much success at all. I'm currently using Windows and have downloaded, saved and installed the library (linked below) to a directory near my project using Cygwin.
As of now, I'm not sure how you load it within your Python code. I followed the instruction in this link: https://github.com/xiph/rnnoise/issues/69 but replaced the library path with a relative path and saved the library inside my project directory. Here's my code and here's the return error
import wave
import os,sys
import ctypes
import contextlib
import numpy as np
from ctypes import util
from scipy.io import wavfile
from pydub import AudioSegment
lib_path = "rnnoise"
lib = ctypes.cdll.LoadLibrary(lib_path)
Here's the version from the instruction:
import wave
import os,sys
import ctypes
import contextlib
import numpy as np
from ctypes import util
from scipy.io import wavfile
from pydub import AudioSegment
lib_path = util.find_library("rnnoise")
if (not("/" in lib_path)):
lib_path = (os.popen('ldconfig -p | grep '+lib_path).read().split('\n')[0].strip().split(" ")[-1] or ("/usr/local/lib/"+lib_path))
lib = ctypes.cdll.LoadLibrary(lib_path)
Traceback (most recent call last):
File "c:/Users/duyba/Documents/CET/Project/vadtest/noisetest.py", line 14, in <module>
lib = ctypes.cdll.LoadLibrary(lib_path)
File "C:\Users\duyba\anaconda3\lib\ctypes\__init__.py", line 459, in LoadLibrary
return self._dlltype(name)
File "C:\Users\duyba\anaconda3\lib\ctypes\__init__.py", line 381, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 1920] The file cannot be accessed by the system
My impression of the instruction version was that they are trying to locate the library in Linux, so I just copied the library to the project folder and didn't know what they are trying to load once the library is located. Also, I think that some environment pathway problems may be at play here since I downloaded and installed this library in Windows.
The library: https://github.com/xiph/rnnoise.
Thanks.
This could be an file access permission error. You might be able to get around it by making a new virtual environment or by uninstalling and reinstalling Python; see this post.

trouble importing precompiled version of PIL on linux

I am writing a plugin for Ultimaker Cura which uses the Python Imaging Library.
Cura has it's own python environment and uses Python 3.5.7.
I want my plugin to be usable for any Cura user, so I have to include PIL in my plugin inside a subdirectory, and because it's _imaging module is written in C, I have to include precompiled versions of PIL for Python 3.5 which i got from here: https://pypi.org/project/Pillow/#files
I included the cp35 win_amd64 version for windows under "lib_win" and the cp35 manylinux1_x86_64 version for linux under "lib_linux". Here is my code:
if sys.platform == 'win32':
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "lib_win"))
from PIL import Image
from PIL import ImageFilter
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageChops
if sys.platform == 'linux':
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "lib_linux"))
from PIL import Image
from PIL import ImageFilter
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageChops
This works without any issues for windows. Under linux, i get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/dipl/.local/share/cura/4.6/plugins/SVGReader/lib_linux/PIL/Image.py", line 93, in <module>
from . import _imaging as core
ImportError: cannot import name '_imaging'
I have "_imaging.cpython-35m-i386-linux-gnu.so" inside the lib_linux/PIL directory, but it does not recognize this.
I have checked other posts with the same error, but they either have older versions of PIL installed, or use the wrong precompiled version, or are missing a DLL. None of this is the case here.
I also have a working version of PIL on my linux system, and the include code and the PIL code look exactly the same, only the python version number is different.
In case you need it, here is the full sys.path for the Cura python environment (my plugin is SVGReader): https://i.stack.imgur.com/Mixzi.png
(Had to use my own console because Cura doesn't come with one)
So, why does it not recognize _imaging? Any ideas?
in case anybody has the same issue, it's really just as simple as changing the filename of the _imaging library. Name it _imaging.so, or create a symlink to it called _imaging.so. No idea why it works on other platforms without doing that. Might need to rename some other stuff in the same manner, too.

Issue converting python script with pyinstaller. Import error: Incompatible library

I am relatively new to programming and I am using macOS Catalina 10.15.4 python 3.7 and pyinstaller3.6 to convert my python script to an executable. I manage to convert the script to an executable but when I click on it I get the following error:
issue:Traceback (most recent call last):
File "QC.py", line 2, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "cv2/__init__.py", line 5, in <module>
ImportError: dlopen(/Users/kapten42/Desktop/OCR/QA/dist/QC/cv2/cv2.cpython-37m-darwin.so, 2): Library not loaded: #loader_path/libpng16.16.dylib
Referenced from: /Users/kapten42/Desktop/OCR/QA/dist/QC/libfreetype.6.dylib
Reason: Incompatible library version: libfreetype.6.dylib requires version 54.0.0 or later, but libpng16.16.dylib provides version 38.0.0
[35604] Failed to execute script QC
I installed most of my packages using pip3 except for tesseract which I had to install using brew. I have been searching everywhere and really stumped with this one. I used brew to update libpng but the it didn't change anything. Below is the list of imports made at the top of my code. I am really stuck so any advice is greatly appreciated
from PIL import Image
import cv2
from skimage.filters import threshold_local
from skimage import io
import csv
import pytesseract
import re
import imutils
import numpy as np
from datetime import datetime
from spellchecker import SpellChecker
import json
import tempfile
import requests
import os
from pdf2image import convert_from_path
from dateutil.relativedelta import relativedelta
So I found a solution to the problem. Pyinstaller was pulling the wrong libpng16.16.dylib into the dist folder that was created. I used
brew install libpng
to get the latest version and then went to the folder
usr/local/cellar/libpng/1.6.37/lib
and copied libpng16.16.dylib and moved it to the dist folder and that resolved the issue.

Problems using webcam in python, openCV

File "C:\Users\Rahul\AppData\Local\Programs\Python\Python36\lib\site-packages\cv2\__init__.py", line 3, in <module>
from .cv2 import *
ImportError: numpy.core.multiarray failed to import
I uninstalled opencv-python and re-installed it again
Ensure that you do not have conflicting versions of numpy in different locations in storage as this seems that the issue is not cv2 but numpy, which is needed when making use of the opencv library

How can I Fix an ImportError

So I am stuck on how can I fix the import error for imresize. I installed all the things I needed like installing Pillow but I haven't been able to find a solution. Also the code that I trying to run is from this github link https://github.com/CSAILVision/IBD
Also side note I do not own this code nor was not the original arthor of it. This is for a research project that I am apart of. In addition I try asked the original arthor with a fix but haven't heard back from them at all.
Installing Pillow, Replacing with code: from scipy.misc.pilutil import imread, installing Pillow-3.3.1-cp27-cp27m-win32.whl, Unninstall Pil and installing Pillow, Reinstalling Pillow.
from util.image_operation import *
from PIL import Image
import numpy as np
from imageio import imresize, imread
from visualize.plot import random_color
from torch.autograd import Variable as V
import torch
I expect it to run but print out the image but it doesn't.
Traceback (most recent call last):
File "test.py", line 4, in <module>
from loader.model_loader import loadmodel
File "/home/joshuayun/Desktop/IBD/loader/model_loader.py", line 5, in <module>
from util.feature_operation import hook_feature, hook_grad
File "/home/joshuayun/Desktop/IBD/util/feature_operation.py", line 6, in <module>
from imageio import imresize, imread, imsave
ImportError: cannot import name 'imresize'
If you want to work with the code as is, i would recommend creating a new environment, and installing an old version of scipy (0.19.1 should do the trick). In your new environment, assuming you are using conda, do:
conda install scipy==0.19.1
If you use pip instead:
pip install scipy==0.19.1
Since scipy 1.3.0rc1 resize has been removed.
I had the same problem, I had scipy 1.4. I did the following and it worked -
pip install scipy==1.1.0

Categories

Resources