<gh_stars>0
#!/usr/bin/env python

#Copyright 2017 <NAME>
#This file is subject to the terms and conditions defined in file 'Readme.md', which is part of this source code package.

#This program extracts frames before and after an event has occurred at set time periods, for later processing

import cv2
import numpy as np
import datetime
import re
import sys

def writeFrameToImage(markerText, extraText):
	filename = "../../data/%s/cropped/%s_%s_rgb.jpg" % (folderName, markerText, extraText)
	cv2.imwrite(filename, frame)
	filename = "../../data/%s/cropped/%s_%s_thermal.jpg" % (folderName, markerText, extraText)
	cv2.imwrite(filename, frame_thermal)
	filename = "../../data/%s/cropped/%s_%s_thermal_obj.jpg" % (folderName, markerText, extraText)
	cv2.imwrite(filename, frame_thermal_obj)

if(len(sys.argv)<4):
	print "Too few arguments: exiting getFramesFromVideoAtTimeIntervals.py"
	sys.exit(-1)

basename = sys.argv[0] 
markerText = sys.argv[1] 
thermalFileName = "%s-thermal1.avi" % basename 
thermalFileName_obj = "%s-thermal2.avi" % basename 
rgbFileName = "%s-rgb.avi" % basename
timesFileName = "%s-times.dat" % basename
cap=cv2.VideoCapture(rgbFileName)
cap_thermal=cv2.VideoCapture(thermalFileName)
cap_thermal_obj=cv2.VideoCapture(thermalFileName_obj)

beforeFrame = int(sys.argv[2])
startFrame = int(sys.argv[3])
folderName = sys.argv[4]
currentFrame=0

print ""
print "= Extract frames at various times (MAY 2017) ="
print basename

cv2.namedWindow('thermal_obj')
cv2.namedWindow('thermal')
cv2.namedWindow('frame')
cv2.moveWindow('thermal_obj', 180, 100)
cv2.moveWindow('thermal', 500, 100)
cv2.moveWindow('frame', 820, 100)


with open(timesFileName, 'r') as f:
	data=list(f)
	myList= re.split('_|-| ', data[startFrame])
	startTime= ((long(myList[1]) * 3600000) + (long(myList[2]) * 60000) +  (long(myList[3]) * 1000) + (long(myList[4])/1000) )

timesToGet = [ ]
framesToGet = [ ]
stepSize=4

framesToGet.append(beforeFrame)
framesToGet.append(beforeFrame+stepSize)
framesToGet.append(beforeFrame+(2*stepSize))

framesToGet.append(startFrame)
framesToGet.append(startFrame+stepSize)
framesToGet.append(startFrame+(2*stepSize))

timesToGet.append(startTime  + 5000)
timesToGet.append(startTime  + 10000)
timesToGet.append(startTime  + 15000)
timesToGet.append(startTime  + 30000)
timesToGet.append(startTime  + 60000)
timesToGet.append(startTime  + 90000)

#make a list of frames to extract
currentTarget=0
for frameIndex in range(startFrame,len(data)):
	myList= re.split('_|-| ', data[frameIndex])
	aTime= ((int(myList[1]) * 3600000) + (int(myList[2]) * 60000) +  (int(myList[3]) * 1000) + (int(myList[4])/1000) )

	if aTime > int(timesToGet[currentTarget]):
		framesToGet.append(frameIndex) 
		framesToGet.append(frameIndex+stepSize)
		framesToGet.append(frameIndex+(2*stepSize))
		currentTarget=currentTarget+1
		if(currentTarget==len(timesToGet)):
			break

#Extract the target frames as images
currentTarget=0
while(cap.isOpened() and cap_thermal.isOpened()):

	ret, frame = cap.read()
	ret, frame_thermal = cap_thermal.read()
	ret, frame_thermal_obj = cap_thermal_obj.read()
	if not (frame is None or frame_thermal is None or frame_thermal_obj is None):
		currentFrame=currentFrame+1

		if currentFrame==framesToGet[currentTarget]:
			writeFrameToImage(markerText, str(currentTarget))
			currentTarget=currentTarget+1
			if(currentTarget==len(framesToGet)):
				break

		cv2.imshow('frame', frame)
		cv2.imshow('thermal', frame_thermal)
		cv2.imshow('thermal_obj', frame_thermal_obj)
	key = cv2.waitKey(5)
	if key == ord('q'):
		break
	

cap.release()
cap_thermal.release()
cap_thermal_obj.release()
cv2.destroyAllWindows()

