Skip to content

Instantly share code, notes, and snippets.

@campanalbero
Created July 22, 2018 10:49
Show Gist options
  • Save campanalbero/c8884c1467515050cd38f5b160dc5531 to your computer and use it in GitHub Desktop.
Save campanalbero/c8884c1467515050cd38f5b160dc5531 to your computer and use it in GitHub Desktop.
capture nearly every 1 second
#!/usr/bin/python3
import numpy as np
import cv2
import datetime
from time import sleep
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
# 15fps: 1/15 = 0.066666667
one_sec = datetime.timedelta(microseconds=990000)
#one_sec = datetime.timedelta(seconds=1)
while(True):
start = datetime.datetime.now()
dt = start.strftime('%Y%m%d-%H%M%S')
ms = '{0:03f}'.format(start.microsecond/1000)
ret, frame = cap.read()
cv2.imwrite("img/" + dt + "-" + ms + ".jpg", frame)
while(True):
end = datetime.datetime.now()
if (end - start) > one_sec:
break
sleep(0.01)
# sleep is not so much accurate
#sleep(1.0 - (start.microsecond - end.microsecond) / 1000000.0)
cap.release()
cv2.destroyAllWindows()
# TODO shebang and exec flag
import cv2
import glob
import numpy as np
import os
import sys
"""calc diff compared with next jpg.
usage: python diff.py PATH
"""
path = sys.argv[-1]
files = sorted(glob.glob(path + '*.jpg'))
count = 0
minimum = 1920 * 1080 * 255
print(path + 'mask.png')
mask = cv2.imread('mask.png', cv2.IMREAD_GRAYSCALE)
curr = cv2.imread(files[-1], cv2.IMREAD_GRAYSCALE) * mask
# TODO if-condition 3600
group_num = 3600 / (len(files) - 3600)
print("path, diff value compared with prev img")
for f in files:
prev = curr
curr = cv2.imread(f, cv2.IMREAD_GRAYSCALE) * mask
diff = cv2.absdiff(prev, curr)
diff_sum = diff.sum()
print(f + ', ' + str(diff_sum))
if minimum > diff_sum:
minimum = diff_sum
path = f
count = count + 1
if count > group_num:
dest = os.path.dirname(path) + "/../unused/" + os.path.basename(path)
print(path + ", " + dest + ", was moved")
os.rename(path, dest)
minimum = 1920 * 1080 * 255
count = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment