def init_capture(source_id, xywh_box, extra_data, fast): global capture try: capture_class = get_capture_class() except ImportError as e: print(e) capture_class = NullCapture if fast and isinstance(capture, capture_class): capture.fast_reinit(source_id, xywh_box, extra_data) else: capture = capture_class(source_id, xywh_box, extra_data) for i in range(50): try: _, image = capture.get_image() if image: print("Capture device ready!") config["capture.source_extra_data"] = capture.extra_data break except Exception: print(f"Capture device not ready. {i}") time.sleep(0.1) continue else: print(f'Capture device cannot be found with "{source_id}"') capture = NullCapture(source_id, xywh_box, extra_data)
def run(self): # Create a socket (SOCK_STREAM means a TCP socket) while not self.stopped(): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True) sock.settimeout(TIME_OUT) try: # Connect to server and send data sock.connect((self.target, self.port)) while not self.stopped(): try: item = self.messageQueue.get_nowait() except queue.Empty: time.sleep(0.001) continue payload = bytes(item, "utf-8") length = len(payload) header = struct.pack("<i", length) sock.sendall(header) sock.sendall(payload) except ConnectionAbortedError: # print ("ConnectAbort") continue except ConnectionRefusedError: # print ("ConnectRefused") continue # server isn't alive yet except ConnectionResetError: # print ("ConnectReset") continue # server restarted except socket.timeout: # print ("ConnectTimeOut") continue # timed out on a response. restart.
def update(self): frame_times = deque([], 10) start_frame_ts = None while self.running: cv2_retval, cv2_image = self.cap.read() avg_ft = self.calculate_avg_frametime(frame_times, start_frame_ts) start_frame_ts = time.time() # deinterlace img1, img2 = self.deinterlace_np(cv2_image) self.inject_image(cv2_retval, img1, time.time()) if avg_ft is not None and img2 is not None: deinterlaced_ft = avg_ft / 2.0 time.sleep(deinterlaced_ft - 0.002) # sleep a bit less than we need. self.inject_image(cv2_retval, img2, time.time()) self.cap.release()
def auto_calibrate_raw(config): # todo: ask the capture method for screen size, and capture full screen. captureAreas = ( (0, 0, 2000, 1000), # 4k screens fullscreen (0, 0, 1000, 1000), # reasonably sized screens ) capture = uncached_capture() interlace_settings = get_mode_res() need_i_resize = need_interlace_resize(interlace_settings) result = None for captureArea in captureAreas: original_xywh_box = capture.xywh_box capture.xywh_box = captureArea # hack, wait for threaded capturing method to populate next frame time.sleep(0.5) _, img = uncached_capture().get_image(rgb=True) # fix image due to interlacing. if need_i_resize: img = img.resize((img.size[0], img.size[1] * 2)) result = auto_calibrate(img) if result: break # try enlargening captured image; that helps sometimes. img = img.resize((img.size[0] * 2, img.size[1] * 2)) result = auto_calibrate(img) if result: x, y, w, h = result result = (x // 2, y // 2, w // 2, h // 2) break if need_i_resize and result is not None: result = fix_interlace_resize(result) capture.xywh_box = original_xywh_box return result
def main(on_cap, check_network_close): strategy = Strategy() # The loop makes sure that the program retries constantly even when # capturing device is having trouble while True: try: read_ts = time.time() ts, image = uncached_capture().get_image(rgb=True) if not ts and not image: break except KeyboardInterrupt: break except Exception: time.sleep(RATE) continue frame_end_ts = (ts or read_ts) + RATE pre_strategy_ts = time.time() strategy.update(ts, image) result = strategy.to_dict() if config["debug.print_benchmark"]: elapsed_time = time.time() - (ts or read_ts) print(f"Elapsed time since capture: {elapsed_time}") strategy_time = time.time() - pre_strategy_ts print(f"Strategy processing time: {strategy_time}") on_cap(result, ts) # error = check_network_close() # if error is not None: # return error time.sleep(max(frame_end_ts - time.time(), 0))
def sendMessage(self, data, isBinary=False): if not isBinary: data = data.encode("utf8") MyClientProtocol.broadcast_message(data, isBinary) def checkNetworkClose(self): return MyClientFactory.kickMessage # called from main thread def close(self): MyClientFactory.closing = True reactor.callLater( 1, MyClientProtocol.close_all) # adds task to reactor thread reactor.callLater(5, reactor.stop) def stop(self): self.close() if __name__ == "__main__": url = "127.0.0.1" # url = "ec2-13-237-232-112.ap-southeast-2.compute.amazonaws.com" connection = CreateClient(url, 3338) for i in range(5): time.sleep(1) connection.sendMessage(str(i)) connection.close() connection.join()
continue payload = bytes(item, "utf-8") length = len(payload) header = struct.pack("<i", length) sock.sendall(header) sock.sendall(payload) except ConnectionAbortedError: # print ("ConnectAbort") continue except ConnectionRefusedError: # print ("ConnectRefused") continue # server isn't alive yet except ConnectionResetError: # print ("ConnectReset") continue # server restarted except socket.timeout: # print ("ConnectTimeOut") continue # timed out on a response. restart. if __name__ == "__main__": client = CreateClient("localhost", 9999) import random try: for i in range(200): client.sendMessage("asdf" + str(random.random())) time.sleep(0.2) except KeyboardInterrupt: client.stop()