Guest User

Untitled

a guest
Mar 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. import subprocess
  2. import cv2
  3. import numpy as np
  4. import time
  5.  
  6. def getWindowID():
  7. #start = time.time()
  8. #get a list of all the windows currently running
  9. windows = subprocess.run("""for i in `xprop -root|grep "_NET_CLIENT_LIST_STACKING(WINDOW): window id" |tr '#' ','|tr ',' '\n'| grep 0x`;do xwininfo -id $i|grep "Window id" ;done""", stdout = subprocess.PIPE, shell=True)
  10. windowList = windows.stdout.decode("utf-8").strip().split("\n")
  11.  
  12. runeIDList = []
  13. for window in windowList:
  14. windowInfo = window.split(" ") #List should look like ["xwininfo:", "window", "id:", ID, WindowName]
  15. windowID = windowInfo[3]
  16. windowName = windowInfo[4]
  17. if '"RuneScape"' in windowName:
  18. runeIDList.append(windowID)
  19.  
  20. if len(runeIDList) < 1: #No runescape window found
  21. return False
  22.  
  23. #Due to Runescape on Linux not closing the splash screen, we need to eliminate the splash screen windowID
  24. #We do this by assuming the rs window will have a larger width than the splash screen
  25. #Impossible to resize the rs window to smaller than the splash screen so this shouldn't break
  26.  
  27. correctID = 0
  28. width = 0
  29.  
  30. for id in runeIDList:
  31. xwininfo= subprocess.run("xwininfo -metric -id "+id, stdout=subprocess.PIPE, shell=True)
  32. windowInfo = xwininfo.stdout.decode("utf-8")
  33. currWidth = windowInfo.partition("Width: ")[2].split(" ")[0]
  34. currWidth = int(currWidth)
  35.  
  36. if width < currWidth:
  37. width = currWidth
  38. correctID = id
  39.  
  40. #print("getWindowID: {:.3}s".format(time.time() - start))
  41. return correctID
  42.  
  43. def captureScreen(windowID):
  44. #start = time.time()
  45.  
  46. screenshot = subprocess.run("import -window "+windowID+" ppm:-", stdout=subprocess.PIPE, shell=True).stdout
  47. nparr = np.fromstring(screenshot, np.uint8)
  48. screenColour = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
  49. screenGray = cv2.cvtColor(screenColour, cv2.COLOR_BGR2GRAY)
  50.  
  51. #print("captureScreen: {:.3}s".format(time.time() - start))
  52. return screenColour, screenGray
  53.  
  54. def match(screenGray, template):
  55. start = time.time()
  56. w, h = template.shape[::-1]
  57.  
  58. res = cv2.matchTemplate(screenGray, template, cv2.TM_CCOEFF_NORMED)
  59. threshold = 0.65
  60. loc = np.where(res >= threshold)
  61. if len(loc[0]) < 1:
  62. #print("Template No Match: {:.3}s".format(time.time() - start))
  63. return []
  64. else:
  65. #print("Template Match: {:.3}s".format(time.time() - start))
  66. return res
  67.  
  68. def getMatchLoopState(screenGray):
  69. #start = time.time()
  70. loginTemplate = cv2.imread("loginTemplate.png", 0)
  71. lobbyTemplate = cv2.imread("lobbyTemplate.png", 0)
  72. statbarTemplate = cv2.imread("statbarTemplate.png", 0)
  73. stateTemplates = [loginTemplate, lobbyTemplate, statbarTemplate][::-1]
  74.  
  75. states = ["Login Screen", "Game Lobby", "Game Screen"][::-1]
  76. for i in range(len(states)):
  77. state = states[i]
  78. res = match(screenGray, stateTemplates[i])
  79. if len(res) > 0:
  80. #print("Get State: {:.3}s".format(time.time() - start))
  81. return state, res
  82. break
  83. else:
  84. pass
  85. return "Something went wrong", "-"
  86.  
  87.  
  88.  
  89. def writeMatchCrop(screenGray, res, w, h):
  90. #start = time.time()
  91.  
  92. coords = cv2.minMaxLoc(res)[-1]
  93. statsCrop = screenGray[coords[1]:coords[1]+h, coords[0]:coords[0]+w]
  94.  
  95. hpCrop = statsCrop[6:6+12, 35:35+83]
  96. prayCrop = statsCrop[6:6+12, 273:273+60]
  97.  
  98. factor = 3
  99.  
  100. hpRescale = cv2.resize(hpCrop, (0,0), fx=factor, fy=factor)
  101. prayRescale = cv2.resize(prayCrop, (0,0), fx=factor, fy=factor)
  102.  
  103. cv2.imwrite("hpText.png", hpRescale)
  104. cv2.imwrite("prayText.png", prayRescale)
  105.  
  106.  
  107.  
  108. #print("Write Match Crop: {:.3}s".format(time.time() - start))
  109.  
  110.  
  111. def readText(tempFileName):
  112. #start = time.time()
  113.  
  114. text = subprocess.run("tesseract "+tempFileName+".png stdout -l rune", stdout=subprocess.PIPE, shell=True).stdout
  115. text = text.decode("utf-8").strip()
  116. #print("Read "+tempFileName+": {:.3}s".format(time.time() - start))
  117. return text
  118.  
  119.  
  120.  
  121. def main():
  122. while True:
  123. start = time.time()
  124. rsWindowID = getWindowID()
  125. if not rsWindowID:
  126. print("No Runescape Window"+"\n---", file = open("gameState.txt", "w"))
  127. #pass
  128. else:
  129. screenColour, screenGray = captureScreen(rsWindowID)
  130.  
  131. state, res = getMatchLoopState(screenGray)
  132.  
  133. if state == "Game Screen":
  134. template = cv2.imread("statbarTemplate.png", 0)
  135. w, h = template.shape[::-1]
  136. writeMatchCrop(screenGray, res, w, h)
  137. hpText = readText("hpText")
  138. prayText = readText("prayText")
  139. print("HP: "+hpText+" Prayer: "+prayText+"\n---", file = open("gameState.txt", "w"))
  140. print("HP: "+hpText+" .Prayer: "+prayText)
  141. else:
  142. if state == "Something went wrong":
  143. print(state+"\n---", file = open("gameState.txt", "w"))
  144. #print(state)
  145. else:
  146. print(state+"\n---", file = open("gameState.txt", "w"))
  147. #print(state)
  148. print("\n--Loop completed in: {:.3}s".format(time.time() - start), file = open("gameState.txt", "a"))
  149. time.sleep(1.5)
  150. main()
Add Comment
Please, Sign In to add comment