October 22, 2024
Chicago 12, Melborne City, USA
python

cv2 pytesseract python image to text/digits


I am trying to make computer vision with game cards to "extract" information from it.
I did a simple python script and the text was extracted quite efficiently with pytesseract however a few information does not come especially "health point" of the card.
For instance that image : https://biggerhat.net/storage/cards/combos/9e4bacf8692cb.jpg
I am supposed to extract 12 (at the bottom on left side)

As a consequence I tried to concentrate only on that part of the card to apply specific processes to get it.

import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\\{PATH TO TESSERACT}\\tesseract.exe'
img = cv2.imread("./Api/factions/malifaux/arcanists/9e4bacf8692cb.jpg")
health = img[865:925,0:550].astype("uint8")
#saving the piece that contains health point
cv2.imwrite('health.png', health)
#make it grey
gray = cv2.cvtColor(health, cv2.COLOR_BGR2GRAY)
#and here my tests
#I use psm 7 digits to extract especially numbers
threshold_img = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
threshold_img = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV, 11, 4)
text = pytesseract.image_to_string(threshold_img,config= '--psm 7 digits')
print(text)`

I can see that numbers are not exactly horizontal and I am wondering if this prevents from extracting
There are several different health size so it can be 12 bubbles but also 4 bubbles such as https://biggerhat.net/storage/cards/combos/93346c896e5b3.jpg

How would you do?

I tried several threshold, and force with psm parameter
I also tried to invert colors but it seems more efficient black on white so I kept it as is.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video