def apply_preproc(*args): from preprocessing.preprocessing import Preprocessing preproc = Preprocessing() preproc_imgs = [] for img in args: new_img = preproc.preprocess_img(img) if new_img is not None: preproc_imgs.append(new_img) return preproc_imgs
def __init__(self, pitch, color, our_side, video_port=0, comm_port='/dev/ttyUSB0', comms=1): """ Entry point for the SDP system. Params: [int] video_port port number for the camera [string] comm_port port number for the arduino [int] pitch 0 - main pitch, 1 - secondary pitch [string] our_side the side we're on - 'left' or 'right' *[int] port The camera port to take the feed from *[Robot_Controller] attacker Robot controller object - Attacker Robot has a RED power wire *[Robot_Controller] defender Robot controller object - Defender Robot has a YELLOW power wire """ assert pitch in [0, 1] assert color in ['yellow', 'blue'] assert our_side in ['left', 'right'] self.pitch = pitch # Set up the Arduino communications self.arduino = Arduino(comm_port, 115200, 1, comms) # Set up camera for frames self.camera = Camera(port=video_port, pitch=self.pitch) frame = self.camera.get_frame() center_point = self.camera.get_adjusted_center(frame) # Set up vision self.calibration = tools.get_colors(pitch) self.vision = Vision( pitch=pitch, color=color, our_side=our_side, frame_shape=frame.shape, frame_center=center_point, calibration=self.calibration) # Set up postprocessing for vision self.postprocessing = Postprocessing() # Set up main planner self.planner = Planner(our_side=our_side, pitch_num=self.pitch) # Set up GUI self.GUI = GUI(calibration=self.calibration, arduino=self.arduino, pitch=self.pitch) self.color = color self.side = our_side self.preprocessing = Preprocessing() self.attacker = Attacker_Controller() self.defender = Defender_Controller()
def main(): preprocessing = Preprocessing() root_path = './test' # before pickle hierarchy directory_dict = {} dir_hierarchy = preprocessing.lookup_directory(root_path, directory_dict) file_list = list() dir_list = list() label_num = 0 for tar_dir in dir_hierarchy: file_list += dir_hierarchy[tar_dir] for file_path in tqdm(file_list): text = extract_text(file_path) new_path = file_path[:-4] with open(new_path, 'wb') as f: pickle.dump(text, f)
def __init__(self, pitch, color, our_side, video_port=0, comm_port="/dev/ttyUSB0", comms=1): """ Entry point for the SDP system. Params: [int] video_port port number for the camera [string] comm_port port number for the arduino [int] pitch 0 - main pitch, 1 - secondary pitch [string] our_side the side we're on - 'left' or 'right' *[int] port The camera port to take the feed from *[Robot_Controller] attacker Robot controller object - Attacker Robot has a RED power wire *[Robot_Controller] defender Robot controller object - Defender Robot has a YELLOW power wire """ assert pitch in [0, 1] assert color in ["yellow", "blue"] assert our_side in ["left", "right"] self.pitch = pitch # Set up the Arduino communications self.arduino = Arduino(comm_port, 115200, 1, comms) # Set up camera for frames self.camera = Camera(port=video_port, pitch=self.pitch) frame = self.camera.get_frame() center_point = self.camera.get_adjusted_center(frame) # Set up vision self.calibration = tools.get_colors(pitch) self.vision = Vision( pitch=pitch, color=color, our_side=our_side, frame_shape=frame.shape, frame_center=center_point, calibration=self.calibration, ) # Set up postprocessing for vision self.postprocessing = Postprocessing() # Set up main planner self.planner = Planner(our_side=our_side, pitch_num=self.pitch) # Set up GUI self.GUI = GUI(calibration=self.calibration, arduino=self.arduino, pitch=self.pitch) self.color = color self.side = our_side self.preprocessing = Preprocessing() self.attacker = Attacker_Controller() self.defender = Defender_Controller()
def process_frame(frame, PREPROCESSING_CONFIG): """ Image processing """ stack = Preprocessing.from_spec(PREPROCESSING_CONFIG) processed_frames = [] for el in frame: s = stack.process(el[0]) s = np.reshape(s, [np.prod(s.shape)]) / 255.0 s = np.expand_dims(s, 0) processed_frames.append(s) return processed_frames
def __init__(self, pitch, color, our_side, video_port=0): """ Entry point for the SDP system. Params: [int] pitch 0 - main pitch, 1 - secondary pitch [string] colour The colour of our teams plates [string] our_side the side we're on - 'left' or 'right' [int] video_port port number for the camera Fields pitch camera calibration vision postporcessing color side preprocessing model_positions regular_positions """ assert pitch in [0, 1] assert color in ['yellow', 'blue'] assert our_side in ['left', 'right'] self.pitch = pitch # Set up camera for frames self.camera = Camera(port=video_port, pitch=pitch) self.frame = self.camera.get_frame() center_point = self.camera.get_adjusted_center(self.frame) # Set up vision self.calibration = tools.get_colors(pitch) self.vision = Vision( pitch=pitch, color=color, our_side=our_side, frame_shape=self.frame.shape, frame_center=center_point, calibration=self.calibration) # Set up preprocessing and postprocessing self.postprocessing = Postprocessing() self.preprocessing = Preprocessing() self.color = color self.side = our_side self.frameQueue = []
class Controller: """ Primary source of robot control. Ties vision and planning together. """ def __init__(self, pitch, color, our_side, video_port=0, comm_port="/dev/ttyUSB0", comms=1): """ Entry point for the SDP system. Params: [int] video_port port number for the camera [string] comm_port port number for the arduino [int] pitch 0 - main pitch, 1 - secondary pitch [string] our_side the side we're on - 'left' or 'right' *[int] port The camera port to take the feed from *[Robot_Controller] attacker Robot controller object - Attacker Robot has a RED power wire *[Robot_Controller] defender Robot controller object - Defender Robot has a YELLOW power wire """ assert pitch in [0, 1] assert color in ["yellow", "blue"] assert our_side in ["left", "right"] self.pitch = pitch # Set up the Arduino communications self.arduino = Arduino(comm_port, 115200, 1, comms) # Set up camera for frames self.camera = Camera(port=video_port, pitch=self.pitch) frame = self.camera.get_frame() center_point = self.camera.get_adjusted_center(frame) # Set up vision self.calibration = tools.get_colors(pitch) self.vision = Vision( pitch=pitch, color=color, our_side=our_side, frame_shape=frame.shape, frame_center=center_point, calibration=self.calibration, ) # Set up postprocessing for vision self.postprocessing = Postprocessing() # Set up main planner self.planner = Planner(our_side=our_side, pitch_num=self.pitch) # Set up GUI self.GUI = GUI(calibration=self.calibration, arduino=self.arduino, pitch=self.pitch) self.color = color self.side = our_side self.preprocessing = Preprocessing() self.attacker = Attacker_Controller() self.defender = Defender_Controller() def wow(self): """ Ready your sword, here be dragons. """ counter = 1L timer = time.clock() try: c = True while c != 27: # the ESC key frame = self.camera.get_frame() pre_options = self.preprocessing.options # Apply preprocessing methods toggled in the UI preprocessed = self.preprocessing.run(frame, pre_options) frame = preprocessed["frame"] if "background_sub" in preprocessed: cv2.imshow("bg sub", preprocessed["background_sub"]) # Find object positions # model_positions have their y coordinate inverted model_positions, regular_positions = self.vision.locate(frame) model_positions = self.postprocessing.analyze(model_positions) # Find appropriate action self.planner.update_world(model_positions) attacker_actions = self.planner.plan("attacker") defender_actions = self.planner.plan("defender") if self.attacker is not None: self.attacker.execute(self.arduino, attacker_actions) if self.defender is not None: self.defender.execute(self.arduino, defender_actions) # Information about the grabbers from the world grabbers = { "our_defender": self.planner._world.our_defender.catcher_area, "our_attacker": self.planner._world.our_attacker.catcher_area, } # Information about states attackerState = (self.planner.attacker_state, self.planner.attacker_strat_state) defenderState = (self.planner.defender_state, self.planner.defender_strat_state) # Use 'y', 'b', 'r' to change color. c = waitKey(2) & 0xFF actions = [] fps = float(counter) / (time.clock() - timer) # Draw vision content and actions self.GUI.draw( frame, model_positions, actions, regular_positions, fps, attackerState, defenderState, attacker_actions, defender_actions, grabbers, our_color=self.color, our_side=self.side, key=c, preprocess=pre_options, ) counter += 1 except: if self.defender is not None: self.defender.shutdown(self.arduino) if self.attacker is not None: self.attacker.shutdown(self.arduino) raise finally: # Write the new calibrations to a file. tools.save_colors(self.pitch, self.calibration) if self.attacker is not None: self.attacker.shutdown(self.arduino) if self.defender is not None: self.defender.shutdown(self.arduino)
# print scalibration vision = _Vision(pitch=0, color='blue', our_side='left', frame_shape=frame.shape, frame_center=cam.get_adjusted_center(frame), calibration=scalibration) # Set up postprocessing for oldvision postprocessing = Postprocessing() GUI = GUI(calibration=scalibration, pitch=0) preprocessing = Preprocessing() frame = cam.get_frame() pre_options = preprocessing.options # Apply preprocessing methods toggled in the UI preprocessed = preprocessing.run(frame, pre_options) frame = preprocessed['frame'] if 'background_sub' in preprocessed: cv2.imshow('bg sub', preprocessed['background_sub']) cv2.waitKey() height, width, channels = frame.shape # frame = frame[425:445, 5:25] frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
def dropfile(input_file: str, root_path: str, cached_DTM=None, cached_vocab=None, cached_synonym_dict=None, verbose=True, preprocessing=None, scoring=None): os.environ['DROPFILE_LOGLEVEL'] = "1" if verbose else "0" global plot_number normalpreprocessing = Preprocessing() dspreprocessing = DependencyStructurePreprocessing() nppreprocessing = NounPhrasePreprocessing() npreprocessing = NounPreprocessing() spacypreprocessing = SpacyPreprocessing() twcpreprocessing = TargetWordChunkingPreprocessing() cfgpreprocessing = CFGPreprocessing() preprocessing_dict = { "Preprocessing": normalpreprocessing, "DependencyStructurePreprocessing": dspreprocessing, "NounPhrasePreprocessing": nppreprocessing, "NounPreprocessing": npreprocessing, "SpacyPreprocessing": spacypreprocessing, "TargetWordChunkingPreprocessing": twcpreprocessing, "CFGPreprocessing": cfgpreprocessing } scoring_dict = { "score_mse": score_mse, "score_cosine": score_cosine, "score_bayes": score_bayes, "score_CFG": score_CFG } if preprocessing is not None and scoring is not None: preprocessing_list = [ "Preprocessing", "DependencyStructurePreprocessing", "NounPhrasePreprocessing", "NounPreprocessing", "SpacyPreprocessing", "TargetWordChunkingPreprocessing", "CFGPreprocessing" ] if preprocessing not in preprocessing_list: print("Enter the valid preprocessing name") return if preprocessing in cached_DTM and preprocessing in cached_vocab and preprocessing in cached_synonym_dict: dir_list, label_score, _, _, _ = \ scoring_dict[scoring](input_file, root_path, preprocessing_dict[preprocessing], cached_DTM[preprocessing], cached_vocab[preprocessing], cached_synonym_dict[preprocessing]) else: dir_list, label_score, _, _, _ = \ scoring_dict[scoring](input_file, root_path, preprocessing_dict[preprocessing], None, None, None) if verbose: print(label_score) score_arr = np.array(label_score).astype(float) score_arr = score_arr / sum(score_arr) dir_path = dir_list[score_arr.argmax()] case = os.listdir(root_path)[0] print(f"********** {case} store score ********") with open(f'MaxMinDev_{case}', 'wb') as file: # OS dependency score_max = np.max(score_arr) score_min = np.min(score_arr) dev = score_max - score_min MaxMindict = defaultdict(list) if OSTYPE == "Darwin": MaxMindict[input_file.split("/")[-1]] = [ score_max, score_min, dev ] elif OSTYPE == "Linux": MaxMindict[input_file.split("/")[-1]] = [ score_max, score_min, dev ] else: MaxMindict[input_file.split("\\")[-1]] = [ score_max, score_min, dev ] pickle.dump(MaxMindict, file) plt.figure(plot_number) plot_number += 1 directory_name = [ path.split('/')[-1].split('\\')[-1] for path in dir_list ] y = score_arr x = np.arange(len(y)) xlabel = directory_name if OSTYPE == "Darwin": plt.title("Label Score of {}".format( input_file.split('/')[-2] + '_' + input_file.split("/")[-1])) elif OSTYPE == "Linux": plt.title("Label Score of {}".format( input_file.split('/')[-2] + '_' + input_file.split("/")[-1])) else: # Windows plt.title("Label Score of {}".format( input_file.split('\\')[-2] + '_' + input_file.split("\\")[-1])) plt.bar(x, y, color="blue") plt.xticks(x, xlabel) if OSTYPE == "Darwin": plt.savefig("label_score_{}.png".format( input_file.split('/')[-2] + '_' + input_file.split("/")[-1])) elif OSTYPE == "Linux": plt.savefig("label_score_{}.png".format( input_file.split('/')[-2] + '_' + input_file.split("/")[-1])) else: # Windows plt.savefig("label_score_{}.png".format( input_file.split('\\')[-2] + '_' + input_file.split("\\")[-1])) plt.close(plot_number - 1) return dir_path, cached_DTM, cached_vocab, cached_synonym_dict ensembles = [ { "preprocessing": "Preprocessing", "scoring": score_cosine, "weight": 1 }, { "preprocessing": "DependencyStructurePreprocessing", "scoring": score_cosine, "weight": 1 }, { "preprocessing": "NounPhrasePreprocessing", "scoring": score_cosine, "weight": 1 }, { "preprocessing": "NounPreprocessing", "scoring": score_cosine, "weight": 1 }, { "preprocessing": "Preprocessing", "scoring": score_bayes, "weight": 1 }, { "preprocessing": "SpacyPreprocessing", "scoring": score_cosine, "weight": 1 }, { "preprocessing": "Preprocessing", "scoring": score_mse, "weight": 1 }, { "preprocessing": "CFGPreprocessing", "scoring": score_CFG, "weight": 1 }, ] label_scores = [] if cached_DTM is None: cached_DTM = dict() if cached_vocab is None: cached_vocab = dict() if cached_synonym_dict is None: cached_synonym_dict = dict() for i, method in enumerate(ensembles): preprocessing_name = method["preprocessing"] if preprocessing_name in cached_DTM and preprocessing_name in cached_vocab \ and preprocessing_name in cached_synonym_dict: dir_list, label_score, DTM, vocab, synonym_dict = \ method['scoring'](input_file, root_path, preprocessing_dict[preprocessing_name], cached_DTM[preprocessing_name], cached_vocab[preprocessing_name], cached_synonym_dict[preprocessing_name]) else: dir_list, label_score, DTM, vocab, synonym_dict= \ method['scoring'](input_file, root_path, preprocessing_dict[preprocessing_name], None, None, None) cached_DTM[preprocessing_name] = DTM cached_vocab[preprocessing_name] = vocab cached_synonym_dict[preprocessing_name] = synonym_dict label_scores.append(label_score) score_arr = np.array(label_scores) for i in range(score_arr.shape[0]): score_arr[i] = score_arr[i] / sum(score_arr[i]) if verbose: print(score_arr) final_label_score = np.array([0.0] * score_arr.shape[1]) for i in range(score_arr.shape[0]): final_label_score += score_arr[i] * ensembles[i]["weight"] case = os.listdir(root_path)[0] print(f"********** {case} store score ********") with open(f'MaxMinDev_{case}', 'wb') as file: # OS dependency score_max = np.max(final_label_score) score_min = np.min(final_label_score) dev = score_max - score_min MaxMindict = defaultdict(list) if OSTYPE == "Darwin": MaxMindict[input_file.split("/")[-1]] = [score_max, score_min, dev] elif OSTYPE == "Linux": MaxMindict[input_file.split("/")[-1]] = [score_max, score_min, dev] else: MaxMindict[input_file.split("\\")[-1]] = [ score_max, score_min, dev ] pickle.dump(MaxMindict, file) print("Your OS is ", OSTYPE) plt.figure(plot_number) plot_number += 1 directory_name = [path.split('/')[-1].split('\\')[-1] for path in dir_list] y = final_label_score x = np.arange(len(y)) xlabel = directory_name if OSTYPE == "Darwin": plt.title("Label Score of {}".format( input_file.split('/')[-2] + '_' + input_file.split("/")[-1])) elif OSTYPE == "Linux": plt.title("Label Score of {}".format( input_file.split('/')[-2] + '_' + input_file.split("/")[-1])) else: # Windows plt.title("Label Score of {}".format( input_file.split('/')[-1].split("\\")[-2] + '_' + input_file.split("\\")[-1])) plt.bar(x, y, color="blue") plt.xticks(x, xlabel) if OSTYPE == "Darwin": plt.savefig("label_score_{}.png".format( input_file.split('/')[-2] + '_' + input_file.split("/")[-1])) elif OSTYPE == "Linux": plt.savefig("label_score_{}.png".format( input_file.split('/')[-2] + '_' + input_file.split("/")[-1])) else: # Windows plt.savefig("label_score_{}.png".format( input_file.split('/')[-1].split("\\")[-2] + '_' + input_file.split('/')[-1].split("\\")[-1])) plt.close(plot_number - 1) try: dir_path = dir_list[final_label_score.argmax()] except: dir_path = '' return dir_path, cached_DTM, cached_vocab, cached_synonym_dict
class Controller: """ Primary source of robot control. Ties vision and planning together. """ def __init__(self, pitch, color, our_side, video_port=0, comm_port='/dev/ttyUSB0', comms=1): """ Entry point for the SDP system. Params: [int] video_port port number for the camera [string] comm_port port number for the arduino [int] pitch 0 - main pitch, 1 - secondary pitch [string] our_side the side we're on - 'left' or 'right' *[int] port The camera port to take the feed from *[Robot_Controller] attacker Robot controller object - Attacker Robot has a RED power wire *[Robot_Controller] defender Robot controller object - Defender Robot has a YELLOW power wire """ assert pitch in [0, 1] assert color in ['yellow', 'blue'] assert our_side in ['left', 'right'] self.pitch = pitch # Set up the Arduino communications self.arduino = Arduino(comm_port, 115200, 1, comms) # Set up camera for frames self.camera = Camera(port=video_port, pitch=self.pitch) frame = self.camera.get_frame() center_point = self.camera.get_adjusted_center(frame) # Set up vision self.calibration = tools.get_colors(pitch) self.vision = Vision( pitch=pitch, color=color, our_side=our_side, frame_shape=frame.shape, frame_center=center_point, calibration=self.calibration) # Set up postprocessing for vision self.postprocessing = Postprocessing() # Set up main planner self.planner = Planner(our_side=our_side, pitch_num=self.pitch) # Set up GUI self.GUI = GUI(calibration=self.calibration, arduino=self.arduino, pitch=self.pitch) self.color = color self.side = our_side self.preprocessing = Preprocessing() self.attacker = Attacker_Controller() self.defender = Defender_Controller() def wow(self): """ Ready your sword, here be dragons. """ counter = 1L timer = time.clock() try: c = True while c != 27: # the ESC key frame = self.camera.get_frame() pre_options = self.preprocessing.options # Apply preprocessing methods toggled in the UI preprocessed = self.preprocessing.run(frame, pre_options) frame = preprocessed['frame'] if 'background_sub' in preprocessed: cv2.imshow('bg sub', preprocessed['background_sub']) # Find object positions # model_positions have their y coordinate inverted model_positions, regular_positions = self.vision.locate(frame) model_positions = self.postprocessing.analyze(model_positions) # Find appropriate action self.planner.update_world(model_positions) attacker_actions = self.planner.plan('attacker') defender_actions = self.planner.plan('defender') if self.attacker is not None: self.attacker.execute(self.arduino, attacker_actions) if self.defender is not None: self.defender.execute(self.arduino, defender_actions) # Information about the grabbers from the world grabbers = { 'our_defender': self.planner._world.our_defender.catcher_area, 'our_attacker': self.planner._world.our_attacker.catcher_area } # Information about states attackerState = (self.planner.attacker_state, self.planner.attacker_strat_state) defenderState = (self.planner.defender_state, self.planner.defender_strat_state) # Use 'y', 'b', 'r' to change color. c = waitKey(2) & 0xFF actions = [] fps = float(counter) / (time.clock() - timer) # Draw vision content and actions self.GUI.draw( frame, model_positions, actions, regular_positions, fps, attackerState, defenderState, attacker_actions, defender_actions, grabbers, our_color=self.color, our_side=self.side, key=c, preprocess=pre_options) counter += 1 except: if self.defender is not None: self.defender.shutdown(self.arduino) if self.attacker is not None: self.attacker.shutdown(self.arduino) raise finally: # Write the new calibrations to a file. tools.save_colors(self.pitch, self.calibration) if self.attacker is not None: self.attacker.shutdown(self.arduino) if self.defender is not None: self.defender.shutdown(self.arduino)
vision = _Vision( pitch=0, color='blue', our_side='left', frame_shape=frame.shape, frame_center=cam.get_adjusted_center(frame), calibration=scalibration) # Set up postprocessing for oldvision postprocessing = Postprocessing() GUI = GUI(calibration=scalibration, pitch=0) preprocessing = Preprocessing() frame = cam.get_frame() pre_options = preprocessing.options # Apply preprocessing methods toggled in the UI preprocessed = preprocessing.run(frame, pre_options) frame = preprocessed['frame'] if 'background_sub' in preprocessed: cv2.imshow('bg sub', preprocessed['background_sub']) cv2.waitKey() height, width, channels = frame.shape # frame = frame[425:445, 5:25] frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
def prepare_env(root_path: str, cached_tokens=None, verbose=False): os.environ['DROPFILE_LOGLEVEL'] = "1" if verbose else "0" normalpreprocessing = Preprocessing() dspreprocessing = DependencyStructurePreprocessing() nppreprocessing = NounPhrasePreprocessing() npreprocessing = NounPreprocessing() spacypreprocessing = SpacyPreprocessing() twcpreprocessing = TargetWordChunkingPreprocessing() cfgpreprocessing = CFGPreprocessing() preprocessing_dict = { "Preprocessing": normalpreprocessing, "DependencyStructurePreprocessing": dspreprocessing, "NounPhrasePreprocessing": nppreprocessing, "NounPreprocessing": npreprocessing, "SpacyPreprocessing": spacypreprocessing, "TargetWordChunkingPreprocessing": twcpreprocessing, "CFGPreprocessing": cfgpreprocessing } DTM_dict = dict() vocab_dict = dict() synonym_dict_dict = dict() start = time.time() directory_dict = defaultdict( list) # empty dictionary for lookup_directory function dir_hierarchy = normalpreprocessing.lookup_directory( root_path, directory_dict) file_list = list() doc_dict = dict() for tar_dir in dir_hierarchy: file_list += dir_hierarchy[tar_dir] if cached_tokens is None: tokens_dict = defaultdict(dict) else: tokens_dict = cached_tokens for file in file_list: if file not in tokens_dict["Preprocessing"]: doc_dict[file] = normalpreprocessing.file2text(file) if verbose: print(f"file2text takes {time.time() - start:.4f} s.") for name, preprocessing in preprocessing_dict.items(): if verbose: print(f"{name} started") # preprocessing : lookup hierarchy of root path directory_dict = defaultdict( list) # empty dictionary for lookup_directory function start = time.time() dir_hierarchy = preprocessing.lookup_directory( root_path, directory_dict) # change it to have 2 parameter if verbose: print(f"{name}.lookup_directory takes {time.time()-start:.4f} s.") file_list = list() dir_list = list() label_num = 0 for tar_dir in dir_hierarchy: file_list += dir_hierarchy[tar_dir] dir_list.append(tar_dir) label_num += 1 # preprocessing : build vocabulary from file_list # if (DTM is None) and (vocab is None) and (synonym_dict is None): doc_list = list() start = time.time() for file in file_list: if name in tokens_dict and file in tokens_dict[name]: tokens = tokens_dict[name][file] else: tokens = preprocessing.text2tok(doc_dict[file]) doc_list.append(tokens) tokens_dict[name][file] = tokens if verbose: print(f"{name}.text2tok takes {time.time()-start:.4f} s.") start = time.time() vocab, synonym_dict = preprocessing.build_vocab(doc_list) if verbose: print(f"{name}.build_vocab takes {time.time()-start:.4f} s.") # preprocessing : build DTM of files under root_path start = time.time() DTM = preprocessing.build_DTM(doc_list, vocab, synonym_dict) if verbose: print(f"{name}.build_DTM takes {time.time()-start:.4f} s.") DTM_dict[name] = DTM vocab_dict[name] = vocab synonym_dict_dict[name] = synonym_dict return DTM_dict, vocab_dict, synonym_dict_dict, tokens_dict
class Controller: """ This class aims to be the bridge in between vision and strategy/logic """ robotCom = None # Set to True if we want to use the real robot. # Set to False if we want to print out commands to console only. USE_REAL_ROBOT = True def __init__(self, pitch, color, our_side, video_port=0, comm_port='/dev/ttyACM0', comms=1): """ Entry point for the SDP system. Params: [int] video_port port number for the camera [string] comm_port port number for the arduino [int] pitch 0 - main pitch, 1 - secondary pitch [string] our_side the side we're on - 'left' or 'right' *[int] port The camera port to take the feed from *[Robot_Controller] attacker Robot controller object - Attacker Robot has a RED power wire *[Robot_Controller] defender Robot controller object - Defender Robot has a YELLOW power wire """ assert pitch in [0, 1] assert color in ['yellow', 'blue'] assert our_side in ['left', 'right'] self.pitch = pitch # Set up communications if thre are any try: self.robotComs = RobotCommunications(debug=False) except: print("arduino unplugged moving on to vision") # Set up robot communications to bet sent to planner. if self.USE_REAL_ROBOT: try: self.robotCom = RobotCommunications(debug=False) except: self.robotCom = TestCommunications(debug=True) print 'Not connected to the radio, using TestCommunications instead.' else: self.robotCom = TestCommunications(debug=True) # Set up main planner if(self.robotCom is not None): # currently we are assuming we are the defender self.planner = Planner(our_side=our_side, pitch_num=self.pitch, robotCom=self.robotCom, robotType='defender') # Set up camera for frames self.camera = Camera(port=video_port, pitch=self.pitch) frame = self.camera.get_frame() center_point = self.camera.get_adjusted_center(frame) # Set up vision self.calibration = tools.get_colors(pitch) print self.calibration self.vision = Vision( pitch=pitch, color=color, our_side=our_side, frame_shape=frame.shape, frame_center=center_point, calibration=self.calibration) # Set up postprocessing for vision self.postprocessing = Postprocessing() # Set up GUI self.GUI = GUI(calibration=self.calibration, pitch=self.pitch) self.color = color self.side = our_side self.preprocessing = Preprocessing() def main(self): """ This main method brings in to action the controller class which so far does nothing but set up the vision and its ouput """ counter = 1L timer = time.clock() try: c = True while c != 27: # the ESC key frame = self.camera.get_frame() pre_options = self.preprocessing.options # Apply preprocessing methods toggled in the UI preprocessed = self.preprocessing.run(frame, pre_options) frame = preprocessed['frame'] if 'background_sub' in preprocessed: cv2.imshow('bg sub', preprocessed['background_sub']) # Find object positions # model_positions have their y coordinate inverted # IMPORTANT model_positions, regular_positions = self.vision.locate(frame) model_positions = self.postprocessing.analyze(model_positions) # Update planner world beliefs self.planner.update_world(model_positions) self.planner.plan() # Use 'y', 'b', 'r' to change color. c = waitKey(2) & 0xFF actions = [] fps = float(counter) / (time.clock() - timer) # Draw vision content and actions self.GUI.draw( frame, model_positions, actions, regular_positions, fps, None, None, None, None, False, our_color=self.color, our_side=self.side, key=c, preprocess=pre_options) counter += 1 except: # This exception is stupid TODO: refactor. print("TODO SOMETHING CLEVER HERE") raise finally: tools.save_colors(self.pitch, self.calibration)
def __init__(self, pitch, color, our_side, video_port=0, comm_port='/dev/ttyACM0', comms=1): """ Entry point for the SDP system. Params: [int] video_port port number for the camera [string] comm_port port number for the arduino [int] pitch 0 - main pitch, 1 - secondary pitch [string] our_side the side we're on - 'left' or 'right' *[int] port The camera port to take the feed from *[Robot_Controller] attacker Robot controller object - Attacker Robot has a RED power wire *[Robot_Controller] defender Robot controller object - Defender Robot has a YELLOW power wire """ assert pitch in [0, 1] assert color in ['yellow', 'blue'] assert our_side in ['left', 'right'] self.pitch = pitch # Set up communications if thre are any try: self.robotComs = RobotCommunications(debug=False) except: print("arduino unplugged moving on to vision") # Set up robot communications to bet sent to planner. if self.USE_REAL_ROBOT: try: self.robotCom = RobotCommunications(debug=False) except: self.robotCom = TestCommunications(debug=True) print 'Not connected to the radio, using TestCommunications instead.' else: self.robotCom = TestCommunications(debug=True) # Set up main planner if(self.robotCom is not None): # currently we are assuming we are the defender self.planner = Planner(our_side=our_side, pitch_num=self.pitch, robotCom=self.robotCom, robotType='defender') # Set up camera for frames self.camera = Camera(port=video_port, pitch=self.pitch) frame = self.camera.get_frame() center_point = self.camera.get_adjusted_center(frame) # Set up vision self.calibration = tools.get_colors(pitch) print self.calibration self.vision = Vision( pitch=pitch, color=color, our_side=our_side, frame_shape=frame.shape, frame_center=center_point, calibration=self.calibration) # Set up postprocessing for vision self.postprocessing = Postprocessing() # Set up GUI self.GUI = GUI(calibration=self.calibration, pitch=self.pitch) self.color = color self.side = our_side self.preprocessing = Preprocessing()
class VisionWrapper: """ Class that handles vision """ def __init__(self, pitch, color, our_side, video_port=0): """ Entry point for the SDP system. Params: [int] pitch 0 - main pitch, 1 - secondary pitch [string] colour The colour of our teams plates [string] our_side the side we're on - 'left' or 'right' [int] video_port port number for the camera Fields pitch camera calibration vision postporcessing color side preprocessing model_positions regular_positions """ assert pitch in [0, 1] assert color in ['yellow', 'blue'] assert our_side in ['left', 'right'] self.pitch = pitch # Set up camera for frames self.camera = Camera(port=video_port, pitch=pitch) self.frame = self.camera.get_frame() center_point = self.camera.get_adjusted_center(self.frame) # Set up vision self.calibration = tools.get_colors(pitch) self.vision = Vision( pitch=pitch, color=color, our_side=our_side, frame_shape=self.frame.shape, frame_center=center_point, calibration=self.calibration) # Set up preprocessing and postprocessing self.postprocessing = Postprocessing() self.preprocessing = Preprocessing() self.color = color self.side = our_side self.frameQueue = [] def update(self): """ Gets this frame's positions from the vision system. """ self.frame = self.camera.get_frame() # Apply preprocessing methods toggled in the UI self.preprocessed = self.preprocessing.run(self.frame, self.preprocessing.options) self.frame = self.preprocessed['frame'] if 'background_sub' in self.preprocessed: cv2.imshow('bg sub', self.preprocessed['background_sub']) # Find object positions # model_positions have their y coordinate inverted self.model_positions, self.regular_positions = self.vision.locate(self.frame) self.model_positions = self.postprocessing.analyze(self.model_positions) #self.model_positions = self.averagePositions(3, self.model_positions) def averagePositions(self, frames, positions_in): """ :param frames: number of frames to average :param positions_in: positions for the current frame :return: averaged positions """ validFrames = self.frameQueue.__len__() + 1 positions_out = deepcopy(positions_in) # Check that the incoming positions have legal values for obj in positions_out.items(): if (positions_out[obj[0]].velocity is None): positions_out[obj[0]].velocity = 0 if positions_out[obj[0]].x is None: positions_out[obj[0]].x = 0 if positions_out[obj[0]].y is None: positions_out[obj[0]].y = 0 positions_out[obj[0]].angle = positions_in[obj[0]].angle # Loop over queue for positions in self.frameQueue: # Loop over each object in the position dictionary isFrameValid = True for obj in positions.items(): # Check if the current object's positions have legal values if (obj[1].x is None) or (obj[1].y is None) or (obj[1].angle is None) or (obj[1].velocity is None): isFrameValid = False else: positions_out[obj[0]].x += obj[1].x positions_out[obj[0]].y += obj[1].y positions_out[obj[0]].velocity += obj[1].velocity if not isFrameValid and validFrames > 1: #validFrames -= 1 pass # Loop over each object in the position dictionary and average the values for obj in positions_out.items(): positions_out[obj[0]].velocity /= validFrames positions_out[obj[0]].x /= validFrames positions_out[obj[0]].y /= validFrames # If frameQueue is already full then pop the top entry off if self.frameQueue.__len__() >= frames: self.frameQueue.pop(0) # Add our new positions to the end self.frameQueue.append(positions_in) return positions_out def saveCalibrations(self): tools.save_colors(self.vision.pitch, self.calibration)
def job(): """ Function to be scheduling. """ current_month = 3 # datetime.now().month current_year = 2017 # datetime.now().year # if is_last_month(current_year, current_month): message_history_list = Repository.get_chat_message_history( month=current_month, year=current_year) if message_history_list: merchant_name = message_history_list[0].name # cleaning chat text results = preprocessing.cleaning(message_history_list) logger.info(f'Preprocessing result {len(results)} items') # build documents documents = [result.content.split() for result in results] documents = Preprocessing.identify_phrase(documents) dictionary = Dictionary(documents) logger.info(f'Preprocessing unique tokens: {len(dictionary)}') # build bag of words bow_corpus = [dictionary.doc2bow(document) for document in documents] # calculate tf idf tf_idf = TfidfModel(bow_corpus) corpus_tf_idf = tf_idf[bow_corpus] # find highest coherence score lda_models_with_coherence_score = {} for index in range(1, NUM_TOPICS + 1): lda_model = LdaModel(corpus=corpus_tf_idf, num_topics=index, id2word=dictionary) coherence_model_lda = CoherenceModel(model=lda_model, texts=documents, corpus=corpus_tf_idf, coherence='c_v') coherence_score = coherence_model_lda.get_coherence() lda_models_with_coherence_score[coherence_score] = lda_model logger.info(f'Coherence score: {coherence_score}') # running the best lda model based on highest coherence score lda_model = lda_models_with_coherence_score[max( lda_models_with_coherence_score)] # save into DB for cluster, topic_term in lda_model.show_topics(-1, num_words=20, formatted=False): for topic in topic_term: logger.info(f'Topic Cluster: {cluster + 1}, ' f'Word: {topic[0]}, ' f'Score: {topic[1]}, ' f'Merchant: {merchant_name}, ' f'Year: {current_year}, ' f'Month: {current_month}') repository.insert_into_online_shop(topic_cluster=cluster + 1, word=topic[0], score=topic[1], merchant_name=merchant_name, year=current_year, month=current_month)
import os import os.path import random import shutil from preprocessing.preprocessing import Preprocessing import dropfile from tqdm import tqdm import time # add time module from collections import defaultdict from itertools import combinations, product, chain import sys import seaborn as sn import pandas as pd import matplotlib.pyplot as plt import matplotlib.figure as fig preprocessing = Preprocessing() # import naivebayes import spacy import pickle INITIAL_TEST_FRAC = 0.8 INITIAL_PATH = './test' # function : prepare environment, build new root_path and relocate each file # input : file_list, locate_flag # ex) file_list : ["/test/nlp-1.pdf","/test/nlp-2.pdf",...] # locate_flag : [true,false,true,true, ...] # (= 해당 인덱스의 파일이 initial로 존재할지, 혹은 test용 # input으로 사용될지에 대한 flag) # output : test_path (test가 진행될 root_path, 새로운 디렉토리에 re-locate시켜주어야함. # test 디렉토리에서 locate_flag가 true인 것들에 대해서만 새로운 root_path(e.g. /eval 디렉토리)로 복사해주어야함)
def __init__(self, pitch, color, our_side, video_port=0, comm_port='/dev/ttyACM0', comms=1): """ Entry point for the SDP system. Params: [int] video_port port number for the camera [string] comm_port port number for the arduino [int] pitch 0 - main pitch, 1 - secondary pitch [string] our_side the side we're on - 'left' or 'right' *[int] port The camera port to take the feed from *[Robot_Controller] attacker Robot controller object - Attacker Robot has a RED power wire *[Robot_Controller] defender Robot controller object - Defender Robot has a YELLOW power wire """ assert pitch in [0, 1] assert color in ['yellow', 'blue'] assert our_side in ['left', 'right'] self.pitch = pitch # Set up the Arduino communications self.arduino = arduinoComm.Communication("/dev/ttyACM0", 9600) time.sleep(0.5) self.arduino.grabberUp() self.arduino.grab() # Set up camera for frames self.camera = Camera(port=video_port, pitch=self.pitch) frame = self.camera.get_frame() center_point = self.camera.get_adjusted_center(frame) # Set up vision self.calibration = tools.get_colors(pitch) self.vision = Vision( pitch=pitch, color=color, our_side=our_side, frame_shape=frame.shape, frame_center=center_point, calibration=self.calibration) # Set up postprocessing for vision self.postprocessing = Postprocessing() # Set up GUI self.GUI = GUI(calibration=self.calibration, arduino=self.arduino, pitch=self.pitch) # Set up main planner self.planner = Planner(our_side=our_side, pitch_num=self.pitch, our_color=color, gui=self.GUI, comm = self.arduino) self.color = color self.side = our_side self.timeOfNextAction = 0 self.preprocessing = Preprocessing() #it doesn't matter whether it is an Attacker or a Defender Controller self.controller = Attacker_Controller(self.planner._world, self.GUI) self.robot_action_list = []
class Controller: """ Primary source of robot control. Ties vision and planning together. """ def __init__(self, pitch, color, our_side, video_port=0, comm_port='/dev/ttyACM0', comms=1): """ Entry point for the SDP system. Params: [int] video_port port number for the camera [string] comm_port port number for the arduino [int] pitch 0 - main pitch, 1 - secondary pitch [string] our_side the side we're on - 'left' or 'right' *[int] port The camera port to take the feed from *[Robot_Controller] attacker Robot controller object - Attacker Robot has a RED power wire *[Robot_Controller] defender Robot controller object - Defender Robot has a YELLOW power wire """ assert pitch in [0, 1] assert color in ['yellow', 'blue'] assert our_side in ['left', 'right'] self.pitch = pitch # Set up the Arduino communications self.arduino = arduinoComm.Communication("/dev/ttyACM0", 9600) time.sleep(0.5) self.arduino.grabberUp() self.arduino.grab() # Set up camera for frames self.camera = Camera(port=video_port, pitch=self.pitch) frame = self.camera.get_frame() center_point = self.camera.get_adjusted_center(frame) # Set up vision self.calibration = tools.get_colors(pitch) self.vision = Vision( pitch=pitch, color=color, our_side=our_side, frame_shape=frame.shape, frame_center=center_point, calibration=self.calibration) # Set up postprocessing for vision self.postprocessing = Postprocessing() # Set up GUI self.GUI = GUI(calibration=self.calibration, arduino=self.arduino, pitch=self.pitch) # Set up main planner self.planner = Planner(our_side=our_side, pitch_num=self.pitch, our_color=color, gui=self.GUI, comm = self.arduino) self.color = color self.side = our_side self.timeOfNextAction = 0 self.preprocessing = Preprocessing() #it doesn't matter whether it is an Attacker or a Defender Controller self.controller = Attacker_Controller(self.planner._world, self.GUI) self.robot_action_list = [] def wow(self): """ Ready your sword, here be dragons. """ counter = 1L timer = time.clock() try: c = True while c != 27: # the ESC key frame = self.camera.get_frame() pre_options = self.preprocessing.options # Apply preprocessing methods toggled in the UI preprocessed = self.preprocessing.run(frame, pre_options) frame = preprocessed['frame'] if 'background_sub' in preprocessed: cv2.imshow('bg sub', preprocessed['background_sub']) # Find object positions # model_positions have their y coordinate inverted model_positions, regular_positions = self.vision.locate(frame) model_positions = self.postprocessing.analyze(model_positions) # Find appropriate action self.planner.update_world(model_positions) if time.time() >= self.timeOfNextAction: if self.robot_action_list == []: plan = self.planner.plan() if isinstance(plan, list): self.robot_action_list = plan else: self.robot_action_list = [(plan, 0)] if self.controller is not None: self.controller.execute(self.arduino, self) # Information about the grabbers from the world grabbers = { 'our_defender': self.planner._world.our_defender.catcher_area, 'our_attacker': self.planner._world.our_attacker.catcher_area } # Information about states robotState = 'test' # Use 'y', 'b', 'r' to change color. c = waitKey(2) & 0xFF actions = [] fps = float(counter) / (time.clock() - timer) # Draw vision content and actions self.GUI.draw( frame, model_positions, actions, regular_positions, fps, robotState, "we dont need it", '', "we dont need it", grabbers, our_color='blue', our_side=self.side, key=c, preprocess=pre_options) counter += 1 if c == ord('a'): self.arduino.grabberUp() self.arduino.grab() except: if self.controller is not None: self.controller.shutdown(self.arduino) raise finally: # Write the new calibrations to a file. tools.save_colors(self.pitch, self.calibration) if self.controller is not None: self.controller.shutdown(self.arduino)