【圖像處理】人臉檢測opencv

【圖像處理】人臉檢測opencv

import cv2


classifier = cv2.CascadeClassifier('D:\software\opencv\sources\data\haarcascades\haarcascade_frontalface_alt2.xml')
color = (255,0,0)

cap = cv2.VideoCapture()
cap.open('Megamind.avi')

fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', '2')

initflag = True
while 1:
	ret,frame = cap.read()
	if ret == False:
		break
	if initflag:
		rows,cols,chans = frame.shape
		save = cv2.VideoWriter('facedetect.avi', fourcc, 25.0, (cols,rows),True)
		initflag = False
	gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
	#t1 = cv2.getTickCount()
	faceRects = classifier.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=3, minSize=(32,32))re
	#t1 = cv2.getTickCount() - t1
	#print('cost time: %.3f', t1 * 1000 / cv2.getTickFrequency())
	if len(faceRects):
		for rect in faceRects:
			x,y,w,h = rect
			cv2.rectangle(frame, (x,y), (x+w,y+h), color, 2)
	save.write(frame)
	cv2.imshow('facedetect.jpg', frame)
	cv2.waitKey(20)

cv2.destroyAllWindows()
cap.release()
save.release()

facedetect.avi

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章