def label(self, task_id, img_file_list, boundingbox): LOG.debug('label function called! labeling following files:') LOG.debug(img_file_list) cache_manager = cachemanagement.cacheManager() cur_dir =os.path.dirname(os.path.realpath(__file__)) # task_id is needed for separating labeling multiple objects cache_dir = os.path.dirname(os.path.realpath(__file__)) + '/tmp/s3/' cache_dir, img_file_list = cache_manager.cacheImageFromFileList(img_file_list, cache_dir) img_file_list = map(lambda x: cache_dir+'/'+x, img_file_list) input_img_file = cache_dir+'/'+ str(task_id) +'labeler_input_img_list.txt' MyUtils.writeListToFile(img_file_list, input_img_file) output_bx_file = cache_dir +'/'+ str(task_id) + 'labeler_output_bx_list.txt' cmd = cur_dir+'/TrackingApp ' cmd += str(input_img_file) + ' ' cmd += str(output_bx_file) + ' ' for bx_vec in boundingbox: cmd += str(bx_vec) + ' ' cmd += 'BOOSTING' # call tracker os.system(cmd) # get input output_bxes = [] with open(output_bx_file, 'r') as bx_file: bxes = bx_file.read().splitlines() for bx in bxes: LOG.debug(bx) output_bx = ast.literal_eval(bx) output_bxes.append(output_bx) return output_bxes
def label_old(self, task_id, img_file_list, boundingbox): LOG.debug('camshift labeler label function called! labeling following files:') LOG.debug(img_file_list) cache_manager = cachemanagement.cacheManager() cache_dir = os.path.dirname(os.path.realpath(__file__)) + '/tmp/s3' cache_dir, img_file_list = cache_manager.cacheImageFromFileList(img_file_list, cache_dir) img_file_list = map(lambda x: cache_dir+'/'+x, img_file_list) MyUtils.writeListToFile(img_file_list, cache_dir+'/'+'labeler_input_img_list.txt') initialized = False result_bounding_boxes=[boundingbox] for each_img_file in img_file_list: LOG.debug('read in image file' + each_img_file) self.frame = cv2.imread(each_img_file) vis = self.frame.copy() hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.))) if not initialized: # set up initial labeled img x0, y0, width, height = boundingbox x1 = x0 + width y1 = y0 + height self.track_window = (x0, y0, width, height) hsv_roi = hsv[y0:y1, x0:x1] mask_roi = mask[y0:y1, x0:x1] hist = cv2.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] ) cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX); self.hist = hist.reshape(-1) initialized = True continue # self.show_hist() prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1) prob &= mask term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ) track_box, self.track_window = cv2.CamShift(prob, self.track_window, term_crit) # pdb.set_trace() # get rectangle track_rect = cv2.boundingRect(cv2.boxPoints(track_box)) # try: # pt1 = (track_rect[0],track_rect[1]) # pt2 = (track_rect[0] + track_rect[2], track_rect[1] + track_rect[3]) # cv2.rectangle(vis, pt1, pt2, (0, 255, 0), 2) # cv2.ellipse(vis, track_box, (0, 0, 255), 2) # except: print track_rect result_bounding_boxes.append(list(track_rect)) return result_bounding_boxes