【发布时间】:2021-12-31 05:31:04
【问题描述】:
对于我的 Mac,我想使用 Python 来捕获图像,然后将它们转换为 numpy 数组(彩色或灰度都适合我)。即使先将图像存储到磁盘,然后再通过脚本转换它们也可以。
我已经读到 cv2 很有用,我已经尝试过设置或使用它但未能成功。欢迎任何帮助,包括代码示例。
【问题讨论】:
标签: machine-learning networking artificial-intelligence
对于我的 Mac,我想使用 Python 来捕获图像,然后将它们转换为 numpy 数组(彩色或灰度都适合我)。即使先将图像存储到磁盘,然后再通过脚本转换它们也可以。
我已经读到 cv2 很有用,我已经尝试过设置或使用它但未能成功。欢迎任何帮助,包括代码示例。
【问题讨论】:
标签: machine-learning networking artificial-intelligence
您可以使用来自https://www.geeksforgeeks.org/python-opencv-capture-video-from-camera/ 的以下代码使用 OpenCV 捕获图像:
# import the opencv library
import cv2
# define a video capture object
vid = cv2.VideoCapture(0)
while(True):
# Capture the video frame
# by frame
ret, frame = vid.read()
# convert to grayscale
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame', frame)
# the 'q' button is set as the
# quitting button you may use any
# desired button of your choice
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()
【讨论】: