Face_mosaic - Mosaic blur processing is applied to multiple faces appearing in the video

Overview

動機

face_recognitionを使用して得られる顔座標は長方形であり、この座標をそのまま用いてぼかし処理を行った場合得られる画像は醜い。

それに対してモザイクによってぼかし処理を行うと自然に見える。

これを静止画だけでなく動画にも適用したい。
同じ働きを持つものはORB-HD/defaceが既に存在する。defacecenterface.onnxを用いるのに比べこちらはdlibを使用する。これらは顔検出のみか顔認識まで行うかで使い分けすると良いと思う。

方法

静止画の場合

face_recognitionのメソッドであるface_recognition.face_locationsの返り値は(top, right, bottom, left)の配列。

顔周りの長方形をぼかす

original.png
original.png

import cv2
import face_recognition

image = cv2.imread('img/original.png')
face_location_list = face_recognition.face_locations(
    image, 0, 'cnn')
if len(face_location_list) > 0:
    dst = image.copy()
    for (top, right, bottom, left) in face_location_list:
        dst[top:top + (bottom - top), left:left + (right - left)] = cv2.blur(
            dst[top:top + (bottom - top), left:left + (right - left)], (50, 50))
    cv2.imwrite('img/rectangle_blur.png', dst)

モザイク処理

モザイク処理の方法はnote.nkmk.me Python, OpenCVで画像にモザイク処理(全面、一部、顔など)様よりアイデアを頂戴致しました。

def mosaic(src, ratio):
    small = cv2.resize(src, None, fx=ratio, fy=ratio,
                       interpolation=cv2.INTER_NEAREST)
    return cv2.resize(small, src.shape[:2][::-1], interpolation=cv2.INTER_NEAREST)


if len(face_location_list) > 0:
    dst = image.copy()
    for (top, right, bottom, left) in face_location_list:
        dst[top:top + (bottom - top), left:left + (right - left)] = mosaic(
            dst[top:top + (bottom - top), left:left + (right - left)], 0.1)
    cv2.imwrite('img/rectangle_mosaic.png', dst)

動画に適用

Pixabayが提供するOybek Ostanovの動画をvideo.mp4としてblur処理を行う。

import cv2
import face_recognition
import PySimpleGUI as sg

vcap = cv2.VideoCapture('img/video.mp4', cv2.CAP_FFMPEG)


def mosaic(src, ratio):
    small = cv2.resize(src, None, fx=ratio, fy=ratio,
                       interpolation=cv2.INTER_NEAREST)
    return cv2.resize(small, src.shape[:2][::-1], interpolation=cv2.INTER_NEAREST)


sg.theme('Reddit')
layout = [
    [sg.Text('blur')],
    [sg.Image(key='display')],
    [sg.Button('terminate', key='terminate', button_color='red')]
]
window = sg.Window('blur', layout, location=(50, 50))

while True:
    ret, frame = vcap.read()
    if ret == False:
        break
    face_location_list = face_recognition.face_locations(
        frame, 0, 'cnn')

    if len(face_location_list) > 0:
        dst = frame.copy()
        for (top, right, bottom, left) in face_location_list:
            # rectangle blur ======
            # dst[top:top + (bottom - top), left:left + (right - left)] = cv2.blur(
            #     dst[top:top + (bottom - top), left:left + (right - left)], (50, 50))
            # mosaic blur =========
            dst[top:top + (bottom - top), left:left + (right - left)] = mosaic(
                dst[top:top + (bottom - top), left:left + (right - left)], 0.1)

    event, _ = window.read(timeout=1)
    imgbytes = cv2.imencode(".png", dst)[1].tobytes()
    window["display"].update(data=imgbytes)
    if event == 'terminate':
        break
vcap.release()

結果


期待通りの結果が得られました。

Reference

Author

yKesamaru

License

MIT License

Owner
Yoshitsugu Kesamaru
Face recognition and others.
Yoshitsugu Kesamaru
Image augmentation for machine learning experiments.

imgaug This python library helps you with augmenting images for your machine learning projects. It converts a set of input images into a new, much lar

Alexander Jung 13.2k Jan 02, 2023
SemTorch

SemTorch This repository contains different deep learning architectures definitions that can be applied to image segmentation. All the architectures a

David Lacalle Castillo 154 Dec 07, 2022
Pytorch implementation of PSEnet with Pyramid Attention Network as feature extractor

Scene Text-Spotting based on PSEnet+CRNN Pytorch implementation of an end to end Text-Spotter with a PSEnet text detector and CRNN text recognizer. We

azhar shaikh 62 Oct 10, 2022
OCR system for Arabic language that converts images of typed text to machine-encoded text.

Arabic OCR OCR system for Arabic language that converts images of typed text to machine-encoded text. The system currently supports only letters (29 l

Hussein Youssef 144 Jan 05, 2023
Tool which allow you to detect and translate text.

Text detection and recognition This repository contains tool which allow to detect region with text and translate it one by one. Description Two pretr

Damian Panek 176 Nov 28, 2022
Python Computer Vision application that allows users to draw/erase on the screen using their webcam.

CV-Virtual-WhiteBoard The Virtual WhiteBoard is a project I made using the OpenCV and Mediapipe Python libraries. Using your index and middle finger y

Stephen Wang 1 Jan 07, 2022
Crop regions in napari manually

napari-crop Crop regions in napari manually Usage Create a new shapes layer to annotate the region you would like to crop: Use the rectangle tool to a

Robert Haase 4 Sep 29, 2022
Self-supervised Equivariant Attention Mechanism for Weakly Supervised Semantic Segmentation, CVPR 2020 (Oral)

SEAM The implementation of Self-supervised Equivariant Attention Mechanism for Weakly Supervised Semantic Segmentaion. You can also download the repos

Hibercraft 459 Dec 26, 2022
Generate a list of papers with publicly available source code in the daily arxiv

2021-06-08 paper code optimal network slicing for service-oriented networks with flexible routing and guaranteed e2e latency networkslicing multi-moda

79 Jan 03, 2023
Super Mario Game With Python

Super_Mario Hello all this is a simple python program which tries to use our body as a controller for the super mario game Here I have used media pipe

Adarsh Badagala 219 Nov 25, 2022
A real-time dolly zoom camera effect

Dolly-Zoom I've always been amazed by the gradual perspective change of dolly zoom, and I have some experience in python and OpenCV, so I decided to c

Dylan Kai Lau 52 Dec 08, 2022
Python Computer Vision Aim Bot for Roblox's Phantom Forces

Python-Phantom-Forces-Aim-Bot Python Computer Vision Aim Bot for Roblox's Phanto

drag0ngam3s 2 Jul 11, 2022
Um RPG de texto orientado a objetos.

RPG de texto Um RPG de texto orientado a objetos, sem história. Um RPG (Role-playing game) baseado em texto em que você pode viajar para alguns locais

Vinicius 3 Oct 05, 2022
Opencv face recognition desktop application

Opencv-Face-Recognition Opencv face recognition desktop application Program developed by Gustavo Wydler Azuaga - 2021-11-19 Screenshots of the program

Gus 1 Nov 19, 2021
scantailor - Scan Tailor is an interactive post-processing tool for scanned pages.

Scan Tailor - scantailor.org This project is no longer maintained, and has not been maintained for a while. About Scan Tailor is an interactive post-p

1.5k Dec 28, 2022
Kornia is a open source differentiable computer vision library for PyTorch.

Open Source Differentiable Computer Vision Library

kornia 7.6k Jan 06, 2023
Detect the mathematical formula from the given picture and the same formula is extracted and converted into the latex code

Mathematical formulae extractor The goal of this project is to create a learning based system that takes an image of a math formula and returns corres

6 May 22, 2022
CNN+Attention+Seq2Seq

Attention_OCR CNN+Attention+Seq2Seq The model and its tensor transformation are shown in the figure below It is necessary ch_ train and ch_ test the p

Tsukinousag1 2 Jul 14, 2022
An Agnostic Computer Vision Framework - Pluggable to any Training Library: Fastai, Pytorch-Lightning with more to come

An Agnostic Object Detection Framework IceVision is the first agnostic computer vision framework to offer a curated collection with hundreds of high-q

airctic 790 Jan 05, 2023