Skip to content
New issue

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

[Question]: How to get original video fps with VideoGear. #371

Closed
4 tasks done
nidhi-jha01 opened this issue Aug 1, 2023 · 8 comments
Closed
4 tasks done

[Question]: How to get original video fps with VideoGear. #371

nidhi-jha01 opened this issue Aug 1, 2023 · 8 comments
Labels
QUESTION ❓ User asked about the working/usage of VidGear APIs. WON'T FIXED 🚫 This issue will not be worked on

Comments

@nidhi-jha01
Copy link

Issue guidelines

Issue Checklist

  • I have searched open or closed issues for my problem and found nothing related or helpful.
  • I have read the Documentation and found nothing related to my problem.
  • I have gone through the Bonus Examples and FAQs and found nothing related or helpful.

Describe your Question

I need to process High-Resolution videos with Higher FPS (60 fps or above ) frame by frame and generate point cloud. When using VideoGear and WriteGear, the video is not showing up as its original fps . It is slow as compared to the original. Also adding stabilize in these videos makes it very slow. Can you help with this? Didn't find anything from issues and documentation.

Terminal log output(Optional)

No response

Python Code(Optional)

No response

VidGear Version

0.3.1

Python version

3.9

Operating System version

Windows 10

Any other Relevant Information?

No response

@nidhi-jha01 nidhi-jha01 added the QUESTION ❓ User asked about the working/usage of VidGear APIs. label Aug 1, 2023
@welcome
Copy link

welcome bot commented Aug 1, 2023

Thanks for opening this issue, a maintainer will get back to you shortly!

In the meantime:

  • Read our Issue Guidelines, and update your issue accordingly. Please note that your issue will be fixed much faster if you spend about half an hour preparing it, including the exact reproduction steps and a demo.
  • Go comprehensively through our dedicated FAQ & Troubleshooting section.
  • For any quick questions and typos, please refrain from opening an issue, as you can reach us on Gitter community channel.

@abhiTronix
Copy link
Owner

Hi @nidhi-jha01, there can be several reasons why it is slow, but stabilizing it, will surely won't produce real-time frames. The stabilizer as mentioned in the docs will have latency because it need certain amount of frames to be in memory before applying filter to stabilize it. And this latency is even worse with HD or high FPS frames. So my advise would be to at least downscale or divide the task into different module such as generating point cloud, saving video, stabilizing video etc., and maybe use multi-threading or multi-processing to make it faster, but still you have to comprise between performance and quality. Hope that helps.

@abhiTronix abhiTronix added the WAITING FOR RESPONSE ⏳ Waiting for the user response. label Aug 3, 2023
@nidhi-jha01
Copy link
Author

@abhiTronix While using VideoGear and WriteGear , the fps is cut down to half of the original FPS for videos of >= 60 fps.

@abhiTronix
Copy link
Owner

@nidhi-jha01 Okay, can you explain the complete pipeline, and what are the various tasks in your pipeline.

@abhiTronix
Copy link
Owner

@abhiTronix While using VideoGear and WriteGear , the fps is cut down to half of the original FPS for videos of >= 60 fps.

@nidhi-jha01 also, can you elaborate "cut down to half of the original FPS", by whom? API or you manually before inputting?

@nidhi-jha01
Copy link
Author

nidhi-jha01 commented Aug 3, 2023

import required libraries

from vidgear.gears import VideoGear
from vidgear.gears import WriteGear
import cv2

Open suitable video stream, such as webcam on first index(i.e. 0)

stream = VideoGear(source=file_name,stabilize=True,**options).start()

Define WriteGear Object with suitable output filename for e.g. Output.mp4

writer = WriteGear(output = 'Output1.mp4')

loop over

while True:

# read frames from stream
frame = stream.read()

# check for frame if None-type
if frame is None:
    break


# {do something with the frame here}

# write frame to writer
writer.write(frame)
cv2.namedWindow("Output Frame", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Output Frame", 600, 700)
# cv2.imshow("REALTIME", frame)
# Show output window
cv2.imshow("Output Frame", frame)

# check for 'q' key if pressed
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
    break

close output window

cv2.destroyAllWindows()

safely close video stream

stream.stop()

safely close writer

writer.close()

This is the script that I am using for processing the video. The video that I feed to this script is of 60 fps with 1280*720 resolution. The video that appears on Output frame is very slow when compared to the actual video I provided as input to the script.

I calculated the prev_frame_time and current frame time and evaluated fps using it as follows:

new_frame_time = time.time()
fps = 1 / (new_frame_time - prev_frame_time)

with this calculation the fps that I get for output window is usually between 20-30 fps .

@abhiTronix
Copy link
Owner

abhiTronix commented Aug 3, 2023

I calculated the prev_frame_time and current frame time and evaluated fps using it as follows:

new_frame_time = time.time()
fps = 1 / (new_frame_time - prev_frame_time)

with this calculation the fps that I get for output window is usually between 20-30 fps .

@nidhi-jha01 The FPS reported will be slower because number of factors. Try these hacks:

  1. Remove following imshow code: (becomes overhead when you just wanna save frames)
cv2.namedWindow("Output Frame", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Output Frame", 600, 700)
# cv2.imshow("REALTIME", frame)
# Show output window
cv2.imshow("Output Frame", frame)

# check for 'q' key if pressed
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
    break
  1. Use stabilize=False in VideoGear Class: (as it is slower inertly due to how it is designed)
  2. Try resizing frames with reducer() function:
# import required libraries
from vidgear.gears.helper import reducer
from vidgear.gears.stabilizer import Stabilizer
from vidgear.gears import CamGear
from vidgear.gears import WriteGear
import cv2

# Open suitable video stream
stream = CamGear(source="unstabilized_stream.mp4").start()

# initiate stabilizer object with default parameters
stab = Stabilizer()

# Define writer with default parameters and suitable output filename for e.g. `Output.mp4`
writer = WriteGear(output="Output.mp4")

# loop over
while True:

    # read frames from stream
    frame = stream.read()

    # check for frame if not None-type
    if frame is None:
        break

   # reducer frames size if you want more performance
   frame = reducer(frame, percentage=30)  # reduce frame by 30%

    # send current frame to stabilizer for processing
    stabilized_frame = stab.stabilize(frame)

    # wait for stabilizer which still be initializing
    if stabilized_frame is None:
        continue

    # {do something with the stabilized frame here}

    # write stabilized frame to writer
    writer.write(stabilized_frame)

# clear stabilizer resources
stab.clean()

# safely close video stream
stream.stop()

# safely close writer
writer.close()

@abhiTronix
Copy link
Owner

@nidhi-jha01 But if you asking to get exact FPS as original it is impossible with stabilize=True in VideoGear class because how the backend Stabilizer class is designed.

@abhiTronix abhiTronix added WON'T FIXED 🚫 This issue will not be worked on and removed WAITING FOR RESPONSE ⏳ Waiting for the user response. labels Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
QUESTION ❓ User asked about the working/usage of VidGear APIs. WON'T FIXED 🚫 This issue will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants