I am having trouble reading images from the S3 bucket. I can read images locally like that.
JavaScript
x
10
10
1
def load_image(path):
2
image = cv2.imread(path)
3
image = cv2.resize(image, (224, 224))
4
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
5
return image
6
7
for image_path in os.listdir(path):
8
image = load_image(path + "/" + image_path)
9
X.append(image)
10
But I have no idea why S3 said error
JavaScript
1
2
1
TypeError: Can't convert object of type 'list' to 'str' for 'filename'
2
Advertisement
Answer
You need to first establish a connection to S3, then download the image data, and finally decode the data with OpenCV.
For the first bit (connecting to S3), Boto3 is a good alternative (it’s the Python SDK for AWS).
Full sample code:
JavaScript
1
23
23
1
from io import BytesIO
2
3
import boto3
4
import cv2
5
import numpy as np
6
7
endpoint_url = {your_s3_endpoint_url}
8
bucket_name = {your_s3_bucket_name}
9
key = {your_image_key_within_the_bucket}
10
aws_access_key_id = {your_aws_access_key_id} # Better not to directly expose in source code
11
aws_secret_access_key = {your_aws_secret_access_key} # Better not to directly expose in source code
12
13
bucket = boto3.resource(
14
"s3",
15
endpoint_url=endpoint_url,
16
aws_access_key_id=aws_access_key_id,
17
aws_secret_access_key=aws_secret_access_key,
18
).Bucket(bucket_name)
19
file_stream = BytesIO()
20
bucket.Object(key).download_fileobj(file_stream)
21
np_1d_array = np.frombuffer(file_stream.getbuffer(), dtype="uint8")
22
img = cv2.imdecode(np_1d_array, cv2.IMREAD_COLOR)
23