Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 空白视频窗口问题,无限拍照问题,程序每次仅运行一次';它打开了_Python_Opencv_Detection - Fatal编程技术网

Python 空白视频窗口问题,无限拍照问题,程序每次仅运行一次';它打开了

Python 空白视频窗口问题,无限拍照问题,程序每次仅运行一次';它打开了,python,opencv,detection,Python,Opencv,Detection,我在使用python程序时遇到了一些问题,该程序用于从视频中检测人脸,并在检测到人脸时拍照 ONE每当我单击“运行模块”时,它都会运行程序。但是如果我第一次尝试运行它,我会收到一条错误消息,它不会运行。要再次运行它,我必须关闭python程序并再次打开它。错误消息是: Traceback (most recent call last): File "C:\Users\Morgan\Documents\Image recognition\Face Detection\test.py", lin

我在使用python程序时遇到了一些问题,该程序用于从视频中检测人脸,并在检测到人脸时拍照

ONE每当我单击“运行模块”时,它都会运行程序。但是如果我第一次尝试运行它,我会收到一条错误消息,它不会运行。要再次运行它,我必须关闭python程序并再次打开它。错误消息是:

Traceback (most recent call last):
  File "C:\Users\Morgan\Documents\Image recognition\Face Detection\test.py", line 56, in <module>
runCam()
  File "C:\Users\Morgan\Documents\Image recognition\Face Detection\test.py", line 26, in runCam
if len(detect_faces(image))>=0:
  File "C:\Users\Morgan\Documents\Image recognition\Face Detection\test.py", line 36, in     detect_faces
detected = cv.HaarDetectObjects(image, cascade, storage, 1.1, 3, cv.CV_HAAR_DO_CANNY_PRUNING, (100,100))
error: Non-positive cols or rows
这就是添加的新功能:

import cv2
import cv2.cv as cv

camera_port = 0

ramp_frames = 1

def operateCamera():

camera = cv2.VideoCapture(camera_port)

def get_image():
     retval, im = camera.read()
     return im

for i in xrange(ramp_frames):
    temp = get_image()
    print("Taking image...")

    camera_capture = get_image()
    cv2.imwrite("c://Users/Morgan/Pictures/Logitech Webcam/color_image.jpeg", camera_capture)

def runCam():
while 1:
    if len(detect_faces(image))>=0:
        operateCamera()
    else:
        print("No faces detected!")

HAAR_CASCADE_PATH = "C:\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml"
CAMERA_INDEX = 0

def detect_faces(image):
faces = []
detected = cv.HaarDetectObjects(image, cascade, storage, 1.1, 3, cv.CV_HAAR_DO_CANNY_PRUNING, (100,100))
if detected:
    for (x,y,w,h),n in detected:
        faces.append((x,y,w,h))
    return faces

if __name__ == "__main__":
cv.NamedWindow("Video", cv.CV_WINDOW_NORMAL)

capture = cv.CaptureFromCAM(CAMERA_INDEX)
storage = cv.CreateMemStorage()
cascade = cv.Load(HAAR_CASCADE_PATH)
faces = []

i = 0
c=-1
while(c==-1)
    image = cv.QueryFrame(capture)
    runCam()
    # Only run the Detection algorithm every 5 frames to improve performance
    if i%5==0:
        faces = detect_faces(image)

        for (x,y,w,h) in faces:
            cv.Rectangle(image, (x,y), (x+w,y+h), 255)

        cv.ShowImage("Video", image)
        i += 1
        c=cv.WaitKey(10)
三个因为我能够成功运行该程序一次,所以我知道它在检测到人脸时会拍照,不幸的是,它会继续拍照,并且在检测到第一张人脸后从不停止。我只想拍一张照片。有人知道如何解决这个问题吗


总之,我知道这是一个很大的问题,但如果有人对如何修复程序有任何想法,那么程序只运行一次,然后需要重新启动问题、灰色视频馈送和/或仅拍摄一张照片问题,请让我知道!谢谢(如果我的缩进看起来有点可笑,也很抱歉……)

您的代码非常非常混乱。我正在尽力解析它,但您有很多问题

ONE和TWO我认为发生错误是因为您试图打开同一个摄像头两次。用格式很难说。灰色的框架肯定是由于打开相机两次;当您尝试打开不存在的相机时,会出现相同的灰色帧

第二个代码示例中有一些输入错误,例如
while(c==-1)
没有冒号。你有很多缩进问题。我会避免使用cv绑定。如果可能,坚持使用cv2

THREE
while(c==-1)
循环期间,每次调用
runCam()
。它将保存每一帧视频

我修改了我正在玩的一个小型人脸检测程序的一小段代码。它使用
cv2.CascadeClassifier()
而不是
cv.haardeticObjects()
。也许对你有帮助。你需要弄清楚如何分辨一张脸和另一张脸

import cv2

cascade = "C:\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml"

scaling_factor = 4
picture_taken = False
frame_name = "Face Tracking"

capture = cv2.VideoCapture(0)
cv2.namedWindow(frame_name)

classifier = cv2.CascadeClassifier(cascade)

while cv2.waitKey(1) == -1:
    success, frame = capture.read()

    downsized_frame_template = (frame.shape[1] / scaling_factor, frame.shape[0] / scaling_factor)
    downsized_frame = cv2.resize(frame, downsized_frame_template)
    possible_faces = classifier.detectMultiScale(downsized_frame)

    if len(possible_faces):
        if not picture_taken:
            cv2.imwrite("c:\\Users\\Morgan\\Pictures\\Logitech Webcam\\color_image.jpeg", camera_capture)
            picture_taken = True
        for face in possible_faces:
            x, y, w, h = [v * scaling_factor for v in face]
            cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255))

    cv2.imshow(frame_name, frame)

欢迎来到SO。来,坐火车。这个问题太多了。你需要发布一个问题,(a)只问一个问题,(b)尽可能简洁。
import cv2

cascade = "C:\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml"

scaling_factor = 4
picture_taken = False
frame_name = "Face Tracking"

capture = cv2.VideoCapture(0)
cv2.namedWindow(frame_name)

classifier = cv2.CascadeClassifier(cascade)

while cv2.waitKey(1) == -1:
    success, frame = capture.read()

    downsized_frame_template = (frame.shape[1] / scaling_factor, frame.shape[0] / scaling_factor)
    downsized_frame = cv2.resize(frame, downsized_frame_template)
    possible_faces = classifier.detectMultiScale(downsized_frame)

    if len(possible_faces):
        if not picture_taken:
            cv2.imwrite("c:\\Users\\Morgan\\Pictures\\Logitech Webcam\\color_image.jpeg", camera_capture)
            picture_taken = True
        for face in possible_faces:
            x, y, w, h = [v * scaling_factor for v in face]
            cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255))

    cv2.imshow(frame_name, frame)