Ric*_*ard 6 python webcam opencv
我正在使用OpenCV2用网络摄像头拍摄一些时间拍照.我想提取网络摄像头看到的最新视图.我试着这样做.
import cv2
a = cv2.VideoCapture(1)
ret, frame = a.read()
#The following garbage just shows the image and waits for a key press
#Put something in front of the webcam and then press a key
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)]
#Since something was placed in front of the webcam we naively expect
#to see it when we read in the next image. We would be wrong.
ret, frame = a.read()
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)]
Run Code Online (Sandbox Code Playgroud)
除了放置在网络摄像头前的图像不显示.这几乎就像有某种缓冲......
所以我清除缓冲区,如下:
import cv2
a = cv2.VideoCapture(1)
ret, frame = a.read()
#Place something in front of the webcam and then press a key
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)]
#Purge the buffer
for i in range(10): #Annoyingly arbitrary constant
a.grab()
#Get the next frame. Joy!
ret, frame = a.read()
cv2.imshow('a',frame); cv2.waitKey(0); cv2.destroyAllWindows(); [cv2.waitKey(25) for i in range(10)]
Run Code Online (Sandbox Code Playgroud)
现在这个工作,但它是令人讨厌的不科学和缓慢.有没有办法专门询问缓冲区中最近的图像?或者,禁止这样,清除缓冲区的更好方法是什么?
小智 -2
我从Capture single picture with opencv 中找到了一些有帮助的代码。我对其进行了修改,使其连续显示最新捕获的图像。它似乎没有缓冲区问题,但我可能误解了你的问题。
import numpy as np
import cv2
cap = cv2.VideoCapture(0) # video capture source camera (Here webcam of laptop)
ret,frame = cap.read() # return a single frame in variable `frame`
while(True):
ret,frame = cap.read() # return a single frame in variable `frame
cv2.imshow('img1',frame) #display the captured image
if cv2.waitKey(1) & 0xFF == ord('y'): #save on pressing 'y'
cv2.imwrite('images/c1.png',frame)
cv2.destroyAllWindows()
break
cap.release()
Run Code Online (Sandbox Code Playgroud)