Advertisement
Guest User

Untitled

a guest
May 28th, 2017
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.46 KB | None | 0 0
  1. import numpy as np
  2. from PIL import ImageGrab
  3. import cv2
  4. import time
  5. from matplotlib import pyplot as plt
  6. import win32gui
  7.  
  8. winlist = []
  9. def dummyfun(x):
  10.     return
  11.  
  12. def createControls():
  13.     #return
  14.     cv2.namedWindow("Control",cv2.WINDOW_NORMAL)
  15.  
  16.     iErode = 2
  17.     iDilate = 3
  18.     iDP = 2
  19.     iMDist = 23
  20.     iParam1 = 100
  21.     iParam2 = 30
  22.     iMinRadius = 5
  23.     iMaxRadius = 11
  24.  
  25.  
  26.     cv2.createTrackbar('dp', 'Control', iDP, 2,dummyfun)
  27.     cv2.createTrackbar('mDist', 'Control', iMDist, 300, dummyfun)
  28.     # cv2.createTrackbar('param1','Control', iParam1, 300, dummyfun)
  29.     # cv2.createTrackbar('param2','Control', iParam2, 300, dummyfun)
  30.     # cv2.createTrackbar('minRadius','Control', iMinRadius, 300, dummyfun)
  31.     # cv2.createTrackbar('maxRadius','Control', iMaxRadius, 300, dummyfun)
  32.  
  33. def roi(img, vertices):
  34.     #blank mask:
  35.     mask = np.zeros_like(img)
  36.     # fill the mask
  37.     cv2.fillPoly(mask, vertices, 255)
  38.     # now only show the area that is the mask
  39.     masked = cv2.bitwise_and(img, mask)
  40.     return masked
  41.  
  42. def process_img(original_image, low_threshold, high_threshold,vertices):#,bbox):
  43.     img_hsv = cv2.cvtColor(original_image, cv2.COLOR_BGR2HSV)
  44.  
  45.  
  46.     img_thresholded = cv2.inRange(img_hsv,low_threshold,high_threshold)
  47.     erode_x = cv2.getTrackbarPos('erode', 'Control')
  48.     erode_y = erode_x
  49.     dilate_x = cv2.getTrackbarPos('dilate', 'Control')
  50.     dilate_y = dilate_x
  51.     ekernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(erode_x,erode_y))
  52.     dkernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (dilate_x,dilate_y))
  53.     #cv2.morphologyEx(img_thresholded,cv2.MORPH_OPEN,kernel,img_thresholded)
  54.     cv2.erode(img_thresholded,ekernel,img_thresholded,iterations = 1)
  55.     cv2.dilate(img_thresholded,dkernel,img_thresholded,iterations = 1)
  56.  
  57.     processed_img = roi(img_thresholded, [vertices])
  58.  
  59.     return processed_img
  60.  
  61. def findPlayer(screen,bbox):
  62.     low_threshold = np.array([0, 0, 235])
  63.     high_threshold = np.array([255, 20, 255])
  64.  
  65.     new_img_rgba = process_img(screen,low_threshold,high_threshold,bbox)
  66.  
  67.     oMoments = cv2.moments(new_img_rgba)
  68.  
  69.     dM01 = oMoments['m01']
  70.     dM10 = oMoments['m10']
  71.     dArea = oMoments['m00']
  72.     posX=0
  73.     posY=0
  74.     if dArea > 10000:
  75.         posX = int(dM10/dArea)
  76.         posY = int(dM01/dArea)
  77.     pt1 = (posX-20,posY-35)
  78.     pt2 = (posX+20,posY+35)
  79.     cv2.rectangle(screen,pt1,pt2,(0,255,0),2)
  80.     return new_img_rgba
  81.  
  82. def findEnemy(screen,bbox):
  83.     low_threshold = np.array([0, 0, 78])
  84.     high_threshold = np.array([0, 0, 153])
  85.  
  86.     vertices = np.array([[0,151],[0,110],[bbox[2],110],[bbox[2],151]], np.int32)
  87.  
  88.     new_img_rgba = process_img(screen,low_threshold,high_threshold,vertices)
  89.     new_img_rgba = roi(new_img_rgba,[vertices])
  90.     new_img_rgba = cv2.GaussianBlur(new_img_rgba,(9,9), 2,2)
  91.  
  92.     dp = cv2.getTrackbarPos('dp','Control')
  93.     mDist = cv2.getTrackbarPos('mDist','Control')
  94.     circles = cv2.HoughCircles(new_img_rgba,cv2.HOUGH_GRADIENT,dp,mDist, param1=100, param2=30, minRadius=5, maxRadius=11)
  95.     if circles != None:
  96.         circles = np.uint16(np.around(circles))
  97.         for i in circles[0, :]:
  98.             cv2.circle(screen, (i[0], i[1]), i[2], (0, 255, 0), 1)  # draw the outer circle
  99.             cv2.circle(screen, (i[0], i[1]), 2, (0, 0, 255), 3)  # draw the center of the circle
  100.  
  101.     return new_img_rgba
  102.  
  103. def enum_cb(hwnd,results):
  104.     winlist.append((hwnd, win32gui.GetWindowText(hwnd)))
  105.  
  106.  
  107. def getGameScreenLocation(windowName):
  108.     toplist = []
  109.     win32gui.EnumWindows(enum_cb, toplist)
  110.  
  111.     game = [(hwnd, title) for hwnd, title in winlist if windowName in title.lower()]
  112.     game = game[0]
  113.     hwnd = game[0]
  114.  
  115.     win32gui.SetForegroundWindow(hwnd)
  116.     win32gui.MoveWindow(hwnd,0,0,800,470,True)
  117.     return win32gui.GetWindowRect(hwnd)
  118.  
  119. def main():
  120.     createControls()
  121.  
  122.     while (True):
  123.         bbox = getGameScreenLocation('one finger death punch')
  124.         bbox = (bbox[0] + 8, bbox[1] + 37, bbox[2]-int(bbox[2]/58), bbox[3]-int(bbox[3]/22))
  125.         printscreen = np.array(ImageGrab.grab(bbox))
  126.  
  127.        # new_printscreen = findPlayer(printscreen,bbox)
  128.         new_printscreen2 = findEnemy(printscreen,bbox)
  129.  
  130.  
  131.         #cv2.imshow('Player Threshold', new_printscreen)
  132.         cv2.imshow('Enemy Threshold', new_printscreen2)
  133.  
  134.         cv2.imshow('window',cv2.cvtColor(printscreen, cv2.COLOR_BGR2RGB))
  135.  
  136.         if cv2.waitKey(25) & 0xFF == ord('q'):
  137.             cv2.destroyAllWindows()
  138.             break
  139.  
  140. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement