def main(): sct = mss() monitor = sct.monitors[-1] Done = False wd = Path(os.getcwd()) mp3 = str(wd / "your-turn.wav") Player_Name = get_player_name_template() templates = get_templates() while True: sct_img = sct.grab(monitor) gray_img = cv2.cvtColor(np.array(sct_img), cv2.COLOR_BGRA2GRAY) if search_img(gray_img, Player_Name) and not Done: winsound.PlaySound(mp3, winsound.SND_ASYNC) Done = True if Done: sleep(7) for ss in templates: if act(gray_img, *ss): Done = False sleep(1.5) continue sleep(1.5)
def screen_record(): sct = mss() last_time = time.time() while (True): img = sct.grab(mon) print('loop took {} seconds'.format(time.time() - last_time)) last_time = time.time() img = np.array(img) processed_image = process_image(img) # Height , Width # roi = processed_image[235:270,180:250] # # mean = np.mean(roi) mean = np.mean(processed_image) print('mean = ', mean) # if not mean == float(0): # pg.press('space') gizmo_image = img cv2.rectangle(gizmo_image, (mon['left'], mon['top']), (mon['left'] + mon['width'], mon['top'] + mon['height']), (255, 255, 255), 20) cv2.imshow('Edges', gizmo_image) #cv2.imshow('ROI', processed_image) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break
def screenshot(coords=None): """returns PIL image of whole screen :param coords (x1, y1, x2, y2) """ if coords is None: with mss() as sct: filename = sct.shot(mon=-1) im = Image.open(filename) im = im.convert("RGBA") return im else: return ImageGrab.grab(bbox=(coords))
def drive(self, device): mon = {"top": 32, "left": 0, "width": 1024, "height": 768} sct = mss() hidden = None while True: start = time.time() if keyboard.is_pressed('q'): exit() if keyboard.is_pressed('i') and not self.driving: self.driving = True print('Started driving...') if keyboard.is_pressed('o') and self.driving: self.driving = False self.ctlr.set_axis(axis[0], 0x4000) self.ctlr.set_axis(axis[1], 0x0000) self.ctlr.set_axis(axis[2], 0x0000) hidden = None print('You have arrived!') if keyboard.is_pressed('`'): time.sleep(0.5) self.cheat_code('RAPIDGT\n') if self.driving: img = np.asarray(sct.grab(mon)) img = cv2.resize(img, (400, 300)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = self.transform(img) img = img.unsqueeze(0).unsqueeze(0).to(device) if hidden is None: out, hidden = self.model(img) else: out, hidden = self.model(img, hidden) #print(out) out = out.squeeze(0).squeeze(0) #print(out) out = (out + 1) * torch.Tensor([ float(0x4000), float(0x4000), float(0x4000) ]).to(device) for i, ax in enumerate(axis): self.ctlr.set_axis(ax, int(out[i].item())) proc_time = time.time() - start wait_time = 1 / 22 - proc_time time.sleep(wait_time if wait_time > 0 else 0) print(1 / (time.time() - start))
def screen_record(): sct = mss() last_time = time.time() while (True): img = sct.grab(monitor) print('loop took {} seconds'.format(time.time() - last_time)) last_time = time.time() img = np.array(img) processed_image = process_image(img) mean = np.mean(processed_image) print('mean = ', mean) if not mean == float(0): pg.press('space') if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break
def get_screen( top=300, left=600, width=700, height=200, continuousRun=False, queue=None, delay=5, ): """Get np.array of the screen area specified. Args: top (int, optional): y position, upper left hand corner. Measured from top left of screen. Defaults to 300. left (int, optional): x position, upper left hand corner. Measured from top left of screen. Defaults to 600. width (int, optional): x offset from 'left' to capture. Width of image. Defaults to 700. height (int, optional): y offset from 'top' to capture. Height of image. Defaults to 200. OTHER PARAMS NOT DESCRIBED ATM Returns: np.array: np.arry of pixels on screen. Captured using mss() library """ with mss() as sct: monitor = { "top": top, "left": left, "width": width, "height": height } if delay: time.sleep(delay) # open game window in this time frame while continuousRun: img = np.array(sct.grab(monitor)) # queue.put(img) #implement later else: img = np.array(sct.grab(monitor)) return img raise EOFError("should never have gotten here")
def __init__(self, modules=["stats", "logging"], scoreboardKey=None): mapNames = ["BQK", "Helix", "Nest", "Pod", "Spire", "Split", "Tally"] self.maps = [] for name in mapNames: self.maps.append(Map(name)) self.mon = { "mon": 0, "top": 0, "left": 0, "width": 1920, "height": 1080 } self.sct = mss() self.state = None self.currSet = None self.stats = "stats" in modules self.logging = "logging" in modules self.textFiles = "streamer" in modules self.scoreboard = "scoreboard" in modules self.scoreboardKey = scoreboardKey self.exit = False
def load_region_image(): screen_cap = None monitor = get_monitors()[0] print('Monitor:', monitor) capture_area = { # "left": int(0.11718 * monitor.width + 3720), "left": int(0.11718 * monitor.width), # "top": int(0.9166 * monitor.height), "top": 0, "width": int(0.05208 * monitor.width), "height": int(0.09259 * monitor.height), } global region_size region_size = (int)(0.05208 * monitor.width) * (0.09259 * monitor.height) with mss() as cap: screen_cap = cap.grab(capture_area) img_data = np.array(Image.frombytes('RGB', screen_cap.size, screen_cap.rgb)) bgr_img_data = cv.cvtColor(img_data, cv.COLOR_RGB2BGR) show_image(bgr_img_data) return bgr_img_data
def drive(self, device): self.model.eval() mon = {"top": 32, "left": 0, "width": 1024, "height": 768} sct = mss() self.ctlr.set_axis(axis[0], 0x4000) self.ctlr.set_axis(axis[1], 0x0000) self.ctlr.set_axis(axis[2], 0x0000) while True: start = time.time() if keyboard.is_pressed('q'): exit() if keyboard.is_pressed('i') and not self.driving: self.driving = True print('Started driving...') if keyboard.is_pressed('o') and self.driving: self.driving = False self.ctlr.set_axis(axis[0], 0x4000) self.ctlr.set_axis(axis[1], 0x0000) self.ctlr.set_axis(axis[2], 0x0000) print('You have arrived!') if keyboard.is_pressed('`'): self.cheat_code('RAPIDGT\n') if self.driving: img = np.asarray(sct.grab(mon)) img = cv2.resize(img, (400, 300)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = self.transform(img) out = self.model(img.view(1, *img.shape).to(device))[0] print(out) out = (out + 1) * 0x4000 for i, ax in enumerate(axis): self.ctlr.set_axis(ax, int(out[i].item()))
def extract(self): """Actually run the extract process.""" scsh = mss() # For each available items for item_id in self.item_ids: # Change the current player's weapon (by writing its ID in the game's memory) self._set_current_weapon(item_id) # Show the player's profile by sending key 7 (required in order to be taken into account by the game) self._toggle_profile() # Take screenshot of the weapon slot weapon_slot = scsh.grab(self.weapon_slot_pos) weapon_slot_img = Image.frombytes('RGB', weapon_slot.size, weapon_slot.rgb) weapon_slot_img_with_alpha = Image.new('RGBA', weapon_slot.size) weapon_slot_img_with_alpha.paste(weapon_slot_img) pixdata = weapon_slot_img_with_alpha.load() width, height = weapon_slot_img_with_alpha.size # Make the grey background transparent for y in range(height): for x in range(width): if pixdata[x, y] == self.item_background_color: pixdata[x, y] = self.item_background_color_replace # Save the image with the item ID as its name weapon_slot_img_with_alpha.save(os.path.join( self.output_dir, item_id + '.png'), optimize=True) # Hide the player's profile by sending key 7 (required in order to be taken into account by the game) self._toggle_profile()
def shot(): while True: sct = mss() for filename in sct.save( output='C:\\Users\\Public\\Libraries\\monitor-1.png'): sleep(15) msg = MIMEMultipart() msg['From'] = em passwd = pas msg['To'] = t attachment = open(filename, "rb") p = MIMEBase('application', 'octet-stream') p.set_payload((attachment).read()) encoders.encode_base64(p) p.add_header('Content-Disposition', "attachment; filename= monitor-1.png") msg.attach(p) attachment.close() s = smtplib.SMTP('smtp.gmail.com', 587) s.starttls() s.login(msg['From'], passwd) text = msg.as_string() s.sendmail(msg['From'], msg['To'], text) s.close()
currMap = "None" for map in maps: hits = np.sum(map['array'] == img) if (hits > currConfidence): currConfidence = hits currMap = map['name'] currConfidence = 100 * currConfidence / img.size if (currConfidence > 60): return (currMap, currConfidence) else: return ("idk", 0) if __name__ == '__main__': mon = {"top": 0, "left": 0, "width": 1920, "height": 1080} sct = mss() input("paused") last_time = time.time() while time.time() - last_time < 5: 1 + 1 print("Recording!") while (True): sct_img = sct.grab(mon) img = Image.new("RGB", sct_img.size) pixels = zip(sct_img.raw[2::4], sct_img.raw[1::4], sct_img.raw[0::4]) img.putdata(list(pixels)) imgArray = np.asarray(img) actualMap = findMap(imgArray) if (not actualMap[0] == "idk"): print("You are on " + actualMap[0] + " with confidence " + str(actualMap[1]) + "%")
if m: amt = m.group(1) try: amt = int(amt) except: continue if amt > 0: ret[x] = amt return ret if not run_continuously: check() else: if useOCR: with mss() as sct: while True: w = GetForegroundWindow() if GetWindowText(w) == 'Elite - Dangerous (CLIENT)': image = grab(sct, GetWindowRect(w)) text = OCR(image) pois = getPOIs(text) print(pois) if pois: check(pois=pois) time.sleep(polling_interval) else: check(True) while True: time.sleep(polling_interval) check()
def screen_record(self, debug=False): pygame.init() # Initialize controller ctlr = pygame.joystick.Joystick(1) ctlr.init() # mss init bs mon = {"top": 32, "left": 0, "width": 1024, "height": 768} title = "Screen Capture" sct = mss() # Keep track of inputs turn = 0.0 gas = -1.0 brake = -1.0 num_straight = 10 while True: start = time.time() # Get frame img = np.asarray(sct.grab(mon)) pygame.event.pump() turn = ctlr.get_axis(0) gas = ctlr.get_axis(4) brake = ctlr.get_axis(5) if keyboard.is_pressed('q') and not self.recording: exit() if keyboard.is_pressed('i') and not self.recording: self.recording = True print('Started recording...') if keyboard.is_pressed('o') and self.recording: self.recording = False print('Saving recording...') self.flush_buffer() num_straight = 10 print('Saved.') if keyboard.is_pressed('k') and self.recording: self.recording = False self.frames = [] print('Recording discarded.') if keyboard.is_pressed('~'): self.cheat_code('RAPIDGT') # # Data balancer: Only save interesting frames # if -0.05 < gas < 0.85 or (turn > 0.2 or turn < -0.2) or (brake > -0.4): # self.save_to_buffer(img, np.array([turn, gas, brake], dtype=np.float16)) # num_straight += 1 # # and sometimes save straight frames # elif num_straight > 0: # self.save_to_buffer(img, np.array([turn, gas, brake], dtype=np.float16)) # num_straight -= 3 self.save_to_buffer(img, np.array([turn, gas, brake], dtype=np.float16)) # cv2 bs self.put_turn_text(img, turn) self.put_gas_text(img, gas) self.put_brake_text(img, brake) cv2.imshow(title, img) proc_time = time.time() - start wait_time = int(1000 / (self.fraps) - proc_time) if cv2.waitKey(wait_time if wait_time > 0 else 1) & 0xFF == ord( "q"): cv2.destroyAllWindows() break #print(1/(time.time() - start)) sct.close()
def screen_record(pos = {"top": 100, "left": 1025, "width": 800, "height": 640}): # 800x600 windowed mode pos = pos sct = mss() return np.asarray(sct.grab(pos))
import datetime import sys import time from pathlib import Path import pytz as pytz from mss.windows import MSS as mss # The simplest use, save a screen shot of the 1st monitor root = sys.argv[1] if len(sys.argv) > 1 else 'cache' interval = 60 * 2 if len(sys.argv) <= 2 else float(sys.argv[2]) * 60 while True: now = datetime.datetime.now(tz=pytz.timezone('Europe/Berlin')) if now.hour < 6: continue if now.hour > 22: continue folder = f'{root}/{now.year}-{now.month:02d}/{now.day:02d}' Path(folder).mkdir(parents=True, exist_ok=True) try: mss().shot(mon=1, output=f'{folder}/{now.hour:02d}_{now.minute:02d}_1.png') except Exception as ex: print(ex) try: mss().shot(mon=2, output=f'{folder}/{now.hour:02d}_{now.minute:02d}_2.png') except Exception as ex: print(ex) time.sleep(interval)