ストーリー
IBM Bluemix と AlchemyAPI を使って画像認識アプリケーションを作る
https://www.ibm.com/developerworks/community/wikis/home?lang=ja#!/wiki/Wdec01e50fbfa_493c_8a88_6dd85c4d983f/page/Bluemix%E3%81%A7%E7%94%BB%E5%83%8F%E8%AA%8D%E8%AD%98%E3%82%B5%E3%83%BC%E3%83%93%E3%82%B9%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%81%BF%E3%82%8B
こちらをもとにBluemixで画像認識アプリを作りました。
URLのパラメータを渡せば、その画像を解析した結果を表示するところまではできています。
プロトタイプも作りました。
URLを指定するのではなく、直接画像をAlchemy API faceに送り、
解析させたいです。
完成イメージ:スマホから写真を撮って直接画像をアップロード
調べたところ
Alchemy APIの顔認識APIで直接画像ファイルを指定する
https://uramoto.wordpress.com/2015/07/12/alchemy-api%E3%81%AE%E9%A1%94%E8%AA%8D%E8%AD%98api%E3%81%A7%E7%9B%B4%E6%8E%A5%E7%94%BB%E5%83%8F%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%E6%8C%87%E5%AE%9A%E3%81%99%E3%82%8B/
こちらの記事が解決策っぽいですが、
curlコマンドをどう使えばいいのかわかりません。
教えて欲しいこと
プロトタイプの
ソースコードを掲載しますので、改良していただきたいです。
成果物
http://hirokitaimage.au-syd.mybluemix.net/?url='画像URL'
http://hirokitaimage.au-syd.mybluemix.net/image.php?url='画像URL'
例:http://hirokitaimage.au-syd.mybluemix.net/image.php?url=http://sweetpower.jp/img/profile/l/maki.jpg
直接画像をアップロード
ご教授下さい。
ソースコード
image.php
php
1<html> 2<head> 3<title>AlchemyAPI サンプル</title> 4</head> 5<body> 6<?php 7$trans1 = 0; 8$trans2 = 0; 9if( isset( $_GET['url'] ) ){ 10 $apikey = 'APIKEY'; 11 12 $url = $_GET['url']; 13 $alchemyurl = 'http://access.alchemyapi.com/calls/url/'; 14?> 15 <img src='<?php echo $url; ?>'/> 16 <p/> 17<?php 18 $apiurl1 = $alchemyurl . 'URLGetRankedImageKeywords?apikey=' . $apikey . '&outputMode=json&url=' . urlencode( $url ); 19 $text1 = file_get_contents( $apiurl1 ); 20 $json1 = json_decode( $text1 ); 21 22 $trans1 = $json1->totalTransactions; 23 $imageKeywords = $json1->imageKeywords; 24 if( count( $imageKeywords ) ){ 25?> 26 <h2>Keywords</h2> 27 <table border='1'> 28 <tr><th>text</th><th>score</th></tr> 29<?php 30 for( $i = 0; $i < count( $imageKeywords ); $i ++ ){ 31 $imageKeyword = $imageKeywords[$i]; 32 $text = $imageKeyword->text; 33 $score = $imageKeyword->score; 34?> 35 <tr><td><?php echo $text; ?></td><td><?php echo $score; ?></td></tr> 36<?php 37 } 38?> 39 </table> 40 <p/> 41<?php 42 } 43 44 $apiurl2 = $alchemyurl . 'URLGetRankedImageFaceTags?apikey=' . $apikey . '&outputMode=json&knowledgeGraph=1&url=' . urlencode( $url ); 45 $text2 = file_get_contents( $apiurl2 ); 46 $json2 = json_decode( $text2 ); 47 48 $trans2 = $json2->totalTransactions; 49 $imageFaces = $json2->imageFaces; 50 if( count( $imageFaces ) ){ 51?> 52 <h2>FaceTags</h2> 53 <table border='1'> 54 <tr><th>attr</th><th>value</th><th>score</th></tr> 55<?php 56 for( $i = 0; $i < count( $imageFaces ); $i ++ ){ 57 $imageFace = $imageFaces[$i]; 58 $positionX = $imageFace->positionX; 59 $positionY = $imageFace->positionY; 60 $width = $imageFace->width; 61 $height = $imageFace->height; 62 $ageO = $imageFace->age; 63 $ageRange = $ageO->ageRange; 64 $ageScore = $ageO->score; 65 $genderO = $imageFace->gender; 66 $gender = $genderO->gender; 67 $genderScore = $genderO->score; 68?> 69 <tr><th colspan='3'><?php echo $i; ?></th></tr> 70 <tr><td>positionX</td><td><?php echo $positionX; ?></td><td> </td></tr> 71 <tr><td>positionY</td><td><?php echo $positionY; ?></td><td> </td></tr> 72 <tr><td>width</td><td><?php echo $width; ?></td><td> </td></tr> 73 <tr><td>height</td><td><?php echo $height; ?></td><td> </td></tr> 74 <tr><td>age</td><td><?php echo $ageRange; ?></td><td><?php echo $ageScore; ?></td></tr> 75 <tr><td>gender</td><td><?php echo $gender; ?></td><td><?php echo $genderScore; ?></td></tr> 76<?php 77 $identityO = $imageFace->identity; 78 if( $identityO ){ 79 $name = $identityO->name; 80 $nameScore = $identityO->score; 81?> 82 <tr><td>name</td><td><?php echo $name; ?></td><td><?php echo $nameScore; ?></td></tr> 83<?php 84 } 85?> 86<?php 87 } 88?> 89 </table> 90 <p/> 91<?php 92 } 93?> 94 <div align='right'> 95 使ったトランザクション数:<?php echo $trans1; ?> + <?php echo $trans2; ?> = <?php echo ($trans1 + $trans2); ?> 96 </div> 97<?php 98} 99?> 100</body> 101</html> 102
プロトタイプ
http://howold.mybluemix.net/wat07
のフロントエンド
html
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <!-- jQueryの読み込み --> 6 <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 7</head> 8<body> 9 10<!-- テキストフォーム --> 11<input type="text" id="inputText" placeholder="解析したい画像のURLを貼り付けてください。"> 12 13<div id="sample2_input"> 14 <label>顔写真: <input id="sample2_input" type="file"/></label> 15</div> 16 17<br> 18<br> 19<br> 20<br> 21 22 23<!-- リンク --> 24<a href="#" id="link">解析実行!</a> 25 26 27<!-- ▼ jQueryによる処理 ▼ --> 28<script> 29/*eslint-env jquery */ 30$(function() { 31 32 // テキストフォームを監視して入力があるたびに実行 33 $('#inputText').on('input change', function() { 34 35 // テキストを取得 36 var param = $(this).val(); 37 38 // リンクを書き換え 39 $('#link').attr('href', 'http://hirokitaimage.au-syd.mybluemix.net/image.php?url=' + param); 40 41 }); 42 43}); 44</script> 45<!-- ▲ jQueryによる処理 ▲ --> 46 47 48</body> 49</html>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。