def create_table(self, table_name, csv_fname, skip_row_num=0, skip_col_num=0, auto_inc_pk_=True, show_=False): if show_: self.logger.info(" # Create \"{}\" table.".format(table_name)) utils.file_exists(csv_fname, exit_=True) df = pd.read_csv(csv_fname, skiprows=skip_row_num) df = df.drop(df.columns[[x for x in range(skip_col_num)]], axis=1) sql = "CREATE TABLE " + table_name + " (" if auto_inc_pk_: sql += "id INT AUTO_INCREMENT PRIMARY KEY," sql += " {} {}".format(df.loc[0][0].strip(), df.loc[0][1].strip()) if isinstance(df.loc[0][2], str) and len(df.loc[0][2]) > 0: sql += " " + df.loc[0][2].strip() for row in range(1, df.shape[0]): if not isinstance(df.loc[row][0], str): break sql += ", {} {}".format(df.loc[row][0].strip(), df.loc[row][1].strip()) if isinstance(df.loc[row][2], str) and len(df.loc[row][2]) > 0: sql += " " + df.loc[row][2].strip() sql += " )" cursor = self.con.cursor() try: cursor.execute(sql) except Exception as e: self.logger.error(e)
def get_images_from_video(vid_fname, out_path, frame_interval, logger=Logger.get_stdout_logger()): utils.file_exists(vid_fname, exit_=True) utils.folder_exists(out_path, exit_=False, create_=True, print_=True) logger.info(" # Extract image from video, {}".format(vid_fname)) vid = mpy.VideoFileClip(vid_fname) base_fname = os.path.splitext(os.path.basename(vid_fname))[0] i_digit = int(np.log10(vid.duration / frame_interval)) + 1 n_digit = int(np.log10(vid.duration)) + 3 for i, s in enumerate(itools.numeric_range(0, vid.duration, frame_interval)): frame = vid.get_frame(s) time_info = "__" + \ "{:0{width}d}".format(i, width=i_digit) + \ "__" + \ "{:0{width}.1f}sec".format(s, width=n_digit) out_fname = os.path.join(out_path, base_fname + time_info + IMAGE_FILE_EXT) utils.imwrite(frame, out_fname) logger.info(" # save image, {}".format(out_fname))
def insert_csv_file_into_table(self, table_name, csv_fname, row_num=-1): utils.file_exists(csv_fname, exit_=True) df = pd.read_csv(csv_fname, dtype=object) sql = "INSERT INTO " + table_name + " (" + ', '.join(df.columns) + ") " sql += "VALUES (" + ", ".join(["%s"] * len(df.columns)) + ")" if row_num < 0: row_num = df.shape[0] vals = [] for i in range(row_num): if not isinstance(df.loc[i][0], str): break val = [] for j in range(len(df.columns)): col = df.loc[i][j] if not isinstance(col, str): col = "" val.append(col) vals.append(tuple(val)) try: cursor = self.con.cursor() cursor.executemany(sql, vals) self.con.commit() self.print_cursor(cursor) self.logger.info("") self.logger.info(" # Insert csv file into table: {:d} was inserted.".format(cursor.rowcount)) except Exception as e: self.logger.error(e) return True
def init_shm_files(self, shm_ini, shm_shape=None): self.shm_idx = 0 self.shm_file_num = int(shm_ini['shm_file_num']) self.prefix = shm_ini['shm_file_prefix'] if shm_shape: self.shm_shape = shm_shape else: shm_width = int(shm_ini['shm_width']) shm_height = int(shm_ini['shm_height']) self.shm_shape = (shm_height, shm_width, 3) self.shm_arr = [] for i in range(self.shm_file_num): shm_name = self.prefix + str(i) shm_path = os.path.join('/dev/shm/', shm_name) if utils.file_exists(shm_path): os.remove(shm_path) shm_size = self.shm_shape[0] * self.shm_shape[1] * self.shm_shape[2] shm = shared_memory.SharedMemory(name=shm_name, create=True, size=shm_size) # shm = np.ndarray(self.shm_shape, dtype=np.uint8, buffer=shm.buf) self.shm_arr.append(shm)