使用 Fortran 和 Tesseract 实现验证码识别

一、准备工作

  1. 安装 Tesseract OCR
    Ubuntu / Debian:
    更多内容访问ttocr.com或联系1436423940
    sudo apt install tesseract-ocr
    macOS:

brew install tesseract
2. 安装 Fortran 编译器(如 gfortran)
Ubuntu:

sudo apt install gfortran
macOS:

brew install gcc # 包含 gfortran
二、Fortran 脚本编写
创建文件 ocr.f90:

program captcha_ocr
implicit none
character(len=256) :: image_path, command, output_file
integer :: status
character(len=512) :: result_line
character(len=32) :: clean_text
integer :: i, j, len_result

! 获取图像路径
call get_command_argument(1, image_path)
if (len_trim(image_path) == 0) then
print *, "用法: ./ocr <图像文件路径>"
stop
end if

output_file = "output_ocr.txt"

! 构造命令行,调用 tesseract 识别图像
command = "tesseract " // trim(image_path) // " " // "output_ocr -l eng -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
status = system(trim(command))

if (status /= 0) then
print *, "执行 Tesseract 命令失败"
stop
end if

! 读取输出文本文件内容
open(unit=10, file=trim(output_file) // ".txt", status='old')
read(10,'(A)', iostat=status) result_line
close(10)

if (status /= 0) then
print *, "读取输出失败"
stop
end if

! 提取有效字符(只保留大写字母和数字)
clean_text = ""
j = 1
do i = 1, len(trim(result_line))
select case (result_line(i:i))
case ('A':'Z', '0':'9')
clean_text(j:j) = result_line(i:i)
j = j + 1
end select
if (j > len(clean_text)) exit
end do

print *, "识别结果:", trim(clean_text(1:j-1))
end program captcha_ocr
三、编译并运行
编译:

gfortran ocr.f90 -o ocr
运行:

./ocr test1.png
示例输出:

识别结果: Z8GW

posted @ 2025-06-25 16:00  ttocr、com  阅读(16)  评论(0)    收藏  举报