Skip to content

Instantly share code, notes, and snippets.

@theSekyi
Last active June 13, 2019 15:42
Show Gist options
  • Select an option

  • Save theSekyi/2cb35ae5bd3335b020e39bc28cefeb90 to your computer and use it in GitHub Desktop.

Select an option

Save theSekyi/2cb35ae5bd3335b020e39bc28cefeb90 to your computer and use it in GitHub Desktop.
'''
STEPS
1. Take user_hash from command line
2. Set timer so it records after every 5 minutes
3. Store image in dataset/user_hash/date/store_by_timestamp
'''
from argparse import ArgumentParser
from datetime import date,datetime,timedelta
from utilities import create_folder_for_dataset
import cv2 as cv
import os
import imutils
import numpy as np
import time
import threading
#passing arguments from the terminal
parser = ArgumentParser()
parser.add_argument("-u","--user_hash")
parser.add_argument("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")
args = parser.parse_args()
today = date.today()
user_hash = args.user_hash
total = 0
cam = cv.VideoCapture(0)
create_folder_for_dataset(user_hash,today)
while True:
#capture each frame
ret, frame = cam.read()
#resize frame
frame = imutils.resize(frame,width=1000, height=1000)
current_time = int(time.time()*1000)
#save image
cv.imwrite(f'data/{user_hash}/{today}/{current_time}.png',frame)
print(f'Picture {total} saved')
#counting number of images saved
total += 1
#wait time before next image is saved
time.sleep(300)
#release video capture object
cam.release()
#closes all the frames
cv.destroyAllWindows()
import os
def create_folder_for_dataset(user_hash,today):
'''
Function to create folder and subfolder using the user_hash and
current date
'''
if not os.path.exists(f'data/{user_hash}'):
os.makedirs(f'data/{user_hash}',exist_ok=True)
print(f'{user_hash} directory created')
os.makedirs(f'data/{user_hash}/{today}',exist_ok=True)
else:
os.makedirs(f'data/{user_hash}/{today}',exist_ok=True)
# @tl.job(interval=timedelta(seconds=3000))
# def take_pic_every_n_secs(frame,user_hash,today):
# current_time = int(time.time()*1000)
# cv.imwrite(f'data/{user_hash}/{today}/{current_time}.png',frame)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment