Last active
May 3, 2016 05:11
-
-
Save smeschke/640a5932d77b10d3f1ff8bc1ea6916d6 to your computer and use it in GitHub Desktop.
adds sunglasses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
cap = cv2.VideoCapture(0) | |
# params for ShiTomasi corner detection | |
feature_params = dict( maxCorners = 50, | |
qualityLevel = 0.2, | |
minDistance = 7, | |
blockSize = 7 ) | |
# Parameters for lucas kanade optical flow | |
lk_params = dict( winSize = (13,13), | |
maxLevel = 3, | |
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 100, 0.5)) | |
# Take first frame and find corners in it | |
ret, img = cap.read() | |
old_frame = img | |
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) | |
#get initial points to track | |
p0 = np.array([[[300,200]]], np.float32) | |
d0 = np.array([[[380,200]]], np.float32) | |
# Create a mask image for drawing purposes | |
mask = np.zeros_like(old_frame) | |
while(1): | |
ret,frame = cap.read() | |
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
# calculate optical flow | |
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params) | |
d1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, d0, None, **lk_params) | |
# Select good points | |
good_new = p1 | |
good_old = p0 | |
xy = int(p1[0][0][0]), int(p1[0][0][1]) | |
good_new_d = d1 | |
good_old_d = d0 | |
xy1 = int(d1[0][0][0]), int(d1[0][0][1]) | |
#make line and circles and show frame | |
frame = cv2.circle(frame,xy,23,(0,0,0),-1) | |
frame = cv2.circle(frame,xy1,23,(0,0,0),-1) | |
frame = cv2.circle(frame,(xy[0],xy[1]+5),8,(255,255,255),-1) | |
frame = cv2.circle(frame,(xy1[0],xy1[1]+5),8,(255,255,255),-1) | |
cv2.line(frame, (xy[0],xy[1]-7), (xy1[0],xy1[1]-7), (0,0,0), 7) | |
#show image | |
cv2.imshow('frame',cv2.flip(frame,1)) | |
k = cv2.waitKey(1) & 0xff | |
if k == 27: | |
break | |
# Now update the previous frame and previous points | |
old_gray = frame_gray.copy() | |
p0 = good_new.reshape(-1,1,2) | |
d0 = good_new_d.reshape(-1,1,2) | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment