- selenium 實作模擬登陸
- PIL 進行頁面截圖
- Opencv實作影像識別,SIFT算子提取區域特征
影響 OpenCV 匹配率的因素
- 背景
背景被人物遮擋的多少直接影響到匹配率,裁剪背景后明顯提升了匹配率,解決辦法,去背景, - 像素
每個人手機不同,提供的搭配截影像素不一樣,有的還發原圖,有的不發,像素越多,匹配率越低, - 人物的輕微運動,擺擺手、裙子之類的,影響最大,動作完全相同的時候,匹配率可大100%,不一樣的時候,約50%,
獲取模板
- 用 webdriver 打開網頁
driver=webdriver.Chrome()
driver.get('http://gamexd.fire2333.com/home/ac?action=/home/game/a/5100013/g/200125/pt/5100074')
- 在登錄賬號之前, F12啟用開發者模式,先關閉快取

- 保持開發者模式開啟,登錄賬號,進入時裝周,在 Network-Img里 找到兩個圖片 bg_3002.jpg 和 clothmatch0.png,右鍵點擊禁用,

- 重繪后在進入時裝周,這個時候應該所有背景,包括那幾個贊的文字也沒有了,然后可以把快取打開,避免切號的時候太慢,刷片的時候F12開發者模式要一直打開,搭配模板可以用盟友發的,如果為了匹配率過得去,建議親自去截圖,截少不截多,寧愿少截點,千萬別把別人帶進去,

- 給自己人和不想讓其進前20的截圖拿到手,在當前目錄建2個檔案夾,分別放入其中,
import os
import cv2
import PIL
import time
import random
from selenium import webdriver
from pymouse import PyMouse
m=PyMouse()
##為了使用 SIFT 算子,opencv 需要安裝以下版本
#pip install opencv-python =3.4.2.16
#pip install opencv-contrib-python=3.4.2.16
class Vote(object):
"""start voting of the game"""
def __init__(self):
self.username = current_account
self.password = "thisispassword"
def login(self):
js = 'document.getElementById("menu-login").click()'
driver.execute_script(js)
time.sleep(1)
js = 'document.getElementsByClassName("changeAccount")[0].click();'
driver.execute_script(js)
time.sleep(1)
driver.find_element_by_class_name("phone").send_keys(self.username)
driver.find_element_by_class_name("password").send_keys(self.passsword)
time.sleep(1)
js = 'document.getElementsByClassName("enter")[0].click();'
driver.execute_script(js)
time.sleep(5)
m.click(673, 252) # 關閉公告
time.sleep(3)
m.click(489, 649) # 進入游戲 (775,618) (883,702)
time.sleep(8)
m.click(670,382) # 進入時裝周
time.sleep(3)
m.click(627,834) # 我當評委
time .sleep(3)
#由于開發者模式的影響,這里的登錄坐標和請安膜拜的不一樣,要改,
def vote(self):
for i in range(1,21):
img_current=ImageGrab.grab((350,310,870,980))
len_match_friend,location_match_friend=computeMatchMatrix(img_current,friend_list)
match_rate_friend = [a / b for a, b in zip(len_match_friend, len_features_friend)]
len_match_block_list,location_match_block_list=computeMatchMatrix(img_current,block_list)
match_rate_block_list= [a / b for a, b in zip(len_match_block_list, len_features_block_list)]
# match_rate 大于50認為圖中有自己人或者很討厭的人(BlockList里面的)
#先判別有自己的人就獲取她坐標,計算匹配的點的x坐標平均值,小于260就認為她在左邊
#260的來源:截圖的x范圍250-870,總共520像素,一半就是260,
#如果判斷沒有自己人,再判斷有沒有BlockList里面的人,有的話不投他,如果兩種都沒有,用random隨機投一個,
if max(match_rate_friend) > 0.5:
id=match_rate_friend.index(max(match_rate_friend,key=abs))
if location_match_friend[id] < 260:
m.click(397,477)
else:
m.click(606,474)
else:
if max(match_rate_block_list) >0.5:
id=match_rate_block_list.index(max(match_rate_block_list,key=abs))
if location_match_block_list[id] > 260:
m.click(397, 477)
else:
m.click(606, 474)
else:
if random.choice([0,1])
m.click(397, 477)
else:
m.click(606, 474)
time.sleep(5)
# readTemp 是要來讀取模板的,包括自己人的,和 BlockList的
def readTemp(path):
file_list=os.listdir(path)
len_features=[]
for i in file_list:
prefix = i.replace('.png', '')
globals()['img_' + prefix] = cv2.imread('NO18/' + i)
temp_var = globals()['img_' + prefix]
globals()['kp_' + prefix], globals()['des_' + prefix] = sift.detectAndCompute(temp_var, None)
len_features.append(len(globals()['kp_' + prefix]))
del globals()['img_'+prefix]
return file_list,len_features
def computeMatchMatrix(img_test,temp_list):
kp_test,des_test=sift.detectAndCompute(img_test,None)
len_match=[]
location_match=[]
for i in temp_list:
prefix = i.replace('.png', '')
temp_kp=globals()['kp_' + prefix]
temp_des=globals()['des_' + prefix]
matches=bf.knnMatch(temp_des,des_test,k=2)
good_features=[]
good_match_pt=[]
for i, (m1, m2) in enumerate(matches):
if m1.distance < 0.75 * m2.distance:
good_features.append([m1])
pt = kp_test[m2.trainIdx].pt
good_match_pt.append(pt[0])
x_averg = np.mean(good_match_pt)
len_match.append(len(good_features))
location_match.append(x_averg)
return len_match,location_match
sift=cv2.xfeatures2d.SIFT_create()
bf=cv2.BFMatcher()
friend_list,len_features_friend=readTemp('NO18/')
block_list,len_features_block_list=readTemp('BlockList/')
for i in range(1,100):
print(time.strftime('%Y-%m-%d %H:%M:%S')+' usr00%d'%i+' start vote')
current_account='usr00%d'%i
current_round=Vote()
current_round.login()
current_round.vote()
print(time.strftime('%Y-%m-%d %H:%M:%S')+' usr00%d'%i+' done!')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/77132.html
標籤:其他
上一篇:js完美拖拽與碰撞檢測
下一篇:Unity一鍵生成MVC框架
