[Python] USB webcam doesn't reach required fps

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have a project for analyzing sports images captured with high-speed cameras capable of recording up to 330fps at a resolution of 640×280. With slightly higher quality, they can record at 120fps. The issue is that I can't get it to go beyond 15fps at any point. The code consists of a main thread and two separate classes working in parallel. Even when I use the now-commented lines to increase and fix the fps, it still captures at the same rate. Changing the video resolution hasn't worked either.

    # import the necessary packages
    from __future__ import print_function
    from imutils.video import WebcamVideoStream
    from imutils.video import FPS
    import argparse
    import imutils
    import cv2
    from os import listdir
    from os.path import isfile, join
    # construct the argument parse and parse the arguments

    ap = argparse.ArgumentParser()
    ap.add_argument("-n", "--num-frames", type=int, default=2000,
    help="# of frames to loop over for FPS test")
    ap.add_argument("-d", "--display", type=int, default=-1,
    help="Whether or not frames should be displayed")
    args = vars(ap.parse_args())
    vs = WebcamVideoStream(src=701).start()
    n=0
    files=[f for f in listdir("XXX") if isfile(join("XXX",f))]
    n=len(files)
    while n<2:
    file_name="XXX//vid%03d.mp4" % (n+1)
    fps = FPS().start()
    cap=cv2.VideoCapture(file_name)
    #cap.set(cv2.CAP_PROP_FPS,330)
    fourcc=cv2.VideoWriter_fourcc(*"mp4v")
    writer=cv2.VideoWriter(file_name,fourcc,2,(400,400),True)
    #cap.set(cv2.CAP_PROP_FOURCC,fourcc)


    # loop over some frames...this time using the threaded stream
    while fps._numFrames < args["num_frames"]:
    # grab the frame from the threaded video stream and resize it
    # to have a maximum width of 400 pixels
    frame = vs.read()
    #frame = imutils.resize(frame, width=400, height=400)
    frame = cv2.resize(frame, (400,400))
    writer.write(frame)
    # check to see if the frame should be displayed to our screen
    if args["display"] > 0:
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF
    # update the FPS counter
    fps.update()
    # stop the timer and display FPS information
    fps.stop()
    #print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
    #print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
    n=n+1
    writer.release()

    vs.stop()


    Aqui la clase FPS

    import datetime
    class FPS:
    def __init__(self):
    # store the start time, end time, and total number of frames
    # that were examined between the start and end intervals
    self._start = None
    self._end = None
    self._numFrames = 0
    def start(self):
    # start the timer
    self._start = datetime.datetime.now()
    return self
    def stop(self):
    # stop the timer
    self._end = datetime.datetime.now()
    def update(self):
    # increment the total number of frames examined during the
    # start and end intervals
    self._numFrames += 1
    def elapsed(self):
    # return the total number of seconds between the start and
    # end interval
    return (self._end - self._start).total_seconds()
    def fps(self):
    # compute the (approximate) frames per second
    return self._numFrames / self.elapsed()


    Aqui la clase WebcamVideoStream()

    from threading import Thread
    import cv2
    class WebcamVideoStream:
    def __init__(self, src=2):
    # initialize the video camera stream and read the first frame
    # from the stream

    self.stream = cv2.VideoCapture(src)

    (self.grabbed, self.frame) = self.stream.read()

    # initialize the variable used to indicate if the thread should
    # be stopped
    self.stopped = False
    def start(self):
    # start the thread to read frames from the video stream
    Thread(target=self.update, args=()).start()
    return self
    def update(self):
    # keep looping infinitely until the thread is stopped
    while True:
    # if the thread indicator variable is set, stop the thread
    if self.stopped:
    return
    # otherwise, read the next frame from the stream
    (self.grabbed, self.frame) = self.stream.read()
    def read(self):
    # return the frame most recently read
    return self.frame
    def stop(self):
    # indicate that the thread should be stopped
    self.stopped = True

    Continue reading...

Compartilhe esta Página