private void loadSourceCards() { using (var Reader = ssSql.dbResult("SELECT * FROM cards WHERE pHash != '0'")) { foreach (DataRow r in Reader.Rows) { var card = new ReferenceCard(); card.cardId = r["id"].ToString(); card.name = r["Name"].ToString(); card.pHash = Convert.ToUInt64(r["pHash"]); card.dataRow = r; referenceCards.Add(card); } } }
private void matchCard() { //Console.WriteLine("matchCard() called with " + magicCards.Count + " cards detected"); var cardTempId = 0; foreach (var card in magicCards) { cardTempId++; //ContrastCorrection filter = new ContrastCorrection(15); //filter.ApplyInPlace(card.cardArtBitmap); // Write the image to disk to be read by the pHash library.. should really find // a way to pass a pointer to image data directly card.cardBitmap.Save("tempCard" + cardTempId + ".jpg", ImageFormat.Jpeg); // Calculate art bitmap hash ulong cardHash = 0; // Phash.ph_dct_imagehash("tempCard" + cardTempId + ".jpg", ref cardHash); Phash.ph_dct_imagehash(".\\tempCard" + cardTempId + ".jpg", ref cardHash); var lowestHamming = int.MaxValue; ReferenceCard bestMatch = null; foreach (var referenceCard in referenceCards) { var hamming = Phash.HammingDistance(referenceCard.pHash, cardHash); if (hamming < lowestHamming) { lowestHamming = hamming; bestMatch = referenceCard; } } if (bestMatch != null) { var g = Graphics.FromImage(cameraBitmap); g.DrawString(currentMatch, new Font("Tahoma", 25), Brushes.Black, new PointF(card.corners[0].X - 29, card.corners[0].Y - 39)); g.DrawString(currentMatch, new Font("Tahoma", 25), Brushes.Red, new PointF(card.corners[0].X - 30, card.corners[0].Y - 40)); g.Dispose(); #if DEBUG Console.WriteLine("DEBUG: Highest Similarity: " + bestMatch.name + " ID: " + bestMatch.cardId); #endif if (bestMatches.ContainsKey(bestMatch.cardId)) { bestMatches[bestMatch.cardId] += 1; } else { bestMatches[bestMatch.cardId] = 1; } //Console.WriteLine("DEBUG: Checking " + bestMatches[bestMatch.cardId]); var maxValue = 0; string bestMatchId = null; foreach (var match in bestMatches) { if (match.Value > maxValue) { maxValue = match.Value; bestMatchId = match.Key; } } if (bestMatchId != bestMatch.cardId) { continue; } } if (bestMatch != null) { card.referenceCard = bestMatch; currentMatch = bestMatch.name; // highly experimental #if NULL const string tessDataDir = @".\\tessdata"; using (var engine = new TesseractEngine(tessDataDir, "eng", EngineMode.Default)) using (var image = Pix.LoadFromFile(".\\tempCard" + cardTempId + ".jpg")) using (var page = engine.Process(image)) { string text = page.GetText(); Console.WriteLine("DEBUG: Mean confidence: {0}", page.GetMeanConfidence()); Console.WriteLine("DEBUG: " + text); } #endif } } }