空白视频窗口问题,无限拍照问题,程序每次打开时只会运行一次



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

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

Traceback (most recent call last):
  File "C:UsersMorganDocumentsImage recognitionFace Detectiontest.py", line 56, in <module>
runCam()
  File "C:UsersMorganDocumentsImage recognitionFace Detectiontest.py", line 26, in runCam
if len(detect_faces(image))>=0:
  File "C:UsersMorganDocumentsImage recognitionFace Detectiontest.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

我试过在谷歌上搜索这个,但没有成功。如果有人有任何关于如何解决这个问题的信息、解决方案或想法,我将不胜感激。

TWO在我最初的视频节目中,流运行得很好。视频窗口显示视频,并根据需要检测人脸。我所做的是在程序中添加一个功能,每次检测到新面孔时都会拍照。但当添加此项时,会产生灰色视频窗口(基本上没有视频流)。我不知道为什么会这样。

这是我的原创视频人脸检测程序,无需拍照功能:

  import cv2
  import cv2.cv as cv
  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.2, 2, 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_AUTOSIZE)
capture = cv.CaptureFromCAM(CAMERA_INDEX)
storage = cv.CreateMemStorage(0)
cascade = cv.Load(HAAR_CASCADE_PATH)
faces = []
i = 0
c=-1
while(c==-1):
    image = cv.QueryFrame(capture)
    # 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)

这是添加了新功能:

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)

THREE因为我能够成功运行该程序一次,我知道它会在检测到人脸时拍照,遗憾的是,它会继续拍照,并且在检测到第一张人脸后从未停止。我只想拍一张照片。有人知道如何解决这个问题吗?

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

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

一和二我认为发生错误是因为您试图打开同一台相机两次。很难用格式来判断。灰框肯定是由于打开相机两次;当你试图打开一个不存在的相机时,会出现相同的灰色边框。

第二个代码示例中有一些拼写错误,例如没有冒号的while(c==-1)。你有很多压痕问题。我会避免使用cv绑定。如有可能,请使用cv2。

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

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

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)

最新更新