I have very simple code in below, I need to see rtsp video from ip camera. But when I try to run, showing video running very slow.
Second 11:49:05 part increase after 3 second around being 11:49:06, so I only see 1 second in 3 seconds. It is working very slow. Where is my problem ? How can I solve this.
I check in local ip address video live record. It show very fast and normal.
I install FFmpeg in my system (Windows 10)
ffmpeg -version
ffmpeg version 2022-11-03-git-5ccd4d3060-essentials_build-www.gyan.dev Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 12.1.0 (Rev2, Built by MSYS2 project)
And I try also this,
cap = cv2.VideoCapture('rtsp://[USERNAME]:[PASS]#192.168.1.64/1',cv2.CAP_FFMPEG)
And nothing has changed.
This is simple code:
import cv2
cap = cv2.VideoCapture('rtsp://[USERNAME]:[PASS]#192.168.1.64/1')
# cv2.CAP_FFMPEG
while(cap.isOpened()):
ret, frame = cap.read()
# frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
if not ret: break
cv2.imshow('frame', frame)
if cv2.waitKey(400) & 0xFF == ord('q'):
break
cap.release()
# out.release() # saved
cv2.destroyAllWindows()
DEBUG WITH FFMPEG
When I working with below code in cmd, I have same issue
ffplay -i rtsp://[PRIVATE]:[PRIVATE]#192.168.1.64/Streaming/Channels/1
But when I working with this:
ffplay -fflags nobuffer rtsp://[PRIVATE]:[PRIVATE]#192.168.1.64/Streaming/Channels/1
There is no time delay, how can I add -fflags nobuffer commands in opencv code ?
For some reason, my script fails to recognize the USB camera connected (MU500), it does, however, recognize and display my other camera which is an analog converted to USB camera. The below script is a portion of the GUI that runs and displays the video capture.
import cv
camera = int(self.camera.GetValue())-1
img_name = self.img_name.GetValue()+'.jpg'
cap = cv2.VideoCapture(camera)
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('Camera',frame)
if cv2.waitKey(1) & 0xFF==ord('q'):
cap.release()
cv2.destroyAllWindows()
break
elif cv2.waitKey(1)== ord('s'):
cv2.imwrite(filename=img_name, img=frame)
cap.release()
print("Image saved!")
break
The user inputs '1' to access camera 1, or '2' for camera 2 and so on. I have tried having just MU500 connected and both connected. Neither recognize MU500. MU500 uses an AmScope driver and is listed as an imaging device in device manager.
Is there anyone who can help me?
You may try to install:
sudo apt-get install v4l-utils
and use:
v4l2-ctl --list-devices
to list all your devices and their properties.
I was using my cam through opencv and suddenly after restarting I ran my code it shows below error:
[ WARN:0] global /io/opencv/modules/videoio/src/cap_v4l.cpp (802) open VIDEOIO ERROR: V4L: can't open camera by index 0
Traceback (most recent call last):
File "test.py", line 20, in <module>
retval, buffer_img = cv2.imencode('.jpg', frame)
cv2.error: OpenCV(4.1.2) /io/opencv/modules/imgcodecs/src/loadsave.cpp:877: error: (-215:Assertion failed) !image.empty() in function 'imencode'
cap = cv2.VideoCapture(0) # here it throws an error
import json
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
retval, buffer_img = cv2.imencode('.jpg', frame)
resdata = base64.b64encode(buffer_img)
resdata = "data:image/png;base64,"+ str(resdata.decode("utf-8"))
PARAMS = {'image': resdata}
# Our operations on the frame come here
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 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 also tried with cap = cv2.VideoCapture(1) but then it shows can't find camera
How can I fix this issue?
I got the same error. Try changing 0 to -1
cap = cv2.VideoCapture(-1)
This solved the issue.
I got the same problem when I created more than one instance of the cv2.VideoCapture(0). So check if your code contains multiple initializations or sections which call cv2.VideoCapture(0) more than once. I was facing this problem while running the flask server in debug mode because it called cv2.VideoCapture(0) twice.
import cv2
cap = cv2.VideoCapture(0)
cap2 = 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()
Error:
python3 debugCamera.py
[ WARN:0] global /io/opencv/modules/videoio/src/cap_v4l.cpp (887) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
Most likely a permission issue on /dev/video0.
Check if you are part of "video" group.
id -a
if you don't see video in your group list add
sudo usermod -a -G video
for Ubuntu users:(20.04)
sudo usermod -a -G video $LOGNAME
Logout and log back in and try it.
I've encountered the same issue and attempted several methods like cv2.VideoCapture(-1) or cv2.VideoCapture(1) but without much success.
I succeeded after reading this article and disabling debug-mode
I had the same problem,
Just change 0 to 1,then to -1 and back again to 0.
Don't know why this worked for me.
I found a solution in https://github.com/opencv/opencv/issues/19527 where the video capture is inside the function instead of outside. That worked for me (ubuntu)
def frame_generation():
camera = cv2.VideoCapture(0) #resolved, correct position
while(True):
I will not go to that part What you are trying to do, here is just a block of code that can open your camera every time you run it,
python: 3.7.3
OpenCV: 4.1.0
import cv2
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()
In my case, I just reconnected cam to the usb port, and then it was solved! I think this error is caused by closing the window in the wrong way. Please check if there is any exception on the terminal right after closing the window.
I also had the same problem. Just changed it to 1 and it was perfectly working. I guess it's related to the number of camera devices you have used.
For example, I guess I have Iruin external camera as my first option which I didn't connect this time.
Here is the error and corrected code.
global /tmp/pip-req-build-f51eratu/opencv/modules/videoio/src/cap_v4l.cpp (890) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
Code to change:
vid = cv2.VideoCapture(1)
The OP appears to be operating on a Raspberry PI. Raspberry is moving to a new system to manage cameras, so when the user upgrades the OS via sudo apt-get upgrade the camera system gets the new library and the legacy camera system is disabled. To enable the legacy camera system again, try
sudo raspi-config
Then select
3 Interface Options Configure connections to peripherals
then select
I1 Legacy Camera Enable/disable legacy camera support
and follow the directions to enable and then reboot.
Of course, this patch will only work for so long, as the legacy system has been deprecated.
I've also had this problem on Ubuntu
I've solved this by these comands
sudo adduser username video
sudo usermod -a -G video username
username - it is the name of your device
than write
id -a
and copy index from ()
for me it is 1000
so than just write:
camera = cv2.VideoCapture(1000)
Tried every anwser but only changing camera id from -1 to 1 and then back to 0 worked.
For Linux, make sure OpenCV is built using the WITH_V4L (with video for linux).
I have do everytime after restart or unplug usb camera and replug
chmod 777 /dev/video0
Don't know if this is still an issue.
In my case, I was getting the same error until I unplugged and plugged the usb camera. Even if I reboot, the error happened.
It's similar to someone said: my camera was already been captured.
The problem is that it was not used by my script, so it was hard to identify.
A few days before the issue, I installed the motion library, but just to test something and I didn't use it anymore. The motion starts at boot, so the camera was being captured by the service. That's why only the unplug-plug worked.
I uninstalled the library, and the error was gone.
video = cv2.VideoCapture(0,cv2.CAP_DSHOW)
This worked for me.
This issue is due to the interruption. Try to end the execution with the key 'q' for example, don't close the window suddenly.
I solved the same issue by opening terminal again and execute the same script again.
This is the code when i execute it:
You can see the frame opens but doesnt show anything
I want to use a usb camera with a raspberry pi 3 model b v1.2 using opencv 3.3 and python 2.7.
I work with opencv in an virtual enviroment.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read() #Capture frame-by-frame
#Our operations on the frame come here
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#Display resulting frame
cv2.imshow('frame',frame)
cv2.waitKey(10)
#if cv2.waitKey(1) & 0xFF == ord('q'):
# break
#when everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I just have no idea how to get around this error. I already searched the error and i am getting helpless, anyone having an idea?
EDIT: i am currently playing around with the code and i can get frames but most of the time the screen stays grey. I use # to show how the code looks now
Ok, it now opens a window and shows the output of the camera
Because of this code:
import sys
sys.path.append('/home/pi/.virtualenvs/cv/lib/python2.7/site-
packages/usr/local/lib/python2.7/site-packages')
and i also use sudo python program.py in the terminal
But this Error :"NameError: name 'CV_CAP_PROP_FRAME_HEIGHT' is not defined" still persists...
I'm trying to write some code for automatically capturing video from a webcam when activated through a batch script on Windows. I've managed to piece together a working script, but it does not appear to be saving the file. I know the code is (at least on a basic level) working because there are no errors and the activation light on the webcam lights up when the code is run. I've reproduced the code below if anyone has any suggestions on how to get it writing to a file, that would be great!
import numpy as np
import cv2
import msvcrt
cap = cv2.VideoCapture(0)
w=int(cap.get(cv2.CAP_PROP_FRAME_WIDTH ))
h=int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT ))
sub=raw_input("Subject#: ")
#Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
fourcc = cv2.VideoWriter_fourcc(*'FMP4')
out = cv2.VideoWriter('C:\path\to\output_' + sub + '.mp4', fourcc, 30, (w,h))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
out.write(frame)
if msvcrt.kbhit():
if ord(msvcrt.getch()) != None:
break
else:
break
#Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
The codec FMP4 is just not being supported by your camera I think. Neither in mine. If I change in your code the encoding to 'MJPG' and write to an '.avi' extension, it works very well.
P.S. Wanted to make a comment but I'm still underage (< 50).