import cv2
def faceDetect(img, face_cascade):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 2)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
return img
def main():
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('D:\opencv\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
#frame = imread("2017-02-26-200818.jpg")
# Our operations on the frame come here
if ret == True:
frame = faceDetect(frame, face_cascade)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
main()
Python之opencv应用
最新推荐文章于 2021-10-14 10:41:50 发布