Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavlume committed Sep 17, 2023
1 parent 45a8d7b commit 3dccdf0
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 0 deletions.
23 changes: 23 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[Coordinates]
x1 = 1
y1 = 2
x2 = 3
y2 = 4
x3 = 5
y3 = 6
x4 = 7
y4 = 8

[Path]
tesseract = C:\\Program Files\\Tesseract-OCR\\tesseract.exe

[Other]
debug = False
savetolog = False
savetoclipboard = True

[Text]
prefix = I'm Listening to
linker = -
suffix = , and it's a good song.

177 changes: 177 additions & 0 deletions stt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import PySimpleGUI as sg

from PIL import ImageGrab
from pytesseract import pytesseract
import win32clipboard
from pyautogui import position
import configparser


config = configparser.ConfigParser()
config.read('config.ini')

coords = config["Coordinates"]
x1, y1, x2, y2, x3, y3, x4, y4 = int(coords['x1']), int(coords['y1']), int(coords['x2']), int(coords['y2']), int(coords['x3']), int(coords['y3']), int(coords['x4']),int(coords['y4'])

# Defining paths to tesseract.exe
# and the image we would be using
path_to_tesseract = config["Path"]["tesseract"]
pytesseract.tesseract_cmd = path_to_tesseract

debug = config.getboolean("Other", "debug")
saveToLog = config.getboolean("Other", "saveToLog")
saveToClipboard = config.getboolean("Other", "saveToClipboard")

prefix, linker, suffix = config["Text"]["prefix"], config["Text"]["linker"], config["Text"]["suffix"]

def validValues(xyValues: list[int]) -> bool:
if xyValues[0] >= xyValues[2] or xyValues[1] >= xyValues[3]:
sg.popup_error("Invalid Title Coordinates")
return False
if xyValues[4] >= xyValues[6] or xyValues[5] >= xyValues[7]:
sg.popup_error("Invalid Artist Coordinates")
return False

return True

def getText(firstBox, secondBox, prefix: str = "", linker = " - ", suffix: str = "", debug: bool = False) -> str:

img = ImageGrab.grab(all_screens=True)
img1 = img.crop(firstBox)
img2 = img.crop(secondBox)

if debug:
img1.save("img1.png")
img2.save("img2.png")
# Providing the tesseract executable
# location to pytesseract library


title = pytesseract.image_to_string(img1)
artist = pytesseract.image_to_string(img2)

title = title.replace("\n", "")
title = title.replace(" ", "")

artist = artist.replace("\n", "")
artist = artist.replace(" ", "")

text = prefix + " " + title + " " + linker + " " + artist + suffix

return text
# Displaying the extracted text

def textToClipboard(text: str):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(text)
win32clipboard.CloseClipboard()

sg.theme('Dark Blue 3') # please make your windows colorful

layout = [
[sg.Text('Current Mouse Position: ( , )', key="mouseLabel")],

[sg.Text('')],

[sg.Text('Title Coordinates: ')],
[sg.Text('Top Left x and y: ', size=(15, 1)), sg.InputText(x1, key='x1',size=(10, 1)), sg.InputText(y1, key='y1', size=(10, 1))],
[sg.Text('Bottom right x and y: ', size=(15, 1)), sg.InputText(x2, key='x2',size=(10, 1)), sg.InputText(y2, key='y2', size=(10, 1))],

[sg.Text('Artist Coordinates: ')],
[sg.Text('Top Left x and y: ', size=(15, 1)), sg.InputText(x3, key='x3',size=(10, 1)), sg.InputText(y3, key='y3', size=(10, 1))],
[sg.Text('Bottom right x and y: ', size=(15, 1)), sg.InputText(x4, key='x4',size=(10, 1)), sg.InputText(y4, key='y4', size=(10, 1))],

[sg.Text('')],

[sg.Text('Prefix: ', size=(15, 1)), sg.InputText(prefix, key='prefix',size=(20, 1))],
[sg.Text('Linker: ', size=(15, 1)), sg.InputText(linker, key='linker',size=(20, 1))],
[sg.Text('Suffix: ', size=(15, 1)), sg.InputText(suffix, key='suffix',size=(20, 1))],

[sg.Checkbox("Debug", default=debug, key="debug"), sg.Checkbox("Save Output To Log File",default=saveToLog, key="saveToLog"), sg.Checkbox("Save Output To Clipboard",default=saveToClipboard, key="saveToClipboard")],

[sg.Text('')],

[sg.Button("Apply"), sg.Button("Save to Config")],

[sg.Text('')],

[sg.Text('Output: ', key="output")]
]

window = sg.Window('Spotify To Text', layout)

while True:
event, values = window.read(timeout=20)

mousePosition = position()
mousePositionText = f"( {mousePosition[0]} , {mousePosition[1]} )"
mousePositionText = "Current Mouse Position: " + mousePositionText
window["mouseLabel"].update(mousePositionText)

if event == sg.WIN_CLOSED:
break

if event != "Apply" and event != "Save to Config":
continue

try:
debug = window["debug"].Get()
saveToLog = window["saveToLog"].Get()
saveToClipboard = window["saveToClipboard"].Get()

prefix, linker, suffix = values['prefix'], values['linker'], values['suffix']

xyValues = [values['x1'], values['y1'], values['x2'], values['y2'], values['x3'], values['y3'], values['x4'], values['y4']]

invalid = False
for coord in xyValues:
if not coord.isdigit():
print("invalid coord:", coord)
invalid = True

if invalid:
continue

xyValues = [int(value) for value in xyValues]

if not validValues(xyValues):
continue

if event == "Save to Config":
if sg.popup_yes_no('Do you really want to save to config?') == 'Yes':
config["Path"]["tesseract"] = path_to_tesseract
config["Other"]["debug"] = str(debug)
config["Other"]["saveToLog"] = str(saveToLog)
config["Other"]["saveToClipboard"] = str(saveToClipboard)
config["Text"]["prefix"], config["Text"]["linker"], config["Text"]["suffix"] = prefix, linker, suffix

xyStringValues = [str(value) for value in xyValues]

for index, key in enumerate(config["Coordinates"].keys()):
config["Coordinates"][key] = xyStringValues[index]

with open('config.ini', 'w') as configfile:
config.write(configfile)

continue


titleBbox = (xyValues[0], xyValues[1], xyValues[2], xyValues[3])
artistBbox = (xyValues[4], xyValues[5], xyValues[6], xyValues[7])

text = getText(titleBbox, artistBbox, prefix, linker, suffix, debug)

window["output"].update("Output: " + text)

if saveToLog:
with open("log.txt", 'a') as f:
f.write(text + "\n")

if saveToClipboard:
textToClipboard(text)
except Exception as error:
sg.popup_error(str(error))


window.close()

0 comments on commit 3dccdf0

Please sign in to comment.