LoginSignup
3
2

More than 1 year has passed since last update.

google cloud vision で画像チェックを行った話

Posted at

弊社には、該当商品を購入して頂いたユーザーにインセンティブを与えるサービスがあります。
これは、商品画像をユーザーにアップロードしてもらってチェックを行ったりしています。

概要

1.画像解析のAPIは、Google Cloud Vision - Vision APIを利用
2.解析の言語は、pythonを使用。最もポピュラーですね。
3.検証時の実行場所は、Colab(正式名称「Colaboratory」)を利用してます。ブラウザ上で実行できるのでとても便利です。
 https://colab.research.google.com/
 ソースコードやファイル等はgoogleドライブに保存して利用できます。

画像ファイルからGoogle Cloud Vision - Vision API を利用

こちらのソースコードは、画像ファイルの中の人間の数をカウントしてます。

from google.cloud import vision
from google.oauth2 import service_account
import io

credentials = service_account.Credentials.from_service_account_file('./hogehoge.json')
client = vision.ImageAnnotatorClient(credentials=credentials)

fpath = "/content/drive/MyDrive/Colab Notebooks/images/"
import glob
files = glob.glob(fpath + "*")
for file in files:
  print(file)
  client = vision.ImageAnnotatorClient(credentials=credentials)
  with io.open(file, 'rb') as image_file:
      content = image_file.read()
  image = vision.Image(content=content)

  #Cloud Vision APIにアクセスして、物体検出
  response = client.face_detection(image=image)
  #response = client.object_localization(image=image)

  # 物体検出結果からPersonをカウント
  labels = response.localized_object_annotations
  num = 0
  for label in labels:
      if label.name=="Person":
          num += 1

  # 人数カウント結果を表示
  print("画像内の人数:" + str(num))

まとめ

VisionAPIとgoogle colabの利用はとても簡易に便利なのでオススメです。

今後

今後、データサイエンス周りの記事も含めてアップしていきたいと思ってます。
モチベーションアップのためフォロー、イイねお願いします。

3
2
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
3
2