Example #1
0
        static void Main(string[] args)
        {
            Recognizer rec = new Recognizer();

            Settings settings;

            using (StreamReader sr = new StreamReader("credentials.json"))
                settings = JsonConvert.DeserializeObject <Settings>(sr.ReadToEnd());

            BanchoApi api = new BanchoApi(settings.ClientId, settings.Secret);

            Console.WriteLine(api.ReloadToken());

            string[] dirFiles = Directory.GetFiles(@"./testImages/");

            foreach (string path in dirFiles)
            {
                Image img = rec.LoadFromFile(path);

                string[]          recedText = rec.RecognizeTopText(img).Split('\n');
                List <Beatmapset> bms       = api.Search(recedText.First(), WAV_Osu_NetApi.Bancho.QuerryParams.MapType.Any);

                foreach (string s in recedText)
                {
                    Console.WriteLine(s);
                }

                Beatmapset bm = bms?.First();

                if (bm is null)
                {
                    Console.WriteLine("No beatmap found");
                }
                else
                {
                    Console.WriteLine($"{bm.artist} - {bm.title}\n{bm.creator}");
                }
            }
        }
        /// <summary>
        /// Начинает распознавание картинки
        /// </summary>
        /// <param name="attachment">Картинка</param>
        private Tuple <Beatmapset, Beatmap> DownloadAndRecognizeImage(DiscordAttachment attachment)
        {
            string webFileName = $"{DateTime.Now.Ticks}-{attachment.FileName}";

            webClient.DownloadFile(attachment.Url, $"downloads/{webFileName}");

            string fileName = $"downloads/{webFileName}";
            Image  image    = Image.FromFile(fileName);

            string[] rawrecedText = recognizer.RecognizeTopText(image).Split('\n');

            sheduler.AddFileDeleteTask(fileName);

            foreach (string s in rawrecedText)
            {
                logger.LogDebug(s);
            }

            // Searching for first non-empty or almost string
            string recedText = string.Empty;

            foreach (string s in rawrecedText)
            {
                if (!string.IsNullOrWhiteSpace(s) && s.Length > 10)
                {
                    recedText = s;
                    break;
                }
            }

            logger.LogDebug($"Recognized text: {recedText}");

            // Cut artist
            int indexStart = recedText.IndexOf('-');

            if (indexStart == -1)
            {
                indexStart = recedText.IndexOf('–');
            }

            if (indexStart != -1)
            {
                logger.LogDebug("Cutting artist");
                recedText = recedText.Substring(indexStart).TrimStart(new char[] { ' ', '-', '–' });
            }

            logger.LogDebug($"Searching for: {recedText}");
            List <Beatmapset> bmsl = api.Search(recedText, WAV_Osu_NetApi.Bancho.QuerryParams.MapType.Any);


            // Get map diff
            indexStart = recedText.IndexOf('[');
            if (indexStart == -1)
            {
                logger.LogInformation($"Coulnd't get map difficulty");
                return(null);
            }

            string diffName = recedText.Substring(indexStart).TrimStart('[').TrimEnd(']');

            logger.LogDebug($"diffName: {diffName}");

            if (bmsl == null || bmsl.Count == 0)
            {
                logger.LogInformation($"Api search return null or empty List");
                return(null);
            }

            string mapper = string.Empty;

            mapper = rawrecedText.Select(x => x)
                     .FirstOrDefault(x =>
            {
                x = x.ToLowerInvariant();
                return(x.Contains("mapped") || x.Contains("beatmap"));
            });


            Beatmapset bms = null;

            if (!string.IsNullOrEmpty(mapper))
            {
                mapper = mapper?.Substring(10);
                logger.LogDebug($"Got mapper: {mapper}. Comparing...");
                List <Tuple <Beatmapset, double> > bsm = bmsl.Select(x => Tuple.Create(x, WAV_Osu_Recognizer.RecStringComparer.Compare(x.creator, mapper)))
                                                         .OrderByDescending(x => x.Item2)
                                                         .ToList();


                foreach (var b in bsm)
                {
                    logger.LogDebug($"{b.Item1.creator}: {b.Item2}");
                }

                if (bsm == null || bsm.Count == 0)
                {
                    bms = bmsl.FirstOrDefault();
                }
                else
                {
                    bms = bsm.First().Item1;
                }
            }
            else
            {
                logger.LogInformation($"Couldn't get mapper");
                bms = bmsl.FirstOrDefault();
            }


            if (bms == null)
            {
                logger.LogInformation($"No matching beatmapsets");
                return(null);
            }
            else
            {
                logger.LogDebug($"Beatmapsets count: {bmsl.Count}");
            }


            List <Tuple <Beatmap, double> > bmds = bms.beatmaps.Select(x => Tuple.Create(x, WAV_Osu_Recognizer.RecStringComparer.Compare(x.version, diffName)))
                                                   .OrderByDescending(x => x.Item2)
                                                   .ToList();

            logger.LogDebug("Comparing beatmap versions:");
            foreach (var k in bmds)
            {
                logger.LogDebug($"{k.Item1.version} {k.Item2}");
            }



            var result = Tuple.Create(bms, bmds.First().Item1);

            logger.LogInformation($"Success. bms_id: {result.Item1.id}, bm_id: {result.Item2.id}");
            return(result);
        }