動かざることバグの如し

近づきたいよ 君の理想に

親要素を無視してブラウザ幅100%で表示するCSS

CSSでは基本的にコンテンツを.containerとか.mainのdivで囲む。 大抵そのdiv要素はwidth768pxとかなんだけど、一部だけその要素を突き抜けてブラウザ幅いっぱいに表示したくなるときがある。

その時の回避法がいくつかあるんだけど、一番スマートで汎用的なのがあったのでメモ

デモを見せろ

こういうの

See the Pen YaKLxW by thr3a (@thr3a) on CodePen.

コード

親要素が.container、幅いっぱいに表示させたい要素を.full-widthとする。とHTMLは以下

<div class="container">
  ほげほげほげほげほげほげほげほげほげほげほげほげほげほげほげほげほげほげほげ
  <div class="full-width">
    ぴよぴよぴよぴよぴよぴよぴよぴよぴよぴよぴよ
  </div>
  ふがふがふがふがふがふがふがふがふがふがふがふがふがふがふがふがふがふがふが
</div>

CSSは以下

.container {
  width:1200px;
  margin-right: auto;
  margin-left : auto;
  background-color: gray;
}

.full-width {
  width: 100vw;
  position: relative;
  left: 50%;
  right: 50%;
  margin-left: -50vw;
  margin-right: -50vw;
  background-color: red;
}

background-colorはわかりやすいようにつけただけなので削って問題ない。ポイントは.full-width

width: 100vw;

これはブラウザのビューポートを指定するもので、これを100%で取ってるからブラウザ幅いっぱいに表示できる。

Darknetで1行もコードを書かずに物体認識する

Darknetを使うと一発で物体認識を行うことができる。学習済みのモデルも配布されてるので自分でが再学習する必要もなしという超お得

環境

  • Ubuntu 16.04
  • CUDA 9(GPUなくても動くっぽい)

インストール

息を吸うようにgit clone

git clone https://github.com/pjreddie/darknet.git
cd darknet
make

これだけ(ぇ

ただしデフォルトだとGPUを使わずにCPUだけを使うので、GPUを使用したビルドにしたい場合は同一ディレクトリのMakefile冒頭のGPUとCUDNNを1に変更する。

GPU=1
CUDNN=1
OPENCV=0
OPENMP=0
DEBUG=0

いざ物体認識

./darknet detect cfg/yolo.cfg yolo.weights data/dog.jpg

以下のような途中経過が流れて最後に結果が表示される

layer     filters    size              input                output
    0 conv     32  3 x 3 / 1   608 x 608 x   3   ->   608 x 608 x  32
    1 max          2 x 2 / 2   608 x 608 x  32   ->   304 x 304 x  32
    2 conv     64  3 x 3 / 1   304 x 304 x  32   ->   304 x 304 x  64
    3 max          2 x 2 / 2   304 x 304 x  64   ->   152 x 152 x  64
    4 conv    128  3 x 3 / 1   152 x 152 x  64   ->   152 x 152 x 128
    5 conv     64  1 x 1 / 1   152 x 152 x 128   ->   152 x 152 x  64
    6 conv    128  3 x 3 / 1   152 x 152 x  64   ->   152 x 152 x 128
    7 max          2 x 2 / 2   152 x 152 x 128   ->    76 x  76 x 128
    8 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256
    9 conv    128  1 x 1 / 1    76 x  76 x 256   ->    76 x  76 x 128
   10 conv    256  3 x 3 / 1    76 x  76 x 128   ->    76 x  76 x 256
   11 max          2 x 2 / 2    76 x  76 x 256   ->    38 x  38 x 256
   12 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512
   13 conv    256  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x 256
   14 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512
   15 conv    256  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x 256
   16 conv    512  3 x 3 / 1    38 x  38 x 256   ->    38 x  38 x 512
   17 max          2 x 2 / 2    38 x  38 x 512   ->    19 x  19 x 512
   18 conv   1024  3 x 3 / 1    19 x  19 x 512   ->    19 x  19 x1024
   19 conv    512  1 x 1 / 1    19 x  19 x1024   ->    19 x  19 x 512
   20 conv   1024  3 x 3 / 1    19 x  19 x 512   ->    19 x  19 x1024
   21 conv    512  1 x 1 / 1    19 x  19 x1024   ->    19 x  19 x 512
   22 conv   1024  3 x 3 / 1    19 x  19 x 512   ->    19 x  19 x1024
   23 conv   1024  3 x 3 / 1    19 x  19 x1024   ->    19 x  19 x1024
   24 conv   1024  3 x 3 / 1    19 x  19 x1024   ->    19 x  19 x1024
   25 route  16
   26 conv     64  1 x 1 / 1    38 x  38 x 512   ->    38 x  38 x  64
   27 reorg              / 2    38 x  38 x  64   ->    19 x  19 x 256
   28 route  27 24
   29 conv   1024  3 x 3 / 1    19 x  19 x1280   ->    19 x  19 x1024
   30 conv    425  1 x 1 / 1    19 x  19 x1024   ->    19 x  19 x 425
   31 detection
mask_scale: Using default '1.000000'
Loading weights from yolo.weights...Done!
data/dog.jpg: Predicted in 0.118000 seconds.
dog: 82%
car: 28%
truck: 64%
bicycle: 85%

predictions.pngが生成される。こんな感じ

f:id:thr3a:20180227222442p:plain

試しにのんたんでやってみた。ちゃんとcatで判別されてて嬉しい。

f:id:thr3a:20180227222457p:plain

PythonからAWS Pollyを実行するサンプルコード

環境

2だと動かないので注意

コード

予めAWSのIAM管理からpolly用のアクセスキーを取得しておくこと。

import boto3
from contextlib import closing

client = boto3.client(
    'polly',
    aws_access_key_id='KEY_ID',
    aws_secret_access_key='ACCESS_KEY',
    region_name='us-east-1'
)
message = '親譲りの無鉄砲で子供の時から損ばかりしている。'

response = client.synthesize_speech(
    OutputFormat='mp3',
    Text=message,
    TextType='text',
    VoiceId='Mizuki'
)

print(response)

if "AudioStream" in response:
    with closing(response["AudioStream"]) as stream:
      data = stream.read()
      fw = open("polly.mp3", "wb")
      fw.write(data)
      fw.close()

日本語に対応しているVoiceIDは現在2種類

両方結構大人びて聞こえる。萌ボイスはよ

日本語音声合成Open JTalkをPythonから実行する

環境

コード

以下をjtalk.pyとして保存する

#coding: utf-8
import subprocess
import tempfile

def run(message, voice_path='voice.wav'):
    speed = 1.0
    dic_path = "/usr/local/Cellar/open-jtalk/1.10_1/dic"
    model_path = "/usr/local/Cellar/open-jtalk/1.10_1/voice/mei/mei_normal.htsvoice"

    with tempfile.NamedTemporaryFile(mode='w+') as tmp:
        tmp.write(message)
        tmp.seek(0)
        command = 'open_jtalk -x {} -m {} -r {} -ow {} {}'.format(dic_path, model_path, speed, voice_path, tmp.name)
        print(command)
        proc = subprocess.run(
            command,
            shell  = True,
        )

で、使いたいときは以下のようにimportして使う 適当にsample.pyとする

import jtalk
from datetime import datetime

d = datetime.now()
text = '%s月%s日、%s時%s分%s秒' % (d.month, d.day, d.hour, d.minute, d.second)
jtalk.run(text)

実行

python sample.py

ディレクトリにvoice.wavが生成されているはず

なんでtempfileつかうの

stdinだと一部文字列が入ったときに正常に動作しないことがあった