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:)
Related
I'm using the CascadeClassifier of the opencv-python package to perform face detection with the haarcascade_frontalface_default.xml with this code:
self.face_cascade = cv2.CascadeClassifier(haar_cascade_path)
...
gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(gray_frame, 1.3, 5)
It's working fine for most of the frames, but sometimes I'm getting this exception:
cv2.error: OpenCV(4.6.0) d:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.hpp:46: error: (-215:Assertion failed) 0 <= scaleIdx && scaleIdx < (int)scaleData->size() in function 'cv::FeatureEvaluator::getScaleData'
The error occurs during the detectMultiScale call. I already checked the gray_frame and it looks good (shape is 1366x1060 and it's not none or something like that). Do you have any idea on how the fix this?
I actually managed to solve this issue. What caused this error to happen was, that I'm preprocessing the dataset multithreaded and I only used a single instance for multiple workers concurrently. It seems like the CascadeClassifier (or at least the detect method) isn't thread-safe and feeding images with a different size concurrently caused the problem. Creating one instance for each thread/worker solved the issue.
I am making a program in python that just draws a rectangle around a car. I am currently stuck on getting the coordinates of the car, here is the code:
#################################################
import cv2
#################################################
car_data = cv2.CascadeClassifier(cv2.data.haarcascades + "cars.xml")
img = cv2.imread("car_front.jpeg")
#################################################
img_but_bnw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
car_coordinates = car_data.detectMultiScale(img_but_bnw)
print(car_coordinates)
#################################################
cv2.imshow("Detect Everything", img_but_bnw)
cv2.waitKey()
print("Code Completed")
#################################################
I am running in on an error with the function "cv2.detectMultiScale".
error:
File "e:\Python2\Body_Detection.py", line 11, in <module>
car_coordinates = car_data.detectMultiScale(img_but_bnw)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
I have tried googling, it says to use cv2.CascadeClassifier(cv2.data.haarcascades + "cars.xml") instead of cv2.CascadeClassifier("cars.xml"). It didn't work :(, Any help would be appreciated.
The file cars.xml is not part of the opencv library although you may find tutorials in the internet that use this filename. The folder addressed by cv2.data.haarcascades includes xml examples for things like eye and face detection (current content see https://github.com/opencv/opencv/tree/master/data).
You can search for an existing cars.xml example by other authors and copy it to your project folder. Then just use "cars.xml" without cv2.data.haarcascades.
E.g. I found this project Vehicle Detection with Haar Cascades that includes a file cars.xml working fine with your code above.
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 trying to use epiphan SDI2USB with ubuntu 18.04. I downloaded the correct driver from their support site and the driver is correctly installed. My kernel version is 4.15.0.
If I try to run vlc and i go to media menu --> Open Capture Device --> video device name --> Here I see both my /dev/video0 (integrated camera) and /dev/video1 (epiphan video) When I select /dev/video1 and click play, I can see it works fine.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
However when I write a simple python program from internet (i changed source from 0 to 1) and run it, I get an error
compass-admin#Alienware-13-R3:~$ sudo python3 cam.py
Insufficient buffer memory on /dev/video1 – decreasing buffers
Insufficient buffer memory on /dev/video1 – decreasing buffers
mmap: Invalid argument
munmap: Invalid argument
VIDEOIO ERROR: V4L: can’t open camera by index 1
munmap: Invalid argument
Traceback (most recent call last):
File “/home/compass-admin/.local/lib/python3.6/site-packages/numpy/lib/shape_base.py”, line 843, in split
len(indices_or_sections)
TypeError: object of type ‘int’ has no len()
I dont know if it is because of permissions or if you have to use a specific function in opencv. No matter what I do, I get the same error.
I changed permissions of /dev/video1 to 777, I also added the current user to video group but no luck. I also tried video source in opencv as 0, 1, -1 but no luck. Any help please.