def test_detect(self, trainsetIdxs, testsetIdx, harvest=False, autoAnnotate=False, displayOptions=None): """ Detect objects in a video :param harvest: whether to use harvesting :param autoAnnotate: whether to automate harvesting (for testing purposes) :param displayOptions: some option about what to display in the video :return: """ self.log.info("Performing detection test") framesStep = 10 # take every frameSteps-th frame if not displayOptions: displayOptions = { 'enabled': True, # display anything at all 'frame': True, # video frame image 'detections': False, # detected objects 'suppressed_detections': True, 'sliding_window': False, 'ground_truth': False } samples = self.getTrainSamples(trainsetIdxs) clsf = self.trainFromSamples(samples) # train a classifier based on the given dataset samples datasetId = self.datasetIds[testsetIdx] # get the dataset id given the dataset index videoFileName = os.path.join(self.projectDirectory, 'videos', '%s.MOV' % datasetId) annotationsFileName = os.path.join(self.projectDirectory, 'labels', "%s_output.txt" % datasetId) video = Video(videoFileName, annotationsFileName) video.cutoutSize = self.cutoutSize detectionsTruth = video.getAnnotations() # get the labeled ground truth for this video self.log.debug("Using video %s" % videoFileName) self.suppressor = Suppressor() cap = cv2.VideoCapture(videoFileName) # load the video file results = [] # results for each frame frameIdx = 0 while cap.isOpened(): ret, frame = cap.read() if ret is False: break if ((frameIdx+1) % framesStep) == 0: # use every <frameStep>th frame self.log.debug("Frame %d" % frameIdx) truth = [] for truthDetection in detectionsTruth[frameIdx]: if truthDetection['lost'] is True: # if the detection is not visible in this frame continue truthDetectionCutoutDimensions = video.getCutoutDimensions(truthDetection, frame.shape[:2]) # dimension of the truth in the frame truth.append(truthDetectionCutoutDimensions) detections = self.detectObjects(frame, clsf, truth, display=displayOptions) # detect objects in frame result = self.matchDetections(detections, truth, frame.shape[:2]) # match the detections agains the truth results.append(result) if harvest: detectionsOverlapRatios = result['detectionsOverlapRatios'] # how much the truth overlaps with each detection newSamples = self.annotateDetections(frame, detections, detectionsOverlapRatios, autoAnnotate=autoAnnotate, overlapThreshold=0.3) # (manually) annotate all the found detections if len(newSamples): samples.extend(newSamples) clsf = self.trainFromSamples(samples) # retrain a classifier based on the given dataset samples and new samples frameIdx += 1 cap.release() totalScore = sum([result['score'] for result in results]) totalPrecision = sum([result['precision'] for result in results]) totalRecall = sum([result['recall'] for result in results]) print ("= Average score:") pprint(totalScore/len(results)) print ("= Average precision:") pprint(totalPrecision/len(results)) print ("= Average recall:") pprint(totalRecall/len(results))
__author__ = 'Rik Smit' parser = argparse.ArgumentParser(description='Creating cutous from video using Vatic annotations.') parser.add_argument('videofile', help='Location of the video file') parser.add_argument('annotationsfile', help='Location of the annotations file') parser.add_argument('outputdir', help='Location of the directory to store the cutouts') parser.add_argument('--cutoutsize', help='Dimensions of the cutouts.', default=(100, 100)) parser.add_argument('--framestep', help='Use every <framestep>th frame.', default=25) parser.add_argument('--negativescount', help='Amount of negatives to cutout for each frame.', default=50) parser.add_argument('--pos', action='store_false', help="Whether to exclude extracting positives") parser.add_argument('--neg', action='store_false', help="Whether to exclude extracting negatives") args = parser.parse_args() videoFileName = args.videofile cutoutsDirectory = args.outputdir annotationsFileName = args.annotationsfile video = Video(videoFileName, annotationsFileName) video.load() video.cutoutSize = args.cutoutsize video.negExamplesPerFrame = args.negativescount video.framesStep = args.framestep video.extractObjects(cutoutsDirectory, extractPositives=args.pos, extractNegatives=args.neg)