我在使用 python 程序时遇到了一些问题,该程序旨在从视频中检测面部并在检测到面部时拍照。
一每当我单击“运行模块”时,它都会运行该程序。但是如果我在第一次之后尝试运行它,我会收到一条错误消息并且它不会运行。要再次运行它,我必须关闭 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
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)
三因为我能够成功运行一次程序,所以我知道它在检测到人脸时会拍照,遗憾的是它会继续拍照,并且在检测到第一张人脸后就不会停止。我只想拍一张照片。有谁知道如何解决这个问题?
总之,我知道这是很多信息和一个非常大的问题,但是如果有人对如何修复程序有任何想法,那么程序将只运行一次,然后需要重新启动问题、灰色视频源和/或仅拍摄一张图有问题请告诉我!谢谢!(如果我的缩进在这里看起来有点滑稽,也很抱歉......)