Class: UdiseCaptchaReader::CharacterRecognizer
- Inherits:
-
Object
- Object
- UdiseCaptchaReader::CharacterRecognizer
- Defined in:
- lib/udise_captcha_reader/character_recognizer.rb
Constant Summary collapse
- CHAR_SIZE =
Target size for normalized characters
32
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ CharacterRecognizer
constructor
A new instance of CharacterRecognizer.
- #recognize_character(image_file) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ CharacterRecognizer
Returns a new instance of CharacterRecognizer.
8 9 10 11 12 13 14 15 |
# File 'lib/udise_captcha_reader/character_recognizer.rb', line 8 def initialize( = {}) @options = { lang: "eng", processor: "mini_magick", psm: 10, oem: 1 }.merge() end |
Instance Method Details
#recognize_character(image_file) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/udise_captcha_reader/character_recognizer.rb', line 17 def recognize_character(image_file) image = MiniMagick::Image.open(image_file.path) # Normalize the character size width = image.width height = image.height scale = CHAR_SIZE / [width, height].max.to_f # Create a normalized version image. do |b| # Resize to standard size b.resize "#{(width * scale).round}x#{(height * scale).round}" # Center in a fixed-size canvas b.background "white" b.gravity "center" b.extent "#{CHAR_SIZE}x#{CHAR_SIZE}" # Convert to grayscale and adjust contrast b.colorspace "gray" b.brightness_contrast "10x20" # Slight brightness boost and contrast # Clean threshold b.black_threshold "80%" b.white_threshold "20%" end # Save normalized image temp_output = Tempfile.new(['norm_char', '.png']) image.write(temp_output.path) # Try recognition with specific configurations results = [] # Configuration attempts configs = [ { psm: 10, whitelist: "0123456789abcdef" }, { psm: 10, whitelist: "0123456789" }, # Numbers only { psm: 10, whitelist: "abcdef" } # Hex letters only ] configs.each do |config| result = RTesseract.new( temp_output.path, lang: @options[:lang], processor: @options[:processor], psm: config[:psm], oem: 1, options: { "tessedit_char_whitelist" => config[:whitelist], "tessedit_ocr_engine_mode" => "1", "tessedit_pageseg_mode" => config[:psm].to_s, "tessdata_dir" => "/usr/local/share/tessdata", # Ensure using latest tessdata "load_system_dawg" => "0", # Disable dictionary "load_freq_dawg" => "0" # Disable frequency-based corrections } ).to_s.strip.downcase results << result unless result.empty? end # Try with inverted image if no results if results.empty? image.negate image.write(temp_output.path) configs.each do |config| result = RTesseract.new( temp_output.path, lang: @options[:lang], processor: @options[:processor], psm: config[:psm], oem: 1, options: { "tessedit_char_whitelist" => config[:whitelist], "tessedit_ocr_engine_mode" => "1", "tessedit_pageseg_mode" => config[:psm].to_s, "tessdata_dir" => "/usr/local/share/tessdata", "load_system_dawg" => "0", "load_freq_dawg" => "0" } ).to_s.strip.downcase results << result unless result.empty? end end # Clean up temp_output.unlink # Process results char_counts = Hash.new(0) results.each do |result| next if result.empty? char = result[0].downcase # Map commonly confused characters char = '1' if char == 'i' || char == 'l' || char == '|' char = '0' if char == 'o' char_counts[char] += 1 if char.match?(/^[a-f0-9]/) end return '' if char_counts.empty? char_counts.max_by { |_, count| count }[0] end |