简体   繁体   English

从相机保存视频得到语法错误

[英]Saving video from camera get syntax error

I am trying to save video from face cam in my dir.我正在尝试将面部摄像头的视频保存在我的目录中。

I think I did everything correct.我认为我做的一切都是正确的。 But I am getting syntax error in variable declaration.但是我在变量声明中遇到语法错误。

Here's the error这是错误

Traceback (most recent call last): File "camera1.py", line 15, in cv2.imshow('frame', frame) cv2.error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow' ^回溯(最后一次调用):文件“camera1.py”,第 15 行,在 cv2.imshow('frame', frame) cv2.error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv- python/opencv/modules/highgui/src/window.cpp:376: 错误: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow' ^

  1 from cv2 import *
  2 
  3 cap = cv2.VideoCapture()
  4 
  5 width = int(cap.get((cv2.CAP_PROP_FRAME_WIDTH))))
  6 height = int(cap.get((cv2.CAP_PROP_FRAME_HEIGHT)))
  7 
  8 writer = cv2.VideoWriter('mysupervideo.mp4', cv2.VideoWriter_fourcc(*'XVID'), 20, (width, height))
  9                                                                                                                  
 10 while True:
 11     ret, frame = cap.read()
 12     writer.write(frame)
 13     cv2.imshow('frame', frame)
 14 
 15     if cv2.waitKey(1) & 0xFF == ord('q'):
 16         break
 17 
 18 cap.release()
 19 writer.release()
 20 cv2.destroyAllWindows()                                                                                                                                                                                                                                                                        

Thanks for any help谢谢你的帮助

There are multiple issues with the above code snippet.上述代码片段存在多个问题。

1) There are extra parenthesis at the ends of line 5. It should be: 1)第5行末尾有多余的括号。应该是:

width = int(cap.get((cv2.CAP_PROP_FRAME_WIDTH)))

2) The error that you get here is because in lines 5 and 6 the values of width and height that you get is " 0 and 0". 2) 你在这里得到的错误是因为在第 5 行和第 6 行你得到的宽度和高度的值是“0 和 0”。 This is because in code above at line 3 you are creating "cap" object but its not capturing from camera hence height width is 0, 0. Instead modify the line to:这是因为在上面第 3 行的代码中,您正在创建“cap” object 但它没有从相机捕获,因此高度宽度为 0、0。而是将行修改为:

cap = cv2.VideoCapture(0)

This (0) indicates capture from camera.这 (0) 表示从相机捕获。 and hence you will get proper width and height.因此您将获得适当的宽度和高度。

3) I ran the following code snippet and it is working for me and it dumps a result video "mysupervideo.mp4" in present working directory: 3)我运行了以下代码片段,它对我有用,它在当前工作目录中转储了结果视频“mysupervideo.mp4”:

from cv2 import *
cap = cv2.VideoCapture(0)
width = int(cap.get((cv2.CAP_PROP_FRAME_WIDTH)))
height = int(cap.get((cv2.CAP_PROP_FRAME_HEIGHT)))
print(width, height)
writer = cv2.VideoWriter('mysupervideo.mp4', cv2.VideoWriter_fourcc(*'XVID'), 20, (width, height))
while True:
    ret, frame = cap.read()
    writer.write(frame)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
writer.release()
cv2.destroyAllWindows()  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM