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.
Related
A simple program to show the feed from my webcam is running fine. I'm getting the error only when I try to run cv2.BackgroundSubtractorKNN() within the loop.
I have applied the following fix:
Uninstalled the latest version of OpenCV (which I was using) and installed an older version 4.5.4. But the error still persists.
Here's my code and the corresponding messages in the terminal.
import cv2
cap = cv2.VideoCapture(0)
mog = cv2.BackgroundSubtractorKNN()
while(True):
ret, frame = cap.read()
fgmask = mog.apply(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Message in terminal
PS D:\Python ground up\Open_CV> python -u "d:\Python ground up\Open_CV\backgroundsub.py"
Traceback (most recent call last):
File "d:\Python ground up\Open_CV\backgroundsub.py", line 7, in <module>
fgmask = mog.apply(frame)
cv2.error: Unknown C++ exception from OpenCV code
I think it's createBackgroundSubtractorKNN not BackgroundSubtractorKNN
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.
import numpy as np
import cv2
capture = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
output = cv2.VideoWriter('output.avi',fourcc,20.0,(640,480))
while(1):
ret,frame = capture.read()
cv2.imshow('webcam',frame)
output.write(frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
capture.release()
output.release()
cv2.destroyAllWindows()
I use this code but the error sign appeared.
it said
Traceback (most recent call last): File "C:/Python27/8.09_kk.py",
line 5, in
fourcc = cv2.VideoWriter_fourcc(*'XVID') AttributeError: 'module' object has no attribute 'VideoWriter_fourcc'
but I can't understand this one how can I handle it?
You are using older version of OpenCV(version 2.4). Try fourcc = cv2.cv.CV_FOURCC(*'XVID')instead of fourcc = cv2.VideoWriter_fourcc(*'XVID'). It will work.
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 "\".
I am trying to save a background subtracted video in python and the following is my code.
import cv2
import numpy as np
capture = cv2.VideoCapture('MAH00119.mp4')
size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*'X264')
out = cv2.VideoWriter('output.mp4', -1 , 20.0 , size)
fgbg= cv2.createBackgroundSubtractorMOG2()
while True:
ret, img = capture.read()
if ret==True:
fgmask = fgbg.apply(img)
out.write(fgmask)
cv2.imshow('img',fgmask)
if(cv2.waitKey(27)!=-1):
break
capture.release()
out.release()
cv2.destroyAllWindows()
However, this keeps throwing the following error: "OpenCV: FFMPEG: tag 0xffffffff/'����' is not found (format 'mp4 / MP4 (MPEG-4 Part 14)')'"
I have FFMPEG installed and have added it to the environment variables. My background subtraction code without having to save to a file works fine, so I know there's nothing wrong with openCV installation. I am stuck at this place. I know that my python doesn't seem to recognize FFMPEG but I don't know what else to do apart from adding FFMPEG to the environment variables.
I am using OpenCV version 3.2 on Windows 10 and Python 2.7.
Any help will be much appreciated!
Modified the code a little bit. It works on my PC with OpenCV 3.2 for Python 2.7 on Windows 10 64-bit.
import cv2
import numpy as np
capture = cv2.VideoCapture('./videos/001.mp4')
size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*'DIVX') # 'x264' doesn't work
out = cv2.VideoWriter('./videos/001_output.mp4',fourcc, 29.0, size, False) # 'False' for 1-ch instead of 3-ch for color
fgbg= cv2.createBackgroundSubtractorMOG2()
while (capture.isOpened()): #while Ture:
ret, img = capture.read()
if ret==True:
fgmask = fgbg.apply(img)
out.write(fgmask)
cv2.imshow('img',fgmask)
#if(cv2.waitKey(27)!=-1): # observed it will close the imshow window immediately
# break # so change to below
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
out.release()
cv2.destroyAllWindows()
Check this for the parameters setting on cv2.VideoWriter().
Hope this help.