Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13951

Invalid path, Path not pointing to a valid file, Annotating images

$
0
0

I am trying to annotate images for a machine learning model i want to create and i am getting the following error:

Annotating C:\mypath\Post_Event_Images_In_JPEG\tile_12_26.jpg and saving to C:\mypath\AnnotatedImages\annotated_after_tile_12_26.jpgError annotating C:\mypath\Post_Event_Images_In_JPEG\tile_12_26.jpg: invalid path, path not pointing to a valid file.

The code is the following:

def pair_images(before_folder, after_folder, num_images):    before_images = os.listdir(before_folder)[:num_images]    after_images = os.listdir(after_folder)[:num_images]    image_pairs = {}    for before_image in before_images:        common_part = os.path.splitext(before_image)[0]        after_image = f"{common_part}.jpg"          if after_image in after_images:            image_pairs[common_part] = (os.path.join(before_folder, before_image), os.path.join(after_folder, after_image))        else:            print(f"Warning: After image not found for {before_image}")    return image_pairsdef annotate_image(image_path, output_path):    detector = ObjectDetection()    detector.setModelTypeAsRetinaNet()    detector.setModelPath("path/to/resnet50_coco_best_v2.1.0.h5")  # Download this file from https://github.com/OlafenwaMoses/ImageAI/releases/tag/essential-v4    detector.loadModel()    print(f"Detecting objects in {image_path}")    detections = detector.detectObjectsFromImage(        input_image=image_path,        output_image_path=output_path,        minimum_percentage_probability=30    )    print("Detected objects:")    for detection in detections:        print(f"{detection['name']} - {detection['percentage_probability']}")    print(f"Annotation completed for {image_path}")def main():    # Specify the paths to the "before" and "after" image folders    before_folder = r"C:\Users\Erevos\Desktop\EYContest\Pre_Event_Images_In_JPEG"    after_folder = r"C:\Users\Erevos\Desktop\EYContest\Post_Event_Images_In_JPEG"    output_folder = r"C:\Users\Erevos\Desktop\EYContest\AnnotatedImages"    # Get the pairs of images    num_images_to_annotate = 500    image_pairs = pair_images(before_folder, after_folder, num_images_to_annotate)    # Annotate the first 500 images using ImageAI    os.makedirs(output_folder, exist_ok=True)    for common_part, (before_image, after_image) in image_pairs.items():        annotated_before_path = os.path.join(output_folder, f"annotated_before_{common_part}.jpg")        annotated_after_path = os.path.join(output_folder, f"annotated_after_{common_part}.jpg")        print(f"Annotating {before_image} and saving to {annotated_before_path}")        try:            annotate_image(before_image, annotated_before_path)        except Exception as e:            print(f"Error annotating {before_image}: {e}")        print(f"Annotating {after_image} and saving to {annotated_after_path}")        try:            annotate_image(after_image, annotated_after_path)        except Exception as e:            print(f"Error annotating {after_image}: {e}")    print("Annotation and saving completed.")if __name__ == "__main__":    main()

Note that i have manually created the folders to check if this was the case, but the error persists.


Viewing all articles
Browse latest Browse all 13951

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>