Last active
August 29, 2015 14:18
-
-
Save avances123/74d1767d87a15efced0c to your computer and use it in GitHub Desktop.
Para trackear a la culebra
This file contains 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 argparse | |
import os | |
import glob | |
import ipdb | |
import numpy as np | |
from matplotlib import pyplot as plt | |
def last_img(): | |
return max(glob.iglob('2015*.jpg'), key=os.path.getctime) | |
parser = argparse.ArgumentParser(description='Detect motion') | |
#parser.add_argument('-b','--base_img', type=unicode,default='base.jpg', help='A clean image of the place without animals') | |
parser.add_argument('-n','--now_img', type=unicode,default=last_img(), help='An image of the place right now') | |
parser.add_argument('-s','--save', dest='save',action='store_true', help='Save a diff file') | |
args = parser.parse_args() | |
def get_banner(last_img): | |
img = cv2.imread(last_img) | |
return img[465:] | |
def get_imgs(last_img): | |
file_list = sorted(glob.iglob('2015*.jpg'), key=os.path.getctime) | |
i = file_list.index(last_img) | |
tbefore = cv2.imread(file_list[i-2],cv2.COLOR_RGB2GRAY)[:465] | |
tnow = cv2.imread(file_list[i-1],cv2.COLOR_RGB2GRAY)[:465] | |
tafter = cv2.imread(file_list[i],cv2.COLOR_RGB2GRAY)[:465] | |
return tbefore,tnow,tafter | |
def diffImg(tbefore,tnow,tafter): | |
d1 = cv2.absdiff(tbefore,tafter) | |
d2 = cv2.absdiff(tbefore, tnow) | |
return cv2.bitwise_and(d1, d2) | |
tbefore,tnow,tafter = get_imgs(args.now_img) | |
diff = diffImg(tbefore,tnow,tafter) | |
#ipdb.set_trace() | |
diff = np.vstack((diff,get_banner(args.now_img))) | |
if args.save: | |
cv2.imwrite("diff_" + args.now_img, diff) | |
else: | |
for i,img in enumerate([tbefore,tnow,tafter,diff]): | |
if i == 0: title = "T-1" | |
if i == 1: title = "T now" | |
if i == 2: title = "T+1" | |
if i == 3: title = "Diff" | |
plt.subplot(2,2,i+1).set_title(title) | |
plt.imshow(img) | |
#plt.imshow(diff) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment