Created
June 11, 2018 09:53
-
-
Save jackhuntcn/9f6091fc258787a488c0d2cdb25a534f to your computer and use it in GitHub Desktop.
[not finished] a script for playing http://sxiao.4399.com/4399swf/upload_swf/ftp25/csya/20180602/1/index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
from selenium import webdriver | |
from selenium.webdriver.common.action_chains import ActionChains | |
import base64 | |
import time | |
import numpy as np | |
import cv2 | |
driver = webdriver.Firefox(executable_path="./geckodriver") | |
url = "http://sxiao.4399.com/4399swf/upload_swf/ftp25/csya/20180602/1/index.html" | |
driver.get(url) | |
time.sleep(10) | |
canvas = driver.find_element_by_id('#canvas') | |
ITEMS = { | |
'empty': (0, 300), | |
'stick': (2800, 3200), | |
'rotten_stick': (3500, 3800), | |
'rock': (4100, 4400), | |
'turtle': (4700, 5300), | |
'crocodile': (5800, 6300) | |
} | |
# x_range = [1380, 1580, 1780, 1980] | |
x_range = [1380, 1580] | |
y_range = [800, 1000] | |
def get_screenshot(): | |
global driver | |
img_base64 = driver.get_screenshot_as_base64() | |
img_decode = base64.b64decode(img_base64) | |
img_array = np.fromstring(img_decode, np.uint8) | |
img = cv2.imdecode(img_array, cv2.COLOR_BGR2GRAY) | |
return img | |
def count_adaptive_pixels(img): | |
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
img_blur = cv2.medianBlur(img_gray, 5) | |
thresh = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) | |
rows, _ = np.where(thresh != 255) | |
return rows.shape[0] | |
def predict_item(nb_pixels): | |
if nb_pixels >= ITEMS['empty'][0] and nb_pixels < ITEMS['empty'][1]: | |
return 'empty' | |
elif nb_pixels >= ITEMS['stick'][0] and nb_pixels < ITEMS['stick'][1]: | |
return 'stick' | |
elif nb_pixels >= ITEMS['rotten_stick'][0] and nb_pixels < ITEMS['rotten_stick'][1]: | |
return 'rotten_stick' | |
elif nb_pixels >= ITEMS['rock'][0] and nb_pixels < ITEMS['rock'][1]: | |
return 'rock' | |
elif nb_pixels >= ITEMS['turtle'][0] and nb_pixels < ITEMS['turtle'][1]: | |
return 'turtle' | |
elif nb_pixels >= ITEMS['crocodile'][0] and nb_pixels < ITEMS['crocodile'][1]: | |
return 'crocodile' | |
else: | |
return 'unknown' | |
def predict_item_in_image(img): | |
pixels = count_adaptive_pixels(img[y_range[0]:y_range[1],x_range[0]:x_range[1]]) | |
return predict_item(pixels) | |
def jump(ty): | |
global driver | |
global canvas | |
if ty == 1: | |
ActionChains(driver).click(canvas).perform() | |
else: | |
ActionChains(driver).context_click(canvas).perform() | |
def take_action(item): | |
if item in ['empty', 'rotten_stick']: | |
print("jump 2 spaces") | |
jump(2) | |
else: | |
print("jump 1 space") | |
jump(1) | |
k = 0 | |
while k < 100: | |
try: | |
img = get_screenshot() | |
item = predict_item_in_image(img) | |
print(item) | |
take_action(item) | |
except: | |
pass | |
k += 1 | |
time.sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment