I have this issue with trying to release my Camera from the python program. It keeps telling me that there is invalid syntax
>>> 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()
Output -:
SyntaxError: invalid syntax on the c of the cap.release
This is IDLE Shell 3.9.4 with opencv_contrib_python-4.5.1., and I use Windows.
Related
I am making a python app i need openCV library here is my code:
# importing the required modules
import cv2
import numpy as np
# capturing from the first camera attached
cap = cv2.VideoCapture(0)
# will continue to capture until 'q' key is pressed
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
# Program will terminate when 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Releasing all the resources
cap.release()
cv2.destroyAllWindows()
when I run the code I got this error:
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wwma2wne\opencv\modules\videoio\src\cap_msmf.cpp (677) CvCapture_MSMF::initStream Failed to set mediaType (stream 0, (640x480 # 30) MFVideoFormat_RGB24(codec not found)
I think may be it requires a codec driver but i don't know how to fix the error
please help me friends
following code resolved this issue for me: cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
try
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
I'm trying to run the most simple script for viewing the laptop's camera in real time. But unfortunately after starting the window shows, but I get only a single frame displayed that doesn't ever get updated.
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
cv2.imshow('test', img)
if cv2.waitKey(-1):
break
cv2.destroyAllWindows()
cap.release()
I was following tutorials for installing this on Windows and installed it on separate environment, using pip and downloaded wheel. The window shows OK, and the image from camera is displayed but static. The program isn't hanged however, because it awaits a key being pressed and closes correctly afterwards.
What can be the cause?
Try using this loop:
while True:
ret, img = cap.read()
cv2.imshow('test', img)
keypressed = cv2.waitKey(30)
if keypressed == ord('q'):
break
The argument of cv2.waitKey(delay) is the delay in millisecond and the returned value is the key pressed:
The function waitKey waits for a key event infinitely (when 𝚍𝚎𝚕𝚊𝚢≤0 ) or for delay milliseconds, when it is positive. [..]
It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.
See: https://docs.opencv.org/4.1.0/d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7
Try this:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I hope this will work it is fast and easy solution.
you can capture image by pressing c and q for quit the window
import cv2
cap = cv2.VideoCapture(0)
count=0
while(True):
ret, frame = cap.read()
cv2.imshow("imshow",frame)
key=cv2.waitKey(30)
if key==ord('q'):
break
if key==ord('c'):
count+=1
cv2.imwrite('/home/user/Desktop/image'+str(count)+'.png', frame)
cap.release()
cv2.destroyAllWindows()
im working with opencv3.4 in python 3.6
and i am trying to capture rtsp stream from my ip camera
code:
if __name__ == "__main__":
cap = cv2.VideoCapture("http://admin::Admin123#192.168.0.10/cgi-bin/mjpeg?stream=1")
while (cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('frame', frame)
k = cv2.waitKey(1)
if k == 27:
cap.release()
cv2.destroyAllWindows()
i had successfully manage to get the stream and display it but if i exit the script and try to run it again i get BSOD (windows blue screen of death)
my suspicion is that opencv release method doesn't manage to release the rtsp connection
someone had this problem and manage to fix it?
thank you !!
I've been trying to run video modules for OpenCV on my machine running Ubuntu 16.04 LTS. I've installed Pycharm. There is no error whatsoever but no video is being displayed.However, it is working perfectly fine with image modules.
import cv2
cap = cv2.VideoCapture('project_video.mp4')
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True:
cv2.imshow('Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
Make sure your indentation is correct like below.
Try to import opencv module in the python shell.
cap = cv2.VideoCapture('project_video.mp4')
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True:
cv2.imshow('Frame', frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
I solved it myself. This happened because both the versions of Python i.e. 2.7 and 3.5 were installed. So one of them has to be uninstalled for the program to work correctly.
May be your code format has problem.
This is after formatted.
import cv2
cap = cv2.VideoCapture('project_video.mp4')
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True:
cv2.imshow('Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
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()
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor,
file /home/pi/opencv-2.4.9/modules/imgproc/src/color.cpp, line 3737
Traceback (most recent call last): File "test.py", line 11, in
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.error: /home/pi/opencv-2.4.9/modules/imgproc/src/color.cpp:3737: error:
(-215) scn == 3 || scn == 4 in function cvtColor
This typically happens to me when the filename doesn't exist or isn't an image.
This is happening because there is an error in reading image from the video. you can try the below code and if you are seeing nothing, then the problem is with your webcam.
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
while ret:
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
ret, frame = cap.read()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
If you are using a docker container to run your code, the issue might be in the way you set up your docker container. In particular, you need to specify a flag --device to enable a camera usage in the docker container when creating the container like this:
docker run --device <device-path> <rest-of-the-paramaters>
Before that, you need to check the path of the camera device by using ls -ltrh /dev/video* for Ubuntu. Usually, the path is /dev/video0.
I ran your code, it works perfectly fine for me.
But I did get some indentation errors.
Here's the updated 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()
If one still gets this error, try doing the following:
check if your webcam works fine with any other library other than OpenCV
change VideoCapture(0) => VideoCapture(1)
Uninstall OpenCV and reinstall it
Relaunch your code editor or whatever
Switch to a different IDE (for example from PyCharm to Jupyter notebook)
Also add these in the above code :
if ret:
assert not isinstance(frame,type(None)), 'frame not found'
OR
assert (frame is not None) == ret, (ret, type(frame))
OR
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break