File size: 4,416 Bytes
86a74e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import logging
import urllib.request
from pathlib import Path

# Set up logging
logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)

# Sample X-ray image URLs (from public sources)
SAMPLE_IMAGES = [
    # Normal chest X-ray
    {
        "url": "https://prod-images-static.radiopaedia.org/images/53448173/322830a37f0fa0852773ca2db3e8d8_big_gallery.jpeg",
        "filename": "normal_chest_xray.jpg",
        "description": "Normal chest X-ray",
    },
    # X-ray with pneumonia
    {
        "url": "https://prod-images-static.radiopaedia.org/images/52465460/e4d8791bd7502ab72af8d9e5c322db_big_gallery.jpg",
        "filename": "pneumonia_xray.jpg",
        "description": "X-ray with pneumonia",
    },
    # X-ray with cardiomegaly
    {
        "url": "https://prod-images-static.radiopaedia.org/images/556520/cf17c05750adb04b2a6e23afb47c7d_big_gallery.jpg",
        "filename": "cardiomegaly_xray.jpg",
        "description": "X-ray with cardiomegaly",
    },
    # X-ray with lung nodule
    {
        "url": "https://prod-images-static.radiopaedia.org/images/19972291/41eed1a2cdad06d26c3f415a6ed65a_big_gallery.jpeg",
        "filename": "nodule_xray.jpg",
        "description": "X-ray with lung nodule",
    },
]


def download_sample_images(output_dir="data/sample"):
    """

    Download sample X-ray images for testing.



    Args:

        output_dir (str): Directory to save images



    Returns:

        list: Paths to downloaded images

    """
    # Get the directory of the script
    script_dir = Path(__file__).resolve().parent.parent

    # Create output directory if it doesn't exist
    output_path = script_dir / output_dir
    output_path.mkdir(parents=True, exist_ok=True)

    downloaded_paths = []

    for image in SAMPLE_IMAGES:
        try:
            filename = image["filename"]
            url = image["url"]
            output_file = output_path / filename

            # Skip if file already exists
            if output_file.exists():
                logger.info(f"File already exists: {output_file}")
                downloaded_paths.append(str(output_file))
                continue

            # Download the image
            logger.info(f"Downloading {url} to {output_file}")

            # Set a user agent to avoid blocking
            opener = urllib.request.build_opener()
            opener.addheaders = [("User-Agent", "Mozilla/5.0")]
            urllib.request.install_opener(opener)

            # Download the file
            urllib.request.urlretrieve(url, output_file)

            logger.info(f"Successfully downloaded {filename}")
            downloaded_paths.append(str(output_file))

        except Exception as e:
            logger.error(f"Error downloading {image['url']}: {e}")

    logger.info(
        f"Downloaded {len(downloaded_paths)} out of {len(SAMPLE_IMAGES)} images"
    )
    return downloaded_paths


def create_sample_info_file(output_dir="data/sample"):
    """

    Create a text file with information about the sample images.



    Args:

        output_dir (str): Directory with sample images

    """
    # Get the directory of the script
    script_dir = Path(__file__).resolve().parent.parent

    # Output path
    output_path = script_dir / output_dir
    info_file = output_path / "sample_info.txt"

    with open(info_file, "w") as f:
        f.write("# Sample X-ray Images\n\n")

        for image in SAMPLE_IMAGES:
            f.write(f"## {image['filename']}\n")
            f.write(f"Description: {image['description']}\n")
            f.write(f"Source: {image['url']}\n\n")

        f.write(
            "\nThese images are used for testing and demonstration purposes only.\n"
        )
        f.write(
            "Please note that these images are from public medical education sources.\n"
        )
        f.write("Do not use for clinical decision making.\n")

    logger.info(f"Created sample info file: {info_file}")


if __name__ == "__main__":
    # Download sample images
    downloaded_paths = download_sample_images()

    # Create info file
    create_sample_info_file()

    print(f"Downloaded {len(downloaded_paths)} sample images.")
    print("Run the application with: python app.py")