-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpubg_health_server.py
165 lines (141 loc) · 6.03 KB
/
pubg_health_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# image processing / support stuff
import numpy as np
from PIL import ImageGrab
from PIL import Image
import cv2
from time import sleep
import os
import json
from collections import deque
# http server
from http.server import *
from threading import Thread
# health percentage global
health_percentage = 100
## config data
try:
with open('config.json') as config_file:
config_data = json.load(config_file)
except:
config_data = {
'SERVER_PORT': 6969,
'GAME_WIDTH': 1920,
'GAME_HEIGHT': 1080,
'CUSTOM_HTML': False
}
with open('config.json', 'w') as config_file:
json.dump(config_data, config_file)
print(config_data)
SERVER_PORT = config_data['SERVER_PORT']
GAME_WIDTH = config_data['GAME_WIDTH']
GAME_HEIGHT = config_data['GAME_HEIGHT']
CUSTOM_HTML = config_data['CUSTOM_HTML']
## support for pyinstaller
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
if(CUSTOM_HTML):
base_path = os.path.abspath(".")
else:
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
## http server
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if(self.path == "/health"): # health endpoint (that js will continuously request)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(bytes('{\"health\":' + str(health_percentage) + '}', 'utf-8'))
elif('images' in self.path): # serve gifs
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
image_name = self.path.split('images/', 1)[1]
image = open(resource_path('images/' + image_name), 'rb')
self.wfile.write(image.read())
image.close()
else: # serve main html
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
html = open(resource_path('index.html'), 'rb')
self.wfile.write(html.read())
def server_run():
server_address = ('', SERVER_PORT)
httpd = HTTPServer(server_address, RequestHandler)
httpd.serve_forever()
server_thread = Thread(target=server_run)
server_thread.setDaemon = True
server_thread.start()
## healthbar detection
frame_ring_buffer = deque([], 3)
while(True):
top_left = (GAME_WIDTH * 0.3974, GAME_HEIGHT * 0.95)
bottom_right = (GAME_WIDTH * 0.601, GAME_HEIGHT * 0.972)
healthbar_width = GAME_WIDTH * (0.601 - 0.3974)
# grab healthbar
screengrab = ImageGrab.grab((top_left[0], top_left[1], bottom_right[0], bottom_right[1]))
frame_ring_buffer.append(screengrab)
# maintain ring buffer of grabs and pick max values (to eliminate problems from flashing health bar)
frame1 = cv2.cvtColor(np.array(frame_ring_buffer[0]), cv2.COLOR_RGB2BGR)
if(len(frame_ring_buffer) == 3):
frame2 = cv2.cvtColor(np.array(frame_ring_buffer[1]), cv2.COLOR_RGB2BGR)
frame3 = cv2.cvtColor(np.array(frame_ring_buffer[2]), cv2.COLOR_RGB2BGR)
frame2 = cv2.max(frame1, frame2)
health_bar = cv2.max(frame2, frame3)
else:
health_bar = frame1
# mask out non red-white pixels and threshold health bar
health_bar = cv2.bilateralFilter(health_bar, 3, 80, 80)
mask1 = cv2.inRange(health_bar, np.array([0, 0, 170]), np.array([255, 255, 255]))
# get mask of 'red' by hsv conversion (bar is deep red when health is very low)
health_bar_hsv = cv2.cvtColor(health_bar, cv2.COLOR_BGR2HSV)
lower_red1 = np.array([170, 110, 85])
upper_red1 = np.array([180, 255, 255])
lower_red2 = np.array([0, 110, 85])
upper_red2 = np.array([10, 255, 255])
mask2 = cv2.inRange(health_bar_hsv, lower_red1, upper_red1)
mask3 = cv2.inRange(health_bar_hsv, lower_red2, upper_red2)
hsvBar = cv2.bitwise_or(mask2, mask3)
health_bar = cv2.bitwise_and(health_bar, health_bar, mask = mask1)
health_bar = cv2.cvtColor(health_bar, cv2.COLOR_BGR2GRAY)
ret,health_bar_thresh = cv2.threshold(health_bar,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# get health rectangle (contour with leftmost left boundary)
_, contoursRedWhite, _ = cv2.findContours(health_bar_thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
_, contoursRed, _ = cv2.findContours(hsvBar,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
lowMinX = minX = bottom_right[0] + 10
for contour in contoursRedWhite:
x,y,w,h = cv2.boundingRect(contour)
if(x < minX):
healthContour = contour
minX = x
for contour in contoursRed:
x,y,w,h = cv2.boundingRect(contour)
if(x < lowMinX):
lowHealthContour = contour
lowMinX = x
# try to use a contour from the 'red-white' image
redWhiteContourFound = False
try:
if (minX > 5): # in the event that we found a contour that doesn't touch the left edge, we ignore it
health_percentage = 100
else:
health_percentage = healthContour[3][0][0] / healthbar_width * 100
redWhiteContourFound = True
except:
pass
# otherwise use a contour from the 'deep red' image
if(not redWhiteContourFound):
try:
if (lowMinX > 5): # in the event that we found a contour that doesn't touch the left edge, we ignore it
health_percentage = 100
else:
health_percentage = lowHealthContour[3][0][0] / healthbar_width * 100
except:
pass
print('Detected health ' + str(health_percentage))
sleep(0.5)