Pixelate Face
Mix.install([
{:image, "~> 0.54"},
{:evision, "~> 0.2"},
{:req, "~> 0.5"},
{:kino, "~> 0.14"}
])
画像の読込
ryo_img =
"https://www.elixirconf.eu/assets/images/ryo-wakabayashi.jpg"
|> Req.get!()
|> Map.get(:body)
|> Image.from_binary!()
モデルのダウンロード
face_model_path = "face_model.xml"
"https://github.com/opencv/opencv/raw/master/data/haarcascades/haarcascade_frontalface_default.xml"
|> Req.get!(connect_options: [timeout: 300_000], into: File.stream!(face_model_path))
face_model = Evision.CascadeClassifier.cascadeClassifier(face_model_path)
顔を検出する
face_rect =
{left, top, width, height} =
ryo_img
|> Image.split_alpha()
|> elem(0)
|> Image.to_evision()
|> elem(1)
|> then(&Evision.CascadeClassifier.detectMultiScale(face_model, &1))
|> List.first()
顔にモザイクをかける
face_img = Image.crop!(ryo_img, left, top, width, height)
pixelated_face_img = Image.pixelate!(face_img)
pixelated_ryo_img = Image.compose!(ryo_img, pixelated_face_img, x: left, y: top)
モザイク処理を繋げた場合
ryo_img
|> Image.crop!(left, top, width, height)
|> Image.pixelate!()
|> then(&Image.compose!(ryo_img, &1, x: left, y: top))