def write_homography_files(self):
        project_dir = get_project_path(self.identifier)
        aerial_pts = literal_eval(self.find_argument('aerial_pts'))
        camera_pts = literal_eval(self.find_argument('camera_pts'))

        if  ((aerial_pts is not None) and (camera_pts is not None)) and\
            (isinstance(aerial_pts, list) and isinstance(camera_pts, list)) and\
            (len(aerial_pts) == len(camera_pts)) and\
            (self.check_points(aerial_pts) and self.check_points(camera_pts)) and\
            (len(aerial_pts) >= 4):

            try:
                homography, mask = cv2.findHomography(\
                            np.array(camera_pts),\
                            self.up_ratio*np.array(aerial_pts))
                np.savetxt(\
                    os.path.join(project_dir,'homography','homography.txt'),\
                    homography)
            except Exception as e:
                self.error_message = "Could not find the homography, check your points and try again"
                StatusHelper.set_status(\
                                        self.identifier,\
                                        Status.Type.HOMOGRAPHY,\
                                        Status.Flag.FAILURE,
                                        failure_message='Failed to find homography: '+str(e))
                raise tornado.web.HTTPError(status_code=500)

        else:
            self.error_message = "Could not interpret the points given. Try again with different points"
            StatusHelper.set_status(\
                                    self.identifier,\
                                    Status.Type.HOMOGRAPHY,\
                                    Status.Flag.FAILURE,
                                    failure_message="Couldn't interpret uploaded points.")
            raise tornado.web.HTTPError(status_code=500)
    def run(self):
        create_highlight_video(self.project_dir, self.video_path,
                               self.near_misses)

        StatusHelper.set_status(self.identifier, Status.Type.HIGHLIGHT_VIDEO,
                                Status.Flag.COMPLETE)
        return self.callback(200, "Highlight video complete.", self.email)
Exemplo n.º 3
0
    def run(self):
        project_path = get_project_path(self.identifier)
        config_path = os.path.join(project_path, "tracking.cfg")
        db_path = os.path.join(project_path, "run", "results.sqlite")
        update_dict = {
            'video-filename': get_project_video_path(self.identifier), # use absolute path to video on server
            'database-filename': db_path # use absolute path to database
        }

        update_config_without_sections(config_path, update_dict)

        if self.prediction_method is None:
            self.prediction_method = 'cv' # default to the least resource intensive method

        # Predict Interactions between road users and compute safety metrics describing them
        try:
            print "Running safety analysis. Please wait as this may take a while."

            subprocess.check_call(["safety-analysis.py", "--cfg", config_path, "--prediction-method", self.prediction_method])
        except subprocess.CalledProcessError as err_msg:
            StatusHelper.set_status(self.identifier, Status.Type.SAFETY_ANALYSIS, Status.Flag.FAILURE, failure_message='Safety analysis failed with error: '+str(err_msg))
            return self.callback(500, str(err_msg), self.identifier, self.email)

        StatusHelper.set_status(self.identifier, Status.Type.SAFETY_ANALYSIS, Status.Flag.COMPLETE)
        return self.callback(200, "Success", self.identifier, self.email)
    def handler(identifier, email):
        project_path = get_project_path(identifier)
        if not os.path.exists(project_path):
            StatusHelper.set_status(identifier, Status.Type.OBJECT_TRACKING, Status.Flag.FAILURE, failure_message='Project directory does not exist.')
            return (500, 'Project directory does not exist. Check your identifier?')

        status_code, reason = ObjectTrackingHandler.handler(identifier, email, AnalysisHandler.object_tracking_callback)
        return (status_code, reason)
Exemplo n.º 5
0
    def handler(identifier, email, callback, prediction_method=None):
        project_path = get_project_path(identifier)
        if not os.path.exists(project_path):
            StatusHelper.set_status(identifier, Status.Type.SAFETY_ANALYSIS, Status.Flag.FAILURE, failure_message='Project directory does not exist.')
            return (500, 'Project directory does not exist. Check your identifier?')

        SafetyAnalysisThread(identifier, email, callback, prediction_method=prediction_method).start()

        return (200, "Success")
    def object_tracking_callback(status_code, response_message, identifier, email):
        if status_code == 200:
            StatusHelper.set_status(identifier, Status.Type.SAFETY_ANALYSIS, Status.Flag.IN_PROGRESS)
            message = "Hello,\n\tWe have finished processing your video and identifying all objects.\nWe will perform safety analysis now.\nThank you for your patience,\nThe Santos Team"
            subject = "Your video has finished processing."

            EmailHelper.send_email(email, subject, message)

            status_code, reason = SafetyAnalysisHandler.handler(identifier, email, AnalysisHandler.safety_analysis_callback)

        print (status_code, response_message)
Exemplo n.º 7
0
 def prepare(self):
     self.identifier = self.find_argument('identifier')
     status_dict = StatusHelper.get_status(self.identifier)
     if status_dict[Status.Type.SAFETY_ANALYSIS] == Status.Flag.IN_PROGRESS:
         status_code = 423
         self.error_message = "Currently analyzing database from your video. Please wait."
         raise tornado.web.HTTPError(status_code = status_code)
     if status_dict[Status.Type.OBJECT_TRACKING] != Status.Flag.COMPLETE:
         status_code = 412
         self.error_message = "Object tracking did not complete successfully, try re-running it."
         raise tornado.web.HTTPError(status_code = status_code)
     StatusHelper.set_status(self.identifier, Status.Type.SAFETY_ANALYSIS, Status.Flag.IN_PROGRESS)
    def get(self):
        h_path = os.path.join(\
                        get_project_path(self.identifier),\
                        'homography',\
                        'homography.txt')
        self.write({'homography': np.ndarray.tolist(np.loadtxt(h_path))})

        StatusHelper.set_status(\
                                self.identifier,\
                                Status.Type.HOMOGRAPHY,\
                                Status.Flag.COMPLETE)
        self.finish()
 def prepare(self):
     self.identifier = self.find_argument('identifier')
     status_dict = StatusHelper.get_status(self.identifier)
     if status_dict[Status.Type.OBJECT_TRACKING] == Status.Flag.IN_PROGRESS:
         status_code = 423
         self.error_message = "Currently analyzing your video. Please wait."
         raise tornado.web.HTTPError(status_code=status_code)
     if status_dict[Status.Type.HOMOGRAPHY] != Status.Flag.COMPLETE:
         status_code = 412
         self.error_message = "Uploading homography did not complete successfully, try re-running it."
         raise tornado.web.HTTPError(status_code=status_code)
     StatusHelper.set_status(self.identifier, Status.Type.OBJECT_TRACKING,
                             Status.Flag.IN_PROGRESS)
    def prepare(self):
        self.identifier = self.find_argument('identifier')

        #TODO: Make sure that the project actually exists, ie. project_exists(id)

        #Handle the status correctly
        if StatusHelper.get_status(self.identifier)[
                Status.Type.HOMOGRAPHY] == Status.Flag.IN_PROGRESS:
            status_code = 423
            self.error_message = "Currently uploading homography. Please wait."
            raise tornado.web.HTTPError(status_code=status_code)
        StatusHelper.set_status(self.identifier, Status.Type.HOMOGRAPHY,
                                Status.Flag.IN_PROGRESS)
    def run(self):
        project_path = get_project_path(self.identifier)
        tracking_path = os.path.join(project_path, "tracking.cfg")
        homography_path = os.path.join(project_path, "homography",
                                       "homography.txt")
        obj_db_path = os.path.join(project_path, ".temp", "test",
                                   "test_object", "test1.sqlite")
        feat_db_path = os.path.join(project_path, ".temp", "test",
                                    "test_feature", "test1.sqlite")
        if os.path.exists(obj_db_path):
            os.remove(obj_db_path)
        shutil.copyfile(feat_db_path, obj_db_path)

        testing_dict = {
            'frame1': self.frame_start,
            'nframes': self.num_frames,
            'video-filename': get_project_video_path(self.identifier),
            'homography-filename': homography_path
        }
        update_config_without_sections(tracking_path, testing_dict)

        try:
            subprocess.check_call([
                "feature-based-tracking", tracking_path, "--gf",
                "--database-filename", obj_db_path
            ])
            subprocess.check_call([
                "classify-objects.py", "--cfg", tracking_path, "-d",
                obj_db_path
            ])  # Classify road users
        except subprocess.CalledProcessError as err_msg:
            StatusHelper.set_status(
                self.identifier,
                Status.Type.OBJECT_TEST,
                Status.Flag.FAILURE,
                failure_message='Failed to run the object test with error: ' +
                str(err_msg))
            return self.callback(500, str(err_msg), self.identifier)

        video_path = get_project_video_path(self.identifier)
        output_path = os.path.join(project_path, 'object_video',
                                   'object_video.mp4')
        create_test_config_video(project_path, video_path, output_path,
                                 obj_db_path, self.frame_start,
                                 self.frame_start + self.num_frames, 'object')

        StatusHelper.set_status(self.identifier, Status.Type.OBJECT_TEST,
                                Status.Flag.COMPLETE)
        return self.callback(200, "Test config done", self.identifier)
    def run(self):
        project_path = get_project_path(self.identifier)
        tracking_path = os.path.join(project_path, "tracking.cfg")
        homography_path = os.path.join(project_path, "homography",
                                       "homography.txt")
        db_path = os.path.join(project_path, ".temp", "test", "test_feature",
                               "test1.sqlite")
        if os.path.exists(db_path):
            os.remove(db_path)

        testing_dict = {
            'frame1': self.frame_start,
            'nframes': self.num_frames,
            'video-filename': get_project_video_path(self.identifier),
            'homography-filename': homography_path
        }
        update_config_without_sections(tracking_path, testing_dict)

        fbt_call = [
            "feature-based-tracking", tracking_path, "--tf",
            "--database-filename", db_path
        ]
        mask_filename = os.path.join(get_project_path(self.identifier),
                                     "mask.jpg")
        if os.path.exists(mask_filename):
            fbt_call.extend(["--mask-filename", mask_filename])
        try:
            subprocess.check_call(fbt_call)
        except subprocess.CalledProcessError as err_msg:
            StatusHelper.set_status(
                self.identifier,
                Status.Type.FEATURE_TEST,
                Status.Flag.FAILURE,
                failure_message='Feature tracking failed with error: ' +
                str(err_msg))
            return self.callback(500, str(err_msg), self.identifier)

        video_path = get_project_video_path(self.identifier)
        output_path = os.path.join(project_path, 'feature_video',
                                   'feature_video.mp4')
        create_test_config_video(project_path, video_path, output_path,
                                 db_path, self.frame_start,
                                 self.frame_start + self.num_frames, 'feature')

        StatusHelper.set_status(self.identifier, Status.Type.FEATURE_TEST,
                                Status.Flag.COMPLETE)
        return self.callback(200, "Test config done", self.identifier)
    def handler(identifier, email, callback):
        """
        Runs TrafficIntelligence trackers and support scripts.
        """
        project_path = get_project_path(identifier)
        if not os.path.exists(project_path):
            StatusHelper.set_status(
                identifier,
                Status.Type.OBJECT_TRACKING,
                Status.Flag.FAILURE,
                failure_message='Project directory does not exist.')
            return (500,
                    'Project directory does not exist. Check your identifier?')

        ObjectTrackingThread(identifier, email, callback).start()

        return (200, "Success")
 def prepare(self):
     self.identifier = self.find_argument('identifier')
     status_dict = StatusHelper.get_status(self.identifier)
     if status_dict[Status.Type.HIGHLIGHT_VIDEO] == Status.Flag.IN_PROGRESS:
         status_code = 423
         self.error_message = "Currently creating a highlight video. Please wait."
         raise tornado.web.HTTPError(status_code=status_code)
     if status_dict[Status.Type.SAFETY_ANALYSIS] != Status.Flag.COMPLETE:
         status_code = 412
         self.error_message = "Safety analysis did not complete successfully, try re-running it."
         raise tornado.web.HTTPError(status_code=status_code)
     if self.request.method.lower() == "get" and status_dict[
             Status.Type.HIGHLIGHT_VIDEO] != Status.Flag.COMPLETE:
         self.error_message = "Highlight Video did not complete successfully, try re-running it."
         raise tornado.web.HTTPError(status_code=500)
     StatusHelper.set_status(self.identifier, Status.Type.HIGHLIGHT_VIDEO,
                             Status.Flag.IN_PROGRESS)
Exemplo n.º 15
0
 def get(self):
     identifier = self.find_argument('identifier')
     status_dict = StatusHelper.get_status_raw(identifier)
     if status_dict != None:
         self.write(status_dict)
     else:
         self.error_message = "Could not get project status"
         raise tornado.web.HTTPError(status_code=500)
 def get(self):
     status = StatusHelper.get_status(self.identifier)
     project_path = get_project_path(self.identifier)
     file_name = os.path.join(project_path, 'final_videos', 'highlight.mp4')
     self.set_header('Content-Disposition',
                     'attachment; filename=highlight.mp4')
     self.set_header('Content-Type', 'application/octet-stream')
     self.set_header('Content-Description', 'File Transfer')
     self.write_file_stream(file_name)
     self.finish()
    def prepare(self):
        self.identifier = self.find_argument('identifier')
        self.test_flag = self.find_argument('test_flag')
        status_dict = StatusHelper.get_status(self.identifier)
        if self.test_flag == "feature":
            status_type = Status.Type.FEATURE_TEST
            if status_dict[Status.Type.HOMOGRAPHY] != Status.Flag.COMPLETE:
                self.error_message = "Uploading homography did not complete successfully, try re-running it."
                status_code = 412
                raise tornado.web.HTTPError(status_code=status_code)
        elif self.test_flag == "object":
            status_type = Status.Type.OBJECT_TEST
            if status_dict[Status.Type.FEATURE_TEST] != Status.Flag.COMPLETE:
                self.error_message = "Feature testing did not complete successfully, try re-running it."
                status_code = 412
                raise tornado.web.HTTPError(status_code=status_code)
        if status_dict[Status.Type.
                       FEATURE_TEST] == Status.Flag.IN_PROGRESS or status_dict[
                           Status.Type.OBJECT_TEST] == Status.Flag.IN_PROGRESS:
            status_code = 423
            self.error_message = "Currently running a test. Please wait."
            raise tornado.web.HTTPError(status_code=status_code)

        request_type = self.request.method.lower()
        if request_type == 'post':
            StatusHelper.set_status(self.identifier, status_type,
                                    Status.Flag.IN_PROGRESS)
        elif request_type == 'get':
            if self.test_flag == "feature":
                if status_dict[
                        Status.Type.FEATURE_TEST] != Status.Flag.COMPLETE:
                    status_code = 500
                    self.error_message = "Feature test not complete, try re-running it."
                    raise tornado.web.HTTPError(status_code=status_code)
            elif self.test_flag == "object":
                if status_dict[
                        Status.Type.OBJECT_TEST] != Status.Flag.COMPLETE:
                    status_code = 500
                    self.error_message = "Object test not complete, try re-running it."
                    raise tornado.web.HTTPError(status_code=status_code)
 def get(self):
     status = StatusHelper.get_status(self.identifier)
     project_path = get_project_path(self.identifier)
     if self.test_flag == "feature":
         self.file_name = os.path.join(project_path, 'feature_video',
                                       'feature_video.mp4')
         self.set_header('Content-Disposition',
                         'attachment; filename=feature_video.mp4')
     elif self.test_flag == "object":
         self.file_name = os.path.join(project_path, 'object_video',
                                       'object_video.mp4')
         self.set_header('Content-Disposition',
                         'attachment; filename=object_video.mp4')
     self.set_header('Content-Type', 'application/octet-stream')
     self.set_header('Content-Description', 'File Transfer')
     self.write_file_stream(self.file_name)
     self.finish()
    def handler(identifier, frame_start, num_frames, test_flag):
        if test_flag == "feature":
            status_type = Status.Type.FEATURE_TEST
        elif test_flag == "object":
            status_type = Status.Type.OBJECT_TEST

        project_path = get_project_path(identifier)
        if not os.path.exists(project_path):
            StatusHelper.set_status(
                identifier,
                status_type,
                Status.Flag.FAILURE,
                failure_message='Project directory does not exist.')
            return (400,
                    'Project directory does not exist. Check your identifier?')

        if test_flag == "feature":
            print "running feature"
            TestConfigFeatureThread(
                identifier, frame_start, num_frames,
                TestConfigHandler.test_feature_callback).start()
        elif test_flag == "object":
            print "running object"
            feat_db_path = os.path.join(project_path, ".temp", "test",
                                        "test_feature", "test1.sqlite")
            if os.path.exists(feat_db_path):
                TestConfigObjectThread(
                    identifier, frame_start, num_frames,
                    TestConfigHandler.test_feature_callback).start()
            else:
                print "Feature tracking not run"
                StatusHelper.set_status(
                    identifier,
                    status_type,
                    Status.Flag.FAILURE,
                    failure_message=
                    "Feature tracking database deosn't exist. Please try running feature tracking again."
                )
                return (
                    400,
                    "Testing of feature tracking did not produce the required files. Try re-running it."
                )
        else:
            print "Incorrect flag passed: " + test_flag
            StatusHelper.set_status(
                identifier,
                status_type,
                Status.Flag.FAILURE,
                failure_message=
                'Please pass "object" or "feature" as your test_flag.')
            return (400, "Incorrect flag passed: " + test_flag)

        return (200, "Success")
Exemplo n.º 20
0
def cleanup_func():
    print('Cleaning up...')
    StatusHelper.mark_all_failed()
    print('Cleaned up.')
    def handler(identifier, email, ttc_threshold, vehicle_only,
                num_near_misses_to_use):

        project_dir = get_project_path(identifier)
        if not os.path.exists(project_dir):
            StatusHelper.set_status(
                identifier,
                Status.Type.HIGHLIGHT_VIDEO,
                Status.Flag.FAILURE,
                failure_message='Project directory does not exist.')
            return (500,
                    'Project directory does not exist. Check your identifier?')

        db = os.path.join(project_dir, 'run', 'results.sqlite')
        #TO-DO: Check to see if tables like "interactions" exist
        if not os.path.exists(db):
            StatusHelper.set_status(
                identifier,
                Status.Type.HIGHLIGHT_VIDEO,
                Status.Flag.FAILURE,
                failure_message=
                'Trajectory analysis must be run before creating a highlight video.'
            )
            return (
                500,
                'Database file does not exist. Trajectory analysis needs to be called first '
            )

        video_path = get_project_video_path(identifier)
        if not os.path.exists(video_path):
            StatusHelper.set_status(
                identifier,
                Status.Type.HIGHLIGHT_VIDEO,
                Status.Flag.FAILURE,
                failure_message='The video file does not exist.')
            return (
                500,
                'Source video file does not exist.  Was the video uploaded?')

        ttc_threshold_frames = int(ttc_threshold *
                                   float(get_framerate(video_path)))

        try:
            near_misses = getNearMissFrames(db, ttc_threshold_frames,
                                            vehicle_only)
        except Exception as error_message:
            StatusHelper.set_status(
                identifier,
                Status.Type.HIGHLIGHT_VIDEO,
                Status.Flag.FAILURE,
                failure_message='Failed to get near miss frames.')
            return (500, str(error_message))

        num_near_misses_to_use = min(10, num_near_misses_to_use)
        if len(near_misses) > num_near_misses_to_use:
            near_misses = near_misses[:num_near_misses_to_use]

        try:
            CreateHighlightVideoThread(
                identifier, project_dir, video_path, near_misses, email,
                CreateHighlightVideoHandler.callback).start()
        except Exception as error_message:
            StatusHelper.set_status(
                identifier,
                Status.Type.HIGHLIGHT_VIDEO,
                Status.Flag.FAILURE,
                failure_message='Error creating highlight video: ' +
                str(error_message))
            return (500, str(error_message))

        return (200, "Success")
Exemplo n.º 22
0
 def prepare(self):
     self.identifier = self.find_argument('identifier')
     status_dict = StatusHelper.get_status(self.identifier)
     if status_dict[Status.Type.SAFETY_ANALYSIS] != Status.Flag.COMPLETE:
         status_code = 412
         self.error_message = "Safety analysis did not complete successfully, try re-running it."
 def post(self):
     self.up_ratio = float(self.find_argument('unit_pixel_ratio'))
     self.write_homography_files()
     StatusHelper.set_status(self.identifier, Status.Type.HOMOGRAPHY,
                             Status.Flag.COMPLETE)
     self.finish()
    def run(self):

        project_path = get_project_path(self.identifier)
        tracking_path = os.path.join(project_path, "tracking.cfg")

        update_dict = {
            'frame1':
            0,
            'nframes':
            0,
            'database-filename':
            'results.sqlite',
            'classifier-filename':
            os.path.join(project_path, "classifier.cfg"),
            'video-filename':
            get_project_video_path(self.identifier),
            'homography-filename':
            os.path.join(project_path, "homography", "homography.txt")
        }
        update_config_without_sections(tracking_path, update_dict)

        db_path = os.path.join(project_path, "run", "results.sqlite")

        if os.path.exists(db_path):  # If results database already exists,
            os.remove(db_path)  # then remove it--it'll be recreated.

        fbttf_call = [
            "feature-based-tracking", tracking_path, "--tf",
            "--database-filename", db_path
        ]
        fbtgf_call = [
            "feature-based-tracking", tracking_path, "--gf",
            "--database-filename", db_path
        ]

        mask_filename = os.path.join(get_project_path(self.identifier),
                                     "mask.jpg")
        if os.path.exists(mask_filename):
            fbttf_call.extend(["--mask-filename", mask_filename])
            fbtgf_call.extend(["--mask-filename", mask_filename])

        try:
            subprocess.check_call(fbttf_call)
            subprocess.check_call(fbtgf_call)

            #Classify Road Users in batches
            total_objs = getObjectCount(db_path)
            batch_size = 100
            for start_index in xrange(0, total_objs, batch_size):
                if start_index + batch_size > total_objs:
                    batch_size = total_objs % batch_size
                subprocess.check_call(["classify-objects.py",\
                                        "--cfg", tracking_path,\
                                        "-d", db_path,\
                                        "-s", str(start_index),\
                                        "-n", str(batch_size)])

        except subprocess.CalledProcessError as excp:
            StatusHelper.set_status(self.identifier,
                                    Status.Type.OBJECT_TRACKING,
                                    Status.Flag.FAILURE,
                                    failure_message='Failed with error: ' +
                                    str(excp))
            return self.callback(500, str(excp), self.identifier, self.email)

        db_make_objtraj(db_path)  # Make our object_trajectories db table
        StatusHelper.set_status(self.identifier, Status.Type.OBJECT_TRACKING,
                                Status.Flag.COMPLETE)
        return self.callback(200, "Success", self.identifier, self.email)