
(这几天本来想分析一下战地6门户的协议,但是和朋友梳理了前端流程做了些测试后发现bug没想象中那么好利用,分析到头疼都没找到如何绕过后端检测的办法,于是转头又去折腾一下刷枪相关的东西)
战地6刷枪比较烦,不能刷一半看看具体等级多少,整了个实时OCR的脚本来判定等级是否达到要求,达到了就发出提示音。想到OCR就直接在mac上做了,调用mac系统的OCR,战地6在PC上运行那就开了个远程桌面(给人一种DMA从机的感觉)。
设定的识别区域和频率(20FPS),测试下来效果挺好的。

备份一下代码,AI糊弄的,写得很垃圾就不往git传了。
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
|
import time import Quartz import objc from AppKit import NSBitmapImageRep import Vision import os
def alert_sound(): os.system("afplay /System/Library/Sounds/Ping.aiff")
def capture_screen_region(x, y, width, height): """截取屏幕指定区域""" region = Quartz.CGRectMake(x, y, width, height) cg_image = Quartz.CGWindowListCreateImage( region, Quartz.kCGWindowListOptionOnScreenOnly, Quartz.kCGNullWindowID, Quartz.kCGWindowImageDefault ) if cg_image is None: raise RuntimeError("无法截屏") return cg_image
def perform_ocr(cg_image): """对 CGImage 执行中文 OCR""" results_list = []
def callback(request, error): if error: print("OCR Error:", error) return observations = request.results() for obs in observations: candidates = obs.topCandidates_(1) if candidates: results_list.append(candidates[0].string())
VNRecognizeTextRequest = objc.lookUpClass('VNRecognizeTextRequest') request = VNRecognizeTextRequest.alloc().initWithCompletionHandler_(callback) request.setRecognitionLanguages_(["zh-Hans"]) request.setRecognitionLevel_(Vision.VNRequestTextRecognitionLevelAccurate)
VNImageRequestHandler = objc.lookUpClass('VNImageRequestHandler') handler = VNImageRequestHandler.alloc().initWithCGImage_options_(cg_image, None) handler.performRequests_error_([request], None)
return results_list
if __name__ == "__main__": import sys
if len(sys.argv) < 5: print("用法: python ocr_screen_live.py x y width height [interval_seconds]") sys.exit(1)
x, y, w, h = map(int, sys.argv[1:5]) interval = float(sys.argv[5]) if len(sys.argv) > 5 else 0.5
print(f"实时 OCR 启动,区域: ({x},{y},{w},{h}),刷新间隔: {interval}s") try: while True: cg_image = capture_screen_region(x, y, w, h) texts = perform_ocr(cg_image) if texts: print("识别结果:") for t in texts: print(t) print("-" * 30) if "势不可挡" in texts: print("检测到关键字 '势不可挡'!") alert_sound() time.sleep(interval) except KeyboardInterrupt: print("已停止实时 OCR")
|