def init_video_capture(self): self.video_capture = video_capture.VideoCapture( self.args.ffmpeg_exec_path, self.args.video_src_name, self.args.video_width, self.args.video_height) self.video_capture.start() self.video_capture.wait_data_ready()
def capture_sample(arg_ffmpeg_exec_path, arg_src_name, arg_width, arg_height, arg_output_folder, arg_disable_sleep=False, arg_classifier=None, arg_label_output_path=None): t = int(time.time() * 1000) output_folder = os.path.join(arg_output_folder, str(t)) clover.common.makedirs(output_folder) vc = video_capture.VideoCapture(arg_ffmpeg_exec_path, arg_src_name, arg_width, arg_height) vc.start() vc.wait_data_ready() while True: t = int(time.time() * 1000) t0 = int(t / 100000) ndata = vc.get_frame() write_ok = True if arg_classifier: label, perfect = arg_classifier.get_state( ndata.astype('float32') * 2 / 255 - 1) print('label={}, perfect={}'.format(label, perfect), file=sys.stderr) write_ok = write_ok and (not perfect) if write_ok: fn_dir = os.path.join(output_folder, str(t0)) fn = os.path.join(fn_dir, '{}.png'.format(t)) clover.common.makedirs(fn_dir) print(fn, file=sys.stderr) cv2.imwrite(fn, ndata) if write_ok and (arg_classifier): ffn_dir = os.path.join(arg_label_output_path, label) ffn = os.path.join(ffn_dir, '{}.png'.format(t)) clover.common.makedirs(ffn_dir) print(ffn, file=sys.stderr) shutil.copyfile(fn, ffn) vc.release_frame() if not arg_disable_sleep: time.sleep(0.05 + 0.05 * random.random()) vc.close()
output_folder = os.path.join('dependency', 'zookeeper_screen_recognition', 'raw_image') t = int(time.time() * 1000) #output_folder = os.path.join(args.output_folder,str(t)) output_folder = os.path.join(output_folder, str(t)) if not os.path.exists(output_folder): os.makedirs(output_folder) state_clr = None if args.scm_path: import classifier_state state_clr = classifier_state.StateClassifier(args.scm_path) scm_img_path = os.path.join(args.scm_img_path, str(t)) vc = video_capture.VideoCapture(FFMPEG_EXEC_PATH, args.src_name, MAIN_WIDTH, MAIN_HEIGHT) vc.start() vc.wait_data_ready() while True: t = int(time.time() * 1000) t0 = int(t / 100000) ndata = vc.get_frame() write_ok = True if state_clr: label, score = state_clr.get_state( ndata.astype('float32') * 2 / 255 - 1) print('{} {}'.format(label, score), file=sys.stderr) write_ok = write_ok and (score < args.scm_score) if write_ok: ffn_dir = os.path.join(scm_img_path, label) ffn = os.path.join(ffn_dir, '{}.png'.format(t))
def main(self, src_name): self.lock = threading.RLock() self.run = True self.vc = video_capture.VideoCapture(FFMPEG_EXEC_PATH, src_name, VIDEO_SIZE[0], VIDEO_SIZE[1]) self.vc.start() self.vc.wait_data_ready() self.adb_shell = adb_shell.AdbShell(ADB_EXEC_PATH) self.adb_shell.connect() pygame.init() img_surf = pygame.pixelcopy.make_surface( np.zeros((VIDEO_SIZE[0], VIDEO_SIZE[1], 3), dtype=np.uint8)) screen = pygame.display.set_mode(APP_DISPLAY_SIZE) self.logic_img_buf = [ np.zeros((VIDEO_SIZE[1], VIDEO_SIZE[0], 3), dtype=np.uint8) for _ in range(async_read_write_judge.BUFFER_COUNT) ] self.logic_result_buf = [None] * async_read_write_judge.BUFFER_COUNT self.logic_result_arwj = async_read_write_judge.AsyncReadWriteJudge( self.lock) self.logic = bot_logic.BotLogic() self.logic_thread = threading.Thread(target=self.logic_run) self.logic_thread.start() img = np.zeros((VIDEO_SIZE[1], VIDEO_SIZE[0], 3), dtype=np.uint8) while self.run: for event in pygame.event.get(): if event.type == pygame.QUIT: self.run = False break self.logic.on_event(event) if not self.run: break screen.fill(WHITE) with self.lock: tmp_img = self.vc.get_frame() np.copyto(img, tmp_img) self.vc.release_frame() tmp_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) tmp_img = np.swapaxes(tmp_img, 0, 1) pygame.pixelcopy.array_to_surface(img_surf, tmp_img) screen.blit(img_surf, (0, 0)) logic_read_idx = self.logic_result_arwj.get_read_idx() if self.logic_result_arwj.get_order_idx(logic_read_idx) != 0: assert (self.logic_result_buf[logic_read_idx] != None) np.copyto(img, self.logic_img_buf[logic_read_idx]) tmp_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) tmp_img = np.swapaxes(tmp_img, 0, 1) pygame.pixelcopy.array_to_surface(img_surf, tmp_img) screen.blit(img_surf, (120, 0)) self.logic.draw(screen, self.logic_result_buf[logic_read_idx]) else: self.logic.draw(screen, None) self.logic_result_arwj.release_read_idx() pygame.display.flip() self.logic_thread.join() self.vc.close() self.adb_shell.release()