-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocr_mevoplus.py
144 lines (118 loc) · 5.12 KB
/
ocr_mevoplus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ocr script to intergrate mevo+ launch monitor with gspro openapi, can be adapted to any device.
#
##todo list##
#pyautogui get tuple of window to screenshot
#algo to crop based off proprotions instead of exact pixels
#recieve json over tcp, confirm send
#add total carry, CHS, AOA
import time
import math
import numpy
import re
import mss
import cv2
import pytesseract
import json
import socket
import sys
#tesseract path
pytesseract.pytesseract.tesseract_cmd = r'C:\Users\*username*\AppData\Local\Programs\Tesseract-OCR\tesseract.exe'
#open socket (SOCK_STREAM means a TCP)
HOST, PORT = "localhost", 921
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
#define screen grab selection, using "letsview" iphone11 screen mirroed and window in top left corner of screen, need to adjust this for each case likely.
mon = {'top': 240, 'left': 360, 'width': 120, 'height': 525} #working on a automated soltuion
#shot counter, start at zero
shot_count = 0
#define last values for shot detection
ballspeed_last = None
totalspin_last = None
sa_last = None
hla_last = None
vla_last = None
with mss.mss() as sct: #screenshot loop
while True:
im = numpy.asarray(sct.grab(mon)) #screenshot to numpy array
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) #array to gray img
#crop numpy img array into parts to feed to OCR for each var
im_ballspeed = im[44:80, 10:115]
im_vla = im[150:185, 10:115]
im_hla = im[255:290, 10:115]
im_sa = im[360:395, 10:115]
im_totalspin = im[465:500, 10:115]
#cv2.imshow('im', im_sa) #use this to debug screenshot crops
#cv2.waitKey() #pause
#ocr
ballspeed = pytesseract.image_to_string(im_ballspeed, lang='eng',config='--psm 6 -c page_separator='' tessedit_char_whitelist=.0123456789LR')
vla = pytesseract.image_to_string(im_vla, lang='eng',config='--psm 6 -c page_separator='' tessedit_char_whitelist=.0123456789LR')
hla = pytesseract.image_to_string(im_hla, lang='eng',config='--psm 6 -c page_separator='' tessedit_char_whitelist=.0123456789LR')
sa = pytesseract.image_to_string(im_sa, lang='eng',config='--psm 6 -c page_separator='' tessedit_char_whitelist=.0123456789LR')
totalspin = pytesseract.image_to_string(im_totalspin, lang='eng',config='--psm 6 -c page_separator='' tessedit_char_whitelist=.0123456789LR')
#clean up output from ocr
ballspeed = ballspeed.strip('\n |')
vla = vla.strip('\n |')
hla = hla.strip('\n |')
sa = sa.strip('\n |')
totalspin = totalspin.strip('\n |')
#parse SA
converted_sa = re.findall("\d+\.\d+", sa)
float_sa = float('.'.join(str(ele) for ele in converted_sa))
#parse spinaxis laterality
lat_sa = sa[-1] #get last char from string
left = "L" #define L is left
if lat_sa.find(left) != -1:
sa = float_sa*-1 #if L set negative
else:
sa = float_sa
#parse HLA
converted_hla = re.findall("\d+\.\d+", hla)
float_hla = float('.'.join(str(ele) for ele in converted_hla))
#parse HLA laterality
lat_hla = hla[-1] #get last char from string
left = "L" #define L is left
if lat_hla.find(left) != -1:
hla = float_hla*-1 #if L set negative
else:
hla = float_hla
#check if vars have changed
if ballspeed != ballspeed_last or totalspin != totalspin_last or sa != sa_last or hla != hla_last or vla != vla_last :
#update last vars
ballspeed_last = ballspeed
totalspin_last = totalspin
sa_last = sa
hla_last = hla
vla_last = vla
#shot counter, add +1 each loop
shot_count = shot_count + 1
print (f"Shot Count = {shot_count}")
print (f"Ballspeed = {ballspeed}")
print (f"VLA = {vla}")
print (f"HLA = {hla}")
print (f"Spin Axis = {sa}")
print (f"Total Spin = {totalspin}")
#data to dict to nested JSON
jsondata = {}
DeviceID = 'GSPro LM 1.1'
Units = 'Yards'
ShotNumber = shot_count
APIversion = '1'
BallData = {}
ShotDataOptions = {}
BallData['Speed'] = ballspeed
BallData['SpinAxis'] = sa
BallData['TotalSpin'] = totalspin
BallData['HLA'] = hla
BallData['VLA'] = vla
ShotDataOptions['ContainsBallData'] = 'true'
ShotDataOptions['ContainsClubData'] = 'false'
jsondata['DeviceID'] = DeviceID
jsondata['Units'] = Units
jsondata['ShotNumber'] = ShotNumber
jsondata['APIversion'] = APIversion
jsondata['BallData'] = BallData
jsondata['ShotDataOptions'] = ShotDataOptions
print(json.dumps(jsondata))
#TCP socket send
sock.sendall(json.dumps(jsondata).encode("utf-8"))
sock.close() #close TCP socket at end