When it’s ready.

出来るまで出来ない

Python温泉 #10 ファイナル

Youtube APIとOpenCVだらだら触った2日間だった。

OpenCV2.3.1aとPythonの組み合わせの資料が少なくて思いの外時間を食った。
ジェスチャーまで行けなかったけど、顔切り出しまでのコード

OpenCVをインストール

Brewで2.3台が入るのでそれを利用させてもらった

brew install opencv

Python ソース

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

import sys
import cv

def detect(image):
  image_size = cv.GetSize(image)

  # create grayscale version
  grayscale = cv.CreateImage(image_size, 8, 1)
  cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)

  # create storage
  storage = cv.CreateMemStorage(0)

  # equalize histogram
  cv.EqualizeHist(grayscale, grayscale)

  # detect objects
  cascade = cv.Load('haarcascade_frontalface_alt.xml')#, (1, 1))
  faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, (50, 50))

  if faces:
    for ((x,  y,  w,  h),  n) in faces:
      print "posX, Y, N : ", x, y, n
      print "Width, Height : ", w, h
      cv.Rectangle(image, ( int(x), int(y)),
                   (int(x + w), int(y + h)),
                   cv.RGB(0, 255, 0), 3, 8, 0)
  return faces


if __name__ == "__main__":
  print "Press ESC to exit ..."

  # create windows
  cv.NamedWindow('Camera', cv.CV_WINDOW_AUTOSIZE)

  # create capture device
  device = 0
  capture = cv.CreateCameraCapture(0)
  cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 640)
  cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 480)

  # check if capture device is OK
  if not capture:
    print "Error opening capture device"
    sys.exit(1)

  while 1:
    frame = cv.QueryFrame(capture)
    if frame is None:
        break

    # mirror
    cv.Flip(frame, None, 1)

    # face detection
    faces = detect(frame)

    if len(faces)>0:
      frame = cv.GetSubRect(frame,  faces[0][0])


    # display webcam image
    cv.ShowImage('Camera', frame)

    # handle events
    k = cv.WaitKey(10)

    if k == 0x1b: # ESC
      print 'ESC pressed. Exiting ...'
      break

顔だけ抜き出して、表示する。顔がないとカメラ全体を表示する。
朝までに、複数顔対応したい。

クロームレス Youtubeプレーヤー

http://youtube.atu.si/
コントロールを表示させないプレーヤーを生成して、外部からボタン等でコントールする。

<script type="text/javascript">

var params = { allowScriptAccess: "always" };
var req_urls = "http://www.youtube.com/apiplayer?enablejsapi=1&version=3&wmode=transparent&video_id="
var atts = { id: "myytplayer" };
var win_width = window.innerWidth * 0.8;
var win_height = window.innerHeight * 0.8;
swfobject.embedSWF(req_urls + "xLYiIBCN9ec&playerapiid=ytplayer",
    "ytapiplayer", ""+win_width, ""+win_height,  "9",  null,  null,  params,  atts);

function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("myytplayer");
}

function play() {
  if (ytplayer) {
    ytplayer.playVideo();
  }
}

function stop() {
  if (ytplayer) {
    ytplayer.stopVideo();
  }
}

</script>


Python温泉は、今回の10回をもって終了らしい。
ここでの出会いと、経験はとても貴重でユニークなモノだった。
毎回まとめてくれたV、クマ。そして参加者の皆様、有り難うございました。
( 写真アップしました )
またいつか近いうちによろしくお願いします。

次は忘年会かな?