def string_to_ms(time): """Converts a string of real-world time into int of ms. Example inputs: 200ms 2s None If no "s" or "ms" is provided, this method assumes "milliseconds." If time is 'None' or a string of 'None', this method returns 0. Returns: An integer. The examples listed above 200, 2000 and 0, respectively """ time = str(time).upper() if time.endswith("MS") or time.endswith("MSEC"): time = ''.join(i for i in time if not i.isalpha()) return int(time) elif time.endswith("S") or time.endswith("SEC"): time = ''.join(i for i in time if not i.isalpha()) return int(float(time) * 1000) elif not time or time == 'NONE': return 0 else: time = ''.join(i for i in time if not i.isalpha()) return int(time)
def pprint_delta(delta): delta = str(delta) days = None s1 = delta.split(', ') if len(s1) > 1: days, time = s1 else: time = s1[0] time = time.split('.')[0] hour, minute, second = map(int, time.split(':')) time = '' if hour: time += '{hour} hour'.format(hour=hour) + ('s' if hour != 1 else '') if minute: if time and not time.endswith(', '): time += ', ' time += '{minute} minute'.format(minute=minute) + \ ('s' if minute != 1 else '') if second: if time and not time.endswith(', '): time += ', ' time += '{second} second'.format(second=second) + \ ('s' if second != 1 else '') ret = '' if days: ret = days + ', ' if time else '' ret += time return ret
def parseTime(time): if time.isdigit(): return int(time) table = { 's': 1, 'sec': 1, 'secs': 1, 'second': 1, 'seconds': 1, 'm': 60, 'min': 60, 'mins': 60, 'minute': 60, 'minutes': 60, 'h': 60 * 60, 'hour': 60 * 60, 'hours': 60 * 60 } for expr in table: if time.endswith(expr): firstPart = time[:-(len(expr))] if firstPart.isdigit(): return int(firstPart) * table[expr] return 3 * 60
def _getLastUpdateTime(self): time = '' with open(self._updateFile, 'r') as updateFile: time = updateFile.read() if time.endswith('\n'): time = time[:-1] return time
def _format_time_of_day(self, dt): ''' Format a time like "2pm" or "3:15pm". ''' fmt = dt.strftime('%l:%M%p').lstrip(' ') time, ampm = fmt[:-2], fmt[-2:].lower() if time.endswith(':00'): time = time[:-3] return time + ampm
def timeArg(arg): time = arg.strip() if not time: raise argparse.ArgumentTypeError("invalid duration") time = time.replace(',', '.') # allowing both , and . for decimal separator try: if time.endswith("ms"): time = float(time[:-2]) / 1000 elif time.endswith("s"): time = float(time[:-1]) elif time.endswith("min"): time = float(time[:-3]) * 60 elif time.endswith("h"): time = float(time[:-1]) * 3600 else: time = float(time) except: raise argparse.ArgumentTypeError("invalid duration '" + arg + "'") if time <= 0: raise argparse.ArgumentTypeError("invalid duration '" + arg + "'") return time
def google_strptime(time): """ Hack: Google always returns the time in the same locale. Sadly if the local system's locale is different, there isn't a way to perfectly handle the time. So instead we handle implement some time formatting """ abbrevTime = time[:-3] parsedTime = datetime.datetime.strptime(abbrevTime, "%m/%d/%y %I:%M") if time.endswith("PM"): parsedTime += datetime.timedelta(hours=12) return parsedTime
def google_strptime(time): """ Hack: Google always returns the time in the same locale. Sadly if the local system's locale is different, there isn't a way to perfectly handle the time. So instead we handle implement some time formatting """ try: abbrevTime = time[:-3] parsedTime = datetime.datetime.strptime(abbrevTime, "%m/%d/%y %I:%M") except: _moduleLogger.exception("Fooey, going with a dummy date") parsedTime = datetime.datetime(1, 1, 1) return parsedTime if time.endswith("PM"): parsedTime += datetime.timedelta(hours=12) elif time.endswith("AM"): pass else: _moduleLogger.error("Unknown time of date for %r" % time) return parsedTime
def time_convert(self, time): print "[YWeather] Time convert" tmp_time = '' if time.endswith('pm'): tmp_time = '%s:%s' % (int(time.split()[0].split(':')[0]) + 12, time.split()[0].split(':')[-1]) else: tmp_time = time.replace('am', '').strip() if len(tmp_time) is 4: return '0%s' % tmp_time else: return tmp_time
def time_convert(self, time): print("[YWeather] Time convert") tmp_time = '' if time.endswith('pm'): tmp_time = '%s:%s' % (int(time.split()[0].split(':')[0]) + 12, time.split()[0].split(':')[-1]) else: tmp_time = time.replace('am', '').strip() if len(tmp_time) is 4: return '0%s' % tmp_time else: return tmp_time
def timeArg(arg): time = arg.strip() if not time: raise argparse.ArgumentTypeError("invalid duration") time = time.replace(",", ".") # allowing both , and . for decimal separator try: if time.endswith("ms"): time = float(time[:-2]) / 1000 elif time.endswith("s"): time = float(time[:-1]) elif time.endswith("min"): time = float(time[:-3]) * 60 elif time.endswith("h"): time = float(time[:-1]) * 3600 else: time = float(time) except: raise argparse.ArgumentTypeError("invalid duration '" + arg + "'") if time <= 0: raise argparse.ArgumentTypeError("invalid duration '" + arg + "'") return time
def parse_time(time): global days_of_week if time.endswith(u"時限"): day_of_week = days_of_week[time[0]] start_period = end_period = to_half_width(time[1]) elif "-" in time or u"−" in time: day_of_week = days_of_week.get(time[0], None) start_period = to_half_width(time[1]) end_period = to_half_width(time[3]) else: start_period = end_period = day_of_week = None return (day_of_week, start_period, end_period)
def string_to_ms(time): """Converts a string of real-world time into int of ms. Example inputs: 200ms 2s If no "s" or "ms" is provided, we assume "milliseconds" returns an integer 200 or 2000, respectively """ time = str(time).upper() if time.endswith("MS") or time.endswith("MSEC"): time = ''.join(i for i in time if not i.isalpha()) return int(time) elif time.endswith("S") or time.endswith("SEC"): time = ''.join(i for i in time if not i.isalpha()) return int(float(time) * 1000) else: time = ''.join(i for i in time if not i.isalpha()) return float(time)
def convertTo24(self, time): if time == "12:00PM" or time == "Noon": return 1200 if time == "Midnight": return 0 hourMin = time[:-2] hourMin = hourMin.split(':') hour = int(hourMin[0]) minutes = 0 if len(hourMin) > 1: minutes = int(hourMin[1]) if time.endswith("PM"): hour = hour + 12 elif hour is 12: hour = 0 return hour * 100 + minutes
def parse_time(time): """Given the time string, parse and turn into normalised minute count""" if time.endswith('m'): time = time[0:-1] if time.find('h') != -1: time = time.replace('h', ':') if time.find(':') != -1: hours, minutes = time.split(':') if minutes.strip() == "": minutes = 0 else: hours = 0 minutes = int(time.strip()) total = int(minutes) + (int(hours) * 60) return total
def readInput(userInput): global state global curQst global running if(userInput=='shutdown'): running = False if(userInput=='help'): os.system('cls') help() if(state=='questMenu'): command = userInput.replace('\n',' ').split(' ') if(len(command)>1): if(command[0]=='add'): if(len(command)==3): if(command[2]!='HIGH' and command[2]!='MED' and command[2]!='LOW'): print("[ERROR]: Invalid Priority Value ") input() else: addQuest(command[1],command[2]) open("db/"+command[1]+".qst",'w') if(command[0]=='delete'): if(len(command)==2): deleteQst(command[1]) if(command[0]=='start'): if(len(command)==2): if(isIdExist(command[1],'fileQst')): print("[Enter Time] (1d = 1day, 1m = 1min, 1s = 1second):",end=' ') time = input() if(time.endswith('s')): time = time.replace('s','') time = float(time) initSPRINT(command[1]) updateQst('fileQst',command[1],4,time) elif(time.endswith('m')): time = time.replace('m','') time = float(time) time = time*60 initSPRINT(command[1]) updateQst('fileQst',command[1],4,time) elif(time.endswith('h')): time = time.replace('h','') time = float(time) time = time*3600 initSPRINT(command[1]) updateQst('fileQst',command[1],4,time) elif(time.endswith('d')): time = time.replace('d','') time = float(time) time = (time*3600)*24 initSPRINT(command[1]) updateQst('fileQst',command[1],4,time) else: if(userInput=='sort'): sortQst() elif(userInput=='add'): print("[Name] : ",end='') name = input() if(len(name)>15): print("[ERROR]: Item name is too long") input() else: print("\t [a] - HIGH") print("\t [b] - MED") print("\t [c] - LOW") print("[Prio] : ",end='') prio = input() if(prio=='a'): prio = 'HIGH' if(prio=='b'): prio = 'MED' if(prio=='c'): prio = 'LOW' addQuest(name,prio) open("db/"+name+".qst",'w') elif(state=='help'): print("sample") else: if(isIdExist(userInput,'fileQst')): state='questBoard' curQst = getName(userInput) trials = int(getVal('fileQst',userInput,3))+1 updateQst('fileQst',userInput,3,trials) if(state=='questBoard'): if(userInput=='back'): state='questMenu' else: if(userInput=='a'): state='questItemsQUEST' if(userInput=='b'): state='questItemsSPRINT' if(userInput=='c'): state='questItemsWIP' if(userInput=='d'): state='questItemsDONE' if(state=='questItemsQUEST' or state=='questItemsSPRINT' or state=='questItemsWIP' or state=='questItemsDONE'): command = userInput.replace('\n',' ').split(' ') if(len(command)>1): if(command[0]=='add'): if(len(command)==3): if(command[2]!='HIGH' and command[2]!='MED' and command[2]!='LOW'): print("[ERROR]: Invalid Priority Value ") input() else: addQuestList(command[1],command[2]) if(command[0]=='delete'): if(len(command)==2): deleteQstList(command[1]) else: if(userInput=='sort'): sortQstList() if(userInput=='back'): state='questBoard' elif(userInput=='add'): print("[Name] : ",end='') name = input() if(len(name)>45): print("[ERROR]: Item name is too long") input() else: print("\t [a] - HIGH") print("\t [b] - MED") print("\t [c] - LOW") print("[Prio] : ",end='') prio = input() if(prio=='a'): prio = 'HIGH' if(prio=='b'): prio = 'MED' if(prio=='c'): prio = 'LOW' addQuestList(name,prio) else: id = userInput if(isIdExist(id,curQst)): print("[MOVE] :") print("a - Quest List") print("b - Sprint") print("c - WIP") print("d - Done") userInput = input() if(userInput=='a'): updateQstList(curQst,id,3,'QUEST') if(userInput=='b'): updateQstList(curQst,id,3,'SPRINT') if(userInput=='c'): if(countBoard('WIP')==0): updateQstList(curQst,id,3,'WIP') initTime(id) if(userInput=='d'): updateQstList(curQst,id,3,'DONE')
def report(self, tracker_names, attribute_name): """ Evaluate the tracker on VideoCube subset. """ assert isinstance(tracker_names, (list, tuple)) if self.subset == 'test': pwd = os.getcwd() # generate compressed submission file for each tracker for tracker_name in tracker_names: # compress all tracking results result_dir = os.path.join(self.result_dir, tracker_name, self.subset) time_dir = os.path.join(self.time_dir, tracker_name, self.subset) submission_dir = os.path.join(self.result_dir, tracker_name, 'submission') makedir(submission_dir) makedir(os.path.join(submission_dir, 'result')) makedir(os.path.join(submission_dir, 'time')) for result in sorted(os.listdir(result_dir)): if result.endswith('_%s.txt' % self.repetition): src_path = os.path.join(result_dir, result) dst_path = os.path.join(submission_dir, 'result', result[:-6] + '.txt') print('Copy result to {}'.format(dst_path)) shutil.copyfile(src_path, dst_path) for time in sorted(os.listdir(time_dir)): if time.endswith('_%s.txt' % self.repetition): src_path = os.path.join(time_dir, time) dst_path = os.path.join(submission_dir, 'time', time[:-6] + '.txt') print('Copy result to {}'.format(dst_path)) shutil.copyfile(src_path, dst_path) compress(submission_dir, submission_dir) print('Records saved at', submission_dir + '.zip') # print submission guides print('\033[93mLogin and follow instructions on') print('http://videocube.aitestunion.com/submit_supports') print('to upload and evaluate your tracking results\033[0m') # switch back to previous working directory os.chdir(pwd) return None subset_report_dir = os.path.join(self.report_dir, self.subset) makedir(subset_report_dir) subset_analysis_dir = os.path.join(self.analysis_dir, self.subset) makedir(subset_analysis_dir) report_dir = os.path.join(subset_report_dir, tracker_names[0]) makedir(report_dir) performance = {} for name in tracker_names: single_report_file = os.path.join( subset_analysis_dir, '{}_{}_{}_{}.json'.format(name, self.subset, attribute_name, str(self.repetition))) if os.path.exists(single_report_file): f = open(single_report_file, 'r', encoding='utf-8') single_performance = json.load(f) performance.update({name: single_performance}) f.close() print('Existing result in {}'.format(name)) continue else: performance.update({name: {'overall': {}, 'seq_wise': {}}}) seq_num = len(self.dataset) # save the ious, dious and gious for success plot succ_curve = np.zeros((seq_num, self.nbins_iou)) succ_dcurve = np.zeros((seq_num, self.nbins_iou)) succ_gcurve = np.zeros((seq_num, self.nbins_iou)) # save the original precision value for original precision plot prec_curve = np.zeros((seq_num, self.nbins_ce)) # save the novel precision value for normalized precision plot norm_prec_curve = np.zeros((seq_num, self.nbins_ce)) # save average speed for each video speeds = np.zeros(seq_num) # save the normalize precision score norm_prec_score = np.zeros(seq_num) for s in range(len(self.dataset.seq_names)): num = self.dataset.seq_names[s] # get the information of selected video img_files, anno, _ = self.dataset[s] print( 'repetition {}: Evaluate tracker {} in video num {} with {} attribute ' .format(self.repetition, name, num, attribute_name)) # read absent info absent_path = os.path.join(self.root_dir, 'attribute', 'absent', '{}.txt'.format(num)) absent = pd.read_csv(absent_path, header=None, names=['absent']) # read shotcut info shotcut_path = os.path.join(self.root_dir, 'attribute', 'shotcut', '{}.txt'.format(num)) shotcut = pd.read_csv(shotcut_path, header=None, names=['shotcut']) # read attribute info if attribute_name != 'normal': attribute_path = os.path.join(self.root_dir, 'attribute', attribute_name, '{}.txt'.format(num)) attribute = pd.read_csv(attribute_path, header=None, names=[attribute_name]) # frame resolution img_height = cv.imread(img_files[0]).shape[0] img_width = cv.imread(img_files[0]).shape[1] img_resolution = (img_width, img_height) bound = img_resolution # read tracking results boxes = np.loadtxt(os.path.join( self.result_dir, name, self.subset, '{}_{}_{}.txt'.format(name, num, self.repetition)), delimiter=',') anno = np.array(anno) boxes = np.array(boxes) for box in boxes: # correction of out-of-range coordinates box[0] = box[0] if box[0] > 0 else 0 box[2] = box[2] if box[ 2] < img_width - box[0] else img_width - box[0] box[1] = box[1] if box[1] > 0 else 0 box[3] = box[3] if box[ 3] < img_height - box[1] else img_height - box[1] assert boxes.shape == anno.shape # calculate ious, gious, dious for success plot # calculate center errors and normalized center errors for precision plot seq_ious, seq_dious, seq_gious, seq_center_errors, seq_norm_center_errors, flags = self._calc_metrics( boxes, anno, bound) seq_ious = pd.DataFrame(seq_ious, columns=['seq_ious']) seq_dious = pd.DataFrame(seq_dious, columns=['seq_dious']) seq_gious = pd.DataFrame(seq_gious, columns=['seq_gious']) seq_center_errors = pd.DataFrame(seq_center_errors, columns=['seq_center_errors']) seq_norm_center_errors = pd.DataFrame( seq_norm_center_errors, columns=['seq_norm_center_errors']) flags = pd.DataFrame(flags, columns=['flags']) if attribute_name != 'normal': data = pd.concat([ seq_ious, seq_dious, seq_gious, seq_center_errors, seq_norm_center_errors, flags, absent, shotcut, attribute ], axis=1) else: data = pd.concat([ seq_ious, seq_dious, seq_gious, seq_center_errors, seq_norm_center_errors, flags, absent, shotcut ], axis=1) # Frames without target and transition frames are not included in the evaluation data = data[data.apply( lambda x: x['absent'] == 0 and x['shotcut'] == 0, axis=1)] if attribute_name != 'normal': # Pick frames with difficult attributes data = self.select_attribute_frames(data, attribute_name) data = data.drop(labels=['absent', 'shotcut'], axis=1) seq_ious = data['seq_ious'] seq_dious = data['seq_dious'] seq_gious = data['seq_gious'] seq_center_errors = data['seq_center_errors'] seq_norm_center_errors = data['seq_norm_center_errors'] flags = data['flags'] # Calculate the proportion of all the frames that fall into area 5 (groundtruth area) norm_prec_score[s] = np.nansum(flags) / len(flags) # Save the 5 curves of the tracker on the current video succ_curve[s], succ_dcurve[s], succ_gcurve[s], prec_curve[ s], norm_prec_curve[s] = self._calc_curves( seq_ious, seq_dious, seq_gious, seq_center_errors, seq_norm_center_errors) # calculate average speed time_file = os.path.join( self.time_dir, name, self.subset, '{}_{}_{}.txt'.format(name, num, self.repetition)) if os.path.isfile(time_file): times = np.loadtxt(time_file) times = times[times > 0] if len(times) > 0: speeds[s] = np.nanmean(1. / times) # Update the results in current video (Only save scores) performance[name]['seq_wise'].update({ num: { 'success_score_iou': np.nanmean(succ_curve[s]), 'success_score_diou': np.nanmean(succ_dcurve[s]), 'success_score_giou': np.nanmean(succ_gcurve[s]), 'precision_score': prec_curve[s][self.ce_threshold], 'norm_prec_score': norm_prec_score[s], 'success_rate_iou': succ_curve[s][self.nbins_iou // 2], 'success_rate_diou': succ_dcurve[s][self.nbins_iou // 2], 'success_rate_giou': succ_gcurve[s][self.nbins_iou // 2], 'speed_fps': speeds[s] if speeds[s] > 0 else -1 } }) # Average each curve succ_curve = np.nanmean(succ_curve, axis=0) succ_dcurve = np.nanmean(succ_dcurve, axis=0) succ_gcurve = np.nanmean(succ_gcurve, axis=0) prec_curve = np.nanmean(prec_curve, axis=0) norm_prec_curve = np.nanmean(norm_prec_curve, axis=0) # Generate average score succ_score = np.nanmean(succ_curve) succ_dscore = np.nanmean(succ_dcurve) succ_gscore = np.nanmean(succ_gcurve) succ_rate = succ_curve[self.nbins_iou // 2] succ_drate = succ_dcurve[self.nbins_iou // 2] succ_grate = succ_gcurve[self.nbins_iou // 2] prec_score = prec_curve[self.ce_threshold] norm_prec_score = np.nansum(norm_prec_score) / np.count_nonzero( norm_prec_score) if np.count_nonzero(speeds) > 0: avg_speed = np.nansum(speeds) / np.count_nonzero(speeds) else: avg_speed = -1 # store overall performance performance[name]['overall'].update({ 'success_curve_iou': succ_curve.tolist(), 'success_curve_diou': succ_dcurve.tolist(), 'success_curve_giou': succ_gcurve.tolist(), 'precision_curve': prec_curve.tolist(), 'normalized_precision_curve': norm_prec_curve.tolist(), 'success_score_iou': succ_score, 'success_score_diou': succ_dscore, 'success_score_giou': succ_gscore, 'precision_score': prec_score, 'norm_prec_score': norm_prec_score, 'success_rate_iou': succ_rate, 'success_rate_diou': succ_drate, 'success_rate_giou': succ_grate, 'speed_fps': avg_speed }) with open(single_report_file, 'w') as f: json.dump(performance[name], f, indent=4) # save performance report_file = os.path.join( report_dir, '{}_performance_{}.json'.format(attribute_name, self.repetition)) with open(report_file, 'w') as f: json.dump(performance, f, indent=4) self.plot_curves_([report_file], tracker_names, attribute_name, self.repetition) return performance