-
Notifications
You must be signed in to change notification settings - Fork 3
Management Module
This module is wrapped inside ManagerCV2
class, if you want to import it, write:
from cv2_tools.Management import ManagerCV2
ManagerCV2 helps to manage videos and streams
With this Class you will be capable to iterete a video frame by frame (if you want, you can also limit the FPS).
Also you can add keystrokes with your own callbacks methods in a easiest way.
At the same time you can ask to this manager the index of the current frame (attribute: count_frames
) and the FPS processing average.
Finally you can set a method you want to execute when finishing the iteration.
- video -- cv2.VideoCapture that it is going to manage.
-
is_stream -- Bool to indicate if it is an stream or not.
It is not necessary to set it to True if you are using an stream.
It is only for managing streams issuess.
On a stream it is possible to lose frames, so, if you set is_stream
to True, it will try to reconnect the stream as many times as
ManagerCV2._tries_reconnect_stream
indicates. (Default: False) - fps_limit -- You can set with it the maximum FPS of the video. If you set it to 0, it means no limit. (Default: 0)
- queue_size -- The maximum number of frames to store in the queue. (Default: 256)
For example, if we want to manage the webcam limiting FPS to 25:
manager_cv2 = ManagerCV2(cv2.VideoCapture(0), is_stream=True, fps_limit=25)
for frame in manager_cv2:
# Do what you want with your cv2 frame
cv2.imshow('Example easy manager', frame) # For example, reproduce the frame
cv2.destroyAllWindows() # Finally close window opened with cv2.imshow
This for
will manage all for you and with better performance. Only with this, you can improve your video file FPS rate by over 52%!
If you want, compare it by your own with the code bellow:
- Same example, not using cv2-tools:
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
# This is not cool becouse if you are getting an streaming you could lose some frames
# cv2_tools will manage it for you :)
if not ret:
break
cv2.imshow('Classic example', frame)
The second option, without cv2-tools, need more lines, is less intuitive and have worst performance.
If you want to press a key and and then execute something (for example, press esc
to exit, or p
to print something). ManagerCV2 will do it for you.
Method to execute when pressed a key Method to execute when pressed a key
Arguments:
-
keystroke -- Key to check if pressed.
-
waitkey -- Ms to wait key (it works exactly as cv2.waitKey).
-
method -- Method to execute.
-
args -- Arguments to pass to the method.
-
kwargs -- Keyword arguments to pass to the method. Note that this method can receive and
exit
param (you can't pass a param with the same name to your own method). -
Example of how to use it:
manager_cv2 = ManagerCV2(cv2.VideoCapture(0), is_stream=True)
manager_cv2.add_keystroke(27, 1, print, 'Pressed esc. Exiting', exit=True)
manager_cv2.add_keystroke(ord(q), 1, print, 'Pressed', 'Q')
for frame in manager_cv2:
cv2.imshow('Example easy manager', frame)
cv2.destroyAllWindows()
IMPORTANT: We are working on this method, because we want to let you decide the method and parameters dynamically, so probably it will change in future versions.
It checks the last pressed keystroke (not neccesarily in the last frame). This is the basic solution we found if you don't want to call the previos method, and you prefer to do it by yourself.
- Example of how to use it:
manager_cv2 = ManagerCV2(cv2.VideoCapture(0), is_stream=True)
for frame in manager_cv2:
# For now if you press a Q one time you will get printed it
# on each frame until you press another key, but we will solve it on next version.
if manager_cv2.get_last_keystroke() == ord(q):
print('Pressed Q')
cv2.imshow('Example easy manager', frame)
cv2.destroyAllWindows()
At anytime you can get the average FPS. You can ask it inside a loop, or when it is over. You will get your average FPS with a precision of 3 deciamls.
- Example of how to use it:
manager_cv2 = ManagerCV2(cv2.VideoCapture(0), is_stream=True, fps_limit=60)
for frame in manager_cv2:
cv2.imshow('Example easy manager', frame)
cv2.destroyAllWindows()
Get in mind that if you limit the FPS in the constructor to a certain amount, you will never obtain this FPS, thats because it will limit the maximum speed, but sometimes your PC processes frames slower.
We are thinking about what we should do, leave it as it is, or, compensate FPS drops to maintain the average. Let us know, your opinion interests us.
Authors: Fernando Pérez Gutiérrez