[PYTHON] I tried to become an Ann Man using OpenCV

Introduction

Synopsis

-The other day, the video recognition did not go well using the fitted OpenCV, but specifically, an error occurred in cv2.imshow It was occurring and remained unsolvable. ――Suddenly, when I tried to recognize if the video was useless but the image could be recognized, the same error occurred, so I found that not only the video but also the image was in a bad state. ――By the way, previously was working with images! When I tried it, it seemed that it did not work on OpenCV3 and worked normally on OpenCV2. --If you use OpenCV2 via Homebrew or pip under Mac environment, it seems that you can only install it with Python2, so I decided to do it there this time. ――I thought it would be interesting as a story, and I thought that my child might be happy with this, so I made it with a simple idea, but it became quite complicated.

reference

Create miscellaneous Photoshop video with Python + OpenCV ② Create still image Photoshop

environment

Python2.7

brew install python

PATH setting

if [ -d $(brew --prefix)/lib/python2.7/site-packages ];then
  export PYTHONPATH=$(brew --prefix)/lib/python2.7/site-packages:$PYTHONPAT
fi

OpenCV2

brew install opencv

Numpy

pip install numpy

Webcam

I'm using the camera that came with my MacBook.

Preparation

Get the image of Ann Man

Have them made at the bread factory.

Make a transparent image

Reference anpan.png

Source code

I haven't done that much, so I haven't added much comment.

webcam.py


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

import cv2
import numpy as np
from PIL import Image

def facedetect(face_cascade, cap, image):
    cnt = 0

    while(True):
        ret, frame = cap.read()

        if ret == False:
            break
        else:
            if (cnt % 10) == 0:
                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

                facerect = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=1, minSize=(1, 1))

            cnt += 1

            if len(facerect) > 0:
                for rect in facerect:

                    x = rect[0]
                    y = rect[1]
                    w = rect[2]
                    h = rect[3]

                    #Adjustment may be necessary depending on the shape of the face
                    x = x - w / 2
                    y = y - h / 1.8
                    w = w * 1.7
                    h = h * 1.7

                    x = int(round(x))
                    y = int(round(y))
                    w = int(round(w))
                    h = int(round(h))

                    #Resize the image to be composited to fit the rectangle.
                    image = cv2.resize(image, (w, h))

                    #Combines the image of the camera face.
                    frame = overlay(frame, image, x, y)

                    #Use this if you want to perform mosaic processing.
                    # dst = frame[y:y+h, x:x+w]
                    # blur = cv2.blur(dst, (50, 50))
                    # frame[y:y+h, x:x+w] = blur

        cv2.imshow('fram', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

def overlay(frame, image, x, y):
    height, width = image.shape[:2]

    layer1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    layer2 = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA)

    layer1 = Image.fromarray(layer1)
    layer2 = Image.fromarray(layer2)

    layer1 = layer1.convert('RGBA')
    layer2 = layer2.convert('RGBA')

    tmp = Image.new('RGBA', layer1.size, (255, 255, 255, 0))
    tmp.paste(layer2, (x, y), layer2)

    result = Image.alpha_composite(layer1, tmp)

    return cv2.cvtColor(np.asarray(result), cv2.COLOR_RGBA2BGRA)

if __name__ == '__main__':
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
    image = cv2.imread('anpan.png', cv2.IMREAD_UNCHANGED)

    cap = cv2.VideoCapture(0)
    cap.set(3, 640)
    cap.set(4, 480)

    facedetect(face_cascade, cap, image)

result

スクリーンショット 2017-06-26 3.05.34.png

Summary

――It was helpful to have unexpected information. --Slight adjustment was needed to match the frame with the degree of recognition. ――It was necessary to make adjustments to draw a rectangle on the entire head and above the shoulders, which only recognizes the face by default. ――I feel that if you recognize the torso and cover it, it will look a little more like that. ――This time you can challenge SSD. (Should) ――I would like to try it if I can make it a smartphone app.

All page links

-I tried to become an Ann Man using OpenCV -I made a multiplayer video chat using SkyWay -I tried face recognition for video chat using SkyWay

Recommended Posts

I tried to become an Ann Man using OpenCV
I tried to get an AMI using AWS Lambda
I tried using GrabCut of OpenCV
I tried using Azure Speech to Text.
I tried to classify text using TensorFlow
I tried to digitize the stamp stamped on paper using OpenCV
I tried to predict Covid-19 using Darts
I tried to make an image similarity function with Python + OpenCV
I want to display an image on Jupyter Notebook using OpenCV (mac)
I tried to get an image by scraping
I tried object detection using Python and OpenCV
I tried to synthesize WAV files using Pydub.
I tried to detect an object with M2Det!
I tried using the image filter of OpenCV
I tried to make a ○ ✕ game using TensorFlow
I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried using aiomysql
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried to debug.
I tried using ngrok
I tried using face_recognition
I tried to paste
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
I tried to create an environment to check regularly using Selenium with AWS Fargate
I tried to extract features with SIFT of OpenCV
I tried to implement an artificial perceptron with python
I tried to approximate the sin function using chainer
I tried to make an OCR application with PySimpleGUI
I tried to identify the language using CNN + Melspectogram
I tried to access Google Spread Sheets using Python
I tried to find an alternating series with tensorflow
I tried to complement the knowledge graph using OpenKE
I tried to draw a configuration diagram using Diagrams
I tried to compress the image using machine learning
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
I tried to operate from Postman using Cisco Guest Shell as an API server
I tried to learn PredNet
[I tried using Pythonista 3] Introduction
I tried using easydict (memo).
I tried to organize SVM.
I tried face recognition using Face ++
I tried using Random Forest
I tried to implement PCANet
I tried using Amazon Glacier