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 get(self): identifier = self.find_argument('identifier') project_dir = get_project_path(identifier) if (not os.path.exists(os.path.join(project_dir,\ 'final_images',\ 'road_user_icon_counts.jpg'))): self.error_message = 'Road User Counts must be run before the report can be generated.' raise tornado.web.HTTPError(status_code=400) if (not os.path.exists(os.path.join(project_dir,\ 'final_images',\ 'velocityPDF.jpg'))): self.error_message = 'Speed Distribution must be run before the report can be generated.' raise tornado.web.HTTPError(status_code=400) status_code, reason = MakeReportHandler.handler(identifier) if status_code == 200: report_path = os.path.join(project_dir,\ 'santosreport.pdf') self.set_header('Content-Disposition',\ 'attachment; filename=santosreport.pdf') self.set_header('Content-Type', 'application/octet-stream') self.set_header('Content-Description', 'File Transfer') self.write_file_stream(report_path) self.finish("Make PDF Report") else: self.error_message = reason raise tornado.web.HTTPError(status_code=status_code)
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 handler(identifier, speed_limit, vehicle_only): project_dir = get_project_path(identifier) if not os.path.exists(project_dir): return (500, 'Project directory does not exist. Check your identifier?') db = os.path.join(project_dir, 'run', 'results.sqlite') if not os.path.exists(db): return ( 500, 'Database file does not exist. Trajectory analysis needs to be called first ' ) final_images = os.path.join(project_dir, 'final_images') if not os.path.exists(final_images): os.mkdir(final_images) video_path = get_project_video_path(identifier) if not os.path.exists(video_path): return ( 500, 'Source video file does not exist. Was the video uploaded?') vel_distribution(db, float(get_framerate(video_path)), speed_limit, final_images, vehicle_only) return (200, "Success")
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): 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)
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 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 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")
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 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 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 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 get(self): vehicle_only = bool(self.find_argument('vehicle_only', default=True)) speed_limit = int(self.find_argument('speed_limit', default=25)) status_code, reason = CreateSpeedDistributionHandler.handler( self.identifier, speed_limit, vehicle_only) if status_code == 200: image_path = os.path.join(\ get_project_path(self.identifier),\ 'final_images',\ 'velocityPDF.jpg') self.set_header('Content-Disposition',\ 'attachment; filename=velocityPDF.jpg') self.set_header('Content-Type', 'application/octet-stream') self.set_header('Content-Description', 'File Transfer') self.write_file_stream(image_path) self.finish("Create Speed Distribution") else: self.error_message = reason raise tornado.web.HTTPError(status_code=status_code)
def handler(identifier): project_dir = get_project_path(identifier) if not os.path.exists(project_dir): return (500, 'Project directory does not exist. Check your identifier?') final_images = os.path.join(project_dir, 'final_images') if not os.path.exists(final_images): os.mkdir(final_images) report_path = os.path.join(project_dir, 'santosreport.pdf') # Hardcoded image file name order, so that the ordering of visuals in the report is consistent image_fns = [ os.path.join(final_images, 'road_user_icon_counts.jpg'), os.path.join(final_images, 'velocityPDF.jpg') ] makePdf(report_path, image_fns, final_images) return (200, 'Success')
def get(self): identifier = self.find_argument('identifier') project_path = get_project_path(identifier) file_videos = os.path.join(project_path, 'final_videos') file_images = os.path.join(project_path, 'final_images') file_report = os.path.join(project_path, 'santosreport.pdf') self.file_name = os.path.join(project_path, 'results.zip') zipf = zipfile.ZipFile(self.file_name, 'w', zipfile.ZIP_DEFLATED, allowZip64=True) # Write videos for root, dirs, files in os.walk(file_videos): for file in files: file_path = os.path.join(root, file) zipf.write(file_path, file) # Write images for root, dirs, files in os.walk(file_images): for file in files: file_path = os.path.join(root, file) zipf.write(file_path, file) # Write report, if there try: zipf.write(file_report, os.path.basename(file_report)) except: status_code = 500 self.error_message = "Report was not generated for sending. Try re-running generation." raise tornado.web.HTTPError(status_code=status_code) zipf.close() self.set_header('Content-Type', 'application/zip') self.set_header('Content-Description', 'File Transfer') self.set_header('Content-Disposition', 'attachment; filename=' + self.file_name) self.write_file_stream(self.file_name) self.finish()
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")
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)