[PYTHON] The best tool to protect your privacy from your photos ...!

Introduction

It goes without saying that the world where information is exchanged these days and the privacy of people are regarded as important. Because of this era, we need something that protects our privacy ... I thought so, I thought about this.

** Should I be able to look at the photos ...? **

So I made it.

(Honestly, I made it without thinking.)

To put a line of sight

I realized that in order to get a glance, you first need to recognize your eyes and face. Therefore, I decided to use a library that recognizes faces and a library that recognizes eyes in OpenCV data. I don't know the details, but somehow only the face recognition code came down, so I played with it a little and completed the eye recognition program. (The library that came down is [from here](https://qiita.com/GoshBird/items/c0c241d08087c70aa394#%E7%94%BB%E5%83%8F%E3%82%92%E8%AA%AD% E3% 82% 80) Please see.)

Here is a photo created using it. The face is plotted as a square and the eyes are plotted as a circle. EJ4zPDIUEAAZevu.jpeg It looks like a child is making a big hole with a red pen in the hole in the picture. However, there is a problem with this photo, and you can see that it recognizes the nose, mouth, and even the double chin as eyes. This cannot protect your privacy ...!

So I thought about facial features. Probably, if the brain is not very big, the eyes should be above half of the face. Therefore, I decided to make the eyes recognize only the points that are more than half above the square of the face.

By doing so, I was able to write the relationship between the face and the eyes, so the size of the square of the line of sight conforms to the square of the face in width, and the vertical is the top of all the places recognized as eyes. I tried to adopt the part of and the bottom part.

program

The program written with this is the program below.

Wanted_photo.ipynb


import cv2
import matplotlib.pyplot as plt

image_name = input("File Name?")

src=cv2.imread(image_name)
image_gray=cv2.cvtColor(src,cv2.COLOR_BGR2GRAY) #Grayscale
color=(0,0,255) #Kakomu guy(Red)
color_c=(255,0,0) #The one who hits(point,Blue)
cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml") #Import training data
# eye_right = cv2.CascadeClassifier("haarcascade_righteye_2splits.xml")#Right eye data
# eye_left = cv2.CascadeClassifier("haarcascade_lefteye_2splits.xml")#Left eye data
eyecascade = cv2.CascadeClassifier("haarcascade_eye.xml")#Eye data

#A spell to manage
facerect = cascade.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=1, minSize=(50,50))
# reyerect = eye_right.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=1, minSize=(30,30))
# leyerect = eye_left.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=1, minSize=(30,30))
eyerect = eyecascade.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=1, minSize=(30,30))

max_width = 0
upper_heigh = 99999
lower_heigh = 0
max_rect = [0,0,0,0]

        
if len(facerect) > 0:#The one who decides the width
    for rect in facerect:
        #print(rect[0:4])
        if rect[2] > max_width:
            max_width = rect[2] - rect[0]
            max_rect = rect[0:4]
            
if len(eyerect) > 0:#The one who decides the vertical width
    for rect in eyerect:
        #print(rect[0:4])
        if max_rect[0] < rect[0] and max_rect[0] + max_rect[2] > rect[0] + rect[2] and max_rect[1] < rect[1] and max_rect[1] + max_rect[3] /2 > rect[1] + rect[3]/2:
            if upper_heigh >rect[1]:
                upper_heigh = rect[1]
            elif lower_heigh < rect[1] + rect[3]:
                lower_heigh = rect[1] + rect[3]
                
rect[0:4] = [max_rect[0],upper_heigh,max_rect[2],lower_heigh - upper_heigh]#The guy who puts his eyes on
cv2.rectangle(src,tuple(rect[0:2]),tuple(rect[0:2]+rect[2:4]),(0,0,0),thickness = -10)
print(rect[0:4])
        
cv2.imwrite(image_name+"_result_eye.jpg ",src)#The one to save
cv2.destroyAllWindows() #Feeling of open memory

There are some traces of trial and error, but don't worry!

result

So, when I processed the photo above, it looked like this. dandaIMGL5054_TP_V.jpg_result_eye.jpg Good vibes! IVOL_STUDIO_005_TP_V4.jpg_result_eye.jpg The best!

So, I was able to protect the privacy of the world.

Problems with this program

That said, in fact, it is a specification that only one person can protect the privacy per photo, so I think that it is practical only on the blog of a famous blogger (and the first front photo). .. that's sad! (I've verified all the faces, and if there are no applicable eyes, I feel like I can do it with the next face without looking at it ...) Also, the correct answer rate was around 70% as I confirmed. There may be room for improvement in terms of accuracy ...

Or rather, I think I should use another library without using OpenCV ...

So, if you feel like it again, I will protect the privacy of a large number of people!

(Thank you for playing around until the end.)

Recommended Posts

The best tool to protect your privacy from your photos ...!
Run the Matrix to your boss's terminal!
Upload photos from LINE Bot to Dropbox
How to operate Linux from the console
How to access the Datastore from the outside
3 best ways to use the less command
Push notifications to your smartphone from the Linux command line or scripts. (Introduction of a tool called ntfy)
I made a tool to generate Markdown from the exported Scrapbox JSON file
Change the decimal point of logging from, to.
How to operate Linux from the outside Procedure
POST images from ESP32-CAM (MicroPython) to the server
How to measure line speed from the terminal
[Python] Do your best to speed up SQLAlchemy
Move your hand to understand the chi-square distribution
From the introduction of pyethapp to the execution of contract
The story of moving from Pipenv to Poetry
[For beginners] Language analysis using the natural language processing tool "GiNZA" (from morphological analysis to vectorization)