1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Raspberry Pi + Apache + Google Cloud Vision API + Slack API を使ってみる

Posted at

目的

  • Raspberry Pi をセットアップする。
  • Webサーバー (Apache HTTP Server) をインストールする。
  • Webカメラで撮影する。
  • Google Cloud Vision API でラベリングをする。
  • ラベリング結果を Slack に送信する。

全体像

image.png

動作確認

http://192.168.x.x/test.jpg
(Raspberry Pi の IPアドレス)

image.png

受信したスラック

image.png

Apache

sudo chmod 777 /var/www/html

Python

test.py

# !/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import io
from time import sleep
from datetime import datetime
import subprocess
import requests
import json
from google.cloud import vision
from google.cloud.vision import types

def exec_cmd(cmd):
    #r = subprocess.check_output(cmd, shell=True)
    #return r.decode('utf-8').strip()
    subprocess.call(cmd, shell=True)

def take_photo():
    #now = datetime.now()
    #f = now.strftime('%Y-%m-%d_%H-%M-%S') + '.jpg'
    f = 'test.jpg'
    exec_cmd('fswebcam ' + f)

def get_label(file_name):
    #key_path = '/home/pi/*/yourkey.json'
    #os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = key_path

    client = vision.ImageAnnotatorClient()

    with io.open(file_name, 'rb') as image_file:
        content = image_file.read()

    image = types.Image(content=content)
    response = client.label_detection(image=image)
    labels = response.label_annotations
    d = labels[0].description
    s = labels[0].score
    text = 'label: ' + d + ' score: ' + '{:.2f}'.format(s)
    return text

def send_slack(text):
    WEB_HOOK_URL = 'https://hooks.slack.com/services/xxx'
    requests.post(WEB_HOOK_URL, data = json.dumps({
        'text': text,
        }))

if __name__ == '__main__':
    take_photo()
    txt = get_label('test.jpg')
    send_slack(txt)

シェルスクリプト

test.sh

sleep 3
python3 test.py
cp test.jpg /var/www/html/ 
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?