PythonでWatson APIのPersonality Insightsを使う

目的

 Watson API の Personality Insights で文書作成者の特徴分析・性格分析がしたい!


参考リンク

 personality-insights - API Reference | IBM Watson Developer Cloud

 

動作環境

 Windows7

 Python 2.7.13 :: Anaconda 4.3.1 (64-bit)

 

やりかた

 1.Python で Watson API を使えるようにする

   コマンドプロンプトから以下を実行

   > pip install watson-developer-cloud


 2.ユーザ名とパスワードを確認

   1) Bluemix にログイン

   2) Personality Insights サービスにログイン

   3) サービス資格情報

   4) 資格情報の表示 で "username" と "password" を確認


 3.分析

   Python で以下を実行

# -*- coding: utf-8 -*
import json
from watson_developer_cloud import PersonalityInsightsV3

personality_insights = PersonalityInsightsV3(
  version='2016-10-20',
  username='ユーザ名',
  password='パスワード')

with open('テキストファイル名') as textdata:
  profile = personality_insights.profile(textdata.read(), content_language='ja', accept_language='ja')

for x in profile['needs']:
  print x['trait_id'], x['percentile']
for x in profile['values']:
  print x['trait_id'], x['percentile']
for x in profile['personality']:
  print x['trait_id'], x['percentile']
for x in profile['warnings']:
  print x['message']


Good Luck !!!

 

PythonでWatson APIのVisual Recognitionを使う

目的

 Watson API の Visual Recognition を使った画像認識がしたい!

 物体認識、顔認識、文字認識がしたい!

 できれば、Python でやりたい!

 

参考リンク

 WatsonのPython SDKを使ってAPIを叩いてみた 13連発 (絶賛途中) - Qiita

 visual-recognition - API Reference | IBM Watson Developer Cloud

 

動作環境

 Windows7

 Python 2.7.13 :: Anaconda 4.3.1 (64-bit)

 

やりかた

 1.Python で Watson API を使えるようにする

   コマンドプロンプトから以下を実行

   > pip install watson-developer-cloud

 2.Visual Recognition のサービス資格情報から api_key を参照

 3.画像認識

  1)既存の画像認識器を使う(物体の認識・顔の認識・文字の認識)

    Python で以下を実行(プログラム中の '2016-05-20' は固定値)

# -*- coding: utf-8 -*
import json
from os.path import join, dirname
from os import environ
from watson_developer_cloud import VisualRecognitionV3
visual_recognition = VisualRecognitionV3('2016-05-20', api_key='api_key')
# 物体認識
with open('画像ファイル名', 'rb') as img:
  print(json.dumps(visual_recognition.classify(images_file=img), indent=2))
# 顔認識
with open('画像ファイル名', 'rb') as img:
  print(json.dumps(visual_recognition.detect_faces(images_file=img), indent=2))
# 文字認識
with open('画像ファイル名', 'rb') as img:
  print(json.dumps(visual_recognition.recognize_text(images_file=img), indent=2))

 

  2)画像認識器を自作して使う

    Python で以下を実行(作成から、リスト、画像認識、削除まで)

# -*- coding: utf-8 -*
import json
from os.path import join, dirname
from watson_developer_cloud import VisualRecognitionV3

# VisualRecognition のサービス資格情報から api_key を参照
visual_recognition = VisualRecognitionV3('2016-05-20', api_key='api_key')

# 独自に画像分類器を作る
with open('分類1の画像zipファイル名', 'rb') as no1, open('分類2の画像zipファイル名', 'rb') as no2:
  print(json.dumps(visual_recognition.create_classifier('分類器名', no1_positive_examples=no1, negative_examples=no2), indent=2))

# 独自に作った画像分類器のリスト
print(json.dumps(visual_recognition.list_classifiers(), indent=2))

# 独自に作った画像分類器による認識
with open('画像ファイル名', 'rb') as img:
  print(json.dumps(visual_recognition.classify(images_file=img, classifier_ids=['classifier_id']), indent=2))

# 独自に作った画像分類器を削除
print(json.dumps(visual_recognition.delete_classifier(classifier_id='classifier_id'), indent=2))

 

Good Luck !!!

 

PythonでWatson APIのNLCを使う

目的

 Watson API の NLC(Natural Language Classifier)を使いたい!

 できれば、Python でやりたい!

 

参考リンク

 nl-classifier - API Reference | IBM Watson Developer Cloud

 

動作環境

 Windows7

 Python 2.7.13 :: Anaconda 4.3.1 (64-bit)

 

やりかた

 1.Python で Watson API を使えるようにする

   コマンドプロンプトから以下を実行

   > pip install watson-developer-cloud

 

 2.自然言語の分類器の操作

  1)分類器の作成

    以下の形式で質問文と分類結果をCSVファイルで作成。UTF-8コードで保存。

質問1,カテゴリA
質問2,カテゴリA
質問3,カテゴリA
質問5,カテゴリB
質問6,カテゴリB
質問7,カテゴリB
質問8,カテゴリC
質問9,カテゴリC

    A.ユーザ名とパスワードを確認

      ⇒ Bluemix にログイン

      ⇒ Natuural Language Classifier サービスにログイン

      ⇒ サービス資格情報

      ⇒ 資格情報の表示 で "username" と "password" を確認

    B.Python で以下を実行

# -*- coding: utf-8 -*
import json
from watson_developer_cloud import NaturalLanguageClassifierV1
natural_language_classifier = NaturalLanguageClassifierV1(username='ユーザ名', password='パスワード')
with open('CSVファイルのパス', 'rb') as training_data:
  classifier = natural_language_classifier.create(
    training_data=training_data,
    name='mikasute',
    language='ja'
  )
print(json.dumps(classifier, indent=2))

  2)分類器のリスト

    Python で以下を実行

# -*- coding: utf-8 -*
import json
from watson_developer_cloud import NaturalLanguageClassifierV1 as NaturalLanguageClassifier
natural_language_classifier = NaturalLanguageClassifier(username='ユーザ名', password='パスワード')
classifiers = natural_language_classifier.list()
print(json.dumps(classifiers, indent=2))

  3)分類器の情報取得

    A.前項の分類器のリスト表示で分類器IDを参照

    B.Python で以下を実行

# -*- coding: utf-8 -*
import json
from watson_developer_cloud import NaturalLanguageClassifierV1 as NaturalLanguageClassifier
natural_language_classifier = NaturalLanguageClassifier(username='ユーザ名', password='パスワード')
status = natural_language_classifier.status('分類器ID')
print (json.dumps(status, indent=2))

  4)分類

    Python で以下を実行

# -*- coding: utf-8 -*
import json
from watson_developer_cloud import NaturalLanguageClassifierV1
natural_language_classifier = NaturalLanguageClassifierV1(username='ユーザ名', password='パスワード')
classes = natural_language_classifier.classify('分類器ID', '質問文')
print classes['text'], '->', classes['top_class']
for x in classes['classes']:
  print x['class_name'], round(x['confidence'],2)

  5)分類器の削除

    Python で以下を実行

# -*- coding: utf-8 -*
import json
from watson_developer_cloud import NaturalLanguageClassifierV1
natural_language_classifier = NaturalLanguageClassifierV1(username='ユーザ名', password='パスワード')
classes = natural_language_classifier.remove('分類器ID')
print(json.dumps(classes, indent=2))

 

Good Luck !!!

 

複数のPython開発環境の構築

目的

 Python2.7 と Python3.5 のどちらの環境でも遊べるようにしたい!

 Anacondaで仮想環境を作り、複数のPython環境を持てるようにする!

 

参考リンク

 PENGUINITIS - Anaconda における Python 2 と Python 3 の共存

 

動作環境

 Windows7

 Python 2.7.13 :: Anaconda 4.3.1 (64-bit)

 

やりかた

 1.存在している環境の確認

   > conda info -e

   現在の環境には * を付けて表示する!

 

 2.新しい環境の作成

   > conda create -n tf python=3.5 anaconda

   tf という名前の環境を Python3.5 でつくる!

 

 3.新しい環境への移動

   > activate tf

   tf という名前の環境で遊ぶ!

   ここでインストールしたものは、この環境のみで反映される!

 

 4.新しい環境から戻る

   > deactivate

   tf という名前の環境から root へ戻る!

 

 5.環境を消す

   > conda remove -n tf --all

   tf という名前の環境を削除する!

 

 

Good Luck !!!

 

Pythonで画像中の数字認識・文字認識

目的

 画像ファイル中に書かれている数字や文字を認識したい!

 できれば、Python でやりたい!

 

参考リンク

 Pythonで画像内の数字認識 - Qiita

 日本語OCRのtesseract-ocrを使ってやってみた | JProgramer

 

動作環境

 Windows7

 Python 2.7.13 :: Anaconda custom (64-bit)

 

やりかた

 1.tesseract のセットアップ

  0)小目的

    まずはPythonとか以前にtesseractで画像ファイルをOCRで読み取ろう!

 

  1)ダウンロード

    https://sourceforge.net/projects/tesseract-ocr-alt/files/

    tesseract-ocr-setup-3.02.02.exe

    tesseract-ocr-3.02.jpn.tar.gz

 

  2)インストール

    tesseract-ocr-setup-3.02.02.exe

    ⇒デフォルトインストール

    tesseract-ocr-3.02.jpn.tar.gz

    ⇒ファイルを解凍

     jpn.traineddataファイルをtesseractのインストールフォルダのtesdataへコピー

 

  3)動作確認

    コマンドプロンプトから以下を実行し、

    > tesseract sample.png result -l jpn

    result.txt ファイルに書かれた結果を確認。

 

 2.pytesseract のセットアップ

  0)小目的

    次に Python から tesseract を使えるように!

 

  1)インストール

    コマンドプロンプトから以下を実行

    > pip install pytesseract

    > conda install -y pillow

 

  2)動作確認

    Python のコンソールから以下を実行し、エラーがないことを確認

    >>> import pytesseract

    >>> import PIL

 

 3.やってみよう

  1)ソースコード

    下に記載しました

  2)実行

    画像ファイルを準備し、実行あるのみ!

    > python sample.py

 

Good Luck !!!

 

# -*- coding: utf-8 -*-
import pytesseract
from PIL import Image

imgfile = 'sample.jpg'
img = Image.open(imgfile)
str = pytesseract.image_to_string(img, lang="jpn")
print unicode(str, 'utf-8')