コード例 #1
0
ファイル: ImageFunctions.cs プロジェクト: szarath/Two_games
        private static Boolean checkVision(byte[] imageByteArray)
        {
            string base64String = Convert.ToBase64String(imageByteArray);

            var visionService = CreateAuthorizedClient(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "\\IMF third year project-46cd5c28569b.json");


            Google.Apis.Vision.v1.Data.BatchAnnotateImagesRequest content = new Google.Apis.Vision.v1.Data.BatchAnnotateImagesRequest();

            content.Requests = new List <AnnotateImageRequest>();
            AnnotateImageRequest newRequest = new AnnotateImageRequest();

            newRequest.Image         = new Google.Apis.Vision.v1.Data.Image();
            newRequest.Image.Content = base64String;
            newRequest.Features      = new List <Feature>();
            newRequest.Features.Add(new Feature()
            {
                Type = "SAFE_SEARCH_DETECTION"
            });
            content.Requests.Add(newRequest);

            ImagesResource.AnnotateRequest request = visionService.Images.Annotate(content);
            Google.Apis.Vision.v1.Data.BatchAnnotateImagesResponse response = request.Execute();

            if (response.Responses.Count > 0)
            {
                string tempAdult = response.Responses[0].SafeSearchAnnotation.Adult.ToString();
                string tempSpoof = response.Responses[0].SafeSearchAnnotation.Spoof.ToString();
                if (tempAdult.Equals("LIKELY") || tempAdult.Equals("VERY_LIKELY"))
                {
                    return(false);
                }
                if (tempSpoof.Equals("LIKELY") || tempSpoof.Equals("VERY_LIKELY"))
                {
                    return(false);
                }
            }
            return(true);
        }
コード例 #2
0
        // <summary>
        // Called automatically whenever an activity finishes
        // </summary>
        // <param name = "requestCode" ></ param >
        // < param name="resultCode"></param>
        /// <param name="data"></param>
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            if (requestCode == 100)
            {
                SetContentView(Resource.Layout.IsThis);
                //var txtName = FindViewById<TextView>(Resource.Id.isThis);  //these are the variables for the IsThis layout
                //var yesbtn = FindViewById<Button>(Resource.Id.ybtn);
                //var nobtn = FindViewById<Button>(Resource.Id.nbtn);
            }
            if (resultCode == Result.FirstUser)
            {
                data.GetStringExtra("newAnswer");
                var myintent = new Intent(this, typeof(InGoogle));
                StartActivity(myintent);
            }
            // Display in ImageView. We will resize the bitmap to fit the display.
            // Loading the full sized image will consume too much memory
            // and cause the application to crash.
            ImageView imageView = FindViewById <ImageView>(Resource.Id.takenPictureImageView);
            //TextView googleResponse = FindViewById<TextView>(Resource.Id.whatBe);
            //TextView googleResp1 = FindViewById<TextView>(Resource.Id.whatBe1);
            //TextView googleResp2 = FindViewById<TextView>(Resource.Id.whatBe2);
            //var isItString = FindViewById<TextView>(Resource.Id.isThis); //don't need this var
            int height = Resources.DisplayMetrics.HeightPixels;
            int width  = imageView.Height;

            //AC: workaround for not passing actual files
            Android.Graphics.Bitmap bitmap = (Android.Graphics.Bitmap)data.Extras.Get("data");

            //convert bitmap into stream to be sent to Google API
            string bitmapString = "";

            using (var stream = new System.IO.MemoryStream())
            {
                bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, stream);

                var bytes = stream.ToArray();
                bitmapString = System.Convert.ToBase64String(bytes);
            }

            //credential is stored in "assets" folder
            string credPath = "google_api.json";

            Google.Apis.Auth.OAuth2.GoogleCredential cred;

            //Load credentials into object form
            using (var stream = Assets.Open(credPath))
            {
                cred = Google.Apis.Auth.OAuth2.GoogleCredential.FromStream(stream);
            }
            cred = cred.CreateScoped(Google.Apis.Vision.v1.VisionService.Scope.CloudPlatform);

            // By default, the library client will authenticate
            // using the service account file (created in the Google Developers
            // Console) specified by the GOOGLE_APPLICATION_CREDENTIALS
            // environment variable. We are specifying our own credentials via json file.
            var client = new Google.Apis.Vision.v1.VisionService(new Google.Apis.Services.BaseClientService.Initializer()
            {
                ApplicationName       = "subtle-isotope-190917",
                HttpClientInitializer = cred
            });

            //set up request
            var request = new Google.Apis.Vision.v1.Data.AnnotateImageRequest();

            request.Image         = new Google.Apis.Vision.v1.Data.Image();
            request.Image.Content = bitmapString;

            //tell google that we want to perform label detection
            request.Features = new List <Google.Apis.Vision.v1.Data.Feature>();
            request.Features.Add(new Google.Apis.Vision.v1.Data.Feature()
            {
                Type = "LABEL_DETECTION"
            });
            var batch = new Google.Apis.Vision.v1.Data.BatchAnnotateImagesRequest();

            batch.Requests = new List <Google.Apis.Vision.v1.Data.AnnotateImageRequest>();
            batch.Requests.Add(request);

            //send request.  Note that I'm calling execute() here, but you might want to use
            //ExecuteAsync instead
            var apiResult = client.Images.Annotate(batch).Execute();

            //googleResponse.Text = apiResult.Responses[0].LabelAnnotations[0].Description;
            //googleResp1.Text = apiResult.Responses[0].LabelAnnotations[1].Description;
            //googleResp2.Text = apiResult.Responses[0].LabelAnnotations[2].Description;
            var txtName = FindViewById <TextView>(Resource.Id.isThis);  //these are the variables for the IsThis layout
            var yesbtn  = FindViewById <Button>(Resource.Id.ybtn);
            var nobtn   = FindViewById <Button>(Resource.Id.nbtn);


            // turn confidence float into decimal notation and then to string in percentage.
            // Using this data for GoogleResponse Activity if user wishes to see the confidence and range of labelAnnotations.
            for (int i = 0; i <= 3; i++)
            {
                float percentConfident = (float)apiResult.Responses[0].LabelAnnotations[i].Score * 100;
                int   confidence       = (int)percentConfident;

                string percent = confidence.ToString() + "%";
                string thing   = apiResult.Responses[0].LabelAnnotations[i].Description;
                MainActivity.Items.Add(new Items(thing, percent));
            }

            var isIt = MainActivity.Items[position]; //bug here 0 v position
            //String whatBe = "Is this a " + apiResult.Responses[0].LabelAnnotations[0].Description + " at " + apiResult.Responses[0].LabelAnnotations[0].Score + " ?!"
            //    "I am " + apiResult.Responses[0].LabelAnnotations[0].Score;

            String whatBe = "Is this a " + isIt.Thing + " ?! I am " + isIt.Percent + " confident.";

            txtName.Text = whatBe;
            //var intent = new Intent(this, typeof(IsItActivity));
            //intent.PutExtra("apiResult", myConfidence);
            //StartActivity(intent); dont need this chunk of code because not passing between activities. Will use a public class instead


            FindViewById <Button>(Resource.Id.nbtn).Click += DarnActivityClick;
            FindViewById <Button>(Resource.Id.ybtn).Click += SuccessActivity;
            // Below are the button onClick methods to direct to the Succeed or Darn layouts.

            //whatBe = apiResult.Responses[0].LabelAnnotations[0].Description;
            if (bitmap != null)
            {
                imageView.SetImageBitmap(bitmap);
                imageView.Visibility = Android.Views.ViewStates.Visible;
                bitmap = null;
            }

            // Dispose of the Java side bitmap.
            System.GC.Collect();
        }