亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Tesseract 識別頁面上的單個字符

使用 Tesseract 識別頁面上的單個字符

牛魔王的故事 2022-07-19 15:10:49
此圖像返回空字符串;基本上我正在嘗試為 WOW 游戲制作一個機器人,但我對這個 OCR 東西真的很陌生。我無法讓 tesseract 閱讀這張圖片;我想要一個無序列表的字符以及包含它們的每個正方形的坐標(如果可能)。有沒有辦法做到這一點?感謝您的時間!這是我的代碼:from PIL import Imageimport cv2from pytesseract import image_to_stringcolumn = Image.open('photo.png')gray = column.convert('L')blackwhite = gray.point(lambda x: 255 if x < 200 else 0, '1')blackwhite.save("code_bw.jpg")print(image_to_string(cv2.imread("code_bw.jpg")))
查看完整描述

1 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

您需要做一些預處理來隔離文本字符。一個簡單的方法是使用 Otsu 的閾值來獲得二值圖像,然后我們可以找到輪廓并使用縱橫比 + 輪廓區域進行過濾。這將為我們提供文本的邊界框坐標,我們可以將其繪制到蒙版上。我們按位和輸入圖像的掩碼得到我們的清潔圖像,然后將其放入 OCR。結果如下:

檢測到的文本字符

http://img1.sycdn.imooc.com//62d65925000195a502740286.jpg

結果


http://img1.sycdn.imooc.com//62d6592c00014c1502750282.jpg

OCR 的結果


A

A R

P

代碼


import cv2

import pytesseract

import numpy as np


pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"


# Load image, grayscale, Otsu's threshold

image = cv2.imread('1.jpg')

original = image.copy()

mask = np.zeros(image.shape, dtype=np.uint8) 

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]


# Find contours and filter using aspect ratio and area

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cnts = cnts[0] if len(cnts) == 2 else cnts[1]

for c in cnts:

    area = cv2.contourArea(c)

    x,y,w,h = cv2.boundingRect(c)

    ar = w / float(h)

    if area > 1000 and ar > .85 and ar < 1.2:

        cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)

        cv2.rectangle(mask, (x, y), (x + w, y + h), (255,255,255), -1)

        ROI = original[y:y+h, x:x+w]


# Bitwise-and to isolate characters 

result = cv2.bitwise_and(original, mask)

result[mask==0] = 255


# OCR

data = pytesseract.image_to_string(result,,config='--psm 6')

print(data)


cv2.imshow('image', image)

cv2.imshow('thresh', thresh)

cv2.imshow('result', result)

cv2.waitKey()


查看完整回答
反對 回復 2022-07-19
  • 1 回答
  • 0 關注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號