【问题标题】:increase opencv webcam speed提高opencv网络摄像头速度
【发布时间】:2022-06-30 14:25:02
【问题描述】:

我需要用我的网络摄像头拍摄视频。我想将 open cv 用于我的用途。 您可以在下面找到的脚本需要大量时间来开始捕获。 你们有谁知道加速这个脚本的解决方案吗?

我尝试将网络摄像头比例降低到 640x480。

webcam = cv2.VideoCapture(0)
##Video Codec
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
width = 640
height = 480



video = VideoWriter(dir_path +"\\" + folder +"\\" + Name +".mp4",fourcc, 20.0, (width,height))



while (True):
    # lese aus Webcam
    ret, frame = webcam.read()
    if ret == False:
        print("Device not Found")
        break
    # Webcam Bild anzeigen
    cv2.imshow('Webcam', frame)
    #print("Aufnahme gestartet")
    # Videosequenz in Datei ablegen
    video.write(frame)
    #Erkennen, ob die Esc-Taste gedrückt wurde
    c = cv2.waitKey(1)
    if c == 27:
        break
## Alle Fenster schließen
cv2.destroyAllWindows()
## Video Aufnahme freigeben
webcam.release()
video.release()

【问题讨论】:

  • 此代码需要约 20 秒以上。开始捕获。
  • 哪一行代码需要 20+s 才能执行?可以调试吗?
  • 如何确定每一行代码所用的时间?您的系统是否以某种方式损坏? 什么是你的系统?
  • 我区分了下面的答案...唯一的变化是webcam.read() 一式三份,有效地读取了三帧,扔掉了其中的两个,只写了第三个.你决定这是否对你有用。 -- imshow 根本不需要太多时间。之前的某些东西一定会占用那个时间。这就是为什么我问如何测量时间。
  • @bfris 不,VideoWriter 将不会重新缩放任何内容。如果你给它的帧不是你在构造函数中承诺的确切大小,它会默默地丢弃那些。关于这些基本情况,请不要猜测。并且永远不要向任何人推荐那些神奇的数字(3 和 4)。他们有名字。

标签: python opencv webcam-capture


【解决方案1】:

变化是 webcam.read() 一式三份,有效地读取三帧,跳过前两帧,只写入第三帧。希望它会工作

webcam = cv2.VideoCapture(0)
##Video Codec
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
width = 640
height = 480



video = VideoWriter(dir_path +"\\" + folder +"\\" + Name +".mp4",fourcc, 20.0, (width,height))



while (True):
    # lese aus Webcam
    ret, frame = webcam.read()
    ret, frame = webcam.read()
    ret, frame = webcam.read()

    if ret == False:
        print("Device not Found")
        break
    # Webcam Bild anzeigen
    cv2.imshow('Webcam', frame)
    #print("Aufnahme gestartet")
    # Videosequenz in Datei ablegen
    video.write(frame)
    #Erkennen, ob die Esc-Taste gedrückt wurde
    c = cv2.waitKey(1)
    if c == 27:
        break
## Alle Fenster schließen
cv2.destroyAllWindows()
## Video Aufnahme freigeben
webcam.release()
video.release()

【讨论】:

  • 这对我有用!谢谢哥们!您能解释一下,为什么需要获取框架并 ret 3 次?
猜你喜欢
  • 1970-01-01
  • 2014-02-06
  • 2016-03-19
  • 1970-01-01
  • 2012-06-12
  • 2015-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多