pythonで顔写真に自動でモザイクをかけるプログラム

# -*- coding: utf-8 -*-
import cv2
import math
import numpy as np
from PIL import Image

def main():
    img_filename = input()
    img = cv2.imread(img_filename)
    img_edit = Image.open(img_filename)

    cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")

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

    faces  =  cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=1, minSize=(3,3))

    if len(faces) > 0:

        for face in faces:

            cut_face = img_edit.crop((face[0],
                                      face[1],
                                      face[0]+face[2],
                                      face[1]+face[3]))

            cut_face = cut_face.resize((int(face[2]/5), int(face[3]/5)), Image.LINEAR)

            cut_face = cut_face.resize(face[2:], Image.LINEAR)

            img_edit.paste(cut_face, tuple(face[:2]))

    img_dst = np.asarray(img_edit)
    img_edit.show()
    img_edit.save('result.jpg')

if __name__ == '__main__':
    main()

コメント

タイトルとURLをコピーしました