This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot.py
252 lines (161 loc) · 6.14 KB
/
plot.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import matplotlib.pyplot as plt
import cv2
import numpy as np
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
PATH_TO_VIDEO = "data/test-your-awareness.avi"
FRAME_RATE = 30
FRAME_HIGH = 710
FRAME_LENGTH = 950
""" ******************************** fonctions de testing ********************************* """
def funnyDot(dot):
dot['positive_x'] = positivitySwitch(dot['x'], dot['positive_x'], FRAME_LENGTH)
dot['positive_y'] = positivitySwitch(dot['y'], dot['positive_y'], FRAME_HIGH)
moveXDot(dot)
moveYDot(dot)
return dot
def moveXDot(dot):
if dot['positive_x']:
dot['x'] += 5
else:
dot['x'] -= 5
def moveYDot(dot):
if dot['positive_y']:
dot['y'] += 5
else:
dot['y'] -= 5
def positivitySwitch(dotCoordinate, dotPositivity, limit):
if dotCoordinate >= limit or dotCoordinate <= 0:
return not dotPositivity
else:
return dotPositivity
""" ********************************* Fonctions principales ********************************** """
def windowRenderer():
fig, ax = plt.subplots(figsize=(12, 9))
plt.subplots_adjust(left=0, bottom=0, right=1, top=1)
ax.axis('off')
return fig, ax
def heatmapWhiteFrame(image, dot):
"""
permet de faire le plotting de la heatmap sur l'image passée en paramètre
:param : image : chaque frame de la vidéo
:param : dot : le point permettant de tracer la heatmap
:return retourne l'image modifiée sous la forme d'un tableau de tableaux
"""
fig, ax = windowRenderer()
canvas = FigureCanvas(fig)
ax.imshow(image)
# PLOTTING : plot the dot to the x, y position
# ro : bleu, markersize: taille du cercle, alpha: opacité
ax.plot(dot['x'], dot['y'], 'bo', markersize=30, alpha=0.01)
canvas.draw()
buf = canvas.buffer_rgba()
# convert to a NumPy array
X = np.asarray(buf) # X = image retournée
return X
"""
:param : image : chaque frame de la vidéo
:param : dot : le point suivant la trajectoire des yeux
"""
def plotDotOnImage(image, dot):
fig, ax = windowRenderer()
canvas = FigureCanvas(fig)
ax.imshow(image)
# PLOTTING : plot the dot to the x, y position
ax.plot(dot['x'], dot['y'], 'bo') # ro = bleu , bo = rouge, go = green
canvas.draw()
buf = canvas.buffer_rgba()
# convert to a NumPy array
X = np.asarray(buf) # X = l'image non utilisée
# Remove this for test the matplotlib plot
# plt.show()
cv2.imshow('frame', X)
# -----------------------------------------------------------------------------------------------------------
def readVideo(data):
cap = cv2.VideoCapture(PATH_TO_VIDEO)
# une image créée à partir de pixels blancs
whiteFrame = 255 * np.ones((710, 950, 3), np.uint8)
movingDot = {'x': 0, 'y': 0, 'positive_x': False, 'positive_y': False}
if not cap.isOpened():
print("Error opening video stream or file")
i = 0
try:
# Read until video is completed
while cap.isOpened():
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
movingDot = {'x': data[i]["LporX"] / 1.6, 'y': data[i]["LporY"] / 1.6, 'positive_x': False,
'positive_y': False}
# cv2.waitKey(int(1000/FRAME_RATE))
plt.imsave("Result.jpg", whiteFrame)
whiteFrame = heatmapWhiteFrame(whiteFrame, movingDot) # stockage des points dans l'image blanche
plotDotOnImage(frame, movingDot) # trajectoire des points sur la vidéo
# funnyDot(movingDot)
################## Permet de transformer en image le plot #######################################
plt.close('all')
if cv2.waitKey(1) == ord('q'):
break
i += 1
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
except:
pass
# Closes all the frames
cv2.destroyAllWindows()
def customReadVideo(data):
cap = cv2.VideoCapture(PATH_TO_VIDEO)
if (cap.isOpened() == False):
print("Error opening video stream or file")
i = 0
# Read until video is completed
while (cap.isOpened()):
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
movingDot = {'x': data[i]["LporX"] / 1.6, 'y': data[i]["LporY"] / 1.6, 'positive_x': False, 'positive_y': False}
cv2.waitKey(int(1000/FRAME_RATE))
drawSquarre(30, int(data[i]["LporX"] / 1.6), int(data[i]["LporY"] / 1.6), frame)
cv2.imshow('frame', frame)
plt.close('all')
if cv2.waitKey(1) == ord('q'):
break
i += 1
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
""" *********************************** TODO functions ****************************************** """
def drawSquarre(size, y_center, x_center, image, color=[0, 255, 0]):
if(size > 0):
image[x_center][y_center] = color
for x in range(-size+1, size):
for y in range(-size+1, size):
image[x_center + x][y_center + y]=color
def drawMap(min_x, max_x, min_y, max_y, image, color=[0, 255, 0]):
for x in range(min_x, max_x):
for y in range (min_y, max_y):
image[x][y] = color
"""def drawCircle(radius, x_center, y_center, list, image, color):
x=0,
y=radius
m=5-4*radius
while x <=y :
list[x_center+x][y_center+y] = "*"
list[x_center+y][y_center+x] = "*"
list[x_center-x][y_center+y] = "*"
list[x_center-y][y_center+x] = "*"
list[x_center+x][y_center+y] = "*"
list[x_center+y][y_center+x] = "*"
list[x_center-x][y_center-y] = "*"
list[x_center-y][y_center-x] = "*"
if m > 0 :
y -= 1
m = m+8*x+4
print (list)
"""