Skip to content

Instantly share code, notes, and snippets.

@smeschke
Created June 10, 2018 16:15
Show Gist options
  • Select an option

  • Save smeschke/85c3090368bf65afdaa3f71eaaef9efe to your computer and use it in GitHub Desktop.

Select an option

Save smeschke/85c3090368bf65afdaa3f71eaaef9efe to your computer and use it in GitHub Desktop.
Tracking object writer with smothing
import cv2
import numpy as np
import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
from scipy import signal
import cv2
from matplotlib import pyplot as plt
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd
#path to source data
data_source = '/home/stephen/Desktop/2.csv'#ball_tracking_data/744_tracking_data.csv'
#read data from csv file into data frame
df = pd.read_csv(data_source)
#path to source vide
source_path = '/home/stephen/Desktop/trick.avi'#source_clips/744.avi'
#path to outfile for video
video_out_name = '/home/stephen/Desktop/smoothed.avi'
cols, rows, fps = 360,580,60.0#480,848, 60.0
#create outfile
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(video_out_name,fourcc, fps, (cols,rows))
#start at frame number 0
frame_number = 0
#capture source
cap = cv2.VideoCapture(source_path)
#make a list of colors
colors = (255,0,0), (0,225,0),(0,0,255), (123,0,123), (123,0,255), (0,255,255), (255,123,123), (123,255,123)
#get column names
cols = []
for i in df: cols.append(i)
font = cv2.FONT_HERSHEY_SIMPLEX
ssss = 60
sss = 33
ggg = 3
while True:
#ssss+=.01
#sss = int(ssss)*2+1
_, img = cap.read()
try: lll = img.shape[:2]
except: break
for obj in range(len(cols)/2):
y = signal.savgol_filter(df[cols[obj*2+1]],sss, ggg)
x = signal.savgol_filter(df[cols[obj*2]],sss, ggg)
#y = df[cols[obj*2+1]]
#x = df[cols[obj*2]]
xa = int(x[frame_number])
ya = int(y[frame_number])
xy = (xa,ya)
color = colors[obj]
#cv2.circle(img, xy, 20, color, 3)
cv2.circle(img, xy, 8, color, 1)
cv2.circle(img, xy, 11, color, 1)
for i in range(25):
if frame_number-i>1:
x1 = int(x[frame_number-i])
y1 = int(y[frame_number-i])
xy1 = (x1,y1)
cv2.line(img, xy, xy1, color, 2)
xy = xy1
cv2.imshow('img', img)
k = cv2.waitKey(1)
if k == 27: break
frame_number += 1
out.write(img)
print lll
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment