最近我一直在使用python 3和opencv以及我的笔记本电脑摄像头进行面部追踪,我已经做好了,当你运行.pyw脚本时,会弹出一个窗口并显示网络摄像头,我疯狂的一个python脚本,用haarcascades检测冻结图像上的脸,但我似乎无法将两者结合起来, 所以这是网络摄像头程序;
import cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
vc.release()
cv2.destroyWindow("preview")
这是面部检测脚本,我在一个带有haar xml文件和jpg的文件夹中,我创建了一个脚本的桌面快捷方式,通过双击图片和正面xml运行它捷径。
import cv2
import sys
import time
imagePath = sys.argv[1]
cascPath = sys.argv[2]
faceCascade = cv2.CascadeClassifier(cascPath)
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(30, 30))#,flags = cv2.cv.CV_HAAR_IMAGE_SCALE)
print("Found {0} faces!".format(len(faces)))
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("Faces found" ,image)
cv2.waitKey(0)&0xFF
请记住我需要做的就是使用第一段代码制作一个实时面部跟踪器,同时注意我是初学者,所以请解释 - 谢谢!
哦,是的,结合起来他们看起来像;
import cv2
import sys
import time
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
imagePath = sys.argv[1]
cascPath = sys.argv[2]
faceCascade = cv2.CascadeClassifier(cascPath)
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize=(30, 30))#,flags = cv2.cv.CV_HAAR_IMAGE_SCALE)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
vc.release()
cv2.destroyWindow("preview")
我的错误如下,
追踪(最近一次通话): 文件“C:\ Users \ tcwissemann \ Desktop \ WebDetect.pyw”,第8行,in imagePath = sys.argv [1] IndexError:列表索引超出范围