예제 #1
0
        public SuccessViewModel CalculateSuccess(SongViewModel song)
        {
            try
            {
                SongBE entity;
                entity = Mapper.Map <SongViewModel, SongBE>(song);
                SongDAL          songDAL          = new SongDAL();
                SuccessViewModel successViewModel = new SuccessViewModel();


                var listSongs = songDAL.GetSongsToCalculate(entity.Category);


                (int sampleRate, double[] audio) = WavFile.ReadMono(FileUtils.GetRepoMusicPath(song.SongKey));


                var spec = new Spectrogram.Spectrogram(sampleRate / 2, fftSize: (16384 / 8), stepSize: (2500 * 5), maxFreq: 2200);
                spec.Add(audio);
                var tempPath = Path.GetTempPath();
                spec.SaveImage(tempPath + "/" + song.SongKey + ".jpg", intensity: 5, dB: true);

                var file = FileUtils.GetImageBytes(tempPath + "/" + song.SongKey + ".jpg");
                successViewModel.ImageBase64 = "data:image/jpg;base64," + Convert.ToBase64String(file);

                var bmHash = this.GetHash(spec.GetBitmap());



                List <Spectrogram.Spectrogram> spectrograms = new List <Spectrogram.Spectrogram>();

                foreach (var son in listSongs)
                {
                    (int sampleRateSong, double[] audioSong) = WavFile.ReadMono(FileUtils.GetRepoMusicPath(son.SongKey));
                    var specSong = new Spectrogram.Spectrogram(sampleRateSong / 2, fftSize: (16384 / 8), stepSize: (2500 * 5), maxFreq: 2200);
                    specSong.Add(audioSong);
                    spectrograms.Add(specSong);
                }

                int equalElements = 0;

                foreach (var sp in spectrograms)
                {
                    equalElements += bmHash.Zip(this.GetHash(sp.GetBitmap()), (i, j) => i == j).Count(eq => eq);
                }

                var con = Convert.ToInt32(equalElements / spectrograms.Count);
                successViewModel.Percentage = Convert.ToInt32((con * 100) / bmHash.Count);
                return(successViewModel);
            }
            catch (Exception ex)
            {
                throw new Exception(Messages.Generic_Error);
            }
        }
예제 #2
0
        public async Task <ActionResult> GetPredictionAsync([FromForm] string page, [FromForm] IFormFile audioFile)
        {
            try
            {
                var modelPath = "";

                if (page == "Login")
                {
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelLogin.zip");
                }
                else if (page == "Language")
                {
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelLanguage.zip");
                }
                else if (page == "Home")
                {
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelHome.zip");
                }
                else if (page == "cart")
                {
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelCartPage.zip");
                }
                else if (page == "singleItem")
                {
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelSingleItemPage.zip");
                }
                else if (page == "category")
                {
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelCategory.zip");
                }
                else if (page == "pay")
                {
                    //var v = @"C:\Users\Frank\source\repos\YorubaModelML\YorubaPredictionAPI\Models\MLModelPayPage.zip";
                    modelPath = Path.Combine(_env.ContentRootPath, "Models", "MLModelPayPage.zip");
                }



                var model = new ConsumeModel(modelPath);

                var uploadPath = Path.Combine(_env.ContentRootPath, "uploads");
                Directory.CreateDirectory(uploadPath);

                if (audioFile.Length > 0)
                {
                    var audioFilePath = Path.Combine(uploadPath, audioFile.FileName);

                    using (var fs = new FileStream(audioFilePath, FileMode.Create))
                    {
                        await audioFile.CopyToAsync(fs);
                    }

                    double[] audio;
                    int      sampleRate;
                    using (var audioFileReader = new AudioFileReader(audioFilePath))
                    {
                        sampleRate = audioFileReader.WaveFormat.SampleRate;
                        var wholeFile   = new List <float>((int)(audioFileReader.Length / 4));
                        var readBuffer  = new float[audioFileReader.WaveFormat.SampleRate * audioFileReader.WaveFormat.Channels];
                        int samplesRead = 0;
                        while ((samplesRead = audioFileReader.Read(readBuffer, 0, readBuffer.Length)) > 0)
                        {
                            wholeFile.AddRange(readBuffer.Take(samplesRead));
                        }
                        audio = Array.ConvertAll(wholeFile.ToArray(), x => (double)x);
                    }

                    int fftSize = 8192;
                    var spec    = new Spectrogram.Spectrogram(sampleRate, 4096, stepSize: 500, maxFreq: 3000, fixedWidth: 250);
                    spec.Add(audio);
                    var info      = new FileInfo(audioFilePath);
                    var imagepath = Path.Combine(uploadPath, info.Name + ".png");
                    spec.SaveImage(imagepath, intensity: 20_000);


                    var md = new ModelInput {
                        ImageSource = imagepath
                    };
                    var result = model.Predict(md);
                    Directory.Delete(uploadPath, true);
                    return(Ok(new { class_id = result.Prediction, probability = result.Score.Max() }));
                    //return Ok(_env.ContentRootPath);
                }
                _logger.LogError("File is Null");
                return(BadRequest("File is null"));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(BadRequest(ex.Message));
            }
        }