コード例 #1
0
        public static AlchemyResult recongnizeWithImg(byte[] img)
        {
            WebClient clients     = new WebClient();
            var       queryString = HttpUtility.ParseQueryString(String.Empty);

            clients.Headers.Add("Content-Type", "application/x-www-form-urlencode");
            queryString["apikey"]         = "<API_KEY>";
            queryString["imagePostMode"]  = "raw";
            queryString["outputMode"]     = "json";
            queryString["forceShowAll"]   = "0";
            queryString["knowledgeGraph"] = "0";
            WebClient client = new WebClient();

            client.Proxy = null;
            var    uri      = new Uri("http://access.alchemyapi.com/calls/image/ImageGetRankedImageKeywords?" + queryString);
            var    response = client.UploadData(uri, img);
            string json     = System.Text.Encoding.UTF8.GetString(response);

            json = json.Replace("person", "people");
            DataContractJsonSerializer contract = new DataContractJsonSerializer(typeof(AlchemyResult));
            MemoryStream  mstream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json));
            AlchemyResult res     = (AlchemyResult)contract.ReadObject(mstream);

            return(res);
        }
コード例 #2
0
        public static AlchemyResult recongnizeWithURL(string url)
        {
            var queryString = HttpUtility.ParseQueryString(String.Empty);

            queryString["url"]            = (new Uri(url)).ToString();
            queryString["apikey"]         = "<API_KEY>";
            queryString["outputMode"]     = "json";
            queryString["forceShowAll"]   = "0";
            queryString["knowledgeGraph"] = "0";
            WebClient client = new WebClient();

            client.Proxy = null;
            var response = client.DownloadString(new Uri("http://access.alchemyapi.com/calls/url/URLGetRankedImageKeywords?" + queryString));

            response = response.Replace("person", "people");
            DataContractJsonSerializer contract = new DataContractJsonSerializer(typeof(AlchemyResult));
            MemoryStream  mstream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(response));
            AlchemyResult res     = (AlchemyResult)contract.ReadObject(mstream);

            return(res);
        }
コード例 #3
0
        void showResult(DetectedResult result, AlchemyResult alchemy)
        {
            new Thread(() =>
            {
                new SoundPlayer(Path.GetDirectoryName(Application.ExecutablePath) + "\\done.wav").PlaySync();
            }).Start();
            this.Invoke(new Action(() =>
            {
                richTextBox1.Text = "State: Done.\n\n";
                richTextBox1.Clear();
                var res = result.face.OrderBy((o) => o.position.center.x).ToArray();
                syn.SetOutputToDefaultAudioDevice();
                bool hasFaces = false;
                //TODO:Check If The API Detected Any Categories In This Photo.
                if (alchemy.imageKeywords != null)
                {
                    if (alchemy.imageKeywords.Count() > 0)
                    {
                        richTextBox1.Text += "Image Category is: ";
                        int count          = 0;
                        foreach (var item in alchemy.imageKeywords)
                        {
                            if (count == alchemy.imageKeywords.Count - 1)
                            {
                                richTextBox1.Text += item.text.Replace('_', ' ') + ".\n";
                            }
                            else
                            {
                                richTextBox1.Text += item.text.Replace('_', ' ') + ", ";
                            }
                            count++;
                        }
                    }
                }

                //Check If Face++ Returned Any Faces
                if (result.face != null)
                {
                    if (result.face.Count() > 0)
                    {
                        hasFaces = true;
                    }
                }
                if (hasFaces)
                {
                    richTextBox1.Text += res.Count() + " Faces Detected.\n";
                    if (res.Count() > 0)
                    {
                        if (res.Count() > 1)
                        {
                            richTextBox1.Text += "Starting from the Left.\n";
                        }
                        int count = 0;
                        foreach (var item in res)
                        {
                            string[] heshe = new string[2];
                            if (item.attribute.gender.value.ToLower() == "female")
                            {
                                heshe[0] = "she";
                                heshe[1] = "her";
                            }
                            else
                            {
                                heshe[0] = "he";
                                heshe[1] = "his";
                            }
                            richTextBox1.Text += "Face Number " + (++count) + ": Age " + item.attribute.age.value + ", the Gender is " + item.attribute.gender.value + ", " + heshe[1] + " Race is " + item.attribute.race.value + ", And " + heshe[0] + " Looks " + faceState(Math.Round(item.attribute.smiling.value, 1)) + " (" + Math.Round(item.attribute.smiling.value, 0) + "% Smiling)." + "\n";
                        }
                    }
                }
                else
                {
                    richTextBox1.Text += "No Faces Detected.\n";
                    button1.Enabled    = true;
                    // button2.Enabled = true;
                }
            }));
        }