Upload load.py
Browse files
load.py
CHANGED
@@ -15,14 +15,16 @@ def download_image_with_retries(image_url, output_file):
|
|
15 |
print(f"Download failed: {e}")
|
16 |
return False
|
17 |
|
18 |
-
def
|
19 |
try:
|
20 |
with Image.open(input_path) as img:
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
return True
|
24 |
except Exception as e:
|
25 |
-
print(f"Error converting
|
26 |
return False
|
27 |
|
28 |
def verify_and_download_images(data):
|
@@ -32,19 +34,19 @@ def verify_and_download_images(data):
|
|
32 |
for key, value in data.items():
|
33 |
image_url = value['imageURL']
|
34 |
ext = os.path.splitext(image_url)[1].lower() # Ensure extension is in lowercase for comparison
|
35 |
-
|
36 |
-
output_ext = '.jpg' if
|
37 |
output_file = f'{images_directory}/{key}{output_ext}'
|
38 |
|
39 |
if not os.path.exists(output_file):
|
40 |
print(f"Image {key}{output_ext} not found, attempting to download...")
|
41 |
-
temp_output_file = output_file if not
|
42 |
if not download_image_with_retries(image_url, temp_output_file):
|
43 |
print(f"Warning: Could not download image {image_url}")
|
44 |
-
elif
|
45 |
-
# Convert
|
46 |
-
if not
|
47 |
-
print(f"Warning: Could not convert
|
48 |
|
49 |
# Load the dataset JSON file (assuming you have a dataset.json file in the same directory)
|
50 |
with open('dataset.json', 'r') as fp:
|
|
|
15 |
print(f"Download failed: {e}")
|
16 |
return False
|
17 |
|
18 |
+
def convert_to_jpg(input_path, output_path):
|
19 |
try:
|
20 |
with Image.open(input_path) as img:
|
21 |
+
# Convert image to RGB to ensure compatibility if original is in a different mode (e.g., RGBA, P)
|
22 |
+
rgb_img = img.convert('RGB')
|
23 |
+
rgb_img.save(output_path, 'JPEG')
|
24 |
+
os.remove(input_path) # Remove the original image file after conversion
|
25 |
return True
|
26 |
except Exception as e:
|
27 |
+
print(f"Error converting image to JPG: {e}")
|
28 |
return False
|
29 |
|
30 |
def verify_and_download_images(data):
|
|
|
34 |
for key, value in data.items():
|
35 |
image_url = value['imageURL']
|
36 |
ext = os.path.splitext(image_url)[1].lower() # Ensure extension is in lowercase for comparison
|
37 |
+
needs_conversion = ext in ['.gif', '.png']
|
38 |
+
output_ext = '.jpg' if needs_conversion else ext
|
39 |
output_file = f'{images_directory}/{key}{output_ext}'
|
40 |
|
41 |
if not os.path.exists(output_file):
|
42 |
print(f"Image {key}{output_ext} not found, attempting to download...")
|
43 |
+
temp_output_file = output_file if not needs_conversion else f'{images_directory}/{key}{ext}'
|
44 |
if not download_image_with_retries(image_url, temp_output_file):
|
45 |
print(f"Warning: Could not download image {image_url}")
|
46 |
+
elif needs_conversion:
|
47 |
+
# Convert image to JPG
|
48 |
+
if not convert_to_jpg(temp_output_file, output_file):
|
49 |
+
print(f"Warning: Could not convert image to JPG for image {image_url}")
|
50 |
|
51 |
# Load the dataset JSON file (assuming you have a dataset.json file in the same directory)
|
52 |
with open('dataset.json', 'r') as fp:
|