コード例 #1
0
        public ActionResult Test1(HttpPostedFileBase ImageFile)
        {
            if (ImageFile == null || ImageFile.ContentLength == 0)
            {
                //Show spinner
                ViewBag.Error = "Please select a file.<br>";
                return(View("Index"));
            }
            else
            {
                if (ImageFile.FileName.EndsWith("jpg") || ImageFile.FileName.EndsWith("png"))
                {
                    //shaun's code
                    string path = Server.MapPath("~/Doc/" + ImageFile.FileName);
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }
                    ImageFile.SaveAs(path);
                    // Configure API key authorization: Apikey
                    Configuration.Default.AddApiKey("Apikey", "XXX");

                    var apiInstance         = new NsfwApi();
                    var imageFile           = new System.IO.FileStream(path, System.IO.FileMode.Open); // System.IO.Stream | Image file to perform the operation on.  Common file formats such as PNG, JPEG are supported.
                    System.IO.Stream stream = ImageFile.InputStream;

                    // Do not actually store your IAM credentials in code. EC2 Role is best
                    var awsKey       = "XXX";
                    var awsSecretKey = "XXX";
                    var bucketRegion = Amazon.RegionEndpoint.USEast1;   // Your bucket region
                    var s3           = new AmazonS3Client(awsKey, awsSecretKey, bucketRegion);
                    var putRequest   = new PutObjectRequest();

                    //imageURL
                    string fileName = ImageFile.FileName;
                    string imgURL   = "csc-assignment-2-2020.s3-ap-southeast-1.amazonaws.com/" + fileName;

                    //TestDBEntities db = new TestDBEntities();
                    //TalentData data = new TalentData();

                    try
                    {
                        // Describe an image in natural language
                        NsfwResult result = apiInstance.NsfwClassify(imageFile);
                        Debug.WriteLine(result);
                        ViewBag.Score   = result.Score;
                        ViewBag.Outcome = result.ClassificationOutcome;
                        //Hide spinner
                        switch (result.ClassificationOutcome)
                        {
                        case "SafeContent_HighProbability":
                        {
                            putRequest.BucketName  = "csc-assignment-2-2020";               // Your bucket name
                            putRequest.ContentType = "image/jpeg";
                            putRequest.InputStream = ImageFile.InputStream;
                            // key will be the name of the image in your bucket
                            putRequest.Key = ImageFile.FileName;
                            PutObjectResponse putResponse = s3.PutObject(putRequest);

                            //data.talentName = fileName;
                            //data.imageURL = imgURL;
                            //db.TalentDatas.Add(data);
                            //db.SaveChanges();

                            //List<TalentDataViewModel> dataList = db.TalentDatas.Select(x => new TalentDataViewModel
                            //{
                            //    talentName = x.talentName,
                            //    imageURL = x.imageURL
                            //}).ToList();

                            return(View("Success", result));
                        }

                        case "UnsafeContent_HighProbability":
                            return(View("Fail", result));

                        case "RacyContent":
                            return(View("Fail", result));

                        case "SafeContent_ModerateProbability":
                        {
                            putRequest.BucketName  = "csc-assignment-2-2020";               // Your bucket name
                            putRequest.ContentType = "image/jpeg";
                            putRequest.InputStream = ImageFile.InputStream;
                            // key will be the name of the image in your bucket
                            putRequest.Key = ImageFile.FileName;
                            PutObjectResponse putResponse = s3.PutObject(putRequest);

                            return(View("Success", result));
                        }

                        default:
                            return(View("Index"));
                        }


                        return(View("Success"));
                    }
                    catch (Exception e)
                    {
                        Debug.Print("Exception when calling RecognizeApi.RecognizeDescribe: " + e.Message);
                        return(View("Index"));
                    }
                }
                else
                {
                    ViewBag.Error = "File type is incorrect.<br>";
                    return(View("Index"));
                }
            }
        }//end of Upload & Save + NSFW
 public void Init()
 {
     instance = new NsfwApi();
 }