Decrease latency on camera view - python

I read Ip camera but 500ms latency occurred on camera view. How can I decrease this value?
import cv2
cap = cv2.VideoCapture("rtsp:admin:admin#192.168.1.108/mpeg4 ")
while(True):
ret, frame = cap.read()
cv2.imshow("frame",frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

The reason why you got that latency and the solution for that can be found here: https://www.pyimagesearch.com/2017/02/06/faster-video-file-fps-with-cv2-videocapture-and-opencv/

Related

Does anyone know how to code for getting a frame using your webcam?

I'm trying to get a frame from my webcam but I don't know where the frame is located.
this is my code:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
if frame is None:
break
cv2.imshow('app', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
In the code that you posted, the frame is the image that is captured from the webcam at that point in time.
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
if frame is None:
break
cv2.imshow('app', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.waitKey(0)
cap.release()
cv2.destroyAllWindows()
In your program indentation problem in if condition
If you press "q" then the frame is captured, and the frame stored in "frame" variable

use cv2.VideoCapture to capture a picture

I want to use my laptop webcam to capture a picture using the code below:
import cv2
cap = cv2.VideoCapture(1)
while(True):
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
but it throws this error:
cv2.imshow('frame', frame) cv2.error: OpenCV(4.0.0)
C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:350:
error: (-215:Assertion failed) size.width>0 && size.height>0 in
function 'cv::imshow'
How can I fix this error?
When OpenCV has problem to get frame from camera or stream then it doesn't raise error but it return False in ret (return status) so you should check it. It also return None in frame and imshow has problem to display None - it has no width and height - so you get error with size.width>0 && size.height>0
As I know mostly laptop webcame has number 0, not 1
This works with my laptop webcam
import cv2
cap = cv2.VideoCapture(0) # zero instead of one
while True:
ret, frame = cap.read()
if not ret: # exit loop if there was problem to get frame to display
break
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
EDIT: as said Dave W. Smith in comment: some laptops may need time to send correct image then here version which doesn't exit loop
while True:
ret, frame = cap.read()
if ret: # display only if status (ret) is True and there is frame
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
import numpy as np
import cv2
cap = cv2.VideoCapture(0) #it can be one also...but generally zero
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
cv2.imshow('Capture', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Try this one...it works for mine...make sure numpy is installed

Cannot Close Video Window in OpenCV

I would like to play a video in openCV using python and close that window at any time, but it is not working.
import numpy as np
import cv2
fileName='test.mp4' # change the file name if needed
cap = cv2.VideoCapture(fileName) # load the video
while(cap.isOpened()):
# play the video by reading frame by frame
ret, frame = cap.read()
if ret==True:
# optional: do some image processing here
cv2.imshow('frame',frame) # show the video
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cv2.waitKey(1)
cv2.destroyAllWindows()
cv2.waitKey(1)
The window opens and starts playing the video, but I cannot close the window.
You can use cv2.getWindowProperty('window-name', index) to detect if the window is closed. I'm not totally sure about the index but this worked for me:
import cv2
filename = 'test.mp4'
cam = cv2.VideoCapture(filename)
while True:
ret, frame = cam.read()
if not ret:
break
cv2.imshow('asd', frame)
cv2.waitKey(1)
if cv2.getWindowProperty('asd', 4) < 1:
break

python opencv: ingnoring delay in webcam recording

I am using opencv python for video recording. I am getting problem with continuous operation. I have given delay for some events not for the recording. When I run the program, the program is only taking frames after the amount of delay I have gave. Can any one help me to record contentiously even if delay is given in the loop. I am sending you example code.
Thanks.
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
fouecc = cv2.VideoWrite_fourcc(*'XVID')
out = cv2.VideoWrite('output.avi', fourcc, 20.0,(640,480))
while(True):
ret, frame = cap.read()
out.write(frame)
cv2.imshow('frame', frame)
pump_5.dispense(1,1)
time.sleep(2)
pump_4.dispense(1,1)
time.sleep(2)
if cv2.waitkey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()

Stream video from drone Parrot 2.0. Python + cv2

I am trying to have access to the stream of my drone's camera.
Here my code:
import cv2
import numpy
import libardrone
drone = libardrone.ARDrone()
cap = drone.image
while(True):
cap = drone.image
if not cap:
continue
ret, frame = convert
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
It does not work. It does not open any frame where I can see the stream video of my drone's camera.
What's wrong? Do you have any suggestions?
Thank you!
import cv2
cam = cv2.VideoCapture('tcp://192.168.1.1:5555')
running = True
while running:
# get current frame of video
running, frame = cam.read()
if running:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == 27:
# escape key pressed
running = False
else:
# error reading frame
print 'error reading video feed'
cam.release()
cv2.destroyAllWindows()
Try this code...This works for me.

Categories