I am getting issues in reading an image using opencv in Python.Can someone please help??
What I had done??
Installed opencv in Anaconda and using jupyter notebook for running the code.I am using python3. Below is the code:
img =cv2.IMREAD("C:/Users/u266772/Desktop/Video/maddy.jpg",cv2.IMREAD.GRAYSCALE)
cv2.imshow ('image',img)
cv2.waitkey(0)
cv2.destroyAllWindows()
The error:
AttributeError Traceback (most recent call
last) in ()
----> 1 img = cv2.IMREAD("C:/Users/u266772/Desktop/Video/maddy.jpg",cv2.IMREAD.GRAYSCALE)
2
3 cv2.imshow ('image',img)
4 cv2.waitkey(0)
5 cv2.destroyAllWindows()
AttributeError: module 'cv2' has no attribute 'IMREAD'
The correct usage is
img=cv2.imread('4.jpg',cv2.IMREAD_GRAYSCALE)
the imread function is lowercase. And its IMREAD_GRAYSCALE not '.'
Python is a case sensitive language.you used IMREAD function which is not a part of opencv. But for reading images you should use imread function.because of this you got that error.
AttributeError: module 'cv2' has no attribute 'IMREAD'.
And the last thing always see the type of error.and google it .after that you should post questions on forums to get an answer.good luck!!!!
For 64 bit machines, cv2.waitkey(0) will throw an error.
Instead, you should use
cv2.waitKey(0) & 0xFF
The below code is working fine now:
img = cv2.imread("C:/Users/u266772/Desktop/Video/photo1.jpg",1)
cv2.imshow ('image',img)
cv2.waitKey(0) & 0xFF
cv2.destroyAllWindows()
Related
I'd like to use cv2 with a Desktop PC that I build myself. I've bought a USB webcamera and successufuly installed it since it works smoothly when I access it. My probem is that it seems that cv2 is not able to open my camera. This is the error I'm getting:
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor
So I've tried using various index (from -1 to 5) in this line of code:
cap = cv2.VideoCapture(0)
But nothing changed, I've also tried to use:
cd /dev
ls video
But this is the error I'm getting:
ls: cannot access 'video': No such file or directory
Is there a way to fix this problem?
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
Before this line of code, did you also write something like cv2.imread(...)? I experienced the error exactly the same with yours when I mistakenly put a wrong image address in the cv2.imread(), so my advice is to double check if you pass a correct image address if there is any. Best:)
I expected to receive a video feed in my laptop from my phone. In my opinion, the code is correct. The same code is running on other devices. But when I tried doing the same in my laptop, it displayed the following error:
[ERROR:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\videoio\src\cap.cpp (142) cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised OpenCV exception:
OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): http://192.168.1.3:8080/video in function 'cv::icvExtractPattern'
Since I am a newbie to the world of opencv, I could'nt understand what caused the error. Hence, I did'nt try anything worthwhile other than changing a few lines of code.
My code is:
import cv2
url= 'http://192.168.1.3:8080/video'
cap=cv2.VideoCapture(url)
cap.set(3,240)
cap.set(4,240)
while(True):
ret,frame=cap.read()
if frame is not None:
cv2.imshow('frame',frame)
q=cv2.waitKey(1)
if q==ord("q"):
break
cv2.destroyAllWindows()
cap.release()
I had encountered the same issue but with a a saved video file as the input.
The file path I had entered did no exist and I got the same error.
I would suggest to check if your have given the right permissions to for accessing your phone's camera and if the feed is live and is returning images in the correct format.
I have been working on this error for a while but can't resolve it.
This is my code
import cv2
import numpy as np
import time
cam = cv2.VideoCapture(0)
time.sleep(2)
while True:
ret,frame = cam.read()
cv2.imshow('webcam', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
And this is the error it is giving me.
[ERROR:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap.cpp (193) cv::VideoCapture::open VIDEOIO(DSHOW): raised unknown C++ exception!
Traceback (most recent call last):
File "C:\Users\VVA\Desktop\test.py", line 10, in
cv2.imshow('webcam', frame)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
just use different index number
On my macbook the reason this happens seem to be that the internal webcam is index number 1, whereas the index number 0 seems to be the "camtwist" webcam driver.
The answer "just use a different index number" is correct. However it's not very satisfying - if you want to write portable code, what is the way to determine the correct index number of the internal webcam of the device? Better do a loop through all devices?
I am on ubuntu 14.04.02, i have python, cython and numpy installed and updated.
i pulled the latest sources of open cv from http://github.com/itseez/opencv, compiled according to the documentation...
when trying to run the python source i pulled from https://github.com/shantnu/FaceDetect/
it's giving me the following error :
modprobe: FATAL: Module nvidia not found.
Traceback (most recent call last):
File "face_detect.py", line 21, in
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
AttributeError: 'module' object has no attribute 'cv'
to make sure i have the python bindings i typed the following in the terminal:
python
import cv2
cv2.__version__
it returned the following
'3.0.0-dev'
what could be wrong with it?
the cv2.cv submodule got removed in opencv3.0, also some constants were changed.
please use cv2.CASCADE_SCALE_IMAGE instead
(do a help(cv2) to see the updated constants)
Apologies for the bump, but the above did not work for me, and I found an alternate "solution", but it may have unwanted side effects, given I know SFA about openCV.
The simple solution is just set it to 0.
# Detect faces in the image
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
# flags = cv2.cv.CV_HAAR_SCALE_IMAGE
flags = 0
)
As you can see... i just set it to 0 and could move on with my life.
I tried all number of combinations, and I couldn't get the CASCADE_SCALE_IMAGE working.
This openCV doco explaination gives me nadda, zip, nothing but confusion.
flags – Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.
That cleared it up...
Anyway, the example on openCV hard codes it to 0.
I wrapped opencv today with simplecv python interface. After going through the official SimpleCV Cookbook I was able to successfully Load, Save, and Manipulate images. Thus, I know the library is being loaded properly.
However, under the Using a Camera, Kinect, or Virtual Camera heading I was unsuccessful in running some commands. In particular, mycam = Camera() worked, but img = mycam.getImage() produced the following error:
In [35]: img = mycam.getImage().save()
OpenCV Error: Bad argument (Array should be CvMat or IplImage) in cvGetSize, file /home/jordan/OpenCV-2.2.0/modules/core/src/array.cpp, line 1237
---------------------------------------------------------------------------
error Traceback (most recent call last)
/home/simplecv/<ipython console> in <module>()
/usr/local/lib/python2.7/dist-packages/SimpleCV-1.1-py2.7.egg/SimpleCV/Camera.pyc in getImage(self)
332
333 frame = cv.RetrieveFrame(self.capture)
--> 334 newimg = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 3)
335 cv.Copy(frame, newimg)
336 return Image(newimg, self)
error: Array should be CvMat or IplImage
I'm running Ubuntu Natty on a HP TX2500 tablet. It has a built in webcam, (CyberLink Youcam?) Has anybody seen this error before? I've been all over the web today looking for a solution, but nothing seems to be doing the trick.
Update 1: I tested cv.QueryFrame(capture) using the code found here in a separate Stack Overflow question and it worked; so I've pretty much nailed this down to a webcam issue.
Update 2: In fact, I get the exact same errors on a machine that doesn't even have a webcam! It's looking like the TX2500 is not compatible...
since the error raised from Camera.py of SimpleCV, you need to debug the getImage() method. If you can edit it:
def getImage(self):
if (not self.threaded):
cv.GrabFrame(self.capture)
frame = cv.RetrieveFrame(self.capture)
import pdb # <-- add this line
pdb.set_trace() # <-- add this line
newimg = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 3)
cv.Copy(frame, newimg)
return Image(newimg, self)
then run your program, it will be paused as pdb.set_trace(), here you can inspect the type of frame, and try to figure out how get the size of frame.
Or you can do the capture in your code, and inspect the frame object:
mycam = Camera()
cv.GrabFrame(mycam.capture)
frame = cv.RetrieveFrame(mycam.capture)
To answer my own question...
I bought a Logitech C210 today and the problem disappeared.
I'm now getting warnings:
Corrupt JPEG data: X extraneous bytes before marker 0xYY.
However, I am able to successfully push a video stream to my web-browser via JpegStreamer(). If I cannot solve this error, I'll open a new thread.
Thus, for now, I'll blame the TX2500.
If anybody finds a fix in the future, please post.
Props to #HYRY for the investigation. Thanks.
I'm geting the camera with OpenCV
from opencv import cv
from opencv import highgui
from opencv import adaptors
def get_image()
cam = highgui.cvCreateCameraCapture(0)
im = highgui.cvQueryFrame(cam)
# Add the line below if you need it (Ubuntu 8.04+)
#im = opencv.cvGetMat(im)
return im
Anthony, one of the SimpleCV developers here.
Also instead of using image.save(), this function writes the file/video to disk, you instead probably want to use image.show(). You can save if you want, but you need to specify a file path like image.save("/tmp/blah.png")
So you want to do:
img = mycam.getImage()
img.show()
As for that model of camera I'm not sure if it works or not. I should note that we also wrapper different camera classes not just OpenCV, this is because OpenCV has a problem with webcams over 640x480, we now can do high resolution cameras.
Also I should mention, which I didn't realize, is that OpenCV less than 2.3 is broken with webcams on Ubuntu 11.04 and up. I didn't realize this as I was running Ubuntu 10.10 before, by the looks of your output you are using python 2.7 which makes me think you are on Ubuntu 11.04 or higher. Anyway, we have a fix for this problem. It is now pushed up into the master, it basically does a check to see if OpenCV is working, if not it will fall back to pygame.
This fix will also be in the 1.2 release of SimpleCV (It's in the master branch now)