In [11]:
import cv2
import numpy as np
def run_preview():
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
cv2.destroyWindow("preview")
# run_preview()
In [38]:
distorted = cv2.imread('/home/moritz/Schreibtisch/kicker_fisheye.png')
distorted = cv2.cvtColor(distorted, cv2.COLOR_BGR2RGB)
npz_calib_file = np.load('calibration_data.npz')
distCoeff = npz_calib_file['distCoeff']
intrinsic_matrix = npz_calib_file['intrinsic_matrix']
In [40]:
import matplotlib.pyplot as plt
plt.imshow(cv2.resize(distorted, (1920, 1080)))
Out[40]:
In [39]:
undistorted = cv2.undistort(cv2.resize(distorted, (1920, 1080)), intrinsic_matrix, distCoeff, None)
plt.imshow(undistorted)
Out[39]:
In [24]:
distCoeff
Out[24]:
In [41]:
intrinsic_matrix[0,0] = 1000
intrinsic_matrix[1,1] = 1000
undistorted = cv2.undistort(cv2.resize(distorted, (1920, 1080)), intrinsic_matrix, distCoeff, None)
plt.imshow(undistorted)
Out[41]:
In [42]:
distCoeff
Out[42]:
In [50]:
cap = cv2.VideoCapture(0)
w = cap.get(cv2.CAP_PROP_FRAME_WIDTH);
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT);
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output2.mp4',fourcc, 15.0, (int(w),int(h)))
In [ ]:
# record video
while (cap.isOpened()):
ret, frame = cap.read()
if ret:
out.write(frame)
cv2.imshow('Video Stream', frame)
else:
break
In [10]:
cap.release()
out.release()
cv2.destroyAllWindows()
In [35]:
# label the video by mouse dragging
param = None
def mousePosition(event,x,y,flags, passed_param):
global param
if event == cv2.EVENT_MOUSEMOVE:
# print(x,y)
param = (x,y)
# If the input is the camera, pass 0 instead of the video file name
cap = cv2.VideoCapture('output.mp4')
# Check if camera opened successfully
if (cap.isOpened()== False):
print("Error opening video stream or file")
delay = 25
cv2.namedWindow('Frame')
cv2.setMouseCallback('Frame', mousePosition)
ball_pos = []
# Read until video is completed
while(cap.isOpened()):
frame_pos = int(cap.get(cv2.CAP_PROP_POS_FRAMES))
# Capture frame-by-frame
ret, frame = cap.read()
if ret is not True:
break
# Display the resulting frame
cv2.imshow('Frame',frame)
# Press Q on keyboard to exit
key = cv2.waitKey(int(delay)) & 0xFF
if key == ord('q'):
break
elif key == ord('n'):
pass
elif key == ord('j'):
delay *= 1.5
elif key == ord('k'):
delay /= 1.5
elif key == ord('h'):
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_pos - 40)
delay = 45
try:
ball_pos[frame_pos] = param
except IndexError:
ball_pos.append(param)
assert len(ball_pos) == frame_pos + 1
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
In [18]:
frame_pos
Out[18]:
In [25]:
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
In [36]:
len(ball_pos)
Out[36]:
In [37]:
ball_pos[100:200]
Out[37]:
In [38]:
pd
Out[38]:
In [39]:
pd.DataFrame(ball_pos)
Out[39]:
In [42]:
pd.DataFrame({'x': [v[0] if v else None for v in ball_pos], 'y': [v[1] if v else None for v in ball_pos]})
Out[42]:
In [ ]: