-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrack_pool_from_images_send_to_dynamodb.py
201 lines (191 loc) · 7.52 KB
/
track_pool_from_images_send_to_dynamodb.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import cv2
import numpy as np
from imutils import contours
import imutils
import glob
import os
import pandas as pd
import boto3
from botocore.exceptions import ClientError
import decimal
def calculateIntersection(a0, a1, b0, b1):
"""
Calculate intersection between interval.
Reference: https://stackoverflow.com/a/48537479
"""
if a0 >= b0 and a1 <= b1: # Contained
intersection = a1 - a0
elif a0 < b0 and a1 > b1: # Contains
intersection = b1 - b0
elif a0 < b0 and a1 > b0: # Intersects right
intersection = a1 - b0
elif a1 > b1 and a0 < b1: # Intersects left
intersection = b1 - a0
else: # No intersection (either side)
intersection = 0
return intersection
if __name__ == "__main__":
dynamodb = boto3.resource('dynamodb', region_name='eu-west-1')
table = dynamodb.Table('PoolTracker')
job_ID = "protoshape_first_video"
frame_ID = 3
response = table.put_item(
Item={
'job_ID': job_ID,
'frame_ID': frame_ID,
'pool_features': {
'area': decimal.Decimal(34.0),
'mean_intensity': decimal.Decimal(0.0),
'radius': decimal.Decimal(23.5)
}
}
)
print(response)
try:
response = table.get_item(
Key={
'job_ID': job_ID,
'frame_ID': frame_ID,
}
)
except ClientError as e:
print(e.response['Error']['Message'])
else:
item = response['Item']
print("GetItem succeeded:")
print(item)
# df = pd.DataFrame(columns=['Frame_Index', 'Area', 'Mean', 'Radius'])
#
# image_file_list = []
# # for filename in glob.glob('./out/protoshape/frames_test_multiple/*.png'):
# for filename in glob.glob('./out/protoshape/frames/*.png'):
# image_file_list.append(filename)
#
# image_file_list.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))
#
# for image_file_path in image_file_list:
# print ("Processing: " + image_file_path)
# # Load an color image in grayscale
# #image = cv2.imread('./out/protoshape/frames/frame_001521.png')
# image = cv2.imread(image_file_path)
#
# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#
# # TODO convert RGB to HSV for better light sensitivity
# # hsv1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2HSV)
# # h, s, v1 = cv2.split(hsv1)
#
#
# # Blur to remove noise (radius must be ODD)
# gray = cv2.GaussianBlur(gray, (5, 5), 0) # 21,21
# # (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
# # image = orig.copy()
# # cv2.circle(image, maxLoc, 21, (255, 0, 0), 2)
#
# # threshold the image to reveal light regions in the blurred image
# thresh = cv2.threshold(gray, 210, 255, cv2.THRESH_BINARY)[1]
# # display the results of the naive attempt
# #cv2.imshow("Naive", image)
#
# # perform a series of erosions and dilations to remove
# # any small blobs of noise from the thresholded image
# # thresh = cv2.erode(thresh, None, iterations=2)
# # thresh = cv2.dilate(thresh, None, iterations=4)
# thresh = cv2.erode(thresh, None, iterations=1)
# thresh = cv2.dilate(thresh, None, iterations=1)
#
# # cv2.imwrite('./out/protoshape/interim/denoised2.png', thresh)
#
# # Part 1 ROI
# part_1_start_x, part_1_start_y, part_1_end_x, part_1_end_y = [65, 270, 172, 352]
#
# # Contours
# cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#
# cnts = imutils.grab_contours(cnts)
# if len(cnts) != 0:
# #Intersecting contours
# intersecting_contours = []
# for contour in cnts:
# contour_start_x, contour_start_y, w, h = cv2.boundingRect(contour)
# contour_end_x = contour_start_x + w
# contour_end_y = contour_start_y + h
#
# width = calculateIntersection(contour_start_x, contour_end_x, part_1_start_x, part_1_end_x)
# height = calculateIntersection(contour_start_y, contour_end_y, part_1_start_y, part_1_end_y)
#
# area = width * height
#
# if area > 400:
# intersecting_contours.append(contour)
#
# if len(intersecting_contours) != 0:
# # sorted_intersecting_contours = imutils.contours.sort_contours(intersecting_contours)[0]
# sorted_intersecting_contours = sorted(intersecting_contours, key=cv2.contourArea, reverse=True)
#
# largest_contour = sorted_intersecting_contours[0]
#
# # Get area
# area = cv2.contourArea(largest_contour)
#
# # Get average intensity
# contour_mask = np.zeros(gray.shape, np.uint8)
# cv2.drawContours(contour_mask, largest_contour, -1, 255, -1)
# mean = cv2.mean(gray, mask=contour_mask)
#
# # Get radius length TODO: or use perimeter aka length or both?
# ((cX, cY), radius) = cv2.minEnclosingCircle(largest_contour)
#
# df = df.append({'Frame_Index': os.path.basename(image_file_path), 'Area': area, 'Mean': mean[0], 'Radius': radius}, ignore_index=True)
# else:
# df = df.append({'Frame_Index': os.path.basename(image_file_path), 'Area': 0, 'Mean': 0, 'Radius': 0}, ignore_index=True)
# else:
# df = df.append({'Frame_Index': os.path.basename(image_file_path), 'Area': 0, 'Mean': 0, 'Radius': 0}, ignore_index=True)
#
# #print (df)
# df.to_csv('./out/protoshape/interim/pool_data_part_1.csv')
#
# #cv2.drawContours(image, [largest_contour], -1, (0, 0, 255), 1)
# #cv2.rectangle(image, (part_1_start_x, part_1_start_y), (part_1_end_x, part_1_end_y), (0, 255, 0))
#
# #cv2.imwrite('./out/protoshape/interim/contour.png', image)
#
# #
# #
# #
# # cnts = imutils.contours.sort_contours(cnts)[0]
# # cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]
# #
# #
# # # Just get the largest contour
# # cnt = cnts[0]
# # area = cv2.contourArea(cnt)
# #
# # cv2.drawContours(image, [cnt], -1, (0, 0, 255), 2)
# # # ((cX, cY), radius) = cv2.minEnclosingCircle(cnt)
# # # cv2.circle(image, (int(cX), int(cY)), int(radius), (0, 0, 255), 3)
# #
# # cv2.putText(image, str(area), (70, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# #
# # # Just get the largest contour
# # cnt = cnts[2]
# # area = cv2.contourArea(cnt)
# #
# # cv2.drawContours(image, [cnt], -1, (0, 0, 255), 2)
# # # ((cX, cY), radius) = cv2.minEnclosingCircle(cnt)
# # # cv2.circle(image, (int(cX), int(cY)), int(radius), (0, 0, 255), 3)
# #
# # # cv2.putText(image, str(area), (70, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# #
# # # Just get the largest contour
# # cnt = cnts[3]
# # area = cv2.contourArea(cnt)
# #
# # cv2.drawContours(image, [cnt], -1, (0, 0, 255), 2)
# # # ((cX, cY), radius) = cv2.minEnclosingCircle(cnt)
# # # cv2.circle(image, (int(cX), int(cY)), int(radius), (0, 0, 255), 3)
# #
# # # cv2.putText(image, str(area), (70, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# #
# # # Write frame
# # cv2.imwrite('./out/protoshape/MTC.png', image)