import cv
import cv2
cap = cv2.VideoCapture(0)
cap.set(8,100)
out = cv2.VideoWriter('/home/pi/Desktop/output.mp4',cv2.cv.CV_FOURCC('D','I','V','X'),20.0,(640,480))
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(10) == 27:
break
cv2.VideoCapture(0).release()
out.release()
cv2.destroyAllWindows()
This code worked but it never stopped and no video file had been saved. Does anyone know how to solve this? Thanks a lot.
Both release() calls can be removed.
cv2.VideoCapture(0).release()
would call release() on a new VideoCapture, what you meant was cap.release().
For the VideoWriter, the release method doesn't exist - you don't have to care about releasing the VideoWriter or VideoCapture in Python. They will be released when their object is destroyed at the end of your program.
Related
i have a simple webcam opening using cv2 module:
cap=cv2.VideoCapture(0)
while True:
ret, img = cap.read()
cv2.imshow('webcam',img)
k=cv2.waitKey(10)
if k == 27:
break
cap.release()
cv2.destroyWindows()
this program runs for ever, although i try to close it, the only way is closing vsCode
You can close the webcam window by clicking on your webcam window and press esc while if you want to close it with specific alphabet then you can use the below code to destroy the window by pressing q.
import cv2
cap=cv2.VideoCapture(0)
while True:
ret, img = cap.read()
cv2.imshow('webcam',img)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I am using below code to access my laptop camera or usb webcam but it doesn't work. I am not sure how to debug this to get to root cause of this issue. Code and output:
import cv2
vid = cv2.VideoCapture(0)
print (vid)
print (vid.isOpened())
while(vid.isOpened()):
ret, frame = vid.read()
print (ret)
print (frame)
if not ret:
break
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
vid.release()
cv2.destroyAllWindows()
The output of above is
<VideoCapture 0000000002DAE970>
True
False
None
When the cv2.VideoCapture(0) is executed I can see my laptop's camera light lit up and same happens when cv2.VideoCapture(1) is executed, I can see my webcam's light lit up. I am not sure why ret, frame = vid.read() ret is being return as False and image as None.
Please note that both cameras work in skype and other utilities. Even on VLC as well. I have checked in my device manager and they both show up. I am running this on Windows 7, openCV version is 4.5.5 and Python version is 3.8.6
Regards,
Yasar
import cv2
cap= cv2.VideoCapture(0)
while True:
ret,frame= cap.read()
cv2.imshow('Our live sketch',frame)
if cv2.waitKey(1)==13:
break
cap.release()
When I use cv2.VideoCapture(1), the programs shows error but the program works properly if I use cv2.VideoCapture(0)
That's the index of the camera it is used to select different cameras if you have more than one attached. By default 0 is your main one.
I just began to learn image processing using python 3.5, in Ubuntu. As I began to learning processing video using the webcam I got stuck with an AttributeError. error is module 'cv2.cv2' has no attribute 'Videocapture'
the code I used is
import cv2
import numpy as np
cap = cv2.Videocapture(0)
while True:
ret,frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Thank you in advance.
You have a typo, the object is called cv2.VideoCapture (note the capital C) and Python is case-sensitive.
I'm beginner learning opencv from the official documentation http://docs.opencv.org/trunk/doc/py_tutorials/py_gui/py_video_display/py_video_display.html#display-video
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
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()
It's giving me error "Segmentation fault (core dumped)"
Can anyone please tell me Why is that happening and how to resolve that issue?
Thanks in advance.
Maybe its a little late but what "user3154952" says is true, when you are working with the C++ api you don't need to use the release method, it is already in the video capture Destructor.
This is the code i tested and worked fine:
import sys
import cv2
cap = cv2.VideoCapture(0)
while(1):
ret,frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == 27:
break
cv2.destroyAllWindows()
Update:
i have been messing around with my ps3 eye and i've realized that with that camera you get the segmentation error for using only the destroyAllWindows method, to fix that i replaced the destroyAllWindows method with the release method and worked fine, i don't know exactly why that happened i'm just sharing in case someone get that issue. I hope that was helpful.