-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
76 lines (66 loc) · 2.69 KB
/
handler.py
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
import boto3
import cv2
import os
import numpy as np
from io import BytesIO
s3 = boto3.resource('s3')
def opencv(event, context):
bucketName = event['Records'][0]['s3']['bucket']['name']
objectKey = event['Records'][0]['s3']['object']['key']
img_obj = s3.Object(
bucket_name=bucketName,
key=objectKey,
)
obj_body = img_obj.get()['Body'].read()
try:
img = cv2.imdecode(np.asarray(bytearray(obj_body)), cv2.IMREAD_COLOR)
#Improve sharpness of image with unsharp masking.
smoothed_image = cv2.GaussianBlur(img, (9, 9), 10)
unsharped_masking = cv2.addWeighted(img, 1.5, smoothed_image, -0.5, 0)
#improve image contrast
# Improve contrast of image with contrast limited adaptive histogram equalization (CLAHE).
clahe = cv2.createCLAHE(clipLimit=4.0)
H, S, V = cv2.split(cv2.cvtColor(unsharped_masking, cv2.COLOR_BGR2HSV))
eq_V = clahe.apply(V)
eq_image = cv2.cvtColor(cv2.merge([H, S, eq_V]), cv2.COLOR_HSV2BGR)
#add padding to image
h, w = img.shape[:2]
if h > w:
diff = h-w
pad_width = np.floor((diff)/2).astype(np.int)
if (diff % 2) == 0:
padded_image= np.pad(eq_image, ((0, 0), (pad_width, pad_width), (0, 0)),
'constant', constant_values=0)
else:
padded_image= np.pad(eq_image, ((0, 0), (pad_width, pad_width+1), (0, 0)),
'constant', constant_values=0)
elif h < w:
diff = w-h
pad_width = np.floor((diff)/2).astype(np.int)
if (diff % 2) == 0:
padded_image= np.pad(eq_image, ((pad_width, pad_width), (0, 0), (0, 0)),
'constant', constant_values=0)
else:
padded_image= np.pad(eq_image, ((pad_width, pad_width+1), (0, 0), (0, 0)),
'constant', constant_values=0)
else:
padded_image= eq_image
dim = (512, 512)
resized_img = cv2.resize(padded_image, dim, interpolation = cv2.INTER_CUBIC)
is_success,img_arr = cv2.imencode('.png',resized_img)
resized_imgbuffer = BytesIO(img_arr)
except Exception as e:
print(e)
print('Error resizing file with OpenCV')
raise e
try:
resized_imgbuffer.seek(0)
resized_imgobj = s3.Object(
bucket_name=os.environ['OPENCV_OUTPUT_BUCKET'],
key=objectKey,
)
resized_imgobj.put(Body=resized_imgbuffer, ContentType='image/png')
except Exception as e:
print(e)
print('Error uploading file to output bucket')
raise e