示例#1
0
 public MyRequest(MySession mySession, bool verbose = false)
 {
     m_session = mySession;
     Verbose   = verbose;
 }
示例#2
0
        static void Main(string[] args)
        {
            try
            {
                // Login...
                MySession mySession = new MySession();
                mySession.Authenticate();
                Console.WriteLine("API: " + mySession.ApiUrl + " Token: " + mySession.Token);
                var myRequest = new MyRequest(mySession);


                // List all the subjects
                //myRequest.Verbose = true;
                var subjects = myRequest.DoRequest("GET", "/subject");

                // Get a subject thumbnail
                //var subjectId = "b28e2f46-d8bd-41ae-8d5a-68293447678c";
                //myRequest.DoRequestToFile("/thumbnail/subject/" + subjectId, "/tmp/file.jpg");


                // Create a new watchlist
                SortedDictionary <string, string> formData = new SortedDictionary <string, string>();
                var random = new Random();
                formData.Add("name", Uri.EscapeDataString("MyWatchlist_" + random.Next().ToString()));
                formData.Add("colour", Uri.EscapeDataString("ff0000"));
                var createWatchlist = myRequest.DoRequest("POST", "/watchlist", formData);
                var watchlistId     = createWatchlist["watchlist"]["id"];
                var watchlistName   = createWatchlist["watchlist"]["name"];
                Console.WriteLine("Created watchlist " + watchlistName + " with id " + watchlistId);

                // Create a new subject
                var createSubjectData = new SortedDictionary <string, string>();
                createSubjectData.Add("name", Uri.EscapeDataString("John Smith"));
                var createSubject = myRequest.DoRequest("POST", "/subject", createSubjectData);
                var subjectId     = createSubject["subject"]["subject_id"];
                var subjectName   = createSubject["subject"]["name"];
                Console.WriteLine("Created subject " + subjectName + " with id " + subjectId);

                // Add the subject to the watchlist
                var    addSubjectToWatchlistData = new SortedDictionary <string, string>();
                string watchlistIds = "[{0}]";
                watchlistIds = string.Format(watchlistIds, watchlistId);
                addSubjectToWatchlistData.Add("watchlist_ids", Uri.EscapeDataString(watchlistIds));
                var addSubjectToWatchlist = myRequest.DoRequest("PUT", "/subject/" + subjectId + "/watchlist", addSubjectToWatchlistData);

                // Lets do a FaceLogImage
                var faceLogImageData = new SortedDictionary <string, string>();
                faceLogImageData.Add("compare_threshold", Uri.EscapeDataString("0.6"));
                faceLogImageData.Add("recognition", Uri.EscapeDataString("true"));

                var imagePath         = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "/kieron01.jpg";
                var faceLogImage      = myRequest.DoFaceLogImage(faceLogImageData, imagePath, true);
                var numberOfSightings = faceLogImage["number_of_sightings"];
                Console.WriteLine("Face Log Task: " + faceLogImage["job_id"] + " found " + numberOfSightings + " sightings");
            }
            catch (SmartVisFaceException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (KeyNotFoundException e)
            {
                Console.WriteLine(e.Message);
            }
        }
示例#3
0
        public dynamic DoFaceLogImage(SortedDictionary <string, string> formData = null, string imagePath = null, bool waitForTask = true)
        {
            if (formData == null)
            {
                formData = new SortedDictionary <string, string>();
            }
            string method   = "POST";
            string endPoint = "/face_log_image";

            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

            string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
            Stream memStream        = new System.IO.MemoryStream();

            if (formData != null)
            {
                foreach (string key in formData.Keys)
                {
                    string formitem      = string.Format(formdataTemplate, key, formData[key]);
                    byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                    memStream.Write(formitembytes, 0, formitembytes.Length);
                }
            }

            var postDataAsBytes = MySession.PostDataAsByteArray(formData);
            var request         = (HttpWebRequest)WebRequest.Create(m_session.ApiUrl + endPoint);

            request.Method    = method;
            request.KeepAlive = true;
            request.Headers.Add("Device", m_session.DeviceData);
            var authorization = m_session.Authorization("POST", endPoint, formData);

            request.Headers.Add("Authorization", authorization);
            request.ContentType = "multipart/form-data; boundary=" + boundary;

            // Do we have to post an image
            var boundarybytes    = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
            var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--");


            string headerTemplate =
                "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
                "Content-Type: application/octet-stream\r\n\r\n";

            memStream.Write(boundarybytes, 0, boundarybytes.Length);
            var header      = string.Format(headerTemplate, "image", "image.jpg");
            var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

            memStream.Write(headerbytes, 0, headerbytes.Length);

            using (var fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
            {
                var buffer    = new byte[1024];
                var bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    memStream.Write(buffer, 0, bytesRead);
                }
            }

            memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            request.ContentLength = memStream.Length;

            using (Stream requestStream = request.GetRequestStream())
            {
                memStream.Position = 0;
                byte[] tempBuffer = new byte[memStream.Length];
                memStream.Read(tempBuffer, 0, tempBuffer.Length);
                memStream.Close();
                requestStream.Write(tempBuffer, 0, tempBuffer.Length);
            }

            // Finally lets do the request
            var responseData = DoRequestToJSON(request);

            if (!ResponseOk(responseData))
            {
                throw new SmartVisFaceException("Failed to perform request: " + responseData["message"]);
            }

            var taskData = responseData["task"];

            if (waitForTask)
            {
                var taskId = responseData["task"]["job_id"];
                taskData = WaitForTask(taskId, endPoint);
            }
            return(taskData);
        }