Skip to content

Instantly share code, notes, and snippets.

@yano
Created August 29, 2017 08:31
Show Gist options
  • Save yano/c9ee4559c028e09269dac6da89a6192d to your computer and use it in GitHub Desktop.
Save yano/c9ee4559c028e09269dac6da89a6192d to your computer and use it in GitHub Desktop.
camera capture and face detect sample
#!/usr/bin/env python
#coding:utf-8
from threading import Thread, Lock
import time
from datetime import datetime
import cv2
import os
if __name__ == '__main__':
# === settings ===
num_of_threads = 4
verbose = True # fps情報,FaceDetect情報をコンソールに表示するかどうか
disp_image = True # 画像をディスプレイに表示するかどうか(テスト用)
exposure = 64
gain = 96
# ================
video_capture = cv2.VideoCapture(0)
# 取得する画面の解像度を変更する
video_capture.set(3, 960)
video_capture.set(4, 720)
# ロックありの共有オブジェクトを使う場合
faceCascade = cv2.CascadeClassifier("/home/ydk/env35a/src/test_opencv/haarcascade_frontalface_alt.xml")
startTime = time.time()
frame_index = 0
while True:
ret, frameTmp = video_capture.read()
gray = cv2.cvtColor(frameTmp, cv2.COLOR_BGR2GRAY)
# 顔検出
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.31, # 1.11-1.51
minNeighbors=2,
minSize=(50, 50), # 56
maxSize=(448, 448),
flags=cv2.CASCADE_SCALE_IMAGE)
rect_info = ""
num_face = 0
# 同じところを2回検出するのを防止する
center_x = 0
center_y = 0
for (x, y, w, h) in faces:
cv2.rectangle(frameTmp, (x, y), (x+w, y+h), (0, 0, 255), 5)
cv2.imshow('Video', frameTmp)
cv2.waitKey(1) # 1[ms] wait
video_capture.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment