瞳検出を行って、4秒間瞳を検出されなかったときにcloseと表示、通知がいくようにしたいのですがどうすればいいですか
該当のプログラム
python
1if (left_eye_ear + right_eye_ear) < 0.45: 2 cv2.putText(rgb,"close", 3 (10,180), cv2.FONT_HERSHEY_PLAIN, 3, (0,0,255), 3, 1) 4 5 6 line_notify_token = 'token' 7 line_notify_api = 'https://notify-api.line.me/api/notify' 8 message = 'message' 9 10 payload = {'message': message} 11 headers = {'Authorization': 'Bearer ' + line_notify_token } # 発行したトークン 12 line_notify = requests.post(line_notify_api, data=payload, headers=headers) 13 14 cv2.imshow('frame_resize', face_gray_resized) 15 16 fps = cv2.getTickFrequency() / (cv2.getTickCount() - tick) 17 cv2.putText(rgb, "FPS:{} ".format(int(fps)), 18 (10, 50), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2, cv2.LINE_AA) 19 20 cv2.imshow('frame', rgb) 21``` 22 23全体のプログラム 24```python 25import os,sys 26import cv2 27import dlib 28from imutils import face_utils 29from scipy.spatial import distance 30import requests 31 32 33cap = cv2.VideoCapture(0) 34 35 36face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml') 37face_parts_detector = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') 38 39def calc_ear(eye): 40 A = distance.euclidean(eye[1], eye[5]) 41 B = distance.euclidean(eye[2], eye[4]) 42 C = distance.euclidean(eye[0], eye[3]) 43 eye_ear = (A + B) / (2.0 * C) 44 return round(eye_ear, 3) 45 46def eye_marker(face_mat, position): 47 for i, ((x, y)) in enumerate(position): 48 cv2.circle(face_mat, (x, y), 1, (255, 255, 255), -1) 49 cv2.putText(face_mat, str(i), (x + 2, y - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 255, 255), 1) 50 51while True: 52 tick = cv2.getTickCount() 53 54 ret, rgb = cap.read() 55 gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY) 56 faces = face_cascade.detectMultiScale( 57 gray, scaleFactor=1.11, minNeighbors=3, minSize=(100, 100)) 58 59 if len(faces) == 1: 60 x, y, w, h = faces[0, :] 61 cv2.rectangle(rgb, (x, y), (x + w, y + h), (255, 0, 0), 2) 62 63 face_gray = gray[y :(y + h), x :(x + w)] 64 scale = 480 / h 65 face_gray_resized = cv2.resize(face_gray, dsize=None, fx=scale, fy=scale) 66 67 face = dlib.rectangle(0, 0, face_gray_resized.shape[1], face_gray_resized.shape[0]) 68 face_parts = face_parts_detector(face_gray_resized, face) 69 face_parts = face_utils.shape_to_np(face_parts) 70 71 left_eye = face_parts[42:48] 72 eye_marker(face_gray_resized, left_eye) 73 74 left_eye_ear = calc_ear(left_eye) 75 cv2.putText(rgb, "LEFT eye EAR:{} ".format(left_eye_ear), 76 (10, 100), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 1, cv2.LINE_AA) 77 78 right_eye = face_parts[36:42] 79 eye_marker(face_gray_resized, right_eye) 80 81 right_eye_ear = calc_ear(right_eye) 82 cv2.putText(rgb, "RIGHT eye EAR:{} ".format(round(right_eye_ear, 3)), 83 (10, 120), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), 1, cv2.LINE_AA) 84 85 if (left_eye_ear + right_eye_ear) < 0.45: 86 cv2.putText(rgb,"close", 87 (10,180), cv2.FONT_HERSHEY_PLAIN, 3, (0,0,255), 3, 1) 88 89 90 line_notify_token = 'token' 91 line_notify_api = 'https://notify-api.line.me/api/notify' 92 message = 'message' 93 94 payload = {'message': message} 95 headers = {'Authorization': 'Bearer ' + line_notify_token } # 発行したトークン 96 line_notify = requests.post(line_notify_api, data=payload, headers=headers) 97 98 cv2.imshow('frame_resize', face_gray_resized) 99 100 fps = cv2.getTickFrequency() / (cv2.getTickCount() - tick) 101 cv2.putText(rgb, "FPS:{} ".format(int(fps)), 102 (10, 50), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2, cv2.LINE_AA) 103 104 cv2.imshow('frame', rgb) 105 if cv2.waitKey(1) == 27: 106 break # esc to quit 107 108cap.release() 109cv2.destroyAllWindows() 110```
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/28 06:53
2020/07/28 07:45
2020/07/28 10:16
2020/07/29 00:24
2020/07/29 01:41
2020/07/29 01:58
2020/07/29 03:12
2020/07/29 04:07
2020/07/29 04:21