- Saved searches
- Use saved searches to filter your results more quickly
- How to speed up python code from 11 to 30 FPS or more in the real-time video? #2088
- How to speed up python code from 11 to 30 FPS or more in the real-time video? #2088
- Comments
- Find frame rate (frames per second-fps) in OpenCV (Python/C++)
- How to find frame rate of a camera / webcam in OpenCV ?
- Python
- C++
- How to find frame rate of a video in OpenCV ?
- Subscribe & Download Code
- Summary
- Key Takeaways
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to speed up python code from 11 to 30 FPS or more in the real-time video? #2088
How to speed up python code from 11 to 30 FPS or more in the real-time video? #2088
Comments
I am trying to detect people based on their clothes in real time video and need to speed up from 11 to 30 frames per second (FPS) or more. Unfortunately, it requests 30 FPS at least to work well.
Have any way to speed up it?
When I trained the model, I followed this https://www.hackevolve.com/create-your-own-object-detector/ which used HOG+SVM
Note: I have GPU (1050)
import cv2 import dlib from imutils.video import FPS detector = dlib.simple_object_detector("clothes_detector.svm") cap = cv2.VideoCapture(0) # video dimension in python-opencv # width = cap.get(3) # float # height = cap.get(4) # float # print width, height # time.sleep(2.0) fps = FPS().start() while (1): _, image = cap.read() if _ is True: # convert to grayscale img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # image = image # image =cv2.cvtColor(image, cv2.COLOR_BGR2RGB) else: continue # hog_image = detector.detect(image, annotate='texture') # Display result # cv2.imshow("Original Tracking", img) boxes = detector(img) for box in boxes: (x, y, xb, yb) = [box.left(), box.top(), box.right(), box.bottom()] cv2.rectangle(image, (x, y), (xb, yb), (0, 0, 255), 2) cv2.imshow("Color Tracking", img) # cv2.imshow("kernel", g_kernel) if cv2.waitKey(1) & 0xFF == ord('q'): cap.release() cv2.destroyAllWindows() break fps.update() fps.stop() # print("[INFO] elapsed time: ".format(fps.elapsed())) print("[INFO] approx. FPS: ".format(fps.fps()))
The text was updated successfully, but these errors were encountered:
Find frame rate (frames per second-fps) in OpenCV (Python/C++)
In OpenCV the class VideoCapture handles reading videos and grabbing frames from connected cameras. There is a lot of information you can find about the video file you are playing by using the get(PROPERTY_NAME) method in VideoCapture. One of the common properties you may want to know is to find frame rate or frames per second. You can download all code and example images used in this post here.
How to find frame rate of a camera / webcam in OpenCV ?
In OpenCV finding the frame rate of a connected camera / webcam is not straight forward. The documentation says that get(CAP_PROP_FPS) or get(CV_CAP_PROP_FPS) gives the frames per second. Now that is true for video files, but not for webcams. For webcams and many other connected cameras, you have to calculate the frames per second manually. You can read a certain number of frames from the video and see how much time has elapsed to calculate frames per second.
Download Code To easily follow along this tutorial, please download code by clicking on the button below. It’s FREE!
Python
#!/usr/bin/env python import cv2 import time if __name__ == '__main__' : # Start default camera video = cv2.VideoCapture(0); # Find OpenCV version (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.') # With webcam get(CV_CAP_PROP_FPS) does not work. # Let's see for ourselves. if int(major_ver) < 3 : fps = video.get(cv2.cv.CV_CAP_PROP_FPS) print("Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): ".format(fps)) else : fps = video.get(cv2.CAP_PROP_FPS) print("Frames per second using video.get(cv2.CAP_PROP_FPS) : ".format(fps)) # Number of frames to capture num_frames = 120; print("Capturing frames".format(num_frames)) # Start time start = time.time() # Grab a few frames for i in range(0, num_frames) : ret, frame = video.read() # End time end = time.time() # Time elapsed seconds = end - start print ("Time taken : seconds".format(seconds)) # Calculate frames per second fps = num_frames / seconds print("Estimated frames per second : ".format(fps)) # Release video video.release()
C++
#include "opencv2/opencv.hpp" #include using namespace cv; using namespace std; int main(int argc, char** argv) < // Start default camera VideoCapture video(0); // With webcam get(CV_CAP_PROP_FPS) does not work. // Let's see for ourselves. // double fps = video.get(CV_CAP_PROP_FPS); // If you do not care about backward compatibility // You can use the following instead for OpenCV 3 double fps = video.get(CAP_PROP_FPS); cout > frame; > // End Time time(&end); // Time elapsed double seconds = difftime (end, start); cout
How to find frame rate of a video in OpenCV ?
If you are reading a video file you can simply use the get method to obtain frames per second. The following examples show the usage.
import cv2 if __name__ == '__main__' : video = cv2.VideoCapture("video.mp4"); # Find OpenCV version (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.') if int(major_ver) < 3 : fps = video.get(cv2.cv.CV_CAP_PROP_FPS) print ("Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): ".format(fps)) else : fps = video.get(cv2.CAP_PROP_FPS) print ("Frames per second using video.get(cv2.CAP_PROP_FPS) : ".format(fps)) video.release()
#include "opencv2/opencv.hpp" using namespace cv; using namespace std; int main(int argc, char** argv) < // Open video file VideoCapture video("video.mp4"); // double fps = video.get(CV_CAP_PROP_FPS); // For OpenCV 3, you can also use the following double fps = video.get(CAP_PROP_FPS); cout
Subscribe & Download Code
If you liked this article and would like to download code (C++ and Python) and example images used in this post, please click here. Alternately, sign up to receive a free Computer Vision Resource Guide. In our newsletter, we share OpenCV tutorials and examples written in C++/Python, and Computer Vision and Machine Learning algorithms and news.
Summary
In this we discussed finding the frames per second-fps in OpenCV. We also provided the Python/C++ code for practice and study.
Key Takeaways
- OpenCV class VideoCapture handles reading videos and grabbing frames from connected cameras.
- The method PROPERTY_NAME helps find lot of information about the video file being played.
- Common property we may want to know, frame rate or frames per second, is discussed in detail.
- When reading a video file simply use the get method to obtain frames per second.