LoginSignup
6
7

More than 5 years have passed since last update.

[python, openCV] base64画像での顔認識

Posted at

環境

  • OSX 10.11.4
  • python 2.7.11

リポジトリ

コード

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import cv2, os
import numpy as np
from PIL import Image
import base64
from StringIO import StringIO

import scipy.misc

# Haar-like特徴分類器
cascadePath = "/path/to/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)

def readb64(base64_string):
    sbuf = StringIO()
    sbuf.write(base64.b64decode(base64_string))
    pimg = Image.open(sbuf)
    return pimg
    #cv2.cvtColor(np.array(pimg), cv2.COLOR_RGB2BGR)

def detect():
    # グレースケールでbase64を読み込む
    image_pil = readb64(sys.argv[1].replace("\\", "\\\\")).convert('L')
    # NumPyの配列に格納
    image = np.array(image_pil, 'uint8')
    # Haar-like特徴分類器で顔を検知 (パラメータは適当)
    faces = faceCascade.detectMultiScale(image,1.1,9,0)

    scipy.misc.imsave('outfile.jpg', image)

    # 検出した顔画像の座標を表示
    for (x, y, w, h) in faces:
        print x,y,w,h

detect()
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7