Created
April 16, 2015 16:51
-
-
Save ksasao/9cc048a6a5417d85a371 to your computer and use it in GitHub Desktop.
C#による 飯テロ判定コード。 License: WTFPL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.IO; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Threading.Tasks; | |
namespace Meshi | |
{ | |
// IBM Watson (Bluemix) を利用した飯画像判定 | |
// 詳細は @TakesxiSximada 様の記事 http://qiita.com/TakesxiSximada/items/a482d78eaa0c854e8a9b | |
// などを参照してください | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
if (args.Length > 0) | |
{ | |
bool isFood = false; | |
Bitmap bmp = new Bitmap(args[0]); | |
Task task = Task.Factory.StartNew(() => isFood = IsFood(bmp).Result); | |
task.Wait(); | |
Console.WriteLine(isFood ? "ご飯" : "普通"); | |
} | |
} | |
static async Task<bool> IsFood(Bitmap bmp) | |
{ | |
string username = ""; // your username | |
string password = ""; // your password | |
string url = "https://gateway.watsonplatform.net/visual-recognition-beta/api/v1/tag/recognize"; | |
bool result = false; | |
using (MemoryStream ms = new MemoryStream()) | |
{ | |
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); | |
ms.Position = 0; | |
string response = ""; | |
using (var client = new HttpClient()) | |
{ | |
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", | |
System.Convert.ToBase64String(System.Text.UTF8Encoding.UTF8.GetBytes(string.Format("{0}:{1}", | |
username, password)))); | |
using (var content = new MultipartFormDataContent()) | |
{ | |
content.Add(new StreamContent(ms), "img_File", "upload.jpg"); | |
using (var message = await client.PostAsync(url, content)) | |
{ | |
response = await message.Content.ReadAsStringAsync(); | |
result = response.IndexOf("Food") > 0; | |
} | |
} | |
} | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment