欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

dlib库眨眼

时间:2023-05-24
先抄为敬

import cv2import cv2 as cvimport dlibimport imutilsfrom imutils import face_utilsfrom scipy.spatial import distance as distdef eye_aspect_ratio(eye): a = dist.euclidean(eye[1], eye[5]) b = dist.euclidean(eye[2], eye[4]) c = dist.euclidean(eye[0], eye[3]) return (a + b) / (2.0 * c)EYE_AR_THRESH = 0.3EYE_AR_frameS = 3COUNTER = 0TOTAL = 0detector = dlib.get_frontal_face_detector()predictor = dlib.shape_predictor('./1.dat')(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"](rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]cap = cv.VideoCapture(0, cv.CAP_DSHOW)while True: ret, frame = cap.read() frame = imutils.resize(frame, width=450) # 设置宽度 ·450 gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) rs = detector(gray, 0) for rect in rs: shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) ear = (leftEAR + rightEAR) / 2.0 leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) if ear < EYE_AR_THRESH: COUNTER += 1 else: if COUNTER >= EYE_AR_frameS: TOTAL += 1 COUNTER = 0 cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.putText(frame, "EAR: {:.2f}".format(ear), (300, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) cv2.imshow("frame", frame) if cv2.waitKey(1) & 0xFF == 27: breakcv2.destroyAllWindows()cap.release()

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。