Python 如何在不创建新的.jpg文件的情况下使用opencv从相机获取图像

Python 如何在不创建新的.jpg文件的情况下使用opencv从相机获取图像,python,opencv,Python,Opencv,我有这段代码,可以使用open cv库检测激光点,当我将.jpg或.png文件作为增强文件提供给它时,它可以工作,但现在我想从相机获取图像。“视频0”我正在使用Ubuntu 16.04这里是我的代码,我用它标记了问题****** 如有任何帮助,将不胜感激: # import the necessary packages from imutils import contours from skimage import measure import numpy as np import argpar

我有这段代码,可以使用open cv库检测激光点,当我将.jpg或.png文件作为增强文件提供给它时,它可以工作,但现在我想从相机获取图像。“视频0”我正在使用Ubuntu 16.04这里是我的代码,我用它标记了问题****** 如有任何帮助,将不胜感激:

# import the necessary packages
from imutils import contours
from skimage import measure
import numpy as np
import argparse
import imutils
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=False,
    help="path to the image file")
args = vars(ap.parse_args())

camera = cv2.VideoCapture(0)
#problem is here ********************************************
ret, image = camera.read()

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
#threshold the image to reveal light regions in the
# blurred image
thresh = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)[1]
# perform a series of erosions and dilations to remove
# any small blobs of noise from the thresholded image
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=4)
# perform a connected component analysis on the thresholded
# image, then initialize a mask to store only the "large"
# components
labels = measure.label(thresh, neighbors=8, background=0)
mask = np.zeros(thresh.shape, dtype="uint8")

# loop over the unique components
for label in np.unique(labels):
    # if this is the background label, ignore it
    if label == 0:
        continue

    # otherwise, construct the label mask and count the
    # number of pixels 
    labelMask = np.zeros(thresh.shape, dtype="uint8")
    labelMask[labels == label] = 255
    numPixels = cv2.countNonZero(labelMask)

    # if the number of pixels in the component is sufficiently
    # large, then add it to our mask of "large blobs"
    if numPixels > 300:
        mask = cv2.add(mask, labelMask)
# find the contours in the mask, then sort them from left to
# right
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = contours.sort_contours(cnts)[0]

# loop over the contours
for (i, c) in enumerate(cnts):
    # draw the bright spot on the image
    (x, y, w, h) = cv2.boundingRect(c)
    ((cX, cY), radius) = cv2.minEnclosingCircle(c)
    #x and y center are cX and cY
    cv2.circle(image, (int(cX), int(cY)), int(radius),
        (0, 0, 255), 3)
    cv2.putText(image, "#{}".format(i + 1), (x, y - 15),
        cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)

# show the output image
cv2.imshow("Image", image)
cv2.waitKey(0)

您的工作正常&从视频源检测人脸。但是,你可以用另一种方式

'''
:: Face Detection using Haar Cascades ::
'''

import numpy as np
import cv2, argparse

# set classifiers
face_cascade = cv2.CascadeClassifier(
    '/opt/opencv/main/data/haarcascades/haarcascade_frontalface_default.xml'
)

cam = cv2.VideoCapture(0)
_, img = cam.read()

# load image & convert
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# find faces; If faces are found, it returns the positions
# of detected faces as Rect(x,y,w,h).

faces = face_cascade.detectMultiScale(gray, 1.2, 5)
print "[ INFO:1] Found ", len(faces), "face(s) in this image."

for (x, y, w, h) in faces:
    cv2.rectangle(
        img,
        (x, y),
        (x+w, y+h),
        (255, 100, 25),
        2
    )


cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

使用中断条件将相机捕获包裹在While循环中可能会有所帮助:

进口cv2
cap=cv2.视频捕获(0)
尽管如此:
ret,frame=cap.read()
cv2.imshow(“帧”,帧)
#在这里添加逻辑
打印(框架形状)
#结束
如果cv2.waitKey(20)&0xFF==ord('q'):
打破
第1章释放()

cv2.destroyAllWindows()
@MarkSetchell我该怎么做?我做了一次编辑以删除imread,但代码仍然不会运行start
ipython
。粘贴到队列中直到
camera.read()
。然后键入
ret
,查看捕获是否有效。然后键入
image.shape
查看您得到了什么。我不明白您要求我做什么,因为我不知道是不是ipython我知道那台相机。read可以工作,因为我可以使用它将相机图像保存到桌面,我将尝试该图像。shape though您希望在相机前的行中粘贴什么。read@马克塞切尔