Created
May 6, 2021 15:49
-
-
Save panther03/b429cdec18454f2578c86588d35f7894 to your computer and use it in GitHub Desktop.
OpenCV dreadnautilus fishing script
This file contains 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
import time | |
import subprocess | |
from subprocess import Popen, PIPE | |
import cv2 | |
import scanf | |
import numpy as np | |
import pytesseract | |
from mss import mss | |
# Install maim, slop, and xdotool before using this script | |
# Frame delay | |
WAIT = 0.0333 | |
# Pick screen region (need maim/slop installed) | |
process = Popen(['slop'], stdout=PIPE, stderr=PIPE) | |
geom_str,_ = process.communicate() | |
geom = scanf.scanf("%dx%d+%d+%d",str(geom_str)[:-1]) | |
print(geom) | |
mon = {'top': geom[3], 'left': geom[2], 'width': geom[0], 'height': geom[1]} | |
def to_hsv(image): | |
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) | |
return hsv | |
# Other potentially useful min/max HSV ranges | |
# lower = np.array([0, 39, 0]) | |
# upper = np.array([179, 255, 255]) | |
# lower = np.array([54, 0, 144]) | |
# upper = np.array([128, 255, 255]) | |
def create_mask(hsv): | |
lower = np.array([168,0,159]) | |
upper = np.array([179,179,255]) | |
mask = cv2.inRange(hsv, lower, upper) | |
return mask | |
def imgOptimize(mask): | |
# Create horizontal kernel and dilate to connect text characters | |
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,3)) | |
dilate = cv2.dilate(mask, kernel, iterations=5) | |
# Find contours and filter using aspect ratio | |
# Remove non-text contours by filling in the contour | |
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | |
cnts = cnts[0] if len(cnts) == 2 else cnts[1] | |
for c in cnts: | |
x,y,w,h = cv2.boundingRect(c) | |
ar = w / float(h) | |
if ar < 5: | |
cv2.drawContours(dilate, [c], -1, (0,0,0), -1) | |
# Bitwise dilated image with mask, invert, then OCR | |
result = 255 - cv2.bitwise_and(dilate, mask) | |
return result | |
def textProcess(image): | |
data = pytesseract.image_to_string(image,lang='eng',config='--psm 6') | |
return data | |
cnt = 0 | |
active = True | |
# Remember to buff first and cast out line for the first time before launching the script. | |
with mss() as sct: | |
while True: | |
if cv2.waitKey(25) & 0xFF == ord('q'): | |
cv2.destroyAllWindows() | |
break | |
last_time = time.time() | |
mss_img = sct.grab(mon) | |
img = np.array(mss_img) | |
mask = create_mask(to_hsv(img)) | |
cv2.imshow('Screen View', mask) | |
text = textProcess(mask) | |
print(f"Text found: {text}") | |
# Other blood moon fishing enemies: | |
# or "blood" in text.lower() or "hemo" in text.lower() or "wander" in text.lower() | |
if ("dread" in text.lower() or "naut" in text.lower()): | |
# Reel in fishing line. Wait until text is empty again | |
print("DREADNAUTILUS DETECTED!!!") | |
if active: | |
subprocess.run(["xdotool","keydown","l","sleep","0.05","keyup","l"]) | |
print("Attempting to reel in..") | |
active = False | |
if (not active and text.strip() == ""): | |
# Text is empty. We can cast the line again | |
time.sleep(2) | |
subprocess.run(["xdotool","keydown","l","sleep","0.05","keyup","l"]) | |
print("Attempting to cast out..") | |
active = True | |
if (cnt == 200): | |
subprocess.run(["xdotool","keydown","b","sleep","0.05","keyup","b"]) | |
cnt = 0 | |
time.sleep(WAIT) | |
cnt += 1 |
This file contains 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
numpy | |
opencv-python | |
pytesseract | |
mss | |
scanf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment