-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2_1.py
132 lines (98 loc) · 3.99 KB
/
ec2_1.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
import boto3
import numpy as np
import cv2
import json
import pafy
import io
from scipy.spatial import distance as dist
# import time
from PIL import Image, ImageDraw, ExifTags, ImageColor
frame_skip = 10
cur_frame = 0
MIN_DISTANCE = 100
# Online Video
# url = "https://www.youtube.com/watch?v=ORrrKXGx2SE"
# video = pafy.new(url)
# best = video.getbest(preftype="mp4")
# capture = cv2.VideoCapture(best.url)
#upload bucket4
#Get video from S3
s3_client = boto3.client('s3')
bucket = 'video-input-bucket2'
key = 'pedestrians.mp4'
url = s3_client.generate_presigned_url('get_object',
Params = {'Bucket': bucket, 'Key': key},
ExpiresIn = 600) #this url will be available for 600 seconds
capture = cv2.VideoCapture(url)
# #local video
# capture = cv2.VideoCapture("pedestrians.mp4")
# Create a Rekognition client
client=boto3.client('rekognition')
while True:
violate = set()
success, frame = capture.read() # get next frame from video
if not success:
break
# frame = cv2.resize(frame, (1080,720), interpolation = cv2.INTER_AREA)
imgHeight,imgWidth, channels = frame.shape
if cur_frame % frame_skip == 0: # only analyze every n frames
print('Working on frame number : {}'.format(cur_frame))
pil_img = Image.fromarray(frame) # convert opencv frame (with type()==numpy) into PIL Image
stream = io.BytesIO()
pil_img.save(stream, format='JPEG') # convert PIL Image to Bytes
bin_img = stream.getvalue()
response = client.detect_labels(Image={'Bytes': bin_img}) # call Rekognition
persons = next(item for item in response['Labels'] if item["Name"] == "Person")
if(len(persons["Instances"]) >= 2):
centroids = np.array([( (r["BoundingBox"]["Left"]+(r["BoundingBox"]["Width"])/2)*imgWidth, \
(r["BoundingBox"]["Top"]+(r["BoundingBox"]["Height"])/2)*imgHeight ) \
for r in persons["Instances"] ])
D = dist.cdist(centroids, centroids, metric="euclidean")
# loop over the upper triangular of the distance matrix
for i in range(0, D.shape[0]):
for j in range(i + 1, D.shape[1]):
# check to see if the distance between any two
# centroid pairs is less than the configured number
# of pixels
if D[i, j] < MIN_DISTANCE:
# update our violation set with the indexes of
# the centroid pairs
violate.add(i)
violate.add(j)
# loop over the results
for j in range(len(persons["Instances"])):
# extract the bounding box and centroid coordinates, then
# initialize the color of the annotation
# (startX, startY, endX, endY) = bbox
dimensions = (persons["Instances"][j]["BoundingBox"])
#Storing them in variables
boxWidth = dimensions['Width']
boxHeight = dimensions['Height']
boxLeft = dimensions['Left']
boxTop = dimensions['Top']
#Plotting points of rectangle
start_point = (int(boxLeft*imgWidth), int(boxTop*imgHeight))
end_point = (int((boxLeft + boxWidth)*imgWidth),int((boxTop + boxHeight)*imgHeight))
#Drawing Bounding Box on the coordinates
# if the index pair exists within the violation set, then
color = (0, 255, 0)
# update the color
if j in violate:
color = (0, 0, 255)
# draw (1) a bounding box around the person and (2) the
# centroid coordinates of the person,
cv2.rectangle(frame, start_point, end_point, color, 2)
if len(violate) > 15:
image_string = cv2.imencode('.jpg', frame)[1].tostring()
s3_client.put_object(Bucket="violations-bucket", Key = str(cur_frame)+".jpeg", Body=image_string)
frame2 = cv2.resize(frame, (20,10), interpolation = cv2.INTER_AREA)
print(frame2.shape)
frame_string = cv2.imencode('.jpg', frame2)[1].tostring()
s3_client.put_object(Bucket="video-input-bucket2", Key = "output.jpeg", Body=frame_string)
# time.sleep(5)
# frame = cv2.resize(frame, (1080,720), interpolation = cv2.INTER_AREA)
# cv2.imshow('labelled.jpg',frame)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
cur_frame += 1
# cv2.destroyAllWindows()