daily pastebin goal
66%
SHARE
TWEET

DinoDetection

bananashavings Oct 12th, 2017 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import cv2
  3. import pyautogui
  4. import time
  5. from PIL import ImageGrab
  6.  
  7. # Defining Template Images
  8. gameOver = cv2.imread('GameOver.png')
  9. dino = cv2.imread('Dino.png')
  10. smallCactus = cv2.imread('SmallCactus.png')
  11. bigCactus = cv2.imread('BigCactus.png')
  12. ptero = cv2.imread('Ptero.png')
  13.  
  14. # Assigning Sample Image Dimensions
  15. h, w = dino.shape[:-1]
  16. sch, scw = smallCactus.shape[:-1]
  17. bch, bcw = bigCactus.shape[:-1]
  18. ph, pw = ptero.shape[:-1]
  19.  
  20. # Time Variables
  21. lastTime = time.time()
  22. runningTime = 0
  23.  
  24. # Key Variables
  25. keyDown = False
  26.  
  27. while True:
  28.     # Capturing Screen
  29.     # 'bbox' Is Rectangle Around The Game
  30.     screen = np.array(ImageGrab.grab(bbox=(150,125,800,300)))
  31.    
  32.     # Time stuff
  33.     #print('Loop took {} seconds'.format(time.time() - lastTime))
  34.     runningTime += time.time() - lastTime
  35.     lastTime = time.time()
  36.    
  37.     # Checking If Game Over
  38.     gameOverRes = cv2.matchTemplate(screen, gameOver, cv2.TM_CCOEFF_NORMED)
  39.     minValG, maxValG, minLocG, maxLocG = cv2.minMaxLoc(gameOverRes)
  40.  
  41.     if maxValG >= 0.9 and runningTime > 4:
  42.         print('Game Ended In ', int(round(runningTime)), ' Seconds')
  43.         pyautogui.press('space')
  44.         runningTime = 0
  45.  
  46.     # Finding Dinosaur
  47.     dinoRes = cv2.matchTemplate(screen, dino, cv2.TM_CCOEFF_NORMED)
  48.     minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(dinoRes)
  49.    
  50.     # Finding Small Cacti
  51.     smallCactusRes = cv2.matchTemplate(screen, smallCactus, cv2.TM_CCOEFF_NORMED)
  52.     smallCactusThreshhold = 0.725
  53.     smallCactusLoc = np.where(smallCactusRes >= smallCactusThreshhold)
  54.    
  55.     # Finding Big Cacti
  56.     bigCactusRes = cv2.matchTemplate(screen, bigCactus, cv2.TM_CCOEFF_NORMED)
  57.     bigCactusThreshhold = 0.725
  58.     bigCactusLoc = np.where(bigCactusRes >= bigCactusThreshhold)
  59.  
  60.     # Finding Pterodactyls
  61.     pteroRes = cv2.matchTemplate(screen, ptero, cv2.TM_CCOEFF_NORMED)
  62.     minValP, maxValP, minLocP, maxLocP = cv2.minMaxLoc(pteroRes)
  63.    
  64.     # Drawing Box Around Dinosaur
  65.     cv2.rectangle(screen, maxLoc, (maxLoc[0] + w, maxLoc[1] + h), (0, 255, 0), 2)
  66.        
  67.     # Avoiding Closest Small Cactus
  68.     if smallCactusLoc[0].size > 0:
  69.         leftmostXS = min(smallCactusLoc[1])
  70.         leftmostYS = min(smallCactusLoc[0])
  71.        
  72.         distS = (leftmostXS - maxLoc[0])
  73.        
  74.         if distS < 175 and distS > 0:
  75.             pyautogui.press('space')
  76.          
  77.         cv2.rectangle(screen, (leftmostXS, leftmostYS), (leftmostXS+scw, leftmostYS+sch), (255, 160, 0), 2)
  78.        
  79.     # Avoiding Closest Big Cactus
  80.     if bigCactusLoc[0].size > 0:
  81.         leftmostXB = min(bigCactusLoc[1])
  82.         leftmostYB = min(bigCactusLoc[0])
  83.        
  84.         distB = (leftmostXB - maxLoc[0])
  85.        
  86.         if distB < 175 and distB > 0:
  87.             pyautogui.press('space')
  88.        
  89.         cv2.rectangle(screen, (leftmostXB, leftmostYB), (leftmostXB+bcw, leftmostYB+bch), (255, 0, 0), 2)
  90.    
  91.     # Avoiding Pterodactyls
  92.     # Check 'maxValP' Because Otherwise Dino Gets Mistaken As Pterodactyl
  93.     # 'keyDown' Is Needed For Down Arrow, Otherwise It Doesn't Work Properly
  94.     if maxValP >= 0.60:
  95.  
  96.         distP = maxLocP[0] - maxLoc[0]
  97.         heightP = maxLoc[1] - maxLocP[1]
  98.  
  99.         if distP < 190 and distP > 0:
  100.             if heightP > 10:
  101.                 keyDown = True
  102.                 pyautogui.keyDown('down')
  103.             else:
  104.                 pyautogui.press('space')           
  105.        
  106.         cv2.rectangle(screen, maxLocP, (maxLocP[0] + pw, maxLocP[1] + ph), (0, 0, 255), 2)
  107.  
  108.     elif keyDown == True:
  109.         pyautogui.keyUp('down')
  110.         keyDown = False
  111.  
  112.     # Showing Image
  113.     cv2.imshow('Dino Game', cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))
  114.    
  115.     # Quit
  116.     if cv2.waitKey(1) & 0xFF == 27:
  117.         cv2.destroyAllWindows()
  118.         break
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top