I'm currently writing a tool to help me count currency in the game Path of Exile.
I can easily find and put bounding boxes around all the types of currencies in this picture, but in the next step I want to count the stacks with Pytesseract.image_to_string like this:
#currently only takes a sample image later will take a screenshot of active monitor
scrn_rec_img_gray = cv2.cvtColor(scrn_rec_img, cv2.COLOR_BGR2GRAY)
# _, img = cv2.threshold(np.array(scrn_rec_img_gray), 100, 200, cv2.THRESH_BINARY)
# _, thresh1 = cv2.threshold(np.array(scrn_rec_img_gray), 240, 255, cv2.THRESH_OTSU)
thresh1 = cv2.adaptiveThreshold(scrn_rec_img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
# unpacks coordinates of bounding boxes previously detected with template matching. total of 2 boxes are stored in rect_bbx
for (x, y, w, h) in rect_bbxs:
cropped = thresh1[y:y+h-32, x:x+w-23]
scaled_up = cv2.pyrUp(cropped)
# tried scaling up etc to maybe detect easier
blurred = cv2.GaussianBlur(scaled_up, (5, 5), 0)
edged = cv2.Canny(blurred, 50, 250, 255)
# edgefinding with canny
print(pytesseract.image_to_string(edged, config='digits'))
# print found digits
cv2.imshow('test', edged)
cv2.waitKey(0)
So far I've tried, scaling, cropping as much as possible, using all types of threshold algorithms with different values that made it very clear for my human eyes to read the numbers. Some adaptive thresholds as well (as far as I understood them from the documentation). Same problem with or without the print(pytesseract.image_to_string(edged, config='digits')) config for digits only.
For testing, I'm currently only checking for this template. Here are some of the results that didn't detect any digits or completely wrong characters:
With this
_, thresh1 = cv2.threshold(np.array(scrn_rec_img_gray), 230, 255, cv2.THRESH_BINARY)
I get: this which is one of the better results
You can find the complete file here (It is very messy, pls don't judge me)