Error, but nothing seems to be wrong? cv2 - python - python-3.x

I followed a tutorial line by line on YouTube, and it worked. As this is my first time doing recognition stuff
I haven't changed anything and now it's giving me an error.
import cv2
url = 'http//192.168.1.88:4747/video'
cap = cv2.VideoCapture(url)
face_cascade = cv2.CascadeClassifier("Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml")
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # <----------THIS LINE IS GIVING ME THE ERROR
face = face_cascade.detectMultiScale(gray, 1.5, 5)
for(x,y,w,h) in faces:
print(x,y,w,h)
cv2.rectangle(gray, (x,y), (x+w, y+h), (255,0,0), 5)
cv2.imshow('Window', frame)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cv2.destroyAllWindows()
I've tried deleting the whole code-block and rewriting it but I still get the error. Its like that saying, only an idiot does the same thing over and over expecting a different result.
Here is the error:
Traceback (most recent call last):
File "C:/Users/Tomas/PycharmProjects/Webcam Phone/venv/Webcam.py", line 9, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.3.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

You're seeing what happens when None gets passed to cvtColor()
You need either test ret, or frame is not None. If you're using a laptop webcam, it's not uncommon in my experience that it take a few frames for the camera to 'warm up' and return images.

CREDIT TO - mkrieger & fmw42 - their comments helped my work out why is was returning nothing.
Missing : in the URL
Caused the cap = cv2.VideoCapture(url) function to not receive any frames from the webcam, so ret, frame = cap.read() just returned False and None

Related

CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -2147483638 in python opencv

I have made a face detection program but it's giving me three errors and I tried to solve them through various sites but couldn't find a clear solution
import cv2
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,127,255),2)
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
The above code is giving me an error like this
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-m9hy83n6\opencv\modules\videoio\src\cap_msmf.cpp (912) CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -2147483638
Traceback (most recent call last):
File "C:\Users\Lenovo\Desktop\code material\Rough.py", line 9, in <module>
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-m9hy83n6\opencv \modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-m9hy83n6\opencv\modules\videoio\src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
please can someone help me with this

Opencv in python gives me a AttributeError

Im trying to get opencv working for a face recognition program. My webcam is working since I tested it using cv2.VideoCapture and the window popped up, then when i added the next step which is turning the image gray using cv2.cvtcolor(frame, cv2.COLOR_BGR2GRAY) I got the error message saying AttributeError: module 'cv2.cv2' has no attribute 'cvtcolor. I tried uninstalling and reinstalling opencv using pip3 install opencv-contrib-python. Im not sure what im doing wrong here, my guess is maybe my files arnt in the write place but im not sure where they would need to be
When I use this code my window of my webcam pops up and works fine.
Working code
import os
import PIL
import cv2
import numpy
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
"haarcascade_frontalface_alt2.xml")
cap = cv2.VideoCapture(0)
while(True):
#frame by frame
ret, frame = cap.read()
gray = cv2.cvtcolor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiscale(gray, scaleFactor=1.5,
minNeighbors=5)
#display frame
cv2.imshow('frame',frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
#when done relase cap
cap.release()
cv2.destroyAllWindows()
Traceback (most recent call last):
File "c:/Users/Nick Miller/all_for_vs/Code/facerecg.py", line 14, in
gray = cv2.cvtcolor(frame, cv2.COLOR_BGR2GRAY)
AttributeError: module 'cv2.cv2' has no attribute 'cvtcolor'
[ WARN:0] terminating async callback
Change
cv2.cvtcolor(frame, cv2.COLOR_BGR2GRAY) to cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY).
Also, change faces = face_cascade.detectMultiscale(gray,scaleFactor=1.5,minNeighbors=5) to faces = face_cascade.detectMultiScale(gray,scaleFactor=1.5,minNeighbors=5)

Bitwise_and function returning error assertion failed and mask is samezise

I'm trying to program a script that cuts out your face and makes all dark pixels become completely black. I begin by using a haarcascade to identify a face. Then I create a roi(region of interest) out of the coordinates from the haarcascade. After that I use an in range filter to select dark pixels and create a mask from that. Lastly, but not least I apply this filter to the cropped image to paste in my face.
The problem is that when I use bitwise_and function it returns an error. I can't understand the error, but when I've searched I've found people with a similar problem. The problem for me is that the answer is quite often not understandable or is just a reformatting of the code. I therefore wonder if you, the people of the internet, can answer in a way that makes it understandable for future readers(and also help me correct the code XD).
This is my code:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while(True):
#get's the cameras video
_ , img = cap.read()
#Converts to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(gray,1.3,5,1)
if len(face) > 0:
for (x,y,w,h) in face:
roi = img[y:y+h, x:x+w]
white = (255)
grey = (75)
#Finds dark pixel
mask = cv2.inRange(gray,grey,white)
res = cv2.bitwise_and(roi,roi,mask=mask)
cv2.imshow('img',res)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
and this is the error:
Traceback (most recent call last):
File "C:\Users\molta\Documents\Pythons\Hologram2.py", line 23, in <module>
res = cv2.bitwise_and(roi,roi,mask=mask)
cv2.error: OpenCV(4.1.0) C:\projects\opencv-
python\opencv\modules\core\src\arithm.cpp:245: error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) &&
_mask.sameSize(*psrc1) in function 'cv::binary_op'
You are getting above error because size of mask is greater than region of interest in your case. You are using roi which is a cropped version of the original image(cropped face) while mask is of size equal to the grayscaled version of the original image. They should be of same size. I think it should be something like below:
if len(face) > 0:
for (x,y,w,h) in face:
roi = img[y:y+h, x:x+w]
img_gray = gray[y:y+h, x:x+w]
white = (255)
grey = (75)
# Finds dark pixel
mask = cv2.inRange(img_gray,grey,white)
res = cv2.bitwise_and(roi,roi,mask=mask)

Hi friends i just now installed opencv and checking the basic code but it results in error. The code is :

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',frame)
cv2.imshow('gray',gray)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()`
Traceback (most recent call last):
File "C:\Users\Kouissi\Desktop\camera-test\camera_test.py", line 11, in <module>
gray = cv2.cvtColor('frame', cv2.COLOR_BGR2GRAY)
TypeError: Expected cv::UMat for argument 'src'
You need to change:
gray = cv2.cvtColor('frame', cv2.COLOR_BGR2GRAY)
to:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
In this case the frame variable is a numpy array.

Build Error For cv2.imshow()

Was using opencv to capture a preexisting video. The video pops up on a frame
buts ends in the following error:
cv2.error: D:\Build\OpenCV\opencv-3.2.0\modules\highgui\src\window.cpp:312: error: (-215) size.width>0 && size.height>0 in function cv::imshow
My code is
import numpy as np
import cv2
cap = cv2.VideoCapture('lol.avi')
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
PS: I do have ffmpeg in windows and environment path set to it.

Resources