専門ユニット2/山内研セミナー(2021/12/01)

関連サイトと資料

プログラム

import cv2
from IPython.display import Image, display
  
def imshow(title, img):
    """ndarray 配列をインラインで Notebook 上に表示する。
    """
    ret, encoded = cv2.imencode('.png', img)
    print(title)
    display(Image(encoded))
    

import numpy as np
import os
import cv2
  
def read_images(path, image_size):
  names = []
  training_images, training_labels = [], []
  label = 0
  
  for dirname, subdirnames, filenames in os.walk(path):
    for subdirname in subdirnames:
      names.append(subdirname)
      subject_path = os.path.join(dirname, subdirname)
  
      for filename in os.listdir(subject_path):
        img = cv2.imread(os.path.join(subject_path, filename), cv2.IMREAD_GRAYSCALE)
  
        if img is None:
          # The file cannot be loaded as an image.
          # Skip it.
          continue
        img = cv2.resize(img, image_size)
        training_images.append(img)
        training_labels.append(label)
  
      label += 1
    
  training_images = np.asarray(training_images, np.uint8)
  training_labels = np.asarray(training_labels, np.int32)
  return names, training_images, training_labels
    

import pickle
  
names_filename = 'names.pickle'
path_to_training_images = './face_imgs'
training_image_size = (200, 200)
names, training_images, training_labels = read_images(path_to_training_images, training_image_size)
  
with open(names_filename, 'wb') as f1:
  pickle.dump(names, f1)
    



import cv2
  
model = cv2.face.EigenFaceRecognizer_create()
model.train(training_images, training_labels)
model.save('EigenFace.yaml')      
    

import cv2
import pickle
  
model = cv2.face.EigenFaceRecognizer_create()
model.read('EigenFace.yaml')
  
names_filename = 'names.pickle'
with open(names_filename, 'rb') as f2:
  names = pickle.load(f2)
  
face_cascade = cv2.CascadeClassifier('./cascades/haarcascade_frontalface_default.xml')
  
camera = cv2.VideoCapture(0)
  
# ['ban', 'kato', 'kino', 'yago', 'yama']
colors = [ (0,0,255), (255,0,0), (0,255,0), (0,255,255), (255,0,255)]
while (cv2.waitKey(1) == -1):
  success, frame = camera.read()
  if success:
    faces = face_cascade.detectMultiScale(frame, 1.3, 5)
  
    for (x, y, w, h) in faces:
      cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
      roi_gray = gray[x:x+w, y:y+h]
  
      if roi_gray.size == 0:
        # The ROI is empty. Maybe the face is at the image edge.
        # Skip it.
        continue
  
      roi_gray = cv2.resize(roi_gray, training_image_size)
      label, confidence = model.predict(roi_gray)
      text = f'{names[label]}, confidence={confidence:.2}'
      cv2.putText(frame, text, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, colors[label], 2)
  
    cv2.imshow('Face Recognition', frame)
  
cv2.destroyAllWindows()
camera.release()
    



import cv2
  
model = cv2.face.FisherFaceRecognizer_create()
model.train(training_images, training_labels)
model.save('FisherFace.yaml')      
    

import cv2
import pickle
  
model = cv2.face.FisherFaceRecognizer_create()
model.read('FisherFace.yaml')
  
names_filename = 'names.pickle'
with open(names_filename, 'rb') as f2:
  names = pickle.load(f2)
  
face_cascade = cv2.CascadeClassifier('./cascades/haarcascade_frontalface_default.xml')
  
camera = cv2.VideoCapture(0)
  
# ['ban', 'kato', 'kino', 'yago', 'yama']
colors = [ (0,0,255), (255,0,0), (0,255,0), (0,255,255), (255,0,255)]
while (cv2.waitKey(1) == -1):
  success, frame = camera.read()
  if success:
    faces = face_cascade.detectMultiScale(frame, 1.3, 5)
  
    for (x, y, w, h) in faces:
      cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
      roi_gray = gray[x:x+w, y:y+h]
  
      if roi_gray.size == 0:
        # The ROI is empty. Maybe the face is at the image edge.
        # Skip it.
        continue
  
      roi_gray = cv2.resize(roi_gray, training_image_size)
      label, confidence = model.predict(roi_gray)
      text = f'{names[label]}, confidence={confidence:.2}'
      cv2.putText(frame, text, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, colors[label], 2)
  
    cv2.imshow('Face Recognition', frame)
  
cv2.destroyAllWindows()
camera.release()
    



import cv2
  
model = cv2.face.LBPHFaceRecognizer_create()
model.train(training_images, training_labels)
model.save('LBPHFace.yaml')      
    

import cv2
import pickle
  
model = cv2.face.LBPHFaceRecognizer_create()
model.read('LBPHFace.yaml')      
  
names_filename = 'names.pickle'
with open(names_filename, 'rb') as f2:
  names = pickle.load(f2)
  
face_cascade = cv2.CascadeClassifier('./cascades/haarcascade_frontalface_default.xml')
  
camera = cv2.VideoCapture(0)
  
# ['ban', 'kato', 'kino', 'yago', 'yama']
colors = [ (0,0,255), (255,0,0), (0,255,0), (0,255,255), (255,0,255)]
while (cv2.waitKey(1) == -1):
  success, frame = camera.read()
  if success:
    faces = face_cascade.detectMultiScale(frame, 1.3, 5)
  
    for (x, y, w, h) in faces:
      cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
      roi_gray = gray[x:x+w, y:y+h]
  
      if roi_gray.size == 0:
        # The ROI is empty. Maybe the face is at the image edge.
        # Skip it.
        continue
  
      roi_gray = cv2.resize(roi_gray, training_image_size)
      label, confidence = model.predict(roi_gray)
      text = f'{names[label]}, confidence={confidence:.2}'
      cv2.putText(frame, text, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, colors[label], 2)
  
    cv2.imshow('Face Recognition', frame)
  
cv2.destroyAllWindows()
camera.release()
    



線分検出の新しい方法

import cv2
import numpy as np
  
img = cv2.imread('lines.jpg', cv2.IMREAD_COLOR)
imshow('original', img)
    

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imshow('gray', gray)
    

edges = cv2.Canny(gray, 50, 120)
  
minLineLength = 20
maxLineGap = 5
lines = cv2.HoughLinesP(edges, 1, np.pi/180.0, 20, minLineLength, maxLineGap)
  
color = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
for [[x1, y1, x2, y2]] in lines:
  cv2.line(color, (x1, y1), (x2, y2), (0,255,0),2)
  
imshow("lineHough", color)
    

lsd = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD)
lines, width, prec, nfa = lsd.detect(gray)
  
drawnLines = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
lsd.drawSegments(drawnLines, lines)
  
imshow('lsd', drawnLines)
    



顔の録画

import cv2
import datetime
  
# 現在の日時を取得
t_delta = datetime.timedelta(hours=9)
JST = datetime.timezone(t_delta, 'JST')
now = datetime.datetime.now(JST)
s_now = now.strftime('%Y%m%d_%H%M%S')
  
# パラメータ   
record_time = 10 # 録画時間 [s]
fps = 30 # 1秒あたりのフレーム数(仮定の値)
savename = f'face_{s_now}.avi'
  
cameraCapture = cv2.VideoCapture(0)
size = (int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
videoWriter = cv2.VideoWriter(savename, cv2.VideoWriter_fourcc('I','4','2','0'), fps, size)
  
success, frame = cameraCapture.read()
numFramesRemaining = record_time * fps - 1 # 10 seconds of frames
while success and numFramesRemaining > 0:
    videoWriter.write(frame)
    success, frame = cameraCapture.read()
    numFramesRemaining -= 1
  
videoWriter.release()
cameraCapture.release()
    

AIによる顔検出

import cv2
import os
import numpy as np
  
def show_detection(image, d):
    h, w = image.shape[:2]
    for i in range(0, d.shape[2]):
        confidence = d[0, 0, i, 2]
        if confidence > 0.5:
            box = d[0, 0, i, 3:7] * np.array([w, h, w, h])
            (x1, y1, x2, y2) = box.astype("int")
            cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 3)
    return image
  
target = 'face_20211201_090824.avi'
base, ext = os.path.splitext(target)
output_folder = f'./result/{base}'
if not os.path.exists(output_folder):
  os.makedirs(output_folder)
  
videoCapture = cv2.VideoCapture(target)
  
MODEL_FILE = './caffe/deploy.prototxt'
PARAM_FILE = './caffe/res10_300x300_ssd_iter_140000_fp16.caffemodel'
net = cv2.dnn.readNetFromCaffe(MODEL_FILE, PARAM_FILE)
  
success, frame = videoCapture.read()
count = 1
while success:
    grayImg = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    resultImg = cv2.cvtColor(grayImg, cv2.COLOR_GRAY2BGR)
    
    (h, w) = frame.shape[:2]
    blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), [104., 117., 123.], False, False)
    net.setInput(blob)
    detections = net.forward()
    faceImg = show_detection(resultImg, detections)
    
    savepath = os.path.join(output_folder, f'{count:04}.png')
    cv2.imwrite(savepath, faceImg)
      
    count = count + 1
    success, frame = videoCapture.read()