编辑代码:如何访问我的网络摄像头而不是照片

Editing a code: how to access my webcame instead of a photo

所以我有一个用于查找图像中最亮像素的预写代码 - 代码中包含加载图片的命令。我需要的是在用我的网络摄像头制作的实时视频中找到最亮的像素。所以我现在需要做的是删除要加载图片的行,并添加访问相机的行。几个小时以来,我一直在尝试这样做,但我总是收到错误消息,有人知道如何解决吗? 这是我需要编辑的代码:

# import the necessary packages
import numpy as np
import argparse
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help = "path to the image file")
ap.add_argument("-r", "--radius", type = int,
    help = "radius of Gaussian blur; must be odd")
args = vars(ap.parse_args())

# load the image and convert it to grayscale
image = cv2.imread(args["image"])
orig = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# perform a naive attempt to find the (x, y) coordinates of
# the area of the image with the largest intensity value
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
cv2.circle(image, maxLoc, 5, (255, 0, 0), 2)

# display the results of the naive attempt
cv2.imshow("Naive", image)

# apply a Gaussian blur to the image then find the brightest
# region
gray = cv2.GaussianBlur(gray, (args["radius"], args["radius"]), 0)
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
image = orig.copy()
cv2.circle(image, maxLoc, args["radius"], (255, 0, 0), 2)

# display the results of our newly improved method
cv2.imshow("Robust", image)
cv2.waitKey(0)

我想删除整个“# load the image and convert it to grayscale”块并想添加以下行:

Import SimpleCV
cam = SimpleCV.Camera()
img = cam.getImage().flipHorizontal().toGray()
img.show()

有人知道如何编辑代码而不收到新的错误消息吗?

在 opencv 中访问网络摄像头流非常容易。

为此,请写类似 cap = cv2.VideoCapture(XXX) 的内容,其中 XXX 是视频文件的路径,网络摄像头的 IP 地址或默认计算机网络摄像头的 0。

获取此流​​后,您可以像这样遍历图像:

while(True):
    didReturnImage, image = cap.read()
    if not didReturnImage:
        #The VideoCapture did not provide an image. 
        #Assuming this to mean that there are no more left 
        #in the video, we leave the loop.
        break
    else:
        #image is now available for use as a regular image

只需将您的代码放入此循环中,从上述循环中的 orig = image.copy() 开始。

(注意:您可能需要将行 cv2.waitKey(0) 更改为 cv2.waitKey(1) 因为第一个将图像永远保留在屏幕上,而第二个将一直保持到显示下一个图像.)