-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplot_centroids.py
38 lines (31 loc) · 1.46 KB
/
plot_centroids.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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import cv2
import random
cap = cv2.VideoCapture('juggle.mp4')
ret, frame = cap.read()
ratio = .5 # resize ratio
image = cv2.resize(frame, (0, 0), None, ratio, ratio) # resize image
df = pd.read_csv('juggle.csv') # reads csv file and makes it a dataframe
rows, columns = df.shape # shape of dataframe
print('Rows:',rows)
print('Columns:',columns)
fig1 = plt.figure(figsize=(10, 8)) # width and height of image
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) # plots first frame of video
for i in range(columns - 1): # loops through all columns of dataframe, -1 since index is counted
y = df.loc[df[str(i)].notnull(), str(i)].tolist() # grabs not null data from column
df2 = pd.DataFrame(y, columns=['xy']) # create another dataframe with only one column
# create another dataframe where it splits centroid x and y values into two columns
df3 = pd.DataFrame(df2['xy'].str[1:-1].str.split(',', expand=True).astype(float))
df3.columns = ['x', 'y'] # renames columns
# plots series with random colors
plt.plot(df3.x, df3.y, marker='x', color=[random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)],
label='ID: ' + str(i))
# plot info
plt.title('Tracking of Centroids')
plt.xlabel('X Position')
plt.ylabel('Y Position')
plt.legend(bbox_to_anchor=(1, 1.2), fontsize='x-small') # legend location and font
plt.show()
fig1.savefig('juggle.png') # saves image