Cannot access built-in webcam Python - python-3.x

I'm trying to access my laptop webcam and from what I have seen, I need to use the cv2 package. I downloaded it from PyPi but the contents of the file seem to be missing a lot of content as it only has __init__.py, cv2.cp36-win32.pyd, and opencv_ffmpeg320.dll. So whenever I try use things such as VideoCapture and imwrite, it says it cannot find a reference to them.
When I run the application, I get the following error message:
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File "C:/Users/RedCode/PycharmProjects/FunApps/ImageFile.py", line 1, in <module>
import cv2
File "C:\Users\RedCode\AppData\Local\Programs\Python\Python36-32\lib\site-packages\cv2\__init__.py", line 7, in <module>
from . import cv2
ImportError: numpy.core.multiarray failed to import
I have got the numpy package installed and imported but it didn't fix the issue so I'm guessing that's not the actual cause of the problem, so I'm certain it must have something to do with the cv2 package.
The code I'm using right now is as follows:
import cv2
import numpy
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
cv2.destroyWindow("preview")
I'm also using Python 3.6 and Windows 10 if that helps with anything.
How can I get my application to work?

In your case you can solve your problem by using the following command:
pip install -U numpy
Check existing package:
pip show numpy
And upgrade it via:
pip install numpy --upgrade
Check the path if you still got error:
> import numpy
> print numpy.__path__

You must install the latest version of numpy. Use the command pip install numpy --upgrade or you can just download the wheel file from http://www.lfd.uci.edu/~gohlke/pythonlibs/ . Just check for your python version and platform and download the necessary wheel file. Next use pip to install the wheel file.

Related

Python package pypdf2 is "already installed" but can not import it

I am a newbie with Python and I am going around in circles over python's inability to find a mod. In jupyter notebook here is what I get:
pip install pypdf2
Requirement already satisfied: pypdf2 in c:\users\stanleydenman\appdata\local\programs\python\python311\lib\site-packages (3.0.1)
Note: you may need to restart the kernel to use updated packages.
import pypdf2
import pypdf2
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[2], line 1
----> 1 import pypdf2
ModuleNotFoundError: No module named 'pypdf2
So python acknowledges pypdf2 is installed, but cannot find it to import it? I
I have installed, and reinstalled python. I selected the option once to have environmental variables set, other times set them myself. Nothing makes a difference.
Environmental variable set as follows:
C:\Users\StanleyDenman\AppData\Local\Programs\Python\Python311\Scripts\site-packages;C:\Users\StanleyDenman\AppData\Local\Programs\Python\Python311\;C:\Users\StanleyDenman\AppData\Local\Microsoft\WindowsApps;C:\Users\StanleyDenman\AppData\Roaming\npm;C:\Users\StanleyDenman\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Tesseract-OCR;
Tried with Anaconda, cmd line, and jupyter notebook. All the same result.
Try pip list to make sure it's installed.

Import NumPy fails using Windows despite it being installed

I just managed to get this figured out but i couldn't find a post about my specific problem so here it is.
I had installed numpy using
py -m pip install NumPy
I got the following error when I tried to import it
>>> import NumPy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'NumPy'
I solved my problem by unistalling NumPy and re-installing it with all LOWER-CASE LETTERS i.e.
py -m pip install numpy
Now I can import it with lower case letters
>>> import numpy
>>>
hope this can help someone else save an hour.

import cv2 doesn't give error on command-Prompt but error on IDLE on Windows 10, Python 3.6.4

I have Installed Python 3 (32bit) on Windows.
I have read this answer, but I can't install cv2 using
pip install opencv_python-3.4.1-cp37-cp37m-win32.whl and it gives error as
opencv_python-3.4.1-cp37-cp37m-win32.whl is not a supported wheel on this platform.
when I tried Python shell, and run the command import cv2 it doesn't give error,
but when I try it as import cv2 in IDLE terminal, then It gives error as
>>> import cv2
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'
You could also install opencv in these three easy steps
Type py -m pip install opencv-python
In the same window type python or open IDLE python.
Now type import cv2.
there you go... simple and easy i hope it helps.
1)Download OpenCV from here
2)Extract the zip file to Root drive(mostly 'C'Drive)
3)Goto folder opencv\build\python\2.7\x86 and copy cv2 file to your main Python directory,
in my case Python directory is
C:\Users\Deshmukh Brothers\AppData\Local\Programs\Python\Python36-32\Lib\site-packages
and paste cv2 file there.
4)Run command as python -m idlelib and now it will prompt idle-shell.
5)now it will work as import cv2, it nothing occurred and cursor went to next line then it is successfully installed.

Scipy install help windows 10

I am trying to get scipy running on my computer and I have been fighting for the last few hours with it. Currently, im running python 3.6 and I am on windows 10. I first tried to install it using pip which failed no matter what I did. Then I went and downloaded the wheel file and installed it manually using pip. Then I used pip to get the rest of the dependencies like the website stated pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose. It installed the rest of the packages just fine.
If I say in IDEL
import scipy
It will give me an error of
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
from scipy import special, optimize
File "C:\Users\Kevin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\__init__.py", line 116, in <module>
from scipy._lib._ccallback import LowLevelCallable
File "C:\Users\Kevin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\scipy\_lib\_ccallback.py", line 1, in <module>
from . import _ccallback_c
ImportError: cannot import name '_ccallback_c'
if I go in that file to comment out that line, it will import but it after that I get a lot more errors anytime I try and do anything.
Also, I tried installing anaconda as a dependency manager and that seemed to work fine for installing, but if I tried to used Atom or IDLE as my text editor it can't seem to find scipy when its compiling and throws and error. I am kinda at a loss and any advice or help on this would be much appreciated. Thank you in advance.

Unable to use pypdf module

I have installed the pyPdf module successfully using the command pip install pydf but when I use the module using the import command I get the following error:
enC:\Anaconda3\lib\site-packages\pyPdf\__init__.py in <module>()
1 from pdf import PdfFileReader, PdfFileWriter
2 __all__ = ["pdf"]
ImportError: No module named 'pdf'
What should I do? I have installed the pdf module as well but still the error does not go away.
This is a problem of an old version of pypdf. The history of pypdf is a bit compliated, but the gist of it:
Use pypdf>=3.1.0. All lowercase, no number. Since December 2022, it's the best supported version.
Install pypdf
$ sudo -H pip install pypdf
You might need to replace pip by pip2 or pip3 if you use Python 2 or Python 3.
Use pypdf
import pypdf
WARNING: PyPDF3 and PyPDF4 are not maintained and PyPDF2 is deprecated - pypdf is the way to go!
Three potential alternatives which are maintained:
pymupdf: uses mupdf
pikepdf: Uses qpdf
pdfminer.six: A pure Python project. Don't confuse it with the unmaintained pdfminer
I've had the same error popping up after installing pypdf via pip and trying to import it in IPython (I'm using python 3.5.2):
In [5]: import pyPdf
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-5-a5780a4295f9> in <module>()
----> 1 import pyPdf
/home/mf/virtual_envs/pdfdataextract/lib/python3.5/site-packages/pyPdf/__init__.py in <module>()
----> 1 from pdf import PdfFileReader, PdfFileWriter
2 __all__ = ["pdf"]
ImportError: No module named 'pdf'
This was even after installing the pdf library using pip.
Luckily, there's a PyPDF2 library which works like a charm for me.
Use PyPDF2.
I've been using it in Python 3 (v3.5.2 to be precise), and it works quite well.
Here's a simple command that you can use to install PyPDF2.
sudo -H pip3 install PyPDF2
For using it:
from PyPDF2 import PdfFileReader
Let me know if you need any clarification.
Firstly, in your code you wrote:
from pdf import PdfFileReader, PdfFileWriter
Instead of:
from PyPDF2 import PdfFileReader, PdfFileWriter
Secondly use
pip3.x install pyPdf
instead of
pip install pyPdf if it will not work
I use pypdf2 , it work for me.
pip install pypdf2.
I use Ubuntu 16.04
I imported PDF library using python 3.8.5 as;
import PyPDF4
or
from PyPDF4 import PdfFileReader
it runs great.....
Your import code should read:
from pyPdf import PdfFileReader, PdfFileWriter

Resources