Skip to content

Commit

Permalink
Modification to get connector working with new range
Browse files Browse the repository at this point in the history
  • Loading branch information
springbok committed Dec 20, 2023
1 parent 5468a30 commit 72ccc5a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,5 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
train.traineddata.new
train.traineddata.old
2 changes: 1 addition & 1 deletion src/MainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LogTableCols:


class MainWindow(QMainWindow, Ui_MainWindow):
version = 'V1.01.17'
version = 'V1.01.20'
app_name = 'MLM2PRO-GSPro-Connector'
good_shot_color = '#62ff00'
good_putt_color = '#fbff00'
Expand Down
67 changes: 44 additions & 23 deletions src/screenshot_base.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import logging
import os
import time

import cv2
import numpy as np
import pyqtgraph as pg
import tesserocr
from PIL import Image
from PIL import Image, ImageOps
from pyqtgraph import ViewBox
from src.ball_data import BallData
from src.labeled_roi import LabeledROI
Expand Down Expand Up @@ -122,6 +118,7 @@ def mse(self, imageA, imageB):
def ocr_image(self):
self.balldata = BallData()
self.new_shot = False
fallback_tesserocr_api = None
if self.__class__.__name__ == 'ScreenshotExPutt':
train_file = 'exputt'
else:
Expand All @@ -131,31 +128,55 @@ def ocr_image(self):
logging.debug(f"Using {train_file}_traineddata for OCR")
tesserocr_api = tesserocr.PyTessBaseAPI(psm=tesserocr.PSM.SINGLE_WORD, lang=train_file, path='.\\')
try:
pil_img = Image.fromarray(self.screenshot_image).convert('L')
sc = np.array(pil_img)
#pil_img = Image.fromarray(self.screenshot_image).convert('L')
#sc = np.array(pil_img)
for roi in self.rois_properties():
cropped_img = self.image_rois[roi].getArrayRegion(sc, self.image_item)
original_height, original_width = cropped_img.shape[:2]
print(f"width: {original_width} height: {original_height}")
factor = 3
cropped_img = cv2.resize(cropped_img,
(int(original_height * 12), int(original_width * 2)),
interpolation=cv2.INTER_LINEAR)
cropped_img = self.image_rois[roi].getArrayRegion(self.screenshot_image, self.image_item)
#original_height, original_width = cropped_img.shape[:2]
#print(f"width: {original_width} height: {original_height}")
#factor = 3
#cropped_img = cv2.resize(cropped_img,
# (int(original_height * 12), int(original_width * 2)),
# interpolation=cv2.INTER_LINEAR)
img = Image.fromarray(np.uint8(cropped_img))
#width, height = img.size
#img = img.resize(int(width * factor), int(height * factor))
#if self.__class__.__name__ != 'ScreenshotExPutt' and self.settings.device_id == LaunchMonitor.MLM2PRO:
if self.__class__.__name__ != 'ScreenshotExPutt' and self.settings.device_id == LaunchMonitor.MLM2PRO:
# Convert to black text on white background, remove background
#threshold = 150
#img = img.point(lambda x: 0 if x > threshold else 255)
#filename = time.strftime(f"{roi}_%Y%m%d-%H%M%S.bmp")
filename = time.strftime(f"{roi}.bmp")
path = f"{os.getcwd()}\\appdata\\logs\\{filename}"
img.save(path)

threshold = 150
img = img.point(lambda x: 0 if x > threshold else 255)
#filename = time.strftime(f"{roi}_%Y%m%d-%H%M%S.bmp")
#filename = time.strftime(f"{roi}.bmp")
#path = f"{os.getcwd()}\\appdata\\logs\\original_{filename}"
#img.save(path)
bbox = ImageOps.invert(img).getbbox()
logging.debug(f'ocr {roi} - bounding box for white space removal: {bbox}')
bbox1 = []
if bbox is not None:
for i in range(len(bbox)):
if (i == 0 or i == 1) and bbox[i] > 0: # left & upper
bbox1.append(bbox[i] - 5)
elif (i == 2 or i == 3): # right & lower
bbox1.append(bbox[i] + 5)
else:
bbox1.append(bbox[i])
logging.debug(f'ocr {roi} - modified bounding box with a small amount of white space added: {bbox1}')
img = img.crop(bbox1)
#filename = time.strftime(f"{roi}.bmp")
#path = f"{os.getcwd()}\\appdata\\logs\\{filename}"
#img.save(path)
tesserocr_api.SetImage(img)
ocr_result = tesserocr_api.GetUTF8Text()
logging.debug(f'ocr {roi}: {ocr_result}')
conf = tesserocr_api.MeanTextConf()
logging.debug(f'ocr {roi} - confidence: {conf} result: {ocr_result}')
if conf <= 0:
logging.debug(f'ocr {roi} confidence <= 0 retrying with RAW_LINE')
if fallback_tesserocr_api is None:
fallback_tesserocr_api = tesserocr.PyTessBaseAPI(psm=tesserocr.PSM.RAW_LINE, lang=train_file, path='.\\')
fallback_tesserocr_api.SetImage(img)
ocr_result = fallback_tesserocr_api.GetUTF8Text()
conf = fallback_tesserocr_api.MeanTextConf()
logging.debug(f'fallback ocr {roi} - confidence: {conf} result: {ocr_result}')
if self.__class__.__name__ == 'ScreenshotExPutt':
self.balldata.process_putt_data(ocr_result, roi, self.previous_balldata)
else:
Expand Down
Binary file modified train.traineddata
Binary file not shown.
Binary file added train.traineddata.old
Binary file not shown.

0 comments on commit 72ccc5a

Please sign in to comment.