实验环境
- 带摄像头笔记本一台windows MacOS均可
- anaconda+pycharm
软件环境配置
anaconda到官网下载,我选择的Python3.7版本,下载后安装即可。
PyCharm到官网下载,下载后安装即可。
上源码
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.flip(frame, 1, frame)
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")
face = face_cascade.detectMultiScale(frame)
eye = eye_cascade.detectMultiScale(frame)
for (x, y, w, h) in face:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
for (x, y, w, h) in eye:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.waitKey(1) & 0xFF == ord('s'):
cv2.imwrite("a.jpg", frame)
cap.release()
网友评论