OpenCV已经包含了处理实时摄像机数据的功能。import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
当然,您不想显示图像,但您的所有处理都可以在那里完成。
记住要睡几百毫秒,这样π就不会过热那么多。
编辑:
“不过,我到底要怎么做呢。我一直使用“img=cv2.imread('image1.jpg')”。我需要用什么来代替这里的“img”变量?我用什么?ret是干什么的?:)“
ret指示读取是否成功。如果没有退出程序。
读取的frame与您的img = cv2.imread('image1.jpg')无关,因此您的检测代码应该完全相同。
唯一的区别是不需要保存并重新打开图像。也可以出于调试目的保存录制的图像,例如:import cv2, time
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
if ret:
cv2.imwrite(time.strftime("%Y%m%d-%H%M%S"), frame)
cap.release()