def filter_video_before_upload(video, filter_night_time=False): try: if not get_blackvue_info(video)['is_Blackvue_video']: print_error( "ERROR: Direct video upload is currently only supported for BlackVue DRS900S and BlackVue DR900M cameras. Please use video_process command for other camera files" ) return True if get_blackvue_info(video)['camera_direction'] != 'Front': print_error( "ERROR: Currently, only front Blackvue videos are supported on this command. Please use video_process command for backwards camera videos" ) return True except: print_error("ERROR: Unable to determine video details, skipping video") return True [gpx_file_path, isStationaryVid] = gpx_from_blackvue(video, use_nmea_stream_timestamp=False) video_start_time = get_video_start_time_blackvue(video) if isStationaryVid: if not gpx_file_path: if os.path.basename(os.path.dirname(video)) != 'no_gps_data': no_gps_folder = os.path.dirname(video) + '/no_gps_data/' if not os.path.exists(no_gps_folder): os.mkdir(no_gps_folder) os.rename(video, no_gps_folder + os.path.basename(video)) print_error( "Skipping file {} due to file not containing gps data".format( video)) return True if os.path.basename(os.path.dirname(video)) != 'stationary': stationary_folder = os.path.dirname(video) + '/stationary/' if not os.path.exists(stationary_folder): os.mkdir(stationary_folder) os.rename(video, stationary_folder + os.path.basename(video)) os.rename(gpx_file_path, stationary_folder + os.path.basename(gpx_file_path)) print_error( "Skipping file {} due to camera being stationary".format(video)) return True if not isStationaryVid: gpx_points = get_points_from_bv(video) gps_video_start_time = gpx_points[0][0] if filter_night_time == True: # Unsupported feature: Check if video was taken at night # TODO: Calculate sun incidence angle and decide based on threshold # angle sunrise_time = 9 sunset_time = 18 try: timeZoneName, local_timezone_offset = get_timezone_and_utc_offset( gpx_points[0][1], gpx_points[0][2]) if timeZoneName is None: print( "Could not determine local time. Video will be uploaded" ) return False local_video_datetime = video_start_time + local_timezone_offset if local_video_datetime.time() < datetime.time( sunrise_time, 0, 0) or local_video_datetime.time() > datetime.time( sunset_time, 0, 0): if os.path.basename(os.path.dirname(video)) != 'nighttime': night_time_folder = os.path.dirname( video) + '/nighttime/' if not os.path.exists(night_time_folder): os.mkdir(night_time_folder) os.rename(video, night_time_folder + os.path.basename(video)) os.rename( gpx_file_path, night_time_folder + os.path.basename(gpx_file_path)) print_error( "Skipping file {} due to video being recorded at night (Before 9am or after 6pm)" .format(video)) return True except Exception as e: print( "Unable to determine time of day. Exception raised: {} \n Video will be uploaded" .format(e)) return False
def send_videos_for_processing(video_import_path, user_name, user_email=None, user_password=None, verbose=False, skip_subfolders=False, number_threads=None, max_attempts=None, organization_username=None, organization_key=None, private=False, master_upload=False, sampling_distance=2, filter_night_time=False): # safe checks if not os.path.isdir(video_import_path) and not ( os.path.isfile(video_import_path) and video_import_path.lower().endswith("mp4")): print("video import path {} does not exist or is invalid, exiting...". format(video_import_path)) sys.exit(1) # User Authentication credentials = None if user_email and user_password: credentials = authenticate_with_email_and_pwd(user_email, user_password) else: try: credentials = authenticate_user(user_name) except: pass if credentials == None or "user_upload_token" not in credentials or "user_permission_hash" not in credentials or "user_signature_hash" not in credentials: print("Error, user authentication failed for user " + user_name) sys.exit(1) user_permission_hash = credentials["user_permission_hash"] user_signature_hash = credentials["user_signature_hash"] response = get_upload_url(credentials) request_params = response['videos'] # upload all videos in the import path # get a list of all videos first all_videos = get_video_file_list( video_import_path, skip_subfolders) if os.path.isdir( video_import_path) else [video_import_path] total_videos_count = len(all_videos) all_videos = [ x for x in all_videos if os.path.basename(os.path.dirname(x)) != 'uploaded' ] # Filter already uploaded videos uploaded_videos_count = total_videos_count - len(all_videos) all_videos = [ x for x in all_videos if os.path.basename(os.path.dirname(x)) != 'stationary' ] all_videos = [ x for x in all_videos if os.path.basename(os.path.dirname(x)) != 'no_gps_data' ] all_videos = [ x for x in all_videos if os.path.basename(os.path.dirname(x)) != 'nighttime' ] skipped_videos_count = total_videos_count - uploaded_videos_count - len( all_videos) if max_attempts == None: max_attempts = MAX_ATTEMPTS progress = { 'total': total_videos_count, 'uploaded': uploaded_videos_count, 'skipped': skipped_videos_count } ipc.send('progress', progress) for video in tqdm(all_videos, desc="Uploading videos for processing"): print("Preparing video {} for upload".format(os.path.basename(video))) if not filter_video_before_upload(video, filter_night_time): video_start_time = get_video_start_time_blackvue(video) # Correct timestamp in case camera time zone is not set correctly. If timestamp is not UTC, sync with GPS track will fail. # Only hours are corrected, so that second offsets are taken into # account correctly gpx_points = get_points_from_bv(video) gps_video_start_time = gpx_points[0][0] delta_t = video_start_time - gps_video_start_time if delta_t.days > 0: hours_diff_to_utc = round(delta_t.total_seconds() / 3600) else: hours_diff_to_utc = round(delta_t.total_seconds() / 3600) * -1 video_start_time_utc = video_start_time + \ datetime.timedelta(hours=hours_diff_to_utc) video_start_timestamp = int( (((video_start_time_utc - datetime.datetime(1970, 1, 1)).total_seconds())) * 1000) upload_video_for_processing( video, video_start_timestamp, max_attempts, credentials, user_permission_hash, user_signature_hash, request_params, organization_username, organization_key, private, master_upload, sampling_distance) progress['uploaded'] += 1 if not DRY_RUN else 0 else: progress['skipped'] += 1 ipc.send('progress', progress) print("Upload completed")
def filter_video_before_upload(video, filter_night_time=False): try: if not get_blackvue_info(video)['is_Blackvue_video']: print("ERROR: Direct video upload is currently only supported for Blackvue DRS900S camera. Please use video_process command for other camera files") return True if get_blackvue_info(video)['camera_direction'] != 'Front': print("ERROR: Currently, only front Blackvue videos are supported on this command. Please use video_process command for backwards camera videos") return True except: print("ERROR: Unable to determine video details, skipping video") return True [gpx_file_path, isStationaryVid] = gpx_from_blackvue( video, use_nmea_stream_timestamp=False) video_start_time = get_video_start_time_blackvue(video) if isStationaryVid: if not gpx_file_path: if os.path.basename(os.path.dirname(video)) != 'no_gps_data': no_gps_folder = os.path.dirname(video) + '/no_gps_data/' if not os.path.exists(no_gps_folder): os.mkdir(no_gps_folder) os.rename(video, no_gps_folder + os.path.basename(video)) print( "Skipping file {} due to file not containing gps data".format(video)) return True if os.path.basename(os.path.dirname(video)) != 'stationary': stationary_folder = os.path.dirname(video) + '/stationary/' if not os.path.exists(stationary_folder): os.mkdir(stationary_folder) os.rename(video, stationary_folder + os.path.basename(video)) os.rename(gpx_file_path, stationary_folder + os.path.basename(gpx_file_path)) print("Skipping file {} due to camera being stationary".format(video)) return True if not isStationaryVid: gpx_points = get_points_from_bv(video) gps_video_start_time = gpx_points[0][0] if filter_night_time == True: # Unsupported feature: Check if video was taken at night # TODO: Calculate sun incidence angle and decide based on threshold # angle sunrise_time = 9 sunset_time = 18 try: timeZoneName, local_timezone_offset = get_timezone_and_utc_offset( gpx_points[0][1], gpx_points[0][2]) if timeZoneName is None: print("Could not determine local time. Video will be uploaded") return False local_video_datetime = video_start_time + local_timezone_offset if local_video_datetime.time() < datetime.time(sunrise_time, 0, 0) or local_video_datetime.time() > datetime.time(sunset_time, 0, 0): if os.path.basename(os.path.dirname(video)) != 'nighttime': night_time_folder = os.path.dirname( video) + '/nighttime/' if not os.path.exists(night_time_folder): os.mkdir(night_time_folder) os.rename(video, night_time_folder + os.path.basename(video)) os.rename(gpx_file_path, night_time_folder + os.path.basename(gpx_file_path)) print( "Skipping file {} due to video being recorded at night (Before 9am or after 6pm)".format(video)) return True except Exception as e: print( "Unable to determine time of day. Exception raised: {} \n Video will be uploaded".format(e)) return False
def send_videos_for_processing(video_import_path, user_name, user_email=None, user_password=None, verbose=False, skip_subfolders=False, number_threads=None, max_attempts=None, organization_username=None, organization_key=None, private=False, master_upload=False, sampling_distance=2, filter_night_time=False): # safe checks if not os.path.isdir(video_import_path) and not (os.path.isfile(video_import_path) and video_import_path.lower().endswith("mp4")): print("video import path {} does not exist or is invalid, exiting...".format( video_import_path)) sys.exit(1) # User Authentication credentials = None if user_email and user_password: credentials = authenticate_with_email_and_pwd( user_email, user_password) else: try: credentials = authenticate_user(user_name) except: pass if credentials == None or "user_upload_token" not in credentials or "user_permission_hash" not in credentials or "user_signature_hash" not in credentials: print("Error, user authentication failed for user " + user_name) sys.exit(1) user_permission_hash = credentials["user_permission_hash"] user_signature_hash = credentials["user_signature_hash"] response = get_upload_url(credentials) request_params = response['videos'] # upload all videos in the import path # get a list of all videos first all_videos = get_video_file_list(video_import_path, skip_subfolders) if os.path.isdir( video_import_path) else [video_import_path] all_videos = [x for x in all_videos if os.path.basename( os.path.dirname(x)) != 'uploaded'] # Filter already uploaded videos all_videos = [x for x in all_videos if os.path.basename( os.path.dirname(x)) != 'stationary'] all_videos = [x for x in all_videos if os.path.basename( os.path.dirname(x)) != 'no_gps_data'] all_videos = [x for x in all_videos if os.path.basename( os.path.dirname(x)) != 'nighttime'] if max_attempts == None: max_attempts = MAX_ATTEMPTS for video in tqdm(all_videos, desc="Uploading videos for processing"): print("Preparing video {} for upload".format(os.path.basename(video))) if not filter_video_before_upload(video, filter_night_time): video_start_time = get_video_start_time_blackvue(video) # Correct timestamp in case camera time zone is not set correctly. If timestamp is not UTC, sync with GPS track will fail. # Only hours are corrected, so that second offsets are taken into # account correctly gpx_points = get_points_from_bv(video) gps_video_start_time = gpx_points[0][0] delta_t = video_start_time - gps_video_start_time if delta_t.days > 0: hours_diff_to_utc = round(delta_t.total_seconds() / 3600) else: hours_diff_to_utc = round(delta_t.total_seconds() / 3600) * -1 video_start_time_utc = video_start_time + \ datetime.timedelta(hours=hours_diff_to_utc) video_start_timestamp = int( (((video_start_time_utc - datetime.datetime(1970, 1, 1)).total_seconds())) * 1000) upload_video_for_processing( video, video_start_timestamp, max_attempts, credentials, user_permission_hash, user_signature_hash, request_params, organization_username, organization_key, private, master_upload, sampling_distance) print("Upload completed")
def send_videos_for_processing(video_import_path, user_name, user_email=None, user_password=None, verbose=False, skip_subfolders=False, number_threads=None, max_attempts=None, organization_username=None, organization_key=None, private=False, master_upload=False, sampling_distance=2, filter_night_time=False, offset_angle=0, orientation=0): # safe checks if not os.path.isdir(video_import_path) and not ( os.path.isfile(video_import_path) and video_import_path.lower().endswith("mp4")): print("video import path {} does not exist or is invalid, exiting...". format(video_import_path)) sys.exit(1) # User Authentication credentials = None if user_email and user_password: credentials = authenticate_with_email_and_pwd(user_email, user_password) else: try: credentials = authenticate_user(user_name) except: pass if credentials == None or "user_upload_token" not in credentials or "user_permission_hash" not in credentials or "user_signature_hash" not in credentials: print("Error, user authentication failed for user " + user_name) sys.exit(1) # user_permission_hash = credentials["user_permission_hash"] # user_signature_hash = credentials["user_signature_hash"] # response = get_upload_url(credentials) # request_params = response['videos'] # upload all videos in the import path # get a list of all videos first all_videos = get_video_file_list( video_import_path, skip_subfolders) if os.path.isdir( video_import_path) else [video_import_path] total_videos_count = len(all_videos) all_videos = [ x for x in all_videos if os.path.basename(os.path.dirname(x)) != 'uploaded' ] # Filter already uploaded videos uploaded_videos_count = total_videos_count - len(all_videos) all_videos = [ x for x in all_videos if os.path.basename(os.path.dirname(x)) != 'stationary' ] all_videos = [ x for x in all_videos if os.path.basename(os.path.dirname(x)) != 'no_gps_data' ] all_videos = [ x for x in all_videos if os.path.basename(os.path.dirname(x)) != 'nighttime' ] skipped_videos_count = total_videos_count - \ uploaded_videos_count - len(all_videos) if max_attempts == None: max_attempts = MAX_ATTEMPTS progress = { 'total': total_videos_count, 'uploaded': uploaded_videos_count, 'skipped': skipped_videos_count } ipc.send('progress', progress) for video in tqdm(all_videos, desc="Uploading videos for processing"): print("Preparing video {} for upload".format(os.path.basename(video))) if filter_video_before_upload(video, filter_night_time): progress['skipped'] += 1 continue video_start_time = get_video_start_time_blackvue(video) # Correct timestamp in case camera time zone is not set correctly. If timestamp is not UTC, sync with GPS track will fail. # Only hours are corrected, so that second offsets are taken into # account correctly gpx_points = get_points_from_bv(video) gps_video_start_time = gpx_points[0][0] delta_t = video_start_time - gps_video_start_time if delta_t.days > 0: hours_diff_to_utc = round(delta_t.total_seconds() / 3600) else: hours_diff_to_utc = round(delta_t.total_seconds() / 3600) * -1 video_start_time_utc = video_start_time + \ datetime.timedelta(hours=hours_diff_to_utc) video_start_timestamp = int( (((video_start_time_utc - datetime.datetime(1970, 1, 1)).total_seconds())) * 1000) metadata = { "camera_angle_offset": float(offset_angle), "exif_frame_orientation": orientation, "images_upload_v2": True, "make": "Blackvue", "model": "DR900S-1CH", "private": private, "sample_interval_distance": float(sampling_distance), "sequence_key": "test_sequence", # TODO: What is the sequence key? "video_start_time": video_start_timestamp, } if organization_key != None: metadata["organization_key"] = organization_key if master_upload != None: metadata['user_key'] = master_upload options = { "api_endpoint": API_ENDPOINT, "token": credentials["user_upload_token"], "client_id": CLIENT_ID } if DRY_RUN: continue upload_video(video, metadata, options) progress['uploaded'] += 1 ipc.send('progress', progress) print("Upload completed")