我正在尝试使用opencv将网络摄像头中的帧另存为jpgs或png或其他格式.尽管这里有这样的例子,但事实证明,这比我想象的要困难得多:Capturing a single image from my webcam in Java or Python
我正在尝试这样做:
if __name__ == "__main__":
print "Press ESC to exit ..."
# create windows
cv.NamedWindow('Raw', cv.CV_WINDOW_AUTOSIZE)
cv.NamedWindow('Processed', cv.CV_WINDOW_AUTOSIZE)
# create capture device
device = 0 # assume we want first device
capture = cv.CaptureFromCAM(0)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 640)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
# check if capture device is OK
if not capture:
print "Error opening capture device"
sys.exit(1)
while 1:
# do forever
# capture the current frame
frame = cv.QueryFrame(capture)
if frame is None:
break
# mirror
cv.Flip(frame, None, 1)
# face detection
detect(frame)
# display webcam image
cv.ShowImage('Raw', frame)
# handle events
k = cv.WaitKey(10)
if k == 0x1b: # ESC
print 'ESC pressed. Exiting ...'
break
if k == 0x63 or k == 0x43:
print 'capturing!'
s, img = capture.read()
if s:
cv.SaveImage("r'C:\test.jpg", img)
如您所见,当我按下字母c时,我尝试使用Froyo在另一个问题中建议的代码修改来使其捕获图像.它不起作用,我找不到使它起作用的文档.
请帮忙!
非常感谢,
亚历克斯
解决方法:
更改您的保存部分,如下所示:
if k == 0x63 or k == 0x43:
print 'capturing!'
cv.SaveImage("test.jpg",frame)
对我有用.由于已经捕获了要检测的帧,因此需要再次捕获以保存它.
cv.CaptureFromCam()和cv.VideoCapture()也不同.他们不要混在一起.
标签:opencv,webcam,python
来源: https://codeday.me/bug/20191201/2078226.html