Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from fuzzywuzzy import fuzz, process
- import os
- import ConfigParser
- from PIL import Image, ImageFilter, ImageOps, ImageEnhance
- from pytesseract import image_to_string as tess
- from collections import namedtuple
- import time
- import requests
- import json
- import psycopg2
- def getresinfo(im):
- config = ConfigParser.SafeConfigParser()
- config.read("./libs/resolution_" + str(im.size[0]) + "x" + str(im.size[1]) + ".ini")
- resdata = {}
- for section in config.sections():
- resdata[section] = {}
- for item in config.items(section):
- resdata[section][item[0]] = item[1]
- return resdata
- def preprocess(img):
- temp = img
- temp = ImageOps.invert(temp).convert(mode="L").point(lambda p: p > 30 and 255)
- return temp
- def getrowelement(section):
- global resdata, row, temp
- x1 = int(resdata[section]["x1"])
- x2 = int(resdata[section]["x2"])
- rowheight = int(resdata["RowHeight"]["height"])
- y1 = int(resdata["Rows"][row])
- box = [x1, y1, x2, y1+rowheight]
- #temp.crop(box).save("./screenshots/debug/5ApFK8q_" + row + ".jpg")
- return tess(temp.crop(box), config="-psm 6" )
- def getfileleveldata():
- pass
- def getrowleveldata():
- pass
- def createmechlistfile():
- f1=open('./mechlist', 'w+')
- for x in mechlist():
- print >>f1, x
- def mechlist():
- url = "http://mwo.smurfy-net.de/api/data/mechs"
- full_json = json.loads(requests.get(url).text)
- mechs = []
- for key,value in full_json.iteritems():
- mechs.append(value["translated_short_name"])
- return sorted(mechs)
- def main():
- global resdata, row, temp
- files = os.listdir('./../../screenshots/Failoe')
- start_time = time.time()
- for file in files:
- filepath = "./../../screenshots/Failoe/"+file
- print filepath
- im = Image.open(filepath)
- temp = preprocess(im)
- resdata = getresinfo(im)
- temp = preprocess(im)
- for row in resdata["Rows"]:
- player = getrowelement("Player")
- mech = getrowelement("Mech")
- status = getrowelement("Status")
- score = getrowelement("Matchscore")
- kills = getrowelement("Kills")
- assists = getrowelement("Assists")
- damage = getrowelement("Damage")
- faction = getrowelement("Faction")
- ping = getrowelement("Ping")
- print time.time(), faction, player, mech, "Status:", status, "Score:", score, "Kills: ", kills, "Assists:", assists, "Damage:", damage, "Ping:", ping
- print time.time() - start_time
- def collectinitialdata():
- with open('./libs/maps.txt', 'r') as f:
- mapnames = [line.strip() for line in f]
- with open('./libs/modes.txt', 'r') as f:
- modes = [line.strip() for line in f]
- with open('./libs/mechlist', 'r') as f:
- mechs = [line.strip() for line in f]
- return (mapnames, modes, mechs)
- def get_processingtime():
- return time.time()
- def get_map():
- global resdata, temp, mapnames
- x1 = int(resdata["Map"]["x1"])
- x2 = int(resdata["Map"]["x2"])
- y1 = int(resdata["Map"]["y1"])
- y2 = int(resdata["Map"]["y2"])
- box = [x1, y1, x2, y2]
- tessmap = tess(temp.crop(box), config="-psm 6" )
- fuzzmap = process.extractOne(tessmap, mapnames)[0]
- distratio = process.extractOne(tessmap, mapnames)[1]
- if distratio > 80:
- return fuzzmap
- else:
- return "Failed to identify: " + tessmap
- #Abort needed
- def get_mode():
- global resdata, modes, im
- x1 = int(resdata["Mode"]["x1"])
- x2 = int(resdata["Mode"]["x2"])
- y1 = int(resdata["Mode"]["y1"])
- y2 = int(resdata["Mode"]["y2"])
- box = [x1, y1, x2, y2]
- temp = im.crop(box)
- tempmode = ImageOps.invert(temp).convert(mode="L").point(lambda p: p > 80 and 255)
- tessmode = tess(tempmode, config="-psm 6" ).replace("GAME MODE:","")
- fuzzmode = process.extractOne(tessmode, modes)[0]
- distratio = process.extractOne(tessmode, modes)[1]
- if distratio > 80:
- return fuzzmode
- else:
- level = 75
- while level > 20:
- tempmode = ImageOps.invert(temp).convert(mode="L").point(lambda p: p > level and 255)
- tessmode = tess(tempmode, config="-psm 6" ).replace("GAME MODE:","")
- fuzzmode = process.extractOne(tessmode, modes)[0]
- distratio = process.extractOne(tessmode, modes)[1]
- level -= 5
- if distratio > 80:
- return fuzzmode
- else:
- return "Failed to identify: " + tessmode
- #Abort needed
- def get_match_time():
- global resdata, im
- x1 = int(resdata["Time"]["x1"])
- x2 = int(resdata["Time"]["x2"])
- y1 = int(resdata["Time"]["y1"])
- y2 = int(resdata["Time"]["y2"])
- box = [x1, y1, x2, y2]
- temp = im.crop(box)
- temptime = ImageOps.invert(temp).convert(mode="L").point(lambda p: p > 80 and 255)
- tesstime = tess(temptime, config="-psm 6" )
- return tesstime
- def get_team():
- global row
- rowint = int(row.replace("row",""))
- if rowint <= 12:
- team = "Team A"
- elif rowint >= 12:
- team = "Team B"
- else:
- pass #Abort
- return team
- def get_match_outcome():
- global row
- rowint = int(row.replace("row",""))
- if rowint <= 12:
- outcome = "Victory"
- elif rowint >= 12:
- outcome = "Defeat"
- else:
- pass #Abort
- return outcome
- def get_lance():
- global row
- rowint = int(row.replace("row",""))
- if rowint in [1,2,3,4,13,14,15,16]:
- lance = "Alpha"
- elif rowint in [5,6,7,8,17,18,19,20]:
- lance = "Bravo"
- elif rowint in [9,10,11,12,21,22,23,24]:
- lance = "Charlie"
- return lance
- def get_faction():
- global resdata, row, temp
- x1 = int(resdata["Faction"]["x1"])
- x2 = int(resdata["Faction"]["x2"])
- rowheight = int(resdata["RowHeight"]["height"])
- y1 = int(resdata["Rows"][row])
- box = [x1, y1, x2, y1+rowheight]
- tessfac = tess(temp.crop(box), config="-psm 6" )
- trimfac = tessfac[1:-1]
- return trimfac
- def get_pilot_name():
- global resdata, row, temp
- x1 = int(resdata["Pilot"]["x1"])
- x2 = int(resdata["Pilot"]["x2"])
- rowheight = int(resdata["RowHeight"]["height"])
- y1 = int(resdata["Rows"][row])
- box = [x1, y1, x2, y1+rowheight]
- tessplayer = tess(temp.crop(box), config="-psm 6" )
- return tessplayer
- def get_mech():
- global resdata, temp, row, mechs, im
- x1 = int(resdata["Mech"]["x1"])
- x2 = int(resdata["Mech"]["x2"])
- y1 = int(resdata["Rows"][row])
- y2 = int(resdata["RowHeight"]["height"]) + y1
- box = [x1, y1, x2, y2]
- tessmech = tess(temp.crop(box), config="-psm 6" )
- fuzzmech = process.extractOne(tessmech, mechs)[0]
- distratio = process.extractOne(tessmech, mechs)[1]
- if distratio > 80:
- return fuzzmech
- else:
- level = 100
- tempim = im.crop(box)
- while level > 20:
- tempmech = ImageOps.invert(tempim).convert(mode="L").point(lambda p: p > level and 255)
- tessmech = tess(tempmech, config="-psm 6" )
- fuzzmech = process.extractOne(tessmech, modes)[0]
- distratio = process.extractOne(tessmech, modes)[1]
- level -= 5
- if distratio > 80:
- return fuzzmech
- else:
- return "Failed to identify: " + tessmech
- #Abort needed
- def get_status():
- global resdata, temp, row, im
- possible_statuses = ["Alive", "Dead", "Disconnected"]
- x1 = int(resdata["Status"]["x1"])
- x2 = int(resdata["Status"]["x2"])
- y1 = int(resdata["Rows"][row])
- y2 = int(resdata["RowHeight"]["height"]) + y1
- box = [x1, y1, x2, y2]
- tessstatus = tess(temp.crop(box), config="-psm 6" )
- fuzzstatus = process.extractOne(tessstatus, possible_statuses)[0]
- distratio = process.extractOne(tessstatus, possible_statuses)[1]
- if distratio > 80:
- return fuzzstatus
- else:
- level = 230
- tempim = im.crop(box)
- while level > 150:
- tempstatus = ImageOps.invert(tempim.convert(mode="L")).point(lambda p: p > level and 255)
- tessstatus = tess(tempstatus, config="-psm 6" )
- fuzzstatus = process.extractOne(tessstatus, possible_statuses)[0]
- distratio = process.extractOne(tessstatus, possible_statuses)[1]
- level -= 5
- if distratio > 80:
- return fuzzstatus
- else:
- return "Failed to identify: " + tessstatus
- #Abort needed
- def get_match_score():
- global resdata, row, temp
- x1 = int(resdata["Matchscore"]["x1"])
- x2 = int(resdata["Matchscore"]["x2"])
- rowheight = int(resdata["RowHeight"]["height"])
- y1 = int(resdata["Rows"][row])
- box = [x1, y1, x2, y1+rowheight]
- tessscore = tess(temp.crop(box), config="-psm 6" )
- return tessscore
- def get_kills():
- pass
- def get_assists():
- pass
- def get_damage():
- pass
- def get_ping():
- pass
- def get_row_value(section):
- global resdata, row, temp
- x1 = int(resdata[section]["x1"])
- x2 = int(resdata[section]["x2"])
- rowheight = int(resdata["RowHeight"]["height"])
- y1 = int(resdata["Rows"][row])
- box = [x1, y1, x2, y1+rowheight]
- tessvalue = tess(temp.crop(box), config="-psm 6" )
- return tessvalue
- def isteamstats(image):
- global resdata
- x1 = int(resdata["PilotLabel"]["x1"])
- x2 = int(resdata["PilotLabel"]["x2"])
- y1 = int(resdata["PilotLabel"]["y1"])
- y2 = int(resdata["PilotLabel"]["y2"])
- box = [x1, y1, x2, y2]
- filtercheck = ImageOps.invert(image.crop(box)).convert(mode="L").point(lambda p: p > 30 and 255)
- tessfilter = tess(filtercheck, config="-psm 6" )
- fuzzratio = fuzz.ratio(tessfilter, "PILOT NAME")
- if fuzzratio > 80:
- return True
- else:
- return False
- def pushdata():
- global mapname, mode, matchtime, team, outcome, matchid, lance, faction, pilot, mech, status, matchscore, kills, assists, damage, ping
- conn = psycopg2.connect(database="gamedata", user="pyuser", password="r4inbows!", host="www.objectivelyperfect.com", port="5432")
- cur = conn.cursor()
- cur.execute(
- """INSERT INTO ocrdata (matchid, processingtime, mapname, mode, matchtime, team, outcome, lance, faction, pilot, mech, status, matchscore, kills, assists, damage, ping) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);""",
- (matchid, time.time(), mapname, mode, matchtime, team, outcome, lance, faction, pilot, mech, status, matchscore, kills, assists, damage, ping))
- conn.commit()
- cur.close()
- conn.close()
- def create_table():
- conn = psycopg2.connect(database="gamedata", user="pyuser", password="r4inbows!", host="www.objectivelyperfect.com", port="5432")
- cur = conn.cursor()
- #cur.execute("DROP TABLE ocrdata;")
- cur.execute("CREATE TABLE ocrdata (matchid numeric, processingtime numeric, mapname varchar, mode varchar, matchtime varchar, team varchar, outcome varchar, lance varchar, faction varchar, pilot varchar, mech varchar, status varchar, matchscore int, kills int, assists int, damage int, ping int);")
- conn.commit()
- cur.close()
- conn.close()
- def test():
- global resdata, temp, mapnames, modes, mechs, im, row, matchid, mapname, mode, matchtime, team, outcome, lance, faction, pilot, mech, status, matchscore, kills, assists, damage, ping
- files = os.listdir('./../../Screenshots/Failoe/')
- (mapnames, modes, mechs) = collectinitialdata()
- start_time = time.time()
- for file in files:
- try:
- matchid = time.time()
- filepath = "./../../Screenshots/Failoe/"+file
- im = Image.open(filepath)
- resdata = getresinfo(im)
- if isteamstats(im):
- temp = preprocess(im)
- mapname = get_map()
- mode = get_mode()
- matchtime = get_match_time()
- for row in resdata["Rows"]:
- team = get_team()
- outcome = get_match_outcome()
- lance = get_lance()
- faction = get_faction()
- pilot = get_pilot_name()
- mech = get_mech()
- status = get_status()
- matchscore = get_row_value("Matchscore")
- kills = get_row_value("Kills")
- assists = get_row_value("Assists")
- damage = get_row_value("Damage")
- ping = get_row_value("Ping")
- #start test formatting
- while len(lance) <= 8:
- lance+=" "
- while len(faction) <= 7:
- faction+=" "
- while len(outcome) <=6:
- outcome+=" "
- while len(pilot) <= 16:
- pilot+=" "
- while len(mech) <= 12:
- mech+=" "
- while len(status) <= 13:
- status += " "
- while len(matchscore) <= 4:
- matchscore += " "
- while len(kills) <= 3:
- kills+=" "
- while len(assists) <= 3:
- assists+=" "
- while len(damage) <= 5:
- damage+=" "
- #end test formatting
- pushdata()
- print time.time(), mapname, mode, matchtime, team, outcome, lance, faction, pilot, mech, status, matchscore, kills, assists, damage, ping
- else:
- pass
- except:
- pass
- create_table()
- test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement