I made a Dir en gray face classifier using TensorFlow-④ Face extraction

Introduction

――You can execute it without knowing the characteristics of OpenCV so much, but the key is whether OpenCV can be read normally.

program

face_detect.py


# -*- coding:utf-8 -*-

import cv2
import numpy as np
import os.path
from pathlib import Path

#Directory with the image data collected earlier
input_data_path = '{Directory path}'

#Directory to save the cropped image
save_path = '{Directory path}'
#Create a destination directory
Path(save_path).mkdir(parents=True, exist_ok=True)

#OpenCV default classifier path.(https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.Use xml file)
cascade_path = '/usr/local/Cellar/opencv3/3.2.0/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml'
faceCascade = cv2.CascadeClassifier(cascade_path)

#Number of images collected(Optional change)
image_count = 1100

#Number of successful face detections(Specify 0 by default)
face_detect_count = 0

#When a face is detected from the collected image data, cut it out and save it.
for i in range(image_count):
  file_name = input_data_path + str(i) + ".jpg "
  if os.path.isfile(file_name):
    img = cv2.imread(file_name, cv2.IMREAD_COLOR)
    assert img is not None, Path(file_name).absolute()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    face = faceCascade.detectMultiScale(gray, 1.1, 3)

    if len(face) > 0:
      for rect in face:
        #Surround the face recognition part with a red line and save(I don't need this part now)
        # cv2.rectangle(img, tuple(rect[0:2]), tuple(rect[0:2]+rect[2:4]), (0, 0,255), thickness=1)
        # cv2.imwrite('detected.jpg', img)
        x = rect[0]
        y = rect[1]
        w = rect[2]
        h = rect[3]

        cv2.imwrite(save_path + '{file name}' + str(face_detect_count) + '.jpg', img[y:y+h, x:x+w])
        face_detect_count = face_detect_count + 1
    else:
      print('image' + str(i) + ':No Face')
  else:
      print('image' + str(i) + ':No File')

Run

python face_detect.py

Supplements and excuses

--find and find haarcascade_frontalface_default.xml. --There are some files whose images cannot be read well. There are quite a few. --There are some files that you can read the image but cannot extract the face well, so please delete it by hand.

All page links

-I made a Dir en gray face classifier using TensorFlow --(1) Introduction -I made a face classifier for Dir en gray using TensorFlow-② Environment construction -I made a face classifier for Dir en gray using TensorFlow-③ Image collection -I made a face classifier for Dir en gray using TensorFlow-④ Face extraction -I made a face classifier for Dir en gray using TensorFlow-⑤ Learning data preparation -I made a Dir en gray face classifier using TensorFlow-⑥ Learning program -I made a face classifier for Dir en gray using TensorFlow-⑦ Learning model -I made a face classifier for Dir en gray using TensorFlow-⑧ Learning execution -I made a Dir en gray face classifier using TensorFlow --⑨ Data visualization -I made a Dir en gray face classifier using TensorFlow --⑩ Face classification test -I made a Dir en gray face classifier using TensorFlow-⑪ Web release preparation -I made a Dir en gray face classifier using TensorFlow --⑫ Web release -I tried to make a Dir en gray face classifier using TensorFlow --⑬ Playing (final)

Recommended Posts

I made a Dir en gray face classifier using TensorFlow-④ Face extraction
I made a Dir en gray face classifier using TensorFlow --(1) Introduction
I made a Dir en gray face classifier using TensorFlow --⑩ Face classification test
I made a Dir en gray face classifier using TensorFlow --⑥ Learning program
I made a Dir en gray face classifier using TensorFlow --⑬ Playing (final)
I made a Dir en gray face classifier using TensorFlow --⑫ Web release
I made a Dir en gray face classifier using TensorFlow --② Environment construction
I made a Dir en gray face classifier using TensorFlow --⑪ Web release preparation
I made a VGG16 model using TensorFlow (on the way)
I made a Line-bot using Python!
Make a face recognizer using TensorFlow
Face detection using a cascade classifier
I tried playing a ○ ✕ game using TensorFlow
Beginner: I made a launcher using dictionary
I tried to make a ○ ✕ game using TensorFlow
〇✕ I made a game
I made a login / logout process using Python Bottle.
[Python] I made a classifier for irises [Machine learning]
I made a school festival introduction game using Ren’py
I made a quick feed reader using feedparser in Python
I tried hosting a TensorFlow deep learning model using TensorFlow Serving
I made a face diagnosis AI for a female professional golfer ③
I made a muscle training estimation app using Qore SDK
I made a Chatbot using LINE Messaging API and Python
I tried face recognition using Face ++
I made a python text
I tried using magenta / TensorFlow
I made a discord bot
I tried scoring a transvestite contest using Face ++'s Detect API
I made a poker game server chat-holdem using websocket with python
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
[Kaggle] I made a collection of questions using the Titanic tutorial
I made a C ++ learning site
I made a CUI-based translation script (2)
I made a fortune with Python.
I made a CUI-based translation script
I made a daemon with Python
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
I made an image discrimination (cifar10) model using a convolutional neural network.