|
import logging
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
SAMPLE_IMAGES = [
|
|
|
|
{
|
|
"url": "https://prod-images-static.radiopaedia.org/images/53448173/322830a37f0fa0852773ca2db3e8d8_big_gallery.jpeg",
|
|
"filename": "normal_chest_xray.jpg",
|
|
"description": "Normal chest X-ray",
|
|
},
|
|
|
|
{
|
|
"url": "https://prod-images-static.radiopaedia.org/images/52465460/e4d8791bd7502ab72af8d9e5c322db_big_gallery.jpg",
|
|
"filename": "pneumonia_xray.jpg",
|
|
"description": "X-ray with pneumonia",
|
|
},
|
|
|
|
{
|
|
"url": "https://prod-images-static.radiopaedia.org/images/556520/cf17c05750adb04b2a6e23afb47c7d_big_gallery.jpg",
|
|
"filename": "cardiomegaly_xray.jpg",
|
|
"description": "X-ray with cardiomegaly",
|
|
},
|
|
|
|
{
|
|
"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
|
|
"""
|
|
|
|
script_dir = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
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
|
|
|
|
|
|
if output_file.exists():
|
|
logger.info(f"File already exists: {output_file}")
|
|
downloaded_paths.append(str(output_file))
|
|
continue
|
|
|
|
|
|
logger.info(f"Downloading {url} to {output_file}")
|
|
|
|
|
|
opener = urllib.request.build_opener()
|
|
opener.addheaders = [("User-Agent", "Mozilla/5.0")]
|
|
urllib.request.install_opener(opener)
|
|
|
|
|
|
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
|
|
"""
|
|
|
|
script_dir = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
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__":
|
|
|
|
downloaded_paths = download_sample_images()
|
|
|
|
|
|
create_sample_info_file()
|
|
|
|
print(f"Downloaded {len(downloaded_paths)} sample images.")
|
|
print("Run the application with: python app.py")
|
|
|