Python Forum

Full Version: File Path not recognised
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Using Python 3.7.4 ,I am providing the filepath of a videofile to the Googlecloud Python VideoIntelligence api for further processing.But it still complains that the path is not specified.Pylinter gives an "Anomalous backslash in string error".I have tried using r' \d "filepath"' but the error still persists.Please let me know , how to resolve this issue.

ERROR
Error:
PS D:\Script\GCloud> d:\Script\GCloud\VideoLabels.py usage: VideoLabels.py [-h] path VideoLabels.py: error: the following arguments are required: path
PYLINT
Anomalous backslash in string: '\\S'. String constant might be missing an r prefix.

CODE
import argparse

from google.cloud import videointelligence

path = "D:\Script\GCloud\sampleVidR1.mp4" #<--path not recognised
    
def analyze_labels(path):
    """ Detects labels given a GCS path. """
    video_client = videointelligence.VideoIntelligenceServiceClient()
    features = [videointelligence.enums.Feature.LABEL_DETECTION]
    operation = video_client.annotate_video(path, features=features)
    print('\nProcessing video for label annotations:')

    result = operation.result(timeout=90)
    print('\nFinished processing.')

    segment_labels = result.annotation_results[0].segment_label_annotations
    for i, segment_label in enumerate(segment_labels):
        print('Video label description: {}'.format(
            segment_label.entity.description))
        for category_entity in segment_label.category_entities:
            print('\tLabel category description: {}'.format(
                category_entity.description))

        for i, segment in enumerate(segment_label.segments):
            start_time = (segment.segment.start_time_offset.seconds +
                          segment.segment.start_time_offset.nanos / 1e9)
            end_time = (segment.segment.end_time_offset.seconds +
                        segment.segment.end_time_offset.nanos / 1e9)
            positions = '{}s to {}s'.format(start_time, end_time)
            confidence = segment.confidence
            print('\tSegment {}: {}'.format(i, positions))
            print('\tConfidence: {}'.format(confidence))
            print('\n')


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('path', help='GCS file path for label detection.')
    args = parser.parse_args()

    analyze_labels(args.path)
	
There is a number of special characters that in pair with \ have particular meaning
https://linuxconfig.org/list-of-python-e...h-examples
To avoid this behaviour use escape slashes
path = "D:\\Script\\GCloud\\sampleVidR1.mp4"
or construct paths using os.path.join
Other available options - use raw string
path = r"D:\Script\GCloud\sampleVidR1.mp4"
or use forward slash
path = "D:/Script/GCloud/sampleVidR1.mp4"
The above Code uses the Argparse module used to parse data from command-line interfaces.

On providing the python and video files locations via the commandline ,the script worked fine.