Unable to capture Video from Laptop Webcam using OpenCV - python

I wrote some code to generate a live feed from my laptop webcam. I'm using the openCV library for Python 3.7 with Pycharm.
This is my code:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while True:
#capture frame by frame
ret, img = cap.read()
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('test', img)
cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()
cap.release()
This is the error message that I got:
Traceback (most recent call last): File "D:/Python
Programs/OpenCV/VideoCapture.py", line 10, in
cv2.imshow('test', img) cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:627:
error: (-2:Unspecified error) The function is not implemented. Rebuild
the library with Windows, GTK+ 2.x or Cocoa support. If you are on
Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run
cmake or configure script in function 'cvShowImage'
[ WARN:0] terminating async callback

Related

OpenCV Function is not Implemented, Rebuild the Library with windows Error

I am trying the following code:
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
frame = frame[120:120+250,200:200+250, :]
cv2.imshow('Image Collection', frame)
if cv2.waitKey(1) & 0XFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
And I am getting this error message:
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-2-99572a850d8f> in <module>
6 frame = frame[120:120+250,200:200+250, :]
7
----> 8 cv2.imshow('Image Collection', frame)
9
10 # Breaking gracefully
error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1272: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
Can anyone help me with this?

imread throws [WARN] can't open/read file: check file path/integrity

what can i do?
WARN:0#0.726] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('pythonProject6/arrow.jpg'): can't open/read file: check file path/integrity
Traceback (most recent call last):
File "C:\Users\PC2\PycharmProjects\pythonProject6\whatsapp try.py", line 17, in <module>
cv2.imshow("image", img)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-
python\opencv\modules\highgui\src\window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
import cv2
img = cv2.imread("pythonProject6/arrow.jpg")
#print(img.shape)
cv2.imshow("image", img)
cv2.waitkey(0)
first of all, make sure you have defined default folder in python correctly. then check the file and address spelling. if it did not resolve, then try this:(use r and instead of / use //.
import cv2
img = cv2.imread(r"pythonProject6//arrow.jpg")
#print(img.shape)
cv2.imshow("image", img)
cv2.waitkey(0)
If it did not resolve, check your file attribution, and uncheck hidden and read-only. always check you can view the image by windows photo viewer.
I had the same problem using VS Code on macOS. The path was correct, but I was getting the same warning, and the resulting image of cv2.imread was empty.
The issue was solved when I gave Full Disk Access to VS Code. I figured it out because I managed to run the code using my terminal (instead of VS Code terminal).
System Settings -> Privacy & Security -> Full Disk Access
The latest versions of opencv 4.6.x.y and later have the cv2.imshow() issue. I experienced the same. If solution is:
First
pip3 uninstall opencv-python
Second:
pip3 install opencv-python==4.5.5.62

(V4L2: /dev/video0) can't open camera by index

I have an error on my algorithm with Jetson Xavier NX. I try to run code but terminal say that :
[ WARN:0] global /home/arc/opencv_build/opencv/modules/videoio/src/cap_v4l.cpp (890) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
Traceback (most recent call last):
File "realtime_quadrant.py", line 41, in
height = frame.shape
AttributeError: 'NoneType' object has no attribute 'shape'
This means that I have to change 0 in camera = cv2.VideoCapture(0).
So I try
cv2.VideoCapture(1),
cv2.VideoCapture(-1)
cv2.VideoCapture(cv2.CAP_V4L2)
yet I still have the same error. Also I installed V4l2 ( Sudo apt-get install v4l2) How can I do that to run?

How to read image from file directory in Jupyter Notebook using OpenCV?

I am trying to read an image from the laptop in Jupyter notebook using imread() of OpenCV. When I execute the program by specifying the address of the file, it shows the following error:
img2 = cv.imread('D:\Photos\roaches.jpg')
cv.imshow("Cockroaches", img2)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
error Traceback (most recent call last)
<ipython-input-8-645759bb8eb6> in <module>
1 img2 = cv.imread('D:\Photos\roaches.jpg')
----> 2 cv.imshow("Cockroaches", img2)
3 cv.waitKey(0)
4 cv.destroyAllWindows()
error: C:\ci\opencv_1512688052760\work\modules\highgui\src\window.cpp:331: error: (-215) size.width>0 && size.height>0 in function cv::imshow
Screenshot of the notebook snippet
However, when I placed the image in the directory where Jupyter notebook is running, the code worked fine.
How can I read any image from the computer without placing it in the Jupyter notebook directory?

Capturing image from Webcam Using Python on Windows

the code i am using now is :-
from VideoCapture import Device
cam = Device()
cam.saveSnapshot('image.jpg')
using py 2.7
and imported pygame and all and videocapture
i am getting this error in pycharm :-
C:\Python27\python.exe F:/Xtraz/Orion/Key-Logger.py
Traceback (most recent call last):
File "F:/Xtraz/Orion/Key-Logger.py", line 3, in <module>
cam.saveSnapshot('image.jpg')
File "C:\Python27\lib\VideoCapture.py", line 200, in saveSnapshot
self.getImage(timestamp, boldfont, textpos).save(filename, **keywords)
File "C:\Python27\lib\VideoCapture.py", line 138, in getImage
im = Image.fromstring('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)
File "C:\Users\admin\AppData\Roaming\Python\Python27\site-packages\PIL\Image.py", line 2080, in fromstring
"Please call frombytes() instead.")
NotImplementedError: fromstring() has been removed. Please call frombytes() instead.
Process finished with exit code 1
the webcam LED lights onn , and then switches off immediately .
or help me with any other code and library that works well with py 2.7 and pycharm on windows only ! and i just want to save the image , not display it !
You might want to downgrade you version of PIL, it seems like VideoCapture hasn't been updated for a while and is still relying on outdated versions of PIL.
PIL 2.x seems to have a working fromstring method: https://github.com/python-pillow/Pillow/blob/2.9.0/PIL/Image.py#L750
Otherwise you can try to change the line 138 in VideoCapture.py from im = Image.fromstring(...) to im = Image.frombytes(...); hopefully it's the only thing that prevents it from working.
Solution #1: Downgrade PIL
If you're using pip, you can just uninstall you current version using pip uninstall Pillow and then install an older one using pip install Pillow==2.9.0 (Pillow is a fork of PIL, PIL being basically dead).
Solution #2: Update VideoCatpure
Open the file C:\Python27\lib\VideoCapture.py and go to line 138. You should have something like that:
im = Image.fromstring('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)
Replace this line with this:
im = Image.frombytes('RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)

Categories