Problem using OpenCV and Raspberry PI camera V2 - python

I had been using the following code for the past two months with no problem whatsoever, however, just yesterday I have been getting errors using the code which I will detail as follows. The code in question is used to take photos of objects using the raspberry pi camera.
I initialize the raspberry pi camera v2 using the following code:
cap = cv2.VideoCapture('/dev/video0', cv2.CAP_V4L)
#set dimensions
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 2560)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1440)
After which I take a photo from the camera and write the image to a filename using the following lines of code:
ret, frame = cap.read()
cv2.imwrite(fileName , frame)
I have been getting the following error quite recently:
[ WARN:0#26.652] global /io/opencv/modules/videoio/src/cap_v4l.cpp (1000) tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.
Traceback (most recent call last):
File "/home/pi/Documents/proj_trash_can/main.py", line 55, in <module>
clickPicture(cam_capture, filename)
File "/home/pi/Documents/proj_trash_can/cam.py", line 22, in clickPicture
cv2.imwrite(fileName , frame)
cv2.error: OpenCV(4.5.5) /io/opencv/modules/imgcodecs/src/loadsave.cpp:801: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'

Try this. Worked for me Using Raspberry pi 4, Bullseye. You haven't mentioned what filename is?
import cv2
import numpy as np
cap = cv2.VideoCapture('/dev/video0', cv2.CAP_V4L)
#set dimensions
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 2560)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1440)
while cap.isOpened():
ret, frame = cap.read()
cv2.imwrite('Atharva_Naik.jpg' , frame)
cv2.imshow('Video', frame)
cv2.waitKey(0)
cap.release()
cv2.destroyAllWindows()

Related

PYTHON 2.7 AttributeError: 'module' object has no attribute 'cv'

i'm using this code to record video with a PS3 camera *the code is in spanish by the way that's why i wrote "camara" and not "camera".
i'm using Python 2.7 and opencv 3.4.0.12, i'm assuming that the problem is the version of opencv but i don't think so BUT HONESTLY AT THIS POINT I DON'T KNOW WHAT'S RIGHT OR WRONG, PLEASE HELP.
When i run it i get this errors:
Traceback (most recent call last): File
"C:\Users\carls\OneDrive\Documentos\VIDEO CAMARA PS3.py", line 31, in
camara1() File "C:\Users\carls\OneDrive\Documentos\VIDEO CAMARA PS3.py", line 9, in camara1
fourcc = cv2.cv.CV_FOURCC(*"XVID") AttributeError: 'module' object has no attribute 'cv'
Also when i run the code instead of recording in the PS3 camera, the camera from my laptop turns on.
THIS IS PART OF THE CODE TOO, SORRY!
import numpy as np
import cv2
import random
import time
def camara1():
cap = cv2.VideoCapture(0)
fourcc = cv2.cv.CV_FOURCC(*"XVID")
size1 = (int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) )
out = cv2.VideoWriter('output.avi',fourcc, 20.0, size1)
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
#Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
camara1() #THIS IS IN THE CODE TOO, SORRY. THIS IS THE LAST CODE LINE
Using
fourcc = cv2.VideoWriter_fourcc(*'XVID')
works for me.
Opencv version 3.x supports cv2.VideoWriter_fourcc, whereas cv2.cv.CV_FOURCC was supported by 2.4.x.

Python OpenCV Camera Error

For some reason, I cannot read camera video data using OpenCV and Python 3.
Here is the code I am using:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(1):
_, frame = cap.read()
cv2.imshow('frame',frame)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
This code returns the following error:
Traceback (most recent call last):
File "C:\Path\To\File\VideoTest.py", line 10, in <module>
cv2.imshow('frame',frame)
cv2.error: D:\Build\OpenCV\opencv-3.3.1\modules\highgui\src\window.cpp:339: error: (-215) size.width>0 && size.height>0 in function cv::imshow
The computer is running Windows Server 2012 R2 and has one USB webcam permanently in use in addition to the new one I am trying to read data from. I have tried changing the line cap = cv2.VideoCapture(0) to cap = cv2.VideoCapture(1) and had an identical error.
I tried replicating the error on my laptop using the same code and USB webcam, but it worked perfectly and I was able to stream the video.
**EDIT**
To debug, I ran the following program line-by-line in the Python shell.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
print(cap.read())
cap = cv2.VideoCapture(1)
print(cap.read())
The code outputted (False, None) twice.

learning python with opencv

When I am running face detection code, I am facing following error:
Traceback (most recent call last):
File "E:\python program\s1.py", line 11, in <module> ret, frame=cap.read()
AttributeError: 'numpy.ndarray' object has no attribute 'read'
Most probably when I am running code which have ret, frame=cap.read() this type of code I get error.
Please provide solution.
My full code is this code I have taken from website only:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('..\haarcascade_frontalface_default.xml')
cap=cv2.VideoCapture(0)
scaling_factor=0.5
while True:
ret, frame=cap.read()
frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor,
interpolation=cv2.INTER_AREA)
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face_rects=face_cascade.detectMultiScale(gray, 1.3,5)
for(x,y,w,h) in face_rects:
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0),3)
cv2.imshow('Face Detector', frame)
c=cv2.waitKey(1)
if c==27:
break
cap.release()
cv2.destroyAllWindows()
Have you verified your webcam works? The error tells cv2.VideoCapture(0) object hasn't been created as cap so that it's no cap.read() attribute. You may set webcam no. to cv2.VideoCapture(-1) for a trial if your webcam works with other application.
Apart from the error, the face_cascade data path should be specified in forward slash "/" as (../haarcascade_frontalface_default.xml) or double backslash "\\ as (..\\haarcascade_frontalface_default.xml) in Windows OS instead of backslash "\".

Trouble with raspberry pi and OpenCV

I have a project in raspberry pi and I am using python. However I have a problem with the OpenCV when I am trying to run this code:
`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()`
I get this error:
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/opencv-3.1.0/modules/imgproc/src/color.cpp, line 8000
Traceback (most recent call last):
File "test.py", line 11, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /home/pi/opencv-3.1.0/modules/imgproc/src/color.cpp:8000: error: (-215) scn == 3 || scn == 4 in function cvtColor"
I have Python 3.4.2,OpenCV 3.1.0 and Numpy 1.8.2.
So I found an answer. All I had to do was run this code on my raspberry pi:
sudo modprobe bcm2835-v4l2
Thank you for all the help.

Not able to play .h264 video on OpenCV?

I captured a standard video from camera of Raspberry pi. The codec of the file is h264.
To play the video I do:
import numpy as np
import cv2
cap = cv2.VideoCapture('foo.h264')
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()
Video starts But stops after sometime throwing this error:
Traceback (most recent call last):
File "play_video.py", line 9, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /home/nikhil/Downloads/opencv-2.4/modules/imgproc/src/color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cvtColor
My machine has Ubuntu 12.04. I played an .avi file it plays smoothly. Is the problem with the .h264 or with the OpenCV?
Your loop should check that frame is not-empty, and not that the video was opened successfully - this check should be done just once at the beginning.
When the frame after last is read frame is empty and you are then trying to convert it to gray.

Categories

Resources