class Boost(AbstractClassifier): classifier = cv2.Boost() def train(self, samples, responses): tflag = cv2.CV_ROW_SAMPLE self.classifier.train(np.array(samples, dtype=np.float32), tflag, np.array(responses, dtype=np.float32))
def load_segmentation(self): if self.boosting: # Classifiers should be located in the ml_classifiers folder. # The rosparam should be `COLOR_classifier` path = os.path.join(rospack.get_path('sub8_perception'), 'ml_classifiers/buoys/{}/{}'.format(self.color, rospy.get_param("/buoys/{}_classifier".format(self.color)))) self.boost = cv2.Boost() rospy.loginfo("BUOY - Loading {} boost...".format(self.color)) self.boost.load(path) rospy.loginfo("BUOY - Classifier for {} buoy loaded.".format(self.color)) else: # Give either an HSV or BGR threshold with param name /COLOR/COLOR_SPACE_low and /COLOR/COLOR_SPACE_high for color_space in ['hsv', 'bgr']: self.color_space = color_space low = '/color/buoy/{}/{}_low'.format(self.color, color_space) high = '/color/buoy/{}/{}_high'.format(self.color, color_space) if not rospy.has_param(low): # Using a different colorspace continue rospy.loginfo("BUOY - Loading {} thresholds for {} buoy...".format(color_space, self.color)) self.thresholds = [np.array(rospy.get_param(low)), np.array(rospy.get_param(high))] rospy.loginfo("BUOY - Thresholds for {} buoy loaded.".format(self.color))
def learn_action_unit(self, auid): booster = cv2.Boost() feature_vectors = [] if auid == 1 and MULTITHREAD: self.say("Learning all action units...") elif not MULTITHREAD: self.say("Learning action unit %d..." % (auid, )) for session in AU_POSITIVE_SAMPLES[auid]: if auid == 1 or not MULTITHREAD: self.say("\tLearning session %s" % (session, )) feature_vector = self.generate_features_from_session(session, auid) if feature_vector: feature_vectors.append(feature_vector) positive_sample_count = len(feature_vectors) for session in AU_NEGATIVE_SAMPLES[auid]: if auid == 1 or not MULTITHREAD: self.say("\tLearning session %s" % (session, )) feature_vector = self.generate_features_from_session(session, auid) if feature_vector: feature_vectors.append(feature_vector) negative_sample_count = len(feature_vectors) - positive_sample_count classes = [1 for p in range(positive_sample_count)] + \ [0 for n in range(negative_sample_count)] var_types = np.array([cv2.CV_VAR_NUMERICAL] * len(feature_vectors[0])\ + [cv2.CV_VAR_CATEGORICAL], np.uint8) if auid == 1 or not MULTITHREAD: self.say("\tBoosting...") feature_vectors = np.array(feature_vectors) feature_vectors = np.float32(feature_vectors) classes = np.array(classes) result = booster.train(feature_vectors, cv2.CV_ROW_SAMPLE, classes, varType=var_types, params=BOOST_PARAMS) if auid == 1 or not MULTITHREAD: self.say("\tBoosted: %s" % (str(result), )) if not result: print len(feature_vectors), feature_vectors print len(classes), classes exit() booster.save( os.path.join(paths.TRAINING_OUTPUT_PATH, "au%d" % (auid, ))) booster.clear()
def trainBoost(input_, response_): logger.info('...training boosted classifier') boostParams = dict(boost_type = cv2.BOOST_REAL, weak_count = 100, weight_trim_rate = 0.95, cv_folds = 3, max_depth = 1) boost = cv2.Boost() boost.train(trainData=input_, tflag=cv2.CV_ROW_SAMPLE, responses=response_, params=boostParams, update=False) return boost
def __init__(self, inLength, outLength): super(MyBoost, self).__init__() self.in_length = inLength self.out_length = outLength self.model = cv2.Boost() self.params = dict(max_depth=1) self.var_type = np.array([cv2.CV_VAR_NUMERICAL] * self.in_length + [cv2.CV_VAR_CATEGORICAL], dtype=np.uint8)
def __init__(self, *args, **kwargs): super(FacialFeatureDetector, self).__init__(self, *args, **kwargs) self.boosters = [] # Load all the classifiers once ahead of time because it takes a while for i in range(len(EXAMINED_POINTS)): self.say("Loading classifier for facial feature %d" % (i,)) booster = cv2.Boost() ff_learned_data = "%s%d" % ("feature", i) booster.load(os.path.join(paths.TRAINING_OUTPUT_PATH, ff_learned_data)) self.boosters.append(booster)
def __init__(self): self.transformer = tf.TransformListener() rospy.sleep(1.0) self.done_once = False self.last_image = None self.last_draw_image = None self.last_poop_image = None self.last_image_time = None self.camera_model = None self.last_t = None self.observations = deque() self.pose_pairs = deque() self.rviz = rviz.RvizVisualizer() self.pose2d_service = rospy.Service('vision/buoys/2D', VisionRequest2D, self.request_buoy) self.pose_service = rospy.Service('vision/buoys/pose', VisionRequest, self.request_buoy3d) self.image_sub = sub8_ros_tools.Image_Subscriber('/stereo/right/image_rect_color', self.image_cb) self.image_pub = sub8_ros_tools.Image_Publisher('/vision/buoy_2d/target_info') # Occasional status publisher self.timer = rospy.Timer(rospy.Duration(1.0), self.publish_target_info) rospack = rospkg.RosPack() boost_path = os.path.join( rospack.get_path('sub8_perception'), 'sub8_vision_tools', 'classifiers', 'boost.cv2' ) self.boost = cv2.Boost() rospy.loginfo("Loading boost") self.boost.load(boost_path) rospy.loginfo("Boost loaded") self.buoys = { 'green': '/color/buoy/green', 'red': '/color/buoy/red', 'yellow': '/color/buoy/yellow', } self.ppf = None self.multi_obs = None self.draw_colors = { 'green': (0.0, 1.0, 0.0, 1.0), 'red': (1.0, 0.0, 0.0, 1.0), 'yellow': (1.0, 1.0, 0.0, 1.0), }
def __init__(self, *args, **kwargs): super(ActionUnitDetector, self).__init__(self, *args, **kwargs) self.set_sequence(kwargs.pop('sequence', None)) self.ffdetector = FacialFeatureDetector(verbose=self.verbose) self.boosters = [] # Load all the classifiers once ahead of time because it takes a while for number in AU_LABELS.keys(): if self.verbose: self.say("Loading classifier for action unit %d" % (number,)) booster = cv2.Boost() au_data = "%s%d" % ("au", number) booster.load(os.path.join(paths.TRAINING_OUTPUT_PATH, au_data)) self.boosters.append(booster)
def boost(data, responses, weak_count=100, max_depth=20, boost_type=cv2.BOOST_DISCRETE): ''' Auto trains an OpenCV SVM. ''' np.float32(data) np.float32(responses) params = dict(boost_type=boost_type, weak_count=weak_count, max_depth=max_depth) model = cv2.Boost() model.train(data, cv2.CV_ROW_SAMPLE, responses, params=params) return StatsModelWrapper(model)
def train_classifier(x, y): n_trees = 5 max_depth = 3 parameters = { # "boost_type": cv2.BOOST_REAL, "boost_type": cv2.BOOST_GENTLE, # "boost_type": cv2.BOOST_DISCRETE, "weak_count": n_trees, "weight_trim_rate": 0, "max_depth": max_depth } boost = cv2.Boost() print 'Training...' boost.train(x, cv2.CV_ROW_SAMPLE, y, params=parameters) return boost
def train_on_data(observation_list, label_list, split_factor=4): ''' `to_train` should be the number of images to train on out of each group of 10. This lets us only train on some of the segmented images and then test on the rest. ''' assert len(observation_list) / split_factor is int print "Done! Training on: {} images.".format(len(observation_list)) all_observations = np.vstack(observation_list) all_labels = np.vstack(label_list) all_observations_split = np.vsplit(all_observations, split_factor) all_labels_split = np.vsplit(all_labels, split_factor) print print "Building classifier..." print param_gen = gen_data() for m, t, d, n in param_gen: f_name = "{}_{}tree_{}depth.dic".format(n, t, d) print "=====================" print "Generating {}...".format(f_name) boost = cv2.Boost() parameters = { "boost_type": m, "weak_count": t, "weight_trim_rate": 0, "max_depth": d } s_time = time.time() process_round = 0 # Split this into multiple passes in an attempt to free RAM (no idea if this works). for x, y in zip(all_observations_split, all_labels_split): print "Training subset {}/{}.".format(process_round + 1, split_factor) boost.train(x, cv2.CV_ROW_SAMPLE, y, params=parameters) process_round += 1 print "Time to complete: {}".format(time.time() - s_time) print "Done! Saving..." boost.save(f_name, 's') print
def determine_aus(self, initial_points, final_points): """ Runs the AU classifiers on sets of points and returns a tuple of the active action units. """ self.say("Determining AUs from acquired facial landmark data") booster = cv2.Boost() active_aus = [] for i, booster in enumerate(self.boosters): feature_vector = utils.distances(initial_points, final_points) feature_vecotr = np.array(feature_vector) feature_vector = np.float32(feature_vector) guess = booster.predict(feature_vector) if guess == True: active_aus.append(AU_ZERO_INDEX_MAPPING[i]) return active_aus
def __init__(self): self.model = cv2.Boost() self.class_n = 2
evalfun('svm', y_val_svm, test_labels, test_number_of_images) #######RTrees########## modelRTtree = cv2.RTrees() sample_n, var_n = train_images.shape var_types = numpy.array([cv2.CV_VAR_NUMERICAL] * var_n + [cv2.CV_VAR_CATEGORICAL], numpy.uint8) params = dict(max_depth=10) modelRTtree.train(train_images, cv2.CV_ROW_SAMPLE, train_labels, varType=var_types, params=params) y_val_RTtree = numpy.float32([modelRTtree.predict(s) for s in test_images]) evalfun('RTtree', y_val_RTtree, test_labels, test_number_of_images) #######Boost######### modelBoost = cv2.Boost() sample_n, var_n = train_images.shape new_train_images = unroll_samples(train_images) new_train_labels = unroll_responses(train_labels) var_types = numpy.array([cv2.CV_VAR_NUMERICAL] * var_n + [cv2.CV_VAR_CATEGORICAL, cv2.CV_VAR_CATEGORICAL], numpy.uint8) params = dict(max_depth=5) #, use_surrogates=False) modelBoost.train(new_train_images, cv2.CV_ROW_SAMPLE, new_train_labels, varType=var_types, params=params) new_test_images = unroll_samples(test_images) y_val_Boost = numpy.array( [modelBoost.predict(s, returnSum=True) for s in new_test_images])
def evalClassifier(classifier_, data_, resp_, showMatrix_ = True, showStats_ = True): issvm = isinstance(classifier_, type(cv2.SVM())) isbst = isinstance(classifier_, type(cv2.Boost())) isnet = isinstance(classifier_, type(cv2.ANN_MLP())) tp = 0 tn = 0 fp = 0 fn = 0 total = len(data_) for i in range(total): sample = numpy.array([data_[i]], dtype = numpy.float32) # predict an output pred = -1 if issvm: dist = classifier_.predict(sample, returnDFVal=True) label = classifier_.predict(sample, returnDFVal=False) pred = int(label) logger.debug('resp: %d - label: %.1f - dist: % .3f', resp_[i], label, dist) elif isbst: label = classifier_.predict(sample, returnSum=False) votes = classifier_.predict(sample, returnSum=True) pred = int(label) logger.debug('resp: %d - label: %.1f - votes: % .3f', resp_[i], label, votes) elif isnet: dummy, out = classifier_.predict(sample) pred = int(out > 0) logger.debug('resp: %d - out: % .3f', resp_[i], out) # accumulate stats if pred == 0: # predicted 0 if resp_[i] == 0: tn = tn + 1 else: fn = fn + 1 else: # predicted 1 if resp_[i] == 1: tp = tp + 1 else: fp = fp + 1 realn = tn + fp realp = fn + tp # generate a confusion matrix matrix = [] matrix.append('-------------------------------------') matrix.append('| | pred: 0 | pred: 1 | SUM |') matrix.append('-------------------------------------') matrix.append('| resp: 0 | {:3n} | {:3n} | {:3n} |'.format(tn, fp, realn)) matrix.append('| resp: 1 | {:3n} | {:3n} | {:3n} |'.format(fn, tp, realp)) matrix.append('-------------------------------------') matrix.append('| SUM | {:3n} | {:3n} | {:3n} |'.format(tn + fn, fp + tp, total)) matrix.append('-------------------------------------') stats = [] if showStats_: with numpy.errstate(invalid='ignore'): # temporarily disable warnings stats.append(' accuracy: {: .3f}'.format((tp + tn) / numpy.float64(total))) stats.append(' missclass: {: .3f}'.format((fp + fn) / numpy.float64(total))) stats.append(' TPR: {: .3f}'.format(tp / numpy.float64(realp))) stats.append(' FPR: {: .3f}'.format(fp / numpy.float64(realn))) stats.append(' specificity: {: .3f}'.format(tn / numpy.float64(realn))) stats.append(' precision: {: .3f}'.format(tp / numpy.float64(tp + fp))) stats.append(' prevalence: {: .3f}'.format(realp / numpy.float64(total))) nmatrix = len(matrix) nstats = len(stats) rprt = '\n' for i in range(nmatrix): if showMatrix_: rprt = rprt + matrix[i] if showStats_ and i < nstats: rprt = rprt + stats[i] rprt = rprt + '\n' logger.info(rprt)
params=rtree_params) results = np.zeros(y_test.shape, dtype=y_test.dtype) for i in xrange(y_test.shape[0]): results[i, 0] = RTtree.predict(x_test[i, :]) acurracy = (y_test == results) print "RTtree acurracy = ", np.mean(acurracy) RTtree_result = results boost_params = dict(max_depth=1) var_type = np.array([cv2.CV_VAR_NUMERICAL] * 81 + [cv2.CV_VAR_CATEGORICAL], dtype=np.uint8) Boost = cv2.Boost() Boost.train(x_train, cv2.CV_ROW_SAMPLE, y_train, varType=var_type, params=boost_params) results = np.zeros(y_test.shape, dtype=y_test.dtype) for i in xrange(y_test.shape[0]): results[i, 0] = Boost.predict(x_test[i, :], returnSum=True) for i in xrange(y_test.shape[0]): if results[i, 0] > 0: results[i, 0] = 1 else:
def run_evaluation(inputDir, outputDir, process_color = 0, processTest = 0): if not os.path.exists(outputDir): os.mkdir(outputDir) edgeThreshold = 14 fastex = FASTex(edgeThreshold = edgeThreshold) modelFile = '/home/busta/outModel.boost' model = cv2.Boost() model.load(modelFile) images = glob.glob('{0}/*.jpg'.format(inputDir)) segmDir = '{0}/segmentations'.format(inputDir) precision = 0; precisionDen = 0 recall = 0 recall05 = 0 recallNonMax = 0 recallDen = 0 wordRecall = 0 wordRecallDen = 0 segm2chars = 0 regionsCount = 0 regionsCountNonMax = 0 missing_segmNonMaxCount = 0 letterKeypointHistogram = defaultdict(lambda : defaultdict(float)) octaveLetterKeypointHistogram = defaultdict(lambda : defaultdict(float)) missing_letters = {} letterHistogram = defaultdict(int) missing_segm = {} missing_segm2 = {} missing_segmNonMax = {} diffMaxOctavesMap = {} diffScoreOctavesMap = {} segmHistogram = [] segmWordHistogram = [] results = [] hist = None histFp = None histDist = None histDistFp = None histDistMax = None histDistMaxWhite = None histDistMaxFp = None hist2dDist =None hist2dDistFp = None hist2dDistScore = None hist2dDistScoreFp = None histDistMaxWhiteFp = None histSegm = np.zeros((256), dtype = np.float) histSegmCount = np.zeros((256), dtype = np.int) stat = np.asarray([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=np.float) times = [] gtSegmCount = 0 wordsOk = [] wordsFp = [] keypointsTotal = 0 keypointsTotalInside = 0 orbTime = 0 lineNo = 0 perfectWords = 0; perfectWordsNS = 0; hasSegm = False for image in images: print('Processing {0}'.format(image)) img = cv2.imread(image, 0) imgc = cv2.imread(image) imgcO = cv2.imread(image) if process_color == 1: imgproc = imgc else: imgproc = img baseName = os.path.basename(image) baseName = baseName[:-4] workPoint = 0.3 segmentations = fastex.getCharSegmentations(imgproc, outputDir, baseName) segmentations = segmentations[:, 0:10] segmentations = np.column_stack( [ segmentations , np.zeros( (segmentations.shape[0], 2), dtype = np.float ) ] ) maskDuplicates = segmentations[:, 8] == -1 segmentationsDuplicates = segmentations[maskDuplicates, :] maskNoNei = segmentationsDuplicates[:, 9] > workPoint segmentationsNoNei = segmentationsDuplicates[maskNoNei, :] if segmentations.shape[0] > 0: print( 'Dupl ratio: {0} - {1}/ {2} - {3}'.format(segmentationsDuplicates.shape[0] / float(segmentations.shape[0]), segmentationsDuplicates.shape[0], segmentations.shape[0], segmentationsNoNei.shape[0] ) ) keypoints = fastex.getLastDetectionKeypoints() keypointsTotal += keypoints.shape[0] statc = fastex.getDetectionStat() times.append([ statc[1], statc[2], statc[3], statc[4], statc[5], statc[6], statc[7], statc[8], statc[9], statc[10]]) stat += statc values = img[ keypoints[:, 1].astype(int), keypoints[:, 0].astype(int) ] valuesMax = img[keypoints[:, 6].astype(int), keypoints[:, 5].astype(int)] diffValMax = np.abs(values - valuesMax) regionsCount += segmentations.shape[0] regionsCountNonMax += segmentationsNoNei.shape[0] segmentations[:, 2] += segmentations[:, 0] segmentations[:, 3] += segmentations[:, 1] keypointsOrb = fastex.getLastDetectionOrbKeypoints() orbTime += keypointsOrb[0][9] segmGt = '{0}/{1}_GT.txt'.format(segmDir, baseName) pden = 0 rden = 0 if os.path.exists(segmGt): hasSegm = True (gt_rects, groups) = utls.read_icdar2013_segm_gt(segmGt) segmImg = '{0}/{1}_GT.bmp'.format(segmDir, baseName) if not os.path.exists(segmImg): segmImg = '{0}/gt_{1}.png'.format(segmDir, baseName) segmImg = cv2.imread(segmImg) try: (hist, histFp, histDist, histDistMax, histDistMaxWhite, hist2dDist, hist2dDistScore, histDistFp, histDistMaxFp, histDistMaxWhiteFp, hist2dDistFp, hist2dDistScoreFp, keypointsInside) = collect_histograms(img, segmImg, keypoints, values, diffValMax, keypointsTotalInside, diffMaxOctavesMap, diffScoreOctavesMap, hist, histFp, histDist, histDistMax, histDistMaxWhite, hist2dDist, hist2dDistScore, histDistFp, histDistMaxFp, histDistMaxWhiteFp, hist2dDistFp, hist2dDistScoreFp) except: pass rcurrent = 0 rcurrent05 = 0 rcurrentNonMax = 0 for k in range(len(gt_rects)): gt_rect = gt_rects[k] best_match = 0 best_match_line = 0 if (gt_rect[4] == ',' or gt_rect[4] == '.' or gt_rect[4] == '\'' or gt_rect[4] == ':' or gt_rect[4] == '-') and not evalPunctuation: continue gtSegmCount += 1 rectMask = np.bitwise_and(np.bitwise_and( keypointsInside[:, 0] >= gt_rect[0], keypointsInside[:, 0] <= gt_rect[2]), np.bitwise_and(keypointsInside[:, 1] >= gt_rect[1], keypointsInside[:, 1] <= gt_rect[3])) letterInside = keypointsInside[rectMask, :] #make keypoints histogram if letterInside.shape[0] > 0: octaves = np.unique( letterInside[:, 2]) maxOctave = np.max(octaves) maxOctavePoints = 0 for i in range(int(maxOctave) + 1): octavePoints = letterInside[letterInside[:, 2] == i, :] maxOctavePoints = max(maxOctavePoints, octavePoints.shape[0]) if maxOctavePoints > 0: octaveLetterKeypointHistogram[gt_rect[4]][0] += 1 if maxOctavePoints > 1: octaveLetterKeypointHistogram[gt_rect[4]][1] += 1 if maxOctavePoints > 2: octaveLetterKeypointHistogram[gt_rect[4]][2] += 1 if maxOctavePoints > 3: octaveLetterKeypointHistogram[gt_rect[4]][3] += 1 if letterInside.shape[0] == 0: if not missing_letters.has_key(gt_rect[4]): missing_letters[gt_rect[4]] = [] missing_letters[gt_rect[4]].append( (image, gt_rect) ) if letterInside.shape[0] > 0: letterKeypointHistogram[gt_rect[4]][0] += 1 if letterInside.shape[0] > 1: letterKeypointHistogram[gt_rect[4]][1] += 1 if letterInside.shape[0] > 2: letterKeypointHistogram[gt_rect[4]][2] += 1 if letterInside.shape[0] > 3: letterKeypointHistogram[gt_rect[4]][3] += 1 letterHistogram[gt_rect[4]] += 1 best_match2 = 0 minSingleOverlap = MIN_SEGM_OVRLAP if gt_rect[4] == 'i' or gt_rect[4] == '!': minSingleOverlap = 0.5 for detId in range(segmentations.shape[0]): rectn = segmentations[detId, :] rect_int = utils.intersect( rectn, gt_rect ) int_area = utils.area(rect_int) union_area = utils.area(utils.union(rectn, gt_rect)) ratio = int_area / float(union_area) rectn[10] = max(ratio, rectn[10]) if rectn[9] > workPoint: gt_rect[6] = max(ratio, gt_rect[6]) if ratio > best_match: best_match = ratio best_segm = segmentations[detId, :] if ratio > best_match_line and rectn[7] == 1.0 : best_match_line = ratio if best_match < minSingleOverlap: if k < len(gt_rects) - 1: gt_rect2 = gt_rects[k + 1] chars2Rect = utils.union(gt_rect2, gt_rect) rect_int = utils.intersect( rectn, chars2Rect ) int_area = utils.area(rect_int) union_area = utils.area(utils.union(rectn, chars2Rect)) ratio = int_area / float(union_area) rectn[10] = max(ratio, rectn[10]) if ratio > best_match2: if ratio > MIN_SEGM_OVRLAP: segm2chars += 1 best_match2 = ratio gt_rect[5] = ratio gt_rect2[5] = ratio thickness = 1 color = (255, 0, 255) if best_match >= minSingleOverlap: color = (0, 255, 0) if best_match > 0.7: thickness = 2 cv2.rectangle(imgc, (gt_rect[0], gt_rect[1]), (gt_rect[2], gt_rect[3]), color, thickness) recall += best_match recallNonMax += gt_rect[6] if best_match >= minSingleOverlap: recall05 += best_match rcurrent05 += best_match else: if not missing_segm.has_key(image): missing_segm[image] = [] missing_segm[image].append(gt_rect) if gt_rect[5] < MIN_SEGM_OVRLAP: if not missing_segm2.has_key(image): missing_segm2[image] = [] missing_segm2[image].append(gt_rect) segm2chars += 1 if gt_rect[6] < minSingleOverlap: if not missing_segmNonMax.has_key(image): missing_segmNonMax[image] = [] missing_segmNonMax[image].append(gt_rect) missing_segmNonMaxCount += 1 rcurrent += best_match rcurrentNonMax += gt_rect[6] recallDen += 1 rden += 1 if best_match > 0 and process_color != 1: val = img[best_segm[5], best_segm[4]] histSegm[val] += best_match histSegmCount[val] += 1 pcurrent = 0 for detId in range(segmentations.shape[0]): best_match = 0 rectn = segmentations[detId, :] for gt_rect in gt_rects: rect_int = utils.intersect( rectn, gt_rect ) int_area = utils.area(rect_int) union_area = utils.area(utils.union(rectn, gt_rect)) ratio = int_area / float(union_area) if ratio > best_match: best_match = ratio precision += best_match pcurrent += best_match precisionDen += 1 pden += 1 if pden == 0: pcurrent = 0 else: pcurrent = pcurrent / pden if rden == 0: rcurrent = 0 rcurrent05 = 0 rcurrentNonMax = 0 else: rcurrent = rcurrent / rden rcurrent05 = rcurrent05 / rden rcurrentNonMax = rcurrentNonMax / rden segmHistogram.append([ segmentations.shape[0], segmentations[segmentations[:, 10] > 0.4].shape[0], segmentations[segmentations[:, 10] > 0.5].shape[0], segmentations[segmentations[:, 10] > 0.6].shape[0], segmentations[segmentations[:, 10] > 0.7].shape[0] ]) segmWordHistogram.append([segmentations.shape[0], segmentations[np.bitwise_or(segmentations[:, 10] > 0.5, segmentations[:, 11] > 0.5 )].shape[0]]) results.append((baseName, rcurrent, pcurrent, rcurrent05)) if precisionDen == 0: pcurrent = 0 else: precision = precision / precisionDen if recallDen == 0: rcurrent = 0 else: recall = recall / recallDen recall05 = recall05 / recallDen recallNonMax = recallNonMax / recallDen wordRecall = wordRecall / max(1, wordRecallDen) try: histSegm = histSegm / max(1, histSegmCount) except ValueError: pass print('Evalation Results:') print( 'recall: {0}, precision: {1}, recall 0.5: {2}, recall NonMax: {3}'.format(recall, precision, recall05, recallNonMax) ) kpTimes = np.histogram(np.asarray(times)[:, 0], bins=20) print('Keypoint Time Histogram: {0}'.format(kpTimes)) print('Detection statistics:') print(stat) for letter in letterKeypointHistogram.keys(): for num in letterKeypointHistogram[letter].keys(): letterKeypointHistogram[letter][num] = letterKeypointHistogram[letter][num] / float(letterHistogram[letter]) for num in octaveLetterKeypointHistogram[letter].keys(): octaveLetterKeypointHistogram[letter][num] = octaveLetterKeypointHistogram[letter][num] / float(letterHistogram[letter]) letterKeypointHistogram[letter] = dict(letterKeypointHistogram[letter]) octaveLetterKeypointHistogram[letter] = dict(octaveLetterKeypointHistogram[letter]) print('Perfect words: {0}'.format(perfectWords)) eval_date = datetime.date.today() np.savez('{0}/evaluation'.format(outputDir), recall=recall, recall05 = recall05, recallNonMax=recallNonMax, precision=precision, eval_date=eval_date, regionsCount=regionsCount, inputDir = inputDir, hist = hist, histSegm = histSegm, stat=stat, letterKeypointHistogram = dict(letterKeypointHistogram), missing_letters=missing_letters, octaveLetterKeypointHistogram=dict(octaveLetterKeypointHistogram), missing_segm=missing_segm, times=np.asarray(times), histFp = histFp, gtSegmCount = gtSegmCount, wordRecall=wordRecall, histDist=histDist, histDistFp = histDistFp, histDistMax=histDistMax, histDistMaxFp=histDistMaxFp, hist2dDist=hist2dDist, hist2dDistFp=hist2dDistFp, hist2dDistScore=hist2dDistScore, hist2dDistScoreFp=hist2dDistScoreFp, histDistMaxWhite=histDistMaxWhite, histDistMaxWhiteFp=histDistMaxWhiteFp, wordsOk=wordsOk, wordsFp=wordsFp, diffMaxOctavesMap = diffMaxOctavesMap, diffScoreOctavesMap = diffScoreOctavesMap, missing_segm2=missing_segm2, segmHistogram=segmHistogram, segmWordHistogram=segmWordHistogram, regionsCountNonMax=regionsCountNonMax, missing_segmNonMax=missing_segmNonMax) print( "GT segmentations count {0}".format(gtSegmCount) ) print('FasTex Inside {0}/{1} ({2})'.format(keypointsTotalInside, keypointsTotal, keypointsTotalInside / float(keypointsTotal) )) print('FasText time: {0}, Orb time: {1} '.format( np.sum(times, 0)[0], orbTime)) print('2 Chars Segmentation: {0}'.format(segm2chars) ) print('NonMax Regions Count: {0}/{1}'.format(regionsCountNonMax, missing_segmNonMaxCount))
def learn_facial_feature(self, number, point_id): booster = cv2.Boost() if number == 0 and MULTITHREAD: self.say("Learning all facial features...") elif not MULTITHREAD: self.say("Learning facial feature %d of %d" % (number + 1, len(EXAMINED_POINTS))) all_features = [] all_classes = [] for i in range(self.train_size): if number == 0 or not MULTITHREAD: self.say( "\tAnalyzing image %s... (%d of %d)" % (os.path.basename(self.images[i]), i + 1, self.train_size)) image = cv2.imread(self.images[i]) # Convert to grayscale and extract face data gray, face, scale = utils.preprocess_face_image(image) if not face: if number == 0 or not MULTITHREAD: self.say("\t%d faces found. Discarding." % (scale, )) continue # Load landmarks points = utils.load_landmarks(self.landmarks[number], scale=scale) # Collect features for the current landmark being trained current_landmark = points[point_id] # Generate positive sample patches positive_samples = [] offset = PATCH_SIZE / 2 for x in range(offset, offset - 3, -1): for y in range(offset, offset - 3, -1): top = current_landmark[1] - y left = current_landmark[0] - x patch = gray[top:top + PATCH_SIZE, left:left + PATCH_SIZE] positive_samples.append(patch) # Randomly choose some negative sample patches negative_samples = [] for i in range(9): x = random.randint(PATCH_SIZE, 2 * PATCH_SIZE) if random.randint(0, 1): x *= -1 y = random.randint(PATCH_SIZE, 2 * PATCH_SIZE) if random.randint(0, 1): y *= -1 top = current_landmark[1] + y left = current_landmark[0] + x patch = gray[top:top + PATCH_SIZE, left:left + PATCH_SIZE] negative_samples.append(patch) # Build a feature vector for each sample patch features = [] for sample in positive_samples: feature_vector = utils.apply_filters_to_sample(sample) features.append(feature_vector) all_features.append(feature_vector) positive_sample_count = len(features) for sample in negative_samples: feature_vector = utils.apply_filters_to_sample(sample) features.append(feature_vector) all_features.append(feature_vector) negative_sample_count = len(features) - positive_sample_count features = np.array(features) features = np.float32(features) classes = [1 for p in range(positive_sample_count)] + \ [0 for n in range(negative_sample_count)] classes = np.array(classes) all_classes = np.append(all_classes, classes) all_features = np.array(all_features) all_features = np.float32(all_features) all_classes = np.array(all_classes) all_classes = np.float32(all_classes) var_types = np.array([cv2.CV_VAR_NUMERICAL] * len(features[0])\ + [cv2.CV_VAR_CATEGORICAL], np.uint8) if number == 0 or not MULTITHREAD: self.say("\tBoosting... (this may take a while)") booster.train(all_features, cv2.CV_ROW_SAMPLE, all_classes, varType=var_types, params=BOOST_PARAMS) booster.save( os.path.join(paths.TRAINING_OUTPUT_PATH, "feature%d" % (number, ))) booster.clear()
def __init__(self): self.model = cv2.Boost()
if __name__ == '__main__': rospy.init_node("Buoy_Test") parser = argparse.ArgumentParser(usage="", description="") parser.add_argument(dest='classifer', type=str, help="Name of the classifer to use.") parser.add_argument(dest='topic', type=str, help="Topic to listen to for image callbacks", default="/camera/front/left/image_rect_color") args = parser.parse_args(sys.argv[1:]) clf = cv2.Boost() clf.load(args.classifer) image_sub = mil_ros_tools.Image_Subscriber(args.topic, got_image, queue_size=1) image_pub = mil_ros_tools.Image_Publisher(args.topic + "_segmented") last_image = None last_image_time = None while not rospy.is_shutdown(): if last_image is not None and last_image_time is not None: process_image(np.copy(last_image), image_pub, clf) print(rospy.Time.now() - last_image_time).to_sec() print rospy.sleep(.1)