Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from PIL import ImageGrab
- import cv2
- import time
- from matplotlib import pyplot as plt
- import win32gui
- winlist = []
- def dummyfun(x):
- return
- def createControls():
- #return
- cv2.namedWindow("Control",cv2.WINDOW_NORMAL)
- iErode = 2
- iDilate = 3
- iDP = 2
- iMDist = 23
- iParam1 = 100
- iParam2 = 30
- iMinRadius = 5
- iMaxRadius = 11
- cv2.createTrackbar('dp', 'Control', iDP, 2,dummyfun)
- cv2.createTrackbar('mDist', 'Control', iMDist, 300, dummyfun)
- # cv2.createTrackbar('param1','Control', iParam1, 300, dummyfun)
- # cv2.createTrackbar('param2','Control', iParam2, 300, dummyfun)
- # cv2.createTrackbar('minRadius','Control', iMinRadius, 300, dummyfun)
- # cv2.createTrackbar('maxRadius','Control', iMaxRadius, 300, dummyfun)
- def roi(img, vertices):
- #blank mask:
- mask = np.zeros_like(img)
- # fill the mask
- cv2.fillPoly(mask, vertices, 255)
- # now only show the area that is the mask
- masked = cv2.bitwise_and(img, mask)
- return masked
- def process_img(original_image, low_threshold, high_threshold,vertices):#,bbox):
- img_hsv = cv2.cvtColor(original_image, cv2.COLOR_BGR2HSV)
- img_thresholded = cv2.inRange(img_hsv,low_threshold,high_threshold)
- erode_x = cv2.getTrackbarPos('erode', 'Control')
- erode_y = erode_x
- dilate_x = cv2.getTrackbarPos('dilate', 'Control')
- dilate_y = dilate_x
- ekernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(erode_x,erode_y))
- dkernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (dilate_x,dilate_y))
- #cv2.morphologyEx(img_thresholded,cv2.MORPH_OPEN,kernel,img_thresholded)
- cv2.erode(img_thresholded,ekernel,img_thresholded,iterations = 1)
- cv2.dilate(img_thresholded,dkernel,img_thresholded,iterations = 1)
- processed_img = roi(img_thresholded, [vertices])
- return processed_img
- def findPlayer(screen,bbox):
- low_threshold = np.array([0, 0, 235])
- high_threshold = np.array([255, 20, 255])
- new_img_rgba = process_img(screen,low_threshold,high_threshold,bbox)
- oMoments = cv2.moments(new_img_rgba)
- dM01 = oMoments['m01']
- dM10 = oMoments['m10']
- dArea = oMoments['m00']
- posX=0
- posY=0
- if dArea > 10000:
- posX = int(dM10/dArea)
- posY = int(dM01/dArea)
- pt1 = (posX-20,posY-35)
- pt2 = (posX+20,posY+35)
- cv2.rectangle(screen,pt1,pt2,(0,255,0),2)
- return new_img_rgba
- def findEnemy(screen,bbox):
- low_threshold = np.array([0, 0, 78])
- high_threshold = np.array([0, 0, 153])
- vertices = np.array([[0,151],[0,110],[bbox[2],110],[bbox[2],151]], np.int32)
- new_img_rgba = process_img(screen,low_threshold,high_threshold,vertices)
- new_img_rgba = roi(new_img_rgba,[vertices])
- new_img_rgba = cv2.GaussianBlur(new_img_rgba,(9,9), 2,2)
- dp = cv2.getTrackbarPos('dp','Control')
- mDist = cv2.getTrackbarPos('mDist','Control')
- circles = cv2.HoughCircles(new_img_rgba,cv2.HOUGH_GRADIENT,dp,mDist, param1=100, param2=30, minRadius=5, maxRadius=11)
- if circles != None:
- circles = np.uint16(np.around(circles))
- for i in circles[0, :]:
- cv2.circle(screen, (i[0], i[1]), i[2], (0, 255, 0), 1) # draw the outer circle
- cv2.circle(screen, (i[0], i[1]), 2, (0, 0, 255), 3) # draw the center of the circle
- return new_img_rgba
- def enum_cb(hwnd,results):
- winlist.append((hwnd, win32gui.GetWindowText(hwnd)))
- def getGameScreenLocation(windowName):
- toplist = []
- win32gui.EnumWindows(enum_cb, toplist)
- game = [(hwnd, title) for hwnd, title in winlist if windowName in title.lower()]
- game = game[0]
- hwnd = game[0]
- win32gui.SetForegroundWindow(hwnd)
- win32gui.MoveWindow(hwnd,0,0,800,470,True)
- return win32gui.GetWindowRect(hwnd)
- def main():
- createControls()
- while (True):
- bbox = getGameScreenLocation('one finger death punch')
- bbox = (bbox[0] + 8, bbox[1] + 37, bbox[2]-int(bbox[2]/58), bbox[3]-int(bbox[3]/22))
- printscreen = np.array(ImageGrab.grab(bbox))
- # new_printscreen = findPlayer(printscreen,bbox)
- new_printscreen2 = findEnemy(printscreen,bbox)
- #cv2.imshow('Player Threshold', new_printscreen)
- cv2.imshow('Enemy Threshold', new_printscreen2)
- cv2.imshow('window',cv2.cvtColor(printscreen, cv2.COLOR_BGR2RGB))
- if cv2.waitKey(25) & 0xFF == ord('q'):
- cv2.destroyAllWindows()
- break
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement