#after attaching camera to the stand, it counts pixels from the right  ( so the higher values are on the left) for the x direction
#also going downwards in the image increases y value so the y pixels are counted from the top


# test3 has motion detection however it records a lot of objects with motion, we might want to average the number of frames


#now the rgb values are working
#help(flycapture2) , dirs(flycapture2) or obj inside instead, vars(obj)


import cv2
import numpy as np
import flycapture2 as fc2
import time
import datetime
import sys
import imutils
import argparse
import json


def getKey(tapeNumber):
    return tapeNumber['avgx']


print "hellodfd"
print "dfdfdfdfddfdfdf"
# set up various things
colorMax = np.array([10, 10, 10],np.uint8) # 3 color channels
colorMin = np.array([250, 250, 250],np.uint8) # must be >= all values in colorThreshMin
dilate_kernel = np.ones((5,5), np.uint8)
threshVal = 10 #CHANGE THIS TO ADJUST LIGHTING CUT-OFFS
# to run program: press "up" arrow so it says "python ....."
frInd = 0
viz = True
size_rank_of_target = 0

# set up logfile
cur_datetime = datetime.datetime.now()
cur_dtime_str = cur_datetime.strftime('%Y-%m-%d_%Hh%Mm%Ss')
logfname = '//arenatracker_%s.csv' % cur_dtime_str
logf = open(logfname, 'w', 0) # 0 forces flush after each write call (buffer = 0)
logf.write('framenum,UTCtime,fps,animx,animy,bbx,bby,bbw,bbh,bbtheta\n')


# set up camera
c = fc2.Context()
c.connect(*c.get_camera_from_index(0))
p = c.get_property(fc2.FRAME_RATE)
c.set_property(**p)
if viz:
    cv2.namedWindow('image', cv2.WINDOW_NORMAL)
print c.get_camera_info()
print 'Camera setup complete'
print "pixel formats"

# start capture process
c.start_capture()
im = fc2.Image()
start = time.time()
progress_t = start
first_start = start
progress_interval = 10 # time between prints in seconds

#other person's code
firstFrame = None
jammer = 0
listOfData = []

startTime =time.time()
print "Starting tracking"
print "...press control-c to stop."
try:
    while True:
        zz =c.retrieve_buffer(im)
        img = np.array(zz) # these are coming in as crazy bayered things--and in grey
        #cv2.imshow('yellow',img)
        imm = img
        # print "OHO"
        # print (dir(zz))
        # print "get format is: ", zz.get_format()
        # print "OHO"
        timestamp = time.time()
        img = cv2.cvtColor(img, cv2.COLOR_BAYER_RG2RGB)# REal is COLOR_BAYER_BG2BGR) # debayer (pattern verified--cv2 likes BGR order, if vizing with cv2, use this)
        frame = img
        imm =img
        #added code here NOT OUR CODE! Took it from other person
        frame = imutils.resize(frame, width=1200, height = 800)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (21, 21), 0)

        # if the first frame is None, initialize it
        if firstFrame is None:
            firstFrame = gray
            continue

            # compute the absolute difference between the current frame and
        # first frame
        frameDelta = cv2.absdiff(firstFrame, gray)
        thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_TOZERO)[1]
        #thresh = cv2.adaptiveThreshold(thresh,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
        # dilate the thresholded image to fill in holes, then find contours
        # on thresholded image
        thresh = cv2.dilate(thresh, None, iterations=2)
        (cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

                # loop over the contours
        for count_ in cnts:
            # if the contour is too small, ignore it
            if cv2.contourArea(count_) < 500:#args["min_area"]
                continue

            # compute the bounding box for the contour, draw it on the frame,
            # and update the text
            (x, y, w, h) = cv2.boundingRect(count_)
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

            print "x: %d y: %d xt: %d yt: %d avgx: %d avgy: %d\n" % (x,y, (x+w), (y+h), ((2*x+w)/2), ((2*y+h)/2))

            #append the list of files to this

            listOfData.append({'x': x, 'y':y, 'xt': (x+w) , 'yt': (y+h), 'avgx': ((2*x+w)/2) , 'avgy':((2*y+h)/2) }) #appending object to array
            text = "Occupied"

            cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"), (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
        print "end countours"
        if(jammer ==1):
            break;
        # print "HEYEHEYENHEYHEYHEY", len(cnts)
        # print dir(cnts)
        # print "HEYEHEYENHEYHEYHEY", cnts.__len__

        if viz:
            cv2.imshow("Security Feed", frame)
            cv2.imshow("Thresh", thresh)
            cv2.imshow("Frame Delta", frameDelta)
            #cv2.imshow('image',imm) #imgtcopy #img
            cv2.waitKey(1)

        frInd = frInd + 1
        start = timestamp
        print "frInd is : %d" % frInd
        if divmod(frInd,100)[1] == 0: # if we go beyond 1m samples, close file and start a new one (prevents nastiness of huge files)
            logf.close()
            print "we had to start a new file"
            cur_datetime = datetime.datetime.now()
            cur_dtime_str = cur_datetime.strftime('%Y-%m-%d_%Hh%Mm%Ss')
            logfname = 'arenatracker_%s.csv' % cur_dtime_str #modified this to do in the current file
            logf = open(logfname, 'w', 0) # 0 forces flush after each write call (buffer = 0)
            logf.write('framenum,UTCtime,fps,animx,animy,bbx,bby,bbw,bbh,bbtheta\n')
        if(time.time()-startTime >= 180): # 40 seconds
            listOfData = [] #clear list
            jammer = 1;

    logf.close()
    c.stop_capture()
    c.disconnect()
    if viz:
        cv2.destroyAllWindows()
    print '\nQuiting...'

    if len(listOfData) != 0:
        #sortedList = sort(listOfData)
        sortedList_=sorted(listOfData,key = getKey)
        with open('tapeConfig.json', 'w') as f:
            json.dump(sortedList_, f)
        #open file
        #print the data
        #close file
        print "Configuration success: please check tapeConfig.json"

except KeyboardInterrupt:
    # close file
    logf.close()
    c.stop_capture()
    c.disconnect()
    if viz:
        cv2.destroyAllWindows()
    print '\nQuiting...'

    if len(listOfData) != 0:
        sortedList_=sorted(listOfData,key = getKey)
        #sortedList = sort(listOfData)
        with open('tapeConfig.json', 'w') as f:
            json.dump(sortedList_, f)
        #open file
        #print the data
        #close file






        print "Configuration success: please check tapeConfig.json"
