Created
August 28, 2018 21:56
-
-
Save shimabox/b1db30c4fb6368f8ac73c8d56632c2cf to your computer and use it in GitHub Desktop.
目をつぶったら画面キャプチャを終了するやつ(Python+OpenCV)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import cv2 # OpenCV のインポート | |
import time | |
from datetime import datetime | |
''' | |
参考 | |
@link http://ensekitt.hatenablog.com/entry/2017/12/19/200000 | |
@link https://note.nkmk.me/python-opencv-face-detection-haar-cascade/ | |
@link https://note.nkmk.me/python-opencv-mosaic/ | |
@link http://workpiles.com/2015/04/opencv-detectmultiscale-scalefactor/ | |
''' | |
# VideoCaptureのインスタンスを作成する。 | |
# 引数でカメラを選べれる。 | |
cap = cv2.VideoCapture(0) | |
if cap.isOpened() is False: | |
print("can not open camera") | |
sys.exit() | |
# 経過時間を描画 | |
def draw_elapsed_time(frame, start_time, closed_time=0): | |
now = closed_time if closed_time > 0 else int(datetime.now().timestamp()) | |
cv2.putText( | |
frame, | |
str(now - start_time), | |
(10, 50), | |
cv2.FONT_HERSHEY_PLAIN, | |
3, | |
(0, 255,0), | |
3, | |
cv2.LINE_AA | |
) | |
# 評価器を読み込み | |
# https://github.com/opencv/opencv/tree/master/data/haarcascades | |
cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_alt2.xml') | |
eye_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_eye_tree_eyeglasses.xml') | |
closed_eyes = False | |
is_started = False | |
start_time = 0 | |
closed_time = 0 | |
while True: | |
# VideoCaptureから1フレーム読み込む | |
ret, frame = cap.read() | |
# そのままの大きさだときついのでリサイズ | |
frame = cv2.resize(frame, (int(frame.shape[1]*0.7), int(frame.shape[0]*0.7))) | |
if closed_eyes == True: | |
draw_elapsed_time(frame=frame, start_time=start_time, closed_time=closed_time) | |
cv2.imshow('frame', frame) # 目が閉じられたであろう瞬間を残す | |
break | |
if is_started == True: | |
# 経過時間を描画 | |
draw_elapsed_time(frame=frame, start_time=start_time) | |
# 処理速度を高めるために画像をグレースケールに変換したものを用意 | |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
# 顔検出 | |
facerect = cascade.detectMultiScale( | |
gray, | |
scaleFactor=1.11, | |
minNeighbors=3, | |
minSize=(100, 100) | |
) | |
if len(facerect) != 0: | |
if is_started == False: # まだスタートしてなくて | |
key = cv2.waitKey(100) | |
if key == 115: # s が押されたら | |
is_started = True | |
start_time = int(datetime.now().timestamp()) | |
for x, y, w, h in facerect: | |
# 顔の部分 | |
face_gray = gray[y: y + h, x: x + w] | |
# 顔の部分から目の検出 | |
eyes = eye_cascade.detectMultiScale( | |
face_gray, | |
scaleFactor=1.11, # ここの値はPCのスペックに依存するので適宜修正してください | |
minNeighbors=3, | |
minSize=(15, 15) | |
) | |
if len(eyes) == 0 and is_started == True: | |
# 目が閉じられたとみなす | |
closed_time = int(datetime.now().timestamp()) | |
closed_eyes = True | |
break | |
elif is_started == True: | |
# 顔検出できなければ目が閉じられたとみなす | |
closed_time = int(datetime.now().timestamp()) | |
closed_eyes = True | |
continue | |
cv2.imshow('frame', frame) | |
# キー入力を1ms待って、k が27(ESC)だったらBreakする | |
k = cv2.waitKey(1) | |
if k == 27: | |
break | |
# 後処理 | |
if closed_eyes == True: | |
while True: | |
k = cv2.waitKey(100) | |
if k == 27: # ESC が押されたらclose | |
break | |
cap.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment