import maya.cmds as cmds
import os
import random
import pymel.core as pm
import socket
import sys
import maya.mel as mm
import csv

recstart = 0
selectedBlend = ""
dataarray = []
gnumcurrenttime = 1

"""
menulabelarr = ['Brow Left UP','Brow Left Down','Brow Right UP','Brow Right Down','Brow Centering','Brow outer left down','Brow outer right down','Eye Close Left','Eye Close Right','Mouse Open',
                'Mouse Left Smile','Mouse Right Smile','Mouse Left Spread','Mouse Right Spread','Mouse Left Frawn','Mouse Right Frawn','Mouse Left Centering','Mouse Right Centering','Cheek Left UP',
                'Cheek Right UP']
"""

# Our mel global proc.
melproc = """
global proc portData(string $arg){
    python(("portData(\\"" + $arg + "\\")"));
}
"""

mm.eval(melproc)

def edit_cell(row, column, value):
    return 1

def startrealtimeexp(*args):
    global recstart
    global recend
    #numrow = cmds.scriptTable('scrtable', query=True, rows=True)
    if len(selectedBlend) >= 0:
        if recstart == 0:
            recstart = 1
            cmds.button( 'realtimecomm' ,edit=True, label = 'Stop Real Time Expression' )
	    #Start Comm
            cmds.commandPort(name="127.0.0.1:7777", echoOutput=False, noreturn=False,prefix="portData", returnNumCommands=True)
            cmds.commandPort(name=":7777", echoOutput=False, noreturn=False,prefix="portData", returnNumCommands=True)
        else:
            recstart = 0
            cmds.button( 'realtimecomm' ,edit=True, label = 'Start Real Time Expression' )
	    #Stop Comm
            deactivateCommandPort('127.0.0.1', '7777')
'''
def delete_sel_row(*args):
    if recstart == 0:
        try:
            selected_row = cmds.scriptTable('scrtable', query=True, selectedRows=True)[0]
            if selected_row == None:
                print('Select Row to Delete')
            else:
                cmds.scriptTable('scrtable', edit=True,deleteRow=selected_row)
        except:
            print('Select Row to Delete')
'''

'''
def savepresetfile(*args):
    blendshapetxt = cmds.textField('selectedBlendShapeText', q=True, tx=True )
    filename = blendshapetxt + "_mayapipeline_setting.txt"
    o_file = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop') + "\\"+ filename

    all_rows = cmds.scriptTable('scrtable', query=True, rows=True)

    if all_rows < 2:
        return 0

    if not (cmds.file(o_file,query=True, exists=True)):
        tmp_csv_file = open(o_file, 'w' ,os.O_CREAT)
    else:
        tmp_csv_file = open(o_file, 'w')
    writer = csv.writer(tmp_csv_file, lineterminator='\n')

    all_rows = cmds.scriptTable('scrtable', query=True, rows=True)
    for i in range(all_rows):
        all_colums = cmds.scriptTable('scrtable', query=True, columns=True)
        data_list = []
        for j in range( all_colums - 1):
            if i == 0:
                blendshapetxt = cmds.textField('selectedBlendShapeText', q=True, tx=True )
                headbonenametxt = cmds.textField('HeadbonenameF', q=True, tx=True )
                headxtxt = cmds.textField('HeadbonenameXF', q=True, tx=True )
                headytxt = cmds.textField('HeadbonenameYF', q=True, tx=True )
                headztxt = cmds.textField('HeadbonenameZF', q=True, tx=True )
                porttxt = cmds.textField('Portnum', q=True, tx=True )
                data_list = [blendshapetxt, headbonenametxt, headxtxt, headytxt, headztxt, porttxt]

            else:
                cell_list = cmds.scriptTable('scrtable', cellIndex=(i,j + 1), query=True, cellValue=True)
                if j == 0:
                    if type(cell_list) == list:
                        cell_text = "".join(cell_list)
                        print(cell_text)
                    elif cell_list == None:
                        cell_text = u''
                    else:
                        cell_text = cell_list
                    data_list.append(cell_text)
                elif j == 1:
                    if type(cell_list) == list:
                        cell_text = "".join(cell_list)
                        print(cell_text)
                    elif cell_list == None:
                        cell_text = u''
                    else:
                        cell_text = cell_list
                    data_list.append(cell_text)
                else:
                    if type(cell_list) == list:
                        cell_text = "".join(cell_list)
                    elif cell_list == None:
                        cell_text = u''
                    else:
                        cell_text = cell_list
                    data_list.append(cell_text)
        writer.writerow(data_list)
    tmp_csv_file.close()
    cmds.warning( "Saved file (" +o_file+ ")" )
'''

'''
def loadpresetfile(*args):
    if recstart != 0:
        return 0

    presetfile = pm.fileDialog2(fileMode=1)

    if presetfile:
        cmds.scriptTable('scrtable', edit=True, selectedRows=[])

        o_file = open(str(presetfile[0]), 'r')
        reader = csv.reader(o_file)

        all_rows = cmds.scriptTable('scrtable', query=True, rows=True)
        for i in range(all_rows):
            cmds.scriptTable('scrtable', edit=True,deleteRow= i)

        row_no = 0
        for row in reader:
            if row_no > 0:
                cmds.scriptTable('scrtable', edit=True,insertRow=row_no)
                cmds.scriptTable('scrtable', cellIndex=(row_no,1), edit=True, cellValue=row[0])
                cmds.scriptTable('scrtable', cellIndex=(row_no,2), edit=True, cellValue=row[1])
                cmds.scriptTable('scrtable', cellIndex=(row_no,3), edit=True, cellValue="0")
                cmds.scriptTable('scrtable', cellIndex=(row_no,4), edit=True, cellValue=row[3])
                cmds.scriptTable('scrtable', cellIndex=(row_no,5), edit=True, cellValue=row[4])
                cmds.scriptTable('scrtable', cellIndex=(row_no,6), edit=True, cellValue=row[5])
            else:
                cmds.textField('selectedBlendShapeText', edit=True, text=row[0] )
                cmds.textField('HeadbonenameF', edit=True, text=row[1] )
                cmds.textField('HeadbonenameXF', edit=True, text=row[2] )
                cmds.textField('HeadbonenameYF', edit=True, text=row[3] )
                cmds.textField('HeadbonenameZF', edit=True, text=row[4] )
                #cmds.textField('Portnum', edit=True, text=row[5] )
                loadTargetList()
            row_no = 1 + row_no
        o_file.close()
'''

def expTrackerWindow():
    if cmds.window('expTrackerWindow', exists = True):
        cmds.deleteUI('expTrackerWindow')

    #window def
    cmds.window('expTrackerWindow',widthHeight=(900,400),title='Mustache_Boy_Facial_Mocap-conelab',minimizeButton=False,maximizeButton=False,resizeToFitChildren = True, sizeable = True)
    #cmds.rowColumnLayout(numberOfColumns=3,columnWidth=[(1,300),(2,300),(3,300)],backgroundColor=[200,200,0])

    '''
    #select an existing blendShape node
    cmds.rowColumnLayout(numberOfColumns=6,columnWidth=[(1,150),(2,25),(3,300),(4,25),(5,200),(6,200)],backgroundColor=[1,0.9843,0.7961])
    cmds.text( label='Blend Shape node name : ' ,align='right' )
    cmds.text( label='' )
    selectedBlendShapeTextField = cmds.textField( 'selectedBlendShapeText' ,backgroundColor=[1,1,1])
    cmds.text( label='' )
    cmds.button( label = 'Load Targets', command = 'loadTargetList()', width=20, align='left',backgroundColor=[0.3412,0.8196,0.7882])
    cmds.text( label='' )

    cmds.columnLayout('temp1', width=900)
    cmds.rowColumnLayout(numberOfColumns=6,columnWidth=[(1,300),(2,40),(3,300),(4,40),(5,200),(6,20)])
    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )

    targetList = cmds.optionMenu( 'targetObjectMenu', label='Target: ',backgroundColor=[1,0.9098,0.4118])
    cmds.menuItem(label=' ' )
    cmds.text( label='' )
    paramList = cmds.optionMenu( 'paramObjectMenu', label='Link: ',backgroundColor=[1,0.9098,0.4118] )
    cmds.menuItem(label='Please select param' )
    cmds.menuItem(label='Brow Left UP')
    cmds.menuItem(label='Brow Left Down')
    cmds.menuItem(label='Brow Right UP')
    cmds.menuItem(label='Brow Right Down')
    cmds.menuItem(label='Brow Centering')
    cmds.menuItem(label='Brow outer left down')
    cmds.menuItem(label='Brow outer right down')
    cmds.menuItem(label='Eye Close Left')
    cmds.menuItem(label='Eye Close Right')
    cmds.menuItem(label='Mouse Open')
    cmds.menuItem(label='Mouse Left Smile')
    cmds.menuItem(label='Mouse Right Smile')
    cmds.menuItem(label='Mouse Left Spread')
    cmds.menuItem(label='Mouse Right Spread')
    cmds.menuItem(label='Mouse Left Frawn')
    cmds.menuItem(label='Mouse Right Frawn')
    cmds.menuItem(label='Mouse Left Centering')
    cmds.menuItem(label='Mouse Right Centering')
    cmds.menuItem(label='Cheek Left UP')
    cmds.menuItem(label='Cheek Right UP')
    cmds.text( label='' )
    cmds.button( label = 'Add link', command = 'addlink()' ,backgroundColor=[0.3412,0.8196,0.7882])
    cmds.text( label='' )

    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )
    '''

    # start Real Time Expression 뒷 배경.
    cmds.columnLayout('mytable', width=900,backgroundColor=[1,0.9098,0.4118])

    '''
    table =  cmds.scriptTable('scrtable',rows=0, columns=6,columnWidth=([1,300],[2,300],[3,145],[4,145],[5,1],[6,1]),
    	label=[(1,"Target"), (2,"Link"), (3,"Receive val"), (4,"Strength"), (5,"ID(Target)"), (6,"ID(Link)")], width=900,
    	cellChangedCmd=edit_cell)
    '''
    #cmds.rowColumnLayout(numberOfColumns = 3, columnWidth = [(1,100),(2, 300), (3, 500)])

    '''
    cmds.text( label='' )
    cmds.text( label='' )
    #cmds.button( label = 'Delete select row',command=delete_sel_row,backgroundColor=[0.3412,0.8196,0.7882] )

    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )

    #cmds.columnLayout('temp2', width=900)
    cmds.rowColumnLayout(numberOfColumns = 4, columnWidth = [(1,100),(2, 300), (3, 300),(4,200)],backgroundColor=[1,0.9098,0.4118])

    cmds.text( label='' )
    #cmds.text( label='Head bone name' )
    #Headbonename = cmds.textField('HeadbonenameF')
    cmds.text( label='' )

    cmds.text( label='' )
    #cmds.text( label='Head bone rotation compensation X' )
    #HeadbonenameX = cmds.textField('HeadbonenameXF',text='0')
    cmds.text( label='' )

    cmds.text( label='' )
    #cmds.text( label='Head bone rotation compensation Y' )
    #HeadbonenameY = cmds.textField('HeadbonenameYF',text='0')
    cmds.text( label='' )

    cmds.text( label='' )
    #cmds.text( label='Head bone rotation compensation Z' )
    #HeadbonenameZ = cmds.textField('HeadbonenameZF',text='0')
    cmds.text( label='' )
    '''

    cmds.columnLayout('temp3', width=900)
    cmds.rowColumnLayout(numberOfColumns=5,columnWidth=[(1,200),(2,50),(3,200),(4,150),(5,300)])

    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )

    #cmds.button( label = 'Save preset', command = savepresetfile,backgroundColor=[0.3412,0.8196,0.7882] )
    cmds.text( label='' )
    #cmds.button( label = 'Load preset', command = loadpresetfile,backgroundColor=[0.3412,0.8196,0.7882] )
    cmds.text( label='' )
    cmds.button( 'realtimecomm', label = 'Start Real Time Expression', command = startrealtimeexp ,backgroundColor=[0.9294,0.3294,0.5216])

    #cmds.button(label='close', command=('cmds.deleteUI(\"' + window + '\", window=True)'), backgroundColor=[0.9294,0.3294,0.5216])

    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )
    cmds.text( label='' )

    cmds.showWindow( 'expTrackerWindow' )

expTrackerWindow()

'''
if (cmds.window("expTrackerWindow", exists=True)):
    cmds.deleteUI("expTrackerWindow")
'''

'''
#function that loads target list to the UI

def loadTargetList():
    global selectedBlend
    selectedBlend = cmds.textField('selectedBlendShapeText', q=True, tx=True )

    existingItems = cmds.optionMenu( 'targetObjectMenu', q=True, itemListLong=True )
    if existingItems != None and existingItems != []:
        cmds.deleteUI(existingItems)

    cmds.menuItem(label='Please select blendShape', parent='targetObjectMenu')

    blendTargets = cmds.listAttr (selectedBlend + '.w', m = True)
    for target in blendTargets:
        cmds.menuItem(label='%s' %(target), parent='targetObjectMenu')
'''

'''
def addlink():
    if recstart == 0:
        selectedBlend = cmds.optionMenu('targetObjectMenu', query=True, value=True )
        selectedBlendI = cmds.optionMenu('targetObjectMenu', query=True, select=True )
        selectedLinkBlend = cmds.optionMenu('paramObjectMenu', query=True, value=True )
        selectedLinkBlendI = cmds.optionMenu('paramObjectMenu', query=True, select=True )

        if selectedBlendI > 1 and selectedLinkBlendI > 1:
            add_row();
'''

'''
def add_row(*args):
    cmds.scriptTable('scrtable', edit=True, selectedRows=[])

    selectedBlend = cmds.optionMenu('targetObjectMenu', query=True, value=True )
    selectedBlendI = cmds.optionMenu('targetObjectMenu', query=True, select=True )
    selectedLinkBlend = cmds.optionMenu('paramObjectMenu', query=True, value=True )
    selectedLinkBlendI = cmds.optionMenu('paramObjectMenu', query=True, select=True )

    last_row_num = cmds.scriptTable('scrtable', query=True, rows=True)
    cmds.scriptTable('scrtable', edit=True,insertRow=last_row_num)

    cmds.scriptTable('scrtable', e=True, ci=[last_row_num, 1], cv=selectedBlend)
    cmds.scriptTable('scrtable', e=True, ci=[last_row_num, 2], cv=selectedLinkBlend)
    cmds.scriptTable('scrtable', e=True, ci=[last_row_num, 3], cv='0')
    cmds.scriptTable('scrtable', e=True, ci=[last_row_num, 4], cv='1')
    cmds.scriptTable('scrtable', e=True, ci=[last_row_num, 5], cv=selectedBlendI)
    cmds.scriptTable('scrtable', e=True, ci=[last_row_num, 6], cv=selectedLinkBlendI)
'''

def deformface():
    global dataarray
    if len(dataarray) < 0:
        # dataarray의 인자의 첫 세개는 목뼈의 x,y,z좌표 이므로 이것이 없다면 facial_mocap의 의미 없으므로 0을 리턴.
    	return 0

    #all_rows = cmds.scriptTable('scrtable', query=True, rows=True)
    global recstart
    global gnumcurrenttime

    all_rows = 1;
    if all_rows > 0 and recstart == 1:
        ornum = 1;

        numcurrenttime = gnumcurrenttime
        #bonename = cmds.textField('HeadbonenameF', q=True, tx=True )

        # bjoint = pm.PyNode(Jaw_CTRL)

        '''
        # LowerLipMid_CTRL (dataarray[0] ~ dataarray[2])

        global LowerLipMid_X
        global LowerLipMid_Y
        global LowerLipMid_Z

        LowerLipMid_X = 0.089617
        LowerLipMid_Y = 0.177767
        LowerLipMid_Z = 0.075221

        print((float(dataarray[2])*(-1)) + LowerLipMid_Y)

        pm.move(float(dataarray[0])*-1 + LowerLipMid_X, float(dataarray[1])*-1 + LowerLipMid_Y, float(dataarray[2])*-1 + LowerLipMid_Z, 'LowerLipMid_CTRL', relative=True, objectSpace=True, worldSpaceDistance=True )
            #if recnow ==1
             # numcurrenttime 증가시키는 함수는 어딨지?
        pm.setKeyframe('LowerLipMid_CTRL', v=float(dataarray[0])*-1 + LowerLipMid_X, attribute='TranslateX', t=[numcurrenttime])
        pm.setKeyframe('LowerLipMid_CTRL', v=float(dataarray[1])*-1 + LowerLipMid_Y, attribute='TranslateY', t=[numcurrenttime])
        pm.setKeyframe('LowerLipMid_CTRL', v=float(dataarray[2])*-1 + LowerLipMid_Z, attribute='TranslateZ', t=[numcurrenttime])
        '''

        '''
        # UpperLipMid_CTRL (dataarray[3] ~ dataarray[5])

        global UpperLipMid_X
        global UpperLipMid_Y
        global UpperLipMid_Z

        UpperLipMid_X = 0.087832
        UpperLipMid_Y = 0.216568
        UpperLipMid_Z = 0.074157

        pm.move(float(dataarray[3])*-1 + UpperLipMid_X, float(dataarray[4])*-1 + UpperLipMid_Y, float(dataarray[5])*-1 + UpperLipMid_Z, 'UpperLipMid_CTRL', relative=True, objectSpace=True, worldSpaceDistance=True )
        pm.setKeyframe('UpperLipMid_CTRL', v=float(dataarray[3])*-1 + UpperLipMid_X, attribute='TranslateX', t=[numcurrenttime])
        pm.setKeyframe('UpperLipMid_CTRL', v=float(dataarray[4])*-1 + UpperLipMid_Y, attribute='TranslateY', t=[numcurrenttime])
        pm.setKeyframe('UpperLipMid_CTRL', v=float(dataarray[5])*-1 + UpperLipMid_Z, attribute='TranslateZ', t=[numcurrenttime])
        '''

        # L_LowerLipMid_CTRL (dataarray[6] ~ dataarray[8])

        global L_LowerLipMid_X
        global L_LowerLipMid_Y
        global L_LowerLipMid_Z

        L_LowerLipMid_X = 0.097783
        L_LowerLipMid_Y = 0.184848
        L_LowerLipMid_Z = 0.106411

        pm.move(float(dataarray[6])*-1 + L_LowerLipMid_X, float(dataarray[7])*-1 + L_LowerLipMid_Y, float(dataarray[8])*-1 + L_LowerLipMid_Z, 'L_LipsBottom_CTRL', relative=True, objectSpace=True, worldSpaceDistance=True )
        pm.setKeyframe('L_LowerLipMid_CTRL', v=float(dataarray[6])*-1 + L_LowerLipMid_X, attribute='TranslateX', t=[numcurrenttime])
        pm.setKeyframe('L_LowerLipMid_CTRL', v=float(dataarray[7])*-1 + L_LowerLipMid_Y, attribute='TranslateY', t=[numcurrenttime])
        pm.setKeyframe('L_LowerLipMid_CTRL', v=float(dataarray[8])*-1 + L_LowerLipMid_Z, attribute='TranslateZ', t=[numcurrenttime])

        # L_UpperLipMid_CTRL (dataarray[9] ~ dataarray[11])

        global L_UpperLipMid_CTRL_X
        global L_UpperLipMid_CTRL_Y
        global L_UpperLipMid_CTRL_Z

        L_UpperLipMid_CTRL_X = 0.092385
        L_UpperLipMid_CTRL_Y = 0.2153
        L_UpperLipMid_CTRL_Z = 0.095308

        pm.move(float(dataarray[9])*-1 + L_UpperLipMid_CTRL_X, float(dataarray[10])*-1 + L_UpperLipMid_CTRL_Y, float(dataarray[11])*-1 + L_UpperLipMid_CTRL_Z, 'L_LipsTop_CTRL', relative=True, objectSpace=True, worldSpaceDistance=True )
        pm.setKeyframe('L_UpperLipMid_CTRL', v=float(dataarray[9])*-1 + L_UpperLipMid_CTRL_X, attribute='TranslateX', t=[numcurrenttime])
        pm.setKeyframe('L_UpperLipMid_CTRL', v=float(dataarray[10])*-1 + L_UpperLipMid_CTRL_Y, attribute='TranslateY', t=[numcurrenttime])
        pm.setKeyframe('L_UpperLipMid_CTRL', v=float(dataarray[11])*-1 + L_UpperLipMid_CTRL_Z, attribute='TranslateZ', t=[numcurrenttime])

        # R_LowerLipMid_CTRL (dataarray[12] ~ dataarray[14])

        global R_LowerLipMid_CTRL_X
        global R_LowerLipMid_CTRL_Y
        global R_LowerLipMid_CTRL_Z

        R_LowerLipMid_CTRL_X = 0.101421
        R_LowerLipMid_CTRL_Y = 0.185972
        R_LowerLipMid_CTRL_Z = 0.043421

        pm.move(float(dataarray[12])*-1 + R_LowerLipMid_CTRL_X, float(dataarray[13])*-1 + R_LowerLipMid_CTRL_Y, float(dataarray[14])*-1 + R_LowerLipMid_CTRL_Z, 'R_LipsBottom_CTRL', relative=True, objectSpace=True, worldSpaceDistance=True )
        pm.setKeyframe('R_LowerLipMid_CTRL', v=float(dataarray[12])*-1 + R_LowerLipMid_CTRL_X, attribute='TranslateX', t=[numcurrenttime])
        pm.setKeyframe('R_LowerLipMid_CTRL', v=float(dataarray[13])*-1 + R_LowerLipMid_CTRL_Y, attribute='TranslateY', t=[numcurrenttime])
        pm.setKeyframe('R_LowerLipMid_CTRL', v=float(dataarray[14])*-1 + R_LowerLipMid_CTRL_Z, attribute='TranslateZ', t=[numcurrenttime])

        # R_UpperLipMid_CTRL (dataarray[15] ~ dataarray[17])

        global R_UpperLipMid_CTRL_X
        global R_UpperLipMid_CTRL_Y
        global R_UpperLipMid_CTRL_Z

        R_UpperLipMid_CTRL_X = 0.094769
        R_UpperLipMid_CTRL_Y = 0.214611
        R_UpperLipMid_CTRL_Z = 0.053779

        pm.move(float(dataarray[15])*-1 + R_UpperLipMid_CTRL_X, float(dataarray[16])*-1 + R_UpperLipMid_CTRL_Y, float(dataarray[17])*-1 + R_UpperLipMid_CTRL_Z, 'R_LipsTop_CTRL', relative=True, objectSpace=True, worldSpaceDistance=True )
        pm.setKeyframe('R_UpperLipMid_CTRL', v=float(dataarray[15])*-1 + R_UpperLipMid_CTRL_X, attribute='TranslateX', t=[numcurrenttime])
        pm.setKeyframe('R_UpperLipMid_CTRL', v=float(dataarray[16])*-1 + R_UpperLipMid_CTRL_Y, attribute='TranslateY', t=[numcurrenttime])
        pm.setKeyframe('R_UpperLipMid_CTRL', v=float(dataarray[17])*-1 + R_UpperLipMid_CTRL_Z, attribute='TranslateZ', t=[numcurrenttime])

def portData(arg):
    """
    Read the 'serial' data passed in from the commandPort
    """
    global recend
    global dataarray

    dataarray=[]
        # 이차 배열의 첫 배열 dataarray.
    recVal = str(arg)
        # 받은 arg 스트링을 recVal에 넣음.
    strArray = recVal.split(",")
        # recVal 스트링을 , 경계로 쪼갬.
    for i in range(0,18):
        # 24가지의 데이터가 들어옴.
        dataarray.append(strArray[i])
            # dataarray에 strArray[i] 첨부.
        #print(strArray[i])
    #createTimer(0.03, deformface)
    deformface()

def deactivateCommandPort(host, port):
    path = host + ":" + port
    active = cmds.commandPort(path, q=True)
    if active:
        cmds.commandPort(name=path, cl=True)
    else:
        print("%s is was not active" % path)

'''
def createTimer(seconds, function, *args, **kwargs):
    def isItTime():
        now = time.time()
        if now - isItTime.then > seconds:
            isItTime.then = now			# swap the order of these two lines ...
            function(*args, **kwargs)	  # ... to wait before restarting timer

        isItTime.then = 0           #time.time() # set this to zero if you want it to fire once immediately
        cmds.scriptJob(event=("idle", isItTime))
'''


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.optim as optim
import torchtext.vocab as vocab

import numpy as np
import time
import os
from six.moves import cPickle
import torch.backends.cudnn as cudnn
import yaml

import opts
import misc.eval_utils
import misc.utils as utils
import misc.AttModel as AttModel
import yaml

# from misc.rewards import get_self_critical_reward
import torchvision.transforms as transforms
import pdb
import argparse
import torch.nn.functional as F
import matplotlib.pyplot as plt
from PIL import Image
plt.switch_backend('agg')
import json
import pickle
import sys
sys.path.append('../faster-rcnn.pytorch/')
from get_ppls import prep_model, get_caption

def decode_sequence_simple(itow, itod, ltow, itoc, wtod, seq, bn_seq, fg_seq, vocab_size, opt):
    N, D = seq.size()
    itod = np.asarray(['__background__',
                       'aeroplane', 'bicycle', 'bird', 'boat',
                       'bottle', 'bus', 'car', 'cat', 'chair',
                       'cow', 'diningtable', 'dog', 'horse',
                       'motorbike', 'person', 'pottedplant',
                       'sheep', 'sofa', 'train', 'tvmonitor'])
    out = []
    for i in range(N):
        txt = ''
        for j in range(D):
            if j >= 1:
                txt = txt + ' '
            ix = seq[i,j].item()
            if ix > vocab_size:
                word = itod[int(fg_seq[i,j].item())]
                # word = '[ ' + word + ' ]'
            else:
                if ix == 0:
                    break
                else:
                    word = itow[str(ix)]
            txt = txt + word
        out.append(txt)
    return out

def demo_simple(obj_det_model, model, img_name):
    model.eval()
    file_path = img_name
    max_proposal = 200

    num_proposal = 6
    num_nms = 6

    # load the image.
    input_imgs = torch.FloatTensor(1)
    img = Image.open(file_path).convert('RGB')
    # resize the image.
    img = transforms.Resize((opt.image_crop_size, opt.image_crop_size))(img)

    ppls = get_caption(obj_det_model, img_name)
    pad_proposals = torch.cat([i.unsqueeze(dim=0) for i in ppls]).unsqueeze(dim=0)
#     pad_proposals = torch.from_numpy(pad_proposals).float().unsqueeze(dim=0)
    num = torch.FloatTensor([1, len(ppls), len(ppls)]).unsqueeze(dim=0)

    img = transforms.ToTensor()(img)
    img = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(img).unsqueeze(dim=0)

    img = img.cuda()
    pad_proposals = pad_proposals.cuda()
    num = num.cuda()

    eval_opt = {'sample_max':1, 'beam_size': opt.beam_size, 'inference_mode' : True, 'tag_size' : opt.cbs_tag_size}
    seq, bn_seq, fg_seq, _, _, _ = model._sample(img, pad_proposals, num, eval_opt)
    sents = utils.decode_sequence(dataset.itow, dataset.itod, dataset.ltow, dataset.itoc, dataset.wtod, \
                                    seq.data, bn_seq.data, fg_seq.data, opt.vocab_size, opt)
    return sents

def init_model(opt):
    cudnn.benchmark = True

    ####################################################################################
    # Build the Model
    ####################################################################################
    from misc.dataloader_coco import DataLoader
    dataset = DataLoader(opt, split='train')
    opt.vocab_size = dataset.vocab_size
    opt.detect_size = dataset.detect_size
    opt.seq_length = opt.seq_length
    opt.fg_size = dataset.fg_size
    opt.fg_mask = torch.from_numpy(dataset.fg_mask).byte()
    opt.glove_fg = torch.from_numpy(dataset.glove_fg).float()
    opt.glove_clss = torch.from_numpy(dataset.glove_clss).float()
    opt.glove_w = torch.from_numpy(dataset.glove_w).float()
    opt.st2towidx = torch.from_numpy(dataset.st2towidx).long()

    opt.itow = dataset.itow
    opt.itod = dataset.itod
    opt.ltow = dataset.ltow
    opt.itoc = dataset.itoc

    if not opt.finetune_cnn: opt.fixed_block = 4 # if not finetune, fix all cnn block

    if opt.att_model == 'topdown':
        model = AttModel.TopDownModel(opt)
    elif opt.att_model == 'att2in2':
        model = AttModel.Att2in2Model(opt)

    infos = {}
    histories = {}
    if opt.start_from is not None:
        if opt.load_best_score == 1:
            model_path = os.path.join(opt.start_from, 'model-best.pth')
            info_path = os.path.join(opt.start_from, 'infos_'+opt.id+'-best.pkl')
        else:
            model_path = os.path.join(opt.start_from, 'model.pth')
            info_path = os.path.join(opt.start_from, 'infos_'+opt.id+'.pkl')

            # open old infos and check if models are compatible
        with open(info_path, 'rb') as f:
            infos = pickle.load(f, encoding='latin1')
            saved_model_opt = infos['opt']

        # opt.learning_rate = saved_model_opt.learning_rate
        print('Loading the model %s...' %(model_path))
        model.load_state_dict(torch.load(model_path))

        if os.path.isfile(os.path.join(opt.start_from, 'histories_'+opt.id+'.pkl')):
            with open(os.path.join(opt.start_from, 'histories_'+opt.id+'.pkl'), 'rb') as f:
                histories = pickle.load(f, encoding='latin1')

    if opt.decode_noc:
        model._reinit_word_weight(opt, dataset.ctoi, dataset.wtoi)

    best_val_score = infos.get('best_val_score', None)
    iteration = infos.get('iter', 0)
    start_epoch = infos.get('epoch', 0)

    val_result_history = histories.get('val_result_history', {})
    loss_history = histories.get('loss_history', {})
    lr_history = histories.get('lr_history', {})
    ss_prob_history = histories.get('ss_prob_history', {})

    if opt.cuda:
        model.cuda()

    params = []
    # cnn_params = []
    for key, value in dict(model.named_parameters()).items():
        if value.requires_grad:
            if 'cnn' in key:
                params += [{'params':[value], 'lr':opt.cnn_learning_rate,
                        'weight_decay':opt.cnn_weight_decay, 'betas':(opt.cnn_optim_alpha, opt.cnn_optim_beta)}]
            else:
                params += [{'params':[value], 'lr':opt.learning_rate,
                    'weight_decay':opt.weight_decay, 'betas':(opt.optim_alpha, opt.optim_beta)}]

    print("Use %s as optmization method" %(opt.optim))
    if opt.optim == 'sgd':
        optimizer = optim.SGD(params, momentum=0.9)
    elif opt.optim == 'adam':
        optimizer = optim.Adam(params)
    elif opt.optim == 'adamax':
    	optimizer = optim.Adamax(params)

    obj_det_model = prep_model('../faster-rcnn.pytorch/data_fr/pretrained_model/faster_rcnn_coco.pth')

    return obj_det_model, model

#     img_name = 'test_images/COCO_val2014_000000184791.jpg'
#     pics = ['COCO_val2014_000000184791.jpg','COCO_val2014_000000550529.jpg',
#             'COCO_val2014_000000579664.jpg','COCO_val2014_000000348881.jpg','COCO_val2014_000000560623.jpg']
#     obj_det_model = prep_model('../faster-rcnn.pytorch/data_fr/pretrained_model/faster_rcnn_coco.pth')
#     for pic in pics:
#         sents = demo_simple(obj_det_model, model, 'test_images/'+pic)
#         print(sents)
if __name__=='__main__':
    sents = demo_simple(obj_det_model, model, 'data/coco/images/val2014/COCO_val2014_000000184791.jpg')
    print(sents)


import random
import actor
import pygame
import weapon

KEYS_DICT = {pygame.K_UP:'up',pygame.K_DOWN:'down',pygame.K_LEFT:'left',pygame.K_RIGHT:'right'}

def spawn_ammo(timer):
    if timer == random.randint(0,100):
        print ('ammo spawn')
        weapon.Ammo(random.randint(100,800),random.randint(100,700))
        weapon.FirstAidBox(random.randint(20,500),random.randint(20,500))

def spawn_enemies(n):
    for i in range(n):
        actor.EActor(random.randint(100,500),random.randint(100,500),random.choice(actor.ENEMY_LIST),random.choice(weapon.WEAPON_LIST))

def has_beaten_level():
    """Switches to a new scene once you have beaten the level.
    Beating the level consists of killing all enemies"""
    if len(actor.enemy_sprites) <=0:
        return True


# -*- coding: utf-8 -*-
import json

from django.shortcuts import render, get_object_or_404, redirect
from django.db import connections
from django.db.models import Count, Sum, Q, F
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.utils import timezone

from .models import Candidates, Terms, Intent, Intent_Likes
from .forms import IntentForm
from commontag.views import paginate

def intents(request, election_year):
    ref = {
        'create_at': 'id',
        'likes': 'likes'
    }
    order_by = ref.get(request.GET.get('order_by'), 'likes')
    intents = Intent.objects.filter(election_year=election_year).order_by('-%s' % order_by)
    intents = paginate(request, intents)
    return render(request, 'candidates/intents.html', {'intents': intents, 'election_year': election_year})

def districts(request, election_year, county):
    districts = Terms.objects.filter(election_year=election_year, county=county)\
                             .values('constituency', 'district')\
                             .annotate(candidates=Count('id'))\
                             .order_by('constituency')
    return render(request, 'candidates/districts.html', {'election_year': election_year, 'county': county, 'districts': districts})

def district(request, election_year, county, constituency):
    candidates = Terms.objects.filter(election_year=election_year, county=county, constituency=constituency).order_by('-votes')
    return render(request, 'candidates/district.html', {'election_year': election_year, 'county': county, 'district': candidates[0].district, 'candidates': candidates})

def intent_home(request):
    return render(request, 'candidates/intent_home.html', )

def intent_upsert(request):
    election_year = '2018'
    if not request.user.is_authenticated:
        return redirect(reverse('candidates:intent_home'))
    try:
        instance = Intent.objects.get(user=request.user, election_year=election_year)
    except:
        instance = None
    if request.method == 'GET':
        form = IntentForm(instance=instance)
        form.fields['name'].initial = request.user.last_name + request.user.first_name
    if request.method == 'POST':
        form = IntentForm(request.POST, instance=instance)
        if form.has_changed() and form.is_valid():
            intent = form.save(commit=False)
            intent.user = request.user
            intent.status = 'intent_apply'
            intent.save()
            c = connections['default'].cursor()
            history = request.POST.copy()
            history.pop('csrfmiddlewaretoken', None)
            history['modify_at'] = timezone.now().strftime('%Y-%m-%d %H:%M:%S')
            c.execute('''
                UPDATE candidates_intent
                SET history = (%s::jsonb || COALESCE(history, '[]'::jsonb))
                WHERE user_id = %s AND election_year = %s
            ''', [json.dumps([history]), request.user.id, election_year])
        return redirect(reverse('candidates:intent_detail', kwargs={'intent_id': instance.uid if instance else intent.uid}))
    return render(request, 'candidates/intent_upsert.html', {'form': form})

def intent_detail(request, intent_id):
    intent = get_object_or_404(Intent.objects.select_related('user'), uid=intent_id)
    if request.user.is_authenticated:
        user_liked = Intent_Likes.objects.filter(intent_id=intent_id, user=request.user).exists()
        if request.method == 'POST':
            if request.POST.get('decision') == 'upvote' and not user_liked:
                Intent_Likes.objects.create(intent_id=intent_id, user=request.user)
                intent.likes += + 1
                intent.save(update_fields=["likes"])
                user_liked = True
            elif request.POST.get('decision') == 'downvote' and user_liked:
                Intent_Likes.objects.filter(intent_id=intent_id, user=request.user).delete()
                intent.likes -= 1
                intent.save(update_fields=["likes"])
                user_liked = False
    return render(request, 'candidates/intent_detail.html', {'intent': intent, 'user_liked': request.user.is_authenticated and user_liked})

def pc(request, candidate_id, election_year):
    candidate = get_object_or_404(Terms.objects, election_year=election_year, candidate_id=candidate_id)
    return render(request, 'candidates/pc.html', {'candidate': candidate})


from flask import Flask
from .view_classes import BasicView, RouteBaseView, ModifiedSuffixAPI
from nose.tools import *

app = Flask('route_base')
RouteBaseView.register(app, route_base="/rb_test2/")
ModifiedSuffixAPI.register(app)

def test_route_base_override():
    client = app.test_client()
    resp = client.get('/rb_test2/')
    eq_(b"Index", resp.data)

def test_modified_suffix():
    client = app.test_client()
    resp = client.get('/modifiedsuffix/')
    eq_(b"Modified Suffix", resp.data)


import json, codecs, os
from tqdm import tqdm
import numpy as np

# 标签路径
labelme_path = "/home/data1/competition/data/tianzhi2/CASIA-Ship/train/mask"  # 原始labelme标注数据路径
saved_path = "/home/data1/competition/data/tianzhi2/CASIA-Ship/train/Annotations"  # 保存路径
os.makedirs(saved_path, exist_ok=True)

# 获取待处理文件
files = os.listdir(labelme_path)
files = list(filter(lambda x: x.endswith('json'), files))
files = [i.split("/")[-1].split(".json")[0] for i in files]

# 读取标注信息并写入 xml
for json_file_ in tqdm(files):
    json_filename = os.path.join(labelme_path, json_file_ + ".json")
    json_file = json.load(open(json_filename, "r", encoding="utf-8"))
    height, width, channels = json_file['imageHeight'], json_file['imageWidth'], 3
    with codecs.open(os.path.join(saved_path, json_file_ + ".xml"), "w", "utf-8") as xml:
        xml.write('<annotation>\n')
        xml.write('\t<folder>' + 'VOC' + '</folder>\n')
        xml.write('\t<filename>' + json_file_ + ".jpg" + '</filename>\n')
        xml.write('\t<size>\n')
        xml.write('\t\t<width>' + str(width) + '</width>\n')
        xml.write('\t\t<height>' + str(height) + '</height>\n')
        xml.write('\t\t<depth>' + str(channels) + '</depth>\n')
        xml.write('\t</size>\n')
        xml.write('\t\t<segmented>0</segmented>\n')
        for multi in json_file["shapes"]:
            points = np.array(multi["points"])
            xmin = min(points[:, 0])
            xmax = max(points[:, 0])
            ymin = min(points[:, 1])
            ymax = max(points[:, 1])
            label = multi["label"]
            if xmax <= xmin:
                pass
            elif ymax <= ymin:
                pass
            else:
                xml.write('\t<object>\n')
                xml.write('\t\t<name>' + label + '</name>\n')
                xml.write('\t\t<pose>Unspecified</pose>\n')
                xml.write('\t\t<truncated>0</truncated>\n')
                xml.write('\t\t<difficult>0</difficult>\n')
                xml.write('\t\t<bndbox>\n')
                xml.write('\t\t\t<xmin>' + str(int(round(xmin))) + '</xmin>\n')
                xml.write('\t\t\t<ymin>' + str(int(round(ymin))) + '</ymin>\n')
                xml.write('\t\t\t<xmax>' + str(int(round(xmax))) + '</xmax>\n')
                xml.write('\t\t\t<ymax>' + str(int(round(ymax))) + '</ymax>\n')
                xml.write('\t\t</bndbox>\n')
                xml.write('\t</object>\n')

        xml.write('</annotation>')


#-*- coding:utf-8 -*-

from flask import (render_template, abort, request, url_for, redirect, g,
                   Blueprint)
from jinja2 import TemplateNotFound
from flask.ext import login as li

from app.core.models import User
from app.core.forms import LoginForm, RegistrationForm
from app.extensions import db

core = Blueprint('core', __name__, template_folder='templates')

def init_login(app):
    manager = li.LoginManager()
    manager.init_app(app)

    manager.login_view = 'core.login'

    @manager.user_loader
    def load_user(user_id):
        return User.query.get(user_id)

    @app.before_request
    def before_request():
        g.user = li.current_user

@core.route('', endpoint='index')
# @li.login_required
def index():
    try:
        return render_template('index.html')
    except TemplateNotFound:
        abort(404)

@core.route('login/', methods=["GET", "POST"], endpoint='login')
def login():
    form = LoginForm(request.form)
    if form.validate_on_submit():
        user = form.get_user()
        li.login_user(user, remember=form.remember.data)
        redirect_to = request.args.get('next', url_for('.index'), type=str)
        return redirect(redirect_to)
    return render_template('core/login.html', form=form)

@core.route('logout/', endpoint='logout')
def logout():
    li.logout_user()
    return redirect(url_for('.index'))

@core.route('register/', methods=["GET", "POST"], endpoint='register')
def register():
    form = RegistrationForm(request.form)
    if form.validate_on_submit():
        user = User()
        form.populate_obj(user)

        db.session.add(user)
        db.session.commit()
        li.login_user(user)
        return redirect(url_for('.index'))
    return render_template('core/register.html', form=form)


import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np
import random

from TransformerBlock import TransformerDecoderBlock as DecoderBlock
from TransformerBlock import get_sinusoid_encoding_table
# from bert_embedding import get_bert_embedding
from layers import BERT, LinearProjection, LinearNet_TwoLayer, FFN, PostNet, LinearProjection
from text.symbols import symbols
import hparams as hp
# print(len(symbols))
from module import *

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# device = torch.device("cpu")

if_parallel = True

class WESS_Encoder(nn.Module):
    """
    Encoder
    (pre-transformer replaced by GRU)
    """

    def __init__(self,
                 vocab_max_size=2000,
                 embedding_size=256,
                 GRU_hidden_size=768,
                 GRU_num_layers=1,
                 GRU_batch_first=True,
                 GRU_bidirectional=True,
                 bert_prenet_hidden=1024,
                 bert_prenet_output=256,
                 bert_hidden=256,
                 bert_n_layers=2,
                 bert_attn_heads=4,
                 #  embedding_postnet_hidden=1024,
                 #  embedding_postnet_output=256,
                 dropout=0.1):
        """
        :param encoder_hparams
        """

        super(WESS_Encoder, self).__init__()
        self.vocab_max_size = vocab_max_size
        self.embedding_size = embedding_size
        self.GRU_hidden = GRU_hidden_size
        self.GRU_num_layers = GRU_num_layers
        self.GRU_batch_first = GRU_batch_first
        self.GRU_bidirectional = GRU_bidirectional
        self.bert_prenet_hidden = bert_prenet_hidden
        self.bert_prenet_output = bert_prenet_output
        self.bert_hidden = bert_hidden
        self.bert_n_layers = bert_n_layers
        self.bert_attn_heads = bert_attn_heads
        # self.embedding_postnet_hidden = embedding_postnet_hidden
        # self.embedding_postnet_output = embedding_postnet_output
        self.dropout = dropout

        # Embeddings
        self.pre_embedding = nn.Embedding(len(symbols)+1, self.embedding_size)
        self.position_embedding = nn.Embedding.from_pretrained(
            get_sinusoid_encoding_table(self.vocab_max_size, self.embedding_size), freeze=True)

        # self.pre_GRU = nn.GRU(input_size=self.embedding_size,
        #                       hidden_size=self.GRU_hidden,
        #                       num_layers=self.GRU_num_layers,
        #                       batch_first=self.GRU_batch_first,
        #                       dropout=self.dropout,
        #                       bidirectional=self.GRU_bidirectional)

        self.pre_GRU = nn.GRU(input_size=self.embedding_size,
                              hidden_size=self.GRU_hidden,
                              num_layers=self.GRU_num_layers,
                              batch_first=self.GRU_batch_first,
                              bidirectional=self.GRU_bidirectional)

        self.bert_pre_net = LinearNet_TwoLayer(
            self.GRU_hidden, self.bert_prenet_hidden, self.bert_prenet_output)

        self.bi_Transformer = BERT(hidden=self.bert_hidden,
                                   n_layers=self.bert_n_layers,
                                   attn_heads=self.bert_attn_heads,
                                   dropout=self.dropout)

        # self.EmbeddingNet = LinearNet_TwoLayer(
        #     self.embedding_size, self.embedding_postnet_hidden, self.embedding_postnet_output)

    def init_GRU_hidden(self, batch_size, num_layers, hidden_size):
        if self.GRU_bidirectional:
            return torch.zeros(num_layers*2, batch_size,  hidden_size).to(device)
        else:
            return torch.zeros(num_layers*1, batch_size,  hidden_size).to(device)

    def get_GRU_embedding(self, GRU_output):
        # print(GRU_output.size())
        out_1 = GRU_output[:, 0:1, :]
        out_2 = GRU_output[:, GRU_output.size(1)-1:, :]

        out = out_1 + out_2
        out = out[:, :, 0:out.size(2)//2] + out[:, :, out.size(2)//2:]

        # print(out.size())
        return out

    def cal_P_GRU(self, batch, gate_for_words_batch):
        list_input = list()
        list_output = list()

        for ind in range(len(gate_for_words_batch)-1):
            list_input.append(
                batch[gate_for_words_batch[ind]:gate_for_words_batch[ind+1]])

        # print(len(list_input))
        for one_word in list_input:
            one_word = torch.stack([one_word])

            # pos_input = torch.Tensor(
            #     [i for i in range(one_word.size(1))]).long().to(device)
            # position_embedding = self.position_embedding(pos_input)
            # position_embedding = position_embedding.unsqueeze(0)

            # one_word = one_word + position_embedding
            # output_one_word = self.P_transformer_block(one_word)
            # print(output_one_word.size())

            # self.pre_GRU.flatten_parameters()
            output_one_word = self.pre_GRU(one_word)[0]
            output_one_word = self.get_GRU_embedding(output_one_word)
            word = output_one_word.squeeze(0)
            # word = output_one_word[output_one_word.size()[0]-1]
            list_output.append(word)

        output = torch.stack(list_output)
        output = output.squeeze(1)
        # print(output.size())

        return output

    # def pad_by_word(self, words_batch):
    #     len_arr = np.array(list())
    #     for ele in words_batch:
    #         len_arr = np.append(len_arr, ele.size(0))
    #     max_size = int(len_arr.max())
    #     # print(max_size)

    #     def pad(tensor, target_length):
    #         embedding_size = tensor.size(1)
    #         pad_tensor = torch.zeros(1, embedding_size).to(device)

    #         for i in range(target_length-tensor.size(0)):
    #             tensor = torch.cat((tensor, pad_tensor))

    #         return tensor

    #     padded = list()
    #     for one_batch in words_batch:
    #         one_batch = pad(one_batch, max_size)
    #         padded.append(one_batch)
    #     padded = torch.stack(padded)

    #     return padded

    # def pad_all(self, word_batch, embeddings):
    #     # print(word_batch.size())
    #     # print(embeddings.size())
    #     if word_batch.size(1) == embeddings.size(1):
    #         return word_batch, embeddings

    #     if word_batch.size(1) > embeddings.size(1):
    #         pad_len = word_batch.size(1) - embeddings.size(1)
    #         pad_vec = torch.zeros(word_batch.size(
    #             0), pad_len, embeddings.size(2)).float().to(device)
    #         embeddings = torch.cat((embeddings, pad_vec), 1)
    #         return word_batch, embeddings

    #     if word_batch.size(1) < embeddings.size(1):
    #         pad_len = embeddings.size(1) - word_batch.size(1)
    #         pad_vec = torch.zeros(word_batch.size(
    #             0), pad_len, embeddings.size(2)).float().to(device)
    #         word_batch = torch.cat((word_batch, pad_vec), 1)
    #         return word_batch, embeddings

    def pad_bert_embedding_and_GRU_embedding(self, bert_embedding, GRU_embedding):
        """One batch"""

        len_bert = len(bert_embedding)
        len_GRU = GRU_embedding.size(0)
        max_len = max(len_bert, len_GRU)

        # pad_embedding = torch.zeros(1, GRU_embedding.size(1)).to(device)

        # for i in range(max_len - len_GRU):
        #     GRU_embedding = torch.cat((GRU_embedding, pad_embedding), 0)

        # for i in range(max_len - len_bert):
        #     bert_embedding = torch.cat((bert_embedding, pad_embedding), 0)

        # print(GRU_embedding.size())
        # print(bert_embedding.size())

        GRU_embedding = torch.cat((GRU_embedding, torch.zeros(
            max_len - len_GRU, GRU_embedding.size(1)).to(device)), 0)
        bert_embedding = torch.cat((bert_embedding, torch.zeros(
            max_len - len_bert, GRU_embedding.size(1)).to(device)), 0)

        # output = bert_embedding + GRU_embedding

        return bert_embedding, GRU_embedding

    def pad_all(self, bert_transformer_input):
        len_list = list()
        for batch in bert_transformer_input:
            len_list.append(batch.size(0))

        max_len = max(len_list)

        # pad_embedding = torch.zeros(
        #     1, bert_transformer_input[0].size(1)).to(device)

        for index, batch in enumerate(bert_transformer_input):
            bert_transformer_input[index] = torch.cat((bert_transformer_input[index], torch.zeros(
                max_len - bert_transformer_input[index].size(0), bert_transformer_input[index].size(1)).to(device)), 0)

        bert_transformer_input = torch.stack(bert_transformer_input)

        return bert_transformer_input

    def forward(self, x, bert_embeddings, gate_for_words, indexs_list):
        """
        :param: x: (batch, length)
        :param: bert_embeddings: (batch, length, 768)
        :param: gate_for_words: (batch, indexs)
        """

        # Embedding
        x = self.pre_embedding(x)
        # print("x:", x.size())

        # P_GRU
        words_batch = list()
        for index, batch in enumerate(x):
            words_batch.append(self.cal_P_GRU(batch, gate_for_words[index]))

        # words_batch = self.pad_by_word(words_batch)
        # bert_embeddings = self.pad_by_word(bert_embeddings)
        # words_batch, bert_embeddings = self.pad_all(
        #     words_batch, bert_embeddings)
        # # print(words_batch.size())
        # # print(bert_embeddings.size())
        # bert_input = words_batch + bert_embeddings

        # # Add Position Embedding
        # pos_input = torch.stack([torch.Tensor([i for i in range(
        #     bert_input.size(1))]).long() for i in range(bert_input.size(0))]).to(device)
        # pos_embedding = self.position_embedding(pos_input)
        # bert_input = bert_input + pos_embedding

        # encoder_output = self.bert_encoder(bert_input)

        # New

        # If you need data parallel, you will need some extra processing.
        if self.training and if_parallel:
            # print(indexs_list)
            bert_embeddings = [bert_embeddings[i] for i in indexs_list]

        if not (len(words_batch) == len(bert_embeddings)):
            raise ValueError(
                "the length of bert embeddings is not equal to the length of GRU embeddings.")

        bert_transformer_input = list()

        for batch_index in range(len(words_batch)):

            # print("bert_embedding:", bert_embeddings[batch_index])
            # print(words_batch[batch_index])

            bert_embedding, GRU_embedding = self.pad_bert_embedding_and_GRU_embedding(
                bert_embeddings[batch_index], words_batch[batch_index])
            add_bert_GRU = bert_embedding + GRU_embedding
            bert_transformer_input.append(add_bert_GRU)

        bert_transformer_input = self.pad_all(bert_transformer_input)

        pos_input_one_batch = torch.Tensor(
            [i for i in range(bert_transformer_input.size(1))]).long()
        pos_input = torch.stack([pos_input_one_batch for _ in range(
            bert_transformer_input.size(0))]).to(device)
        position_embedding = self.position_embedding(pos_input)

        #  print("position_embedding:", position_embedding.size())
        # print("position embedding:", position_embedding)

        # bert_transformer_input = bert_transformer_input + position_embedding

        # print("bert_transformer_input:", bert_transformer_input.size())
        bert_transformer_input = self.bert_pre_net(bert_transformer_input)
        # print("bert_transformer_input:", bert_transformer_input.size())

        bert_transformer_input = bert_transformer_input + position_embedding

        encoder_output_word = self.bi_Transformer(bert_transformer_input)

        # encoder_output_alpha = self.EmbeddingNet(x)
        pos_input_alpha = torch.stack([torch.Tensor([i for i in range(x.size(1))]).to(
            device) for _ in range(x.size(0))]).long()
        position_embedding_alpha = self.position_embedding(pos_input_alpha)
        encoder_output_alpha = x + position_embedding_alpha

        return encoder_output_word, encoder_output_alpha

class TacotronDecoder(nn.Module):
    """
    Tacotron Decoder
    """

    def __init__(self, hidden_size=128):
        super(TacotronDecoder, self).__init__()
        self.prenet = Prenet(hp.num_mels, hidden_size * 2, hidden_size)
        self.attn_decoder = AttentionDecoder(hidden_size * 2)
        self.postnet = PostNet()

    def forward(self, decoder_input, memory):
        # Initialize hidden state of GRUcells
        attn_hidden, gru1_hidden, gru2_hidden = self.attn_decoder.inithidden(
            decoder_input.size()[0])
        outputs = list()

        # Training phase
        if self.training:
            # Prenet
            dec_input = self.prenet.forward(decoder_input)
            timesteps = dec_input.size()[2] // hp.outputs_per_step

            # [GO] Frame
            # prev_output = dec_input[:, :, 0]
            # print(prev_output.size())
            GO_frame = torch.zeros(dec_input.size(
                0), dec_input.size(1)).to(device)
            # print(GO_frame.size())

            prev_output, attn_hidden, gru1_hidden, gru2_hidden = self.attn_decoder.forward(
                GO_frame, memory, attn_hidden=attn_hidden, gru1_hidden=gru1_hidden, gru2_hidden=gru2_hidden)
            outputs.append(prev_output)

            for i in range(timesteps):
                # Teacher Forced
                if random.random() < hp.teacher_forcing_ratio:
                    # Get spectrum at rth position
                    prev_output = dec_input[:, :, (i+1)*hp.outputs_per_step-1]
                else:
                    # Get last output
                    prev_output = prev_output[:, :, -1]
                    # prev_output = output[-1]

                prev_output, attn_hidden, gru1_hidden, gru2_hidden = self.attn_decoder.forward(
                    prev_output, memory, attn_hidden=attn_hidden, gru1_hidden=gru1_hidden, gru2_hidden=gru2_hidden)

                outputs.append(prev_output)

                # if random.random() < hp.teacher_forcing_ratio:
                #     # Get spectrum at rth position
                #     prev_output = dec_input[:, :, i * hp.outputs_per_step]
                # else:
                #     # Get last output
                #     prev_output = prev_output[:, :, -1]

            # Concatenate all mel spectrogram

            outputs = torch.cat(outputs, 2)

            # print(outputs.size())
            # print(decoder_input.size())

            outputs = outputs[:, :, 0:decoder_input.size(2)]

            # outputs_postnet = self.postnet(outputs) + outputs

        # Test
        else:
            # [GO] Frame
            prev_output = decoder_input

            for i in range(hp.max_iters):
                prev_output = self.prenet.forward(prev_output)
                prev_output = prev_output[:, :, 0]
                prev_output, attn_hidden, gru1_hidden, gru2_hidden = self.attn_decoder.forward(
                    prev_output, memory, attn_hidden=attn_hidden, gru1_hidden=gru1_hidden, gru2_hidden=gru2_hidden)
                outputs.append(prev_output)
                prev_output = prev_output[:, :, -1].unsqueeze(2)

            outputs = torch.cat(outputs, 2)

            # outputs_postnet = self.postnet(outputs) + outputs

        # return outputs, outputs_postnet
        return outputs

# class PostProcessingNet(nn.Module):
#     """
#     Post-processing Network
#     """

#     def __init__(self, hidden_size=125, num_freq=1025):
#         super(PostProcessingNet, self).__init__()
#         self.postcbhg = CBHG(
#             hidden_size, K=8, projection_size=hp.num_mels, is_post=True)
#         self.linear = SeqLinear(hidden_size * 2, num_freq)

#     def forward(self, decoder_output):
#         out = self.postcbhg.forward(decoder_output)
#         out = self.linear.forward(torch.transpose(out, 1, 2))

#         return out

class WETaSS(nn.Module):
    """
    WESS combine with Tacotron
    """

    def __init__(self):
        super(WETaSS, self).__init__()
        self.encoder = WESS_Encoder()
        self.decoder_word = TacotronDecoder()
        self.decoder_alpha = TacotronDecoder()
        # self.postnet = PostProcessingNet()
        self.postnet = PostNet()

    def forward(self, x, bert_embeddings, gate_for_words, mel_target=None, indexs_list=None):
        if self.training:
            encoder_output_word, encoder_output_alpha = self.encoder(
                x, bert_embeddings, gate_for_words, indexs_list)
            decoder_output_word = self.decoder_word(
                mel_target, encoder_output_word)
            decoder_output_alpha = self.decoder_alpha(
                mel_target, encoder_output_alpha)

            mel_output = decoder_output_word + decoder_output_alpha
            mel_output = mel_output.contiguous().transpose(1, 2)

            mel_output_postnet = self.postnet(mel_output) + mel_output

            mel_target = mel_target.contiguous().transpose(1, 2)

            return mel_output, mel_output_postnet, mel_target
        else:
            decoder_input = torch.zeros(1, hp.num_mels, 1).to(device)

            encoder_output_word, encoder_output_alpha = self.encoder(
                x, bert_embeddings, gate_for_words, indexs_list)
            decoder_output_word = self.decoder_word(
                decoder_input, encoder_output_word)
            decoder_output_alpha = self.decoder_alpha(
                decoder_input, encoder_output_alpha)

            mel_output = decoder_output_word + decoder_output_alpha
            mel_output = mel_output.contiguous().transpose(1, 2)

            mel_output_postnet = self.postnet(mel_output) + mel_output

            return mel_output, mel_output_postnet

# if __name__ == "__main__":
#     # Test
#     test_decoder = TacotronDecoder().to(device)
#     output = test_decoder(torch.randn(2, 80, 121).to(
#         device), torch.randn(2, 56, 256).to(device))
#     print(output.size())

#     # test_postnet = PostProcessingNet().to(device)
#     # out = test_postnet(output)
#     # print(out.size())

#     test_WETaSS = WETaSS().to(device)
#     print(test_WETaSS)


import pygame

def FindJoysticks():
    print("Found " + str(pygame.joystick.get_count()) + " devices")
    if pygame.joystick.get_count() > 1:
        for i in range(0, pygame.joystick.get_count()):
            print('[' + str(i) + ']' + pygame.joystick.Joystick(i).get_name())
        device = int(input("Choose one: "))
    else:
        device = 0

    return device

class Control_device():
    axis_data = None
    button_data = None
    power = None
    direction = None

    def __init__(self):
        pygame.init()
        pygame.joystick.init()
        if(pygame.joystick.get_count()):
            try:
                self.keyboard = 0
                self.joy = pygame.joystick.Joystick(FindJoysticks())
                self.joy.init()
                print(self.joy.get_name(), ' connected')
                print(self.joy.get_numaxes(), 'Axes,',
                      self.joy.get_numbuttons(), 'Buttons')

            except pygame.error as message:
                self.keyboard = 1
                print("Keyboard control")

        else:
            self.keyboard = 1
            print("Keyboard control")


import torch

def clip_eta(eta, ord, eps):
    """
    Helper fucntion to clip the perturbation to epsilon norm ball.

    Args:
        eta: A tensor with the current perturbation
        ord: Order of the norm (mimics Numpy)
             Possible values: np.inf, 1 or 2.
        eps: Epsilon, bound of the perturbation.
    """

    # Clipping perturbation eta to self.ord norm ball
    if ord not in [np.inf, 1, 2]:
        raise ValueError('ord must be np.inf, 1, or 2.')

    reduce_ind = list(xrange(1, len(eta.shape)))
    azdiv = 1e-12

    if ord == np.inf:
        eta = torch.clamp(eta, -eps, eps)
    else:
        if ord == 1:
            raise NotImplementedError("The expression below is not the correct way"
                                      " to project onto the L1 norm ball.")
            norm = torch.maximum(azdiv, torch.mean(torch.abs(eta), reduce_ind))

        elif ord == 2:
            # azdiv(avoid_zero_div) must go inside sqrt to avoid a divide by zero
            # in the gradient through this operation.
            norm = torch.sqrt(torch.maximum(azdiv, torch.mean(eta**2, reduce_ind)))

        # We must clip to within the norm ball, not 'normalize' onto the
        # surface of the ball
        factor = torch.minimum(1., div(eps, norm))
        eta *= factor

    return eta


import logging
import redis
from django.apps import apps
from .settings import DRC_PREFIX
from .settings import DRC_CONNECTIONS

logger = logging.getLogger(__name__)

class Storage(object):

    def __init__(self, prefix, config):
        self.prefix = prefix
        self.config = config
        self.connections = {}

    @property
    def names(self):
        ns = list(self.config.keys())
        ns.sort()
        return ns

    def make_connection(self, connection_config):
        url = connection_config["url"]
        options = {
            "decode_responses": True,
        }
        connection_options = connection_config.get("options", {})
        options.update(connection_options)
        return redis.Redis.from_url(url, **options)

    def get_connection(self, name):
        if not name in self.connections:
            self.connections[name] = self.make_connection(self.config[name])
        return self.connections[name]

    def make_key(self, model, content):
        if isinstance(model, str):
            model_name = model
        else:
            model_name = model._meta.label
        if isinstance(content, (int, str)):
            content_value = str(content)
        else:
            content_value = str(content.pk)
        return ":".join([self.prefix, model_name, content_value])

    def incr(self, model, content, value=1, using="default"):
        save_key = self.make_key(model, content)
        connection = self.get_connection(using)
        return connection.incr(save_key, amount=value)

    def decr(self, model, content, value=1, using="default"):
        save_key = self.make_key(model, content)
        connection = self.get_connection(using)
        return connection.decr(save_key, amount=value)

    def close(self, name=None):
        if name:
            del self.connections[name]
        else:
            self.connections = {}

    def get_items(self, using="default"):
        items = []
        connection = self.get_connection(using)
        for item_key in connection.keys(self.prefix + ":*"):
            value = connection.get(item_key)
            _, model, content = item_key.split(":")
            items.append({
                "model": model,
                "content": int(content),
                "value": int(value),
            })
        return items

    def dump(self):
        for name in self.names:
            items = self.get_items(name)
            for item in items:
                model = item["model"]
                app_label, model_name = model.split(".")
                model = apps.get_model(app_label, model_name)
                content = item["content"]
                value = item["value"]
                model.update(content, value)
                self.decr(model, content, value=value, using=name)

connections = Storage(DRC_PREFIX, DRC_CONNECTIONS)


from django.shortcuts import render, HttpResponse
import requests

def index(request):
    # Verifica si hay un parámetro value en la petición GET
    if 'latitud' in request.GET:
        pregunta1 = request.GET['pregunta1']
        pregunta2 = request.GET['pregunta2']
        pregunta3 = request.GET['pregunta3']
        latitud = request.GET['latitud']
        altitud = request.GET['altitud']
        # Verifica si el value no esta vacio
        if latitud:
            # Crea el json para realizar la petición POST al Web Service
            args = {'pregunta1': pregunta1, 'pregunta2':pregunta2, 'pregunta3':pregunta3, 'latitud':latitud, 'altitud':altitud}
            #response = requests.post('http://127.0.0.1:8000/formularios/', args)
            response = requests.post('http://aquicabeunarbol.azurewebsites.net/formularios/', args)
            # Convierte la respuesta en JSON
            acuafrs_json = response.json()
    if 'cupo' in request.GET:
        tipo = request.GET['tipo']
        carac = request.GET['carac']
        direction = request.GET['direction']
        cupo = request.GET['cupo']
        # Verifica si el value no esta vacio
        if cupo:
            # Crea el json para realizar la petición POST al Web Service
            args = {'tipo': tipo, 'carac':carac, 'direction':direction, 'cupo':cupo}
            #response = requests.post('http://127.0.0.1:8000/eventos/', args)
            response = requests.post('http://aquicabeunarbol.azurewebsites.net/eventos/', args)
            # Convierte la respuesta en JSON
            acuafrs_json = response.json()
    # Realiza una petición GET al Web Services
    #response = requests.get('http://127.0.0.1:8000/formularios/')
    response = requests.get('http://aquicabeunarbol.azurewebsites.net/formularios/')

    # Convierte la respuesta en JSON
    arboles = response.json()
    # Rederiza la respuesta en el template measure
    return render(request, "acuafr/index.html", {'arboles': arboles})


import torch
import sys
from utils.logger_utils import get_logger

def set_manual_seed(seed):
    logger = get_logger(__name__)
    cuda = is_cuda()
    logger.info(f"CUDA Available? {cuda}")
    torch.cuda.manual_seed(seed) if cuda else torch.manual_seed(seed)

def is_cuda():
    return torch.cuda.is_available()

def get_device_type():
  use_cuda = torch.cuda.is_available()
  device = torch.device("cuda" if use_cuda else "cpu")
  return device

if __name__ == "__main__":
    set_manual_seed(20)
    print(is_cuda())


#!/usr/bin/env python3
#
# search is the search component of cve-search querying the MongoDB database.
#
# Software is free software released under the "Modified BSD license"
#
# Copyright (c) 2012 		Wim Remes
# Copyright (c) 2012-2014 	Alexandre Dulaunoy - a@foo.be
# Copyright (c) 2015	 	Pieter-Jan Moreels - pieterjan.moreels@gmail.com

# Imports
import os
import sys
_runPath = os.path.dirname(sys.argv[0])
sys.path.append(os.path.join(_runPath, "./lib/"))

import pymongo

import re
import argparse
import csv
from urllib.parse import urlparse
import json
from bson import json_util
import configparser

import cves

# config
Config = configparser.ConfigParser()
Config.read(os.path.join(_runPath, "./configuration.ini"))
# default config
mongoHost = 'localhost'
mongoPort = 27017
# read config from file
if ("Host" in Config['Mongo']) and (Config.get("Mongo", "Host")!=""):
    mongoHost = Config.get("Mongo", "Host")
if ("Port" in Config['Mongo']) and (Config.get("Mongo", "Port")!=""):
    mongoPort = int(Config.get("Mongo", "Port"))

# connect to DB
connect = pymongo.MongoClient(mongoHost,mongoPort)
db = connect.cvedb
collection = db.cves

# init control variables
csvOutput = 0
htmlOutput = 0
jsonOutput = 0
xmlOutput = 0

# init various variables :-)
vSearch = ""
vOutput = ""
vFreeSearch = ""

# parse command-line arguments
argParser = argparse.ArgumentParser(description='Search for vulnerabilities in the National Vulnerability DB. Data from http://nvd.nist.org.')
argParser.add_argument('-p', type=str, help='S = search product, e.g. o:microsoft:windows_7 or o:cisco:ios:12.1')
argParser.add_argument('-f', type=str, help='F = free text search in vulnerability summary')
argParser.add_argument('-c', action='append', help='search one or more CVE-ID')
argParser.add_argument('-o', type=str, help='O = output format [csv|html|json|xml|cveid]')
argParser.add_argument('-l', action='store_true', help='sort in descending mode')
argParser.add_argument('-n', action='store_true', help='lookup complete cpe (Common Platform Enumeration) name for vulnerable configuration')
argParser.add_argument('-r', action='store_true', help='lookup ranking of vulnerable configuration')
argParser.add_argument('-v', type=str, help='vendor name to lookup in reference URLs')
args = argParser.parse_args()
vSearch = args.p
cveSearch = args.c
vOutput = args.o
vFreeSearch = args.f
sLatest = args.l
namelookup = args.n
rankinglookup = args.r

cves = cves.last(rankinglookup = rankinglookup, namelookup = namelookup)

# replace special characters in vSearch with encoded version.
# Basically cuz I'm to lazy to handle conversion on DB creation ...
if vSearch:
    vSearch = re.sub(r'\(','%28', vSearch)
    vSearch = re.sub(r'\)','%29', vSearch)

# define which output to generate.
if vOutput == "csv":
    csvOutput = 1
elif vOutput == "html":
    htmlOutput = 1
elif vOutput == "xml":
    xmlOutput = 1
    from xml.etree.ElementTree import Element, SubElement, tostring
    from xml.sax.saxutils import escape as SaxEscape
    r = Element('cve-search')
elif vOutput == "json":
    jsonOutput = 1
elif vOutput == "cveid":
    cveidOutput = 1
else:
    cveidOutput = False

# Print first line of html output
if htmlOutput and args.p is not None:
    print("
CVE search "+ args.p +"
")
elif htmlOutput and args.c is not None:
    print("
CVE-ID "+ str(args.c) +"
")

# search default is ascending mode
sorttype=1
if sLatest:sorttype=-1

if cveSearch:
    for cveid in cveSearch:
        for item in collection.find({'id': cveid}).sort("Modified",sorttype):
            if not namelookup and not rankinglookup:
                print(json.dumps(item, sort_keys=True, default=json_util.default))
            else:
                if "vulnerable_configuration" in item:
                    vulconf = []
                    ranking = []
                    for conf in item['vulnerable_configuration']:
                        if namelookup:
                            vulconf.append(cves.getcpe(cpeid=conf))
                        if rankinglookup:
                            rank = cves.getranking(cpeid=conf)
                            if rank and rank not in ranking:
                                ranking.append(rank)
                    if namelookup:
                        item['vulnerable_configuration'] = vulconf
                    if rankinglookup:
                        item['ranking'] = ranking
                print(json.dumps(item, sort_keys=True, default=json_util.default))

# Basic freetext search (in vulnerability summary).
# Full-text indexing is more efficient to search across all CVEs.
if vFreeSearch:
    for item in collection.find({'summary': {'$regex' :  re.compile(vFreeSearch, re.IGNORECASE)}}).sort("Modified",sorttype):
        print(item)
# Search Product (best to use CPE notation, e.g. cisco:ios:12.2
if vSearch:
    for item in collection.find({"vulnerable_configuration": {'$regex' : vSearch}}).sort("Modified",sorttype):
        if csvOutput:
            # We assume that the vendor name is usually in the hostame of the
            # URL to avoid any match on the resource part
            refs=[]
            for entry in item['references']:
                if args.v is not None:
                    url = urlparse(entry)
                    hostname = url.netloc
                    if re.search(args.v, hostname):
                        refs.append(entry)
            if not refs:
                refs = "[no vendor link found]"
            if namelookup:
                nl = " ".join(item['vulnerable_configuration'])
            csvoutput = csv.writer(sys.stdout, delimiter='|', quotechar='|', quoting=csv.QUOTE_MINIMAL)
            if not namelookup:
                csvoutput.writerow([item['id'],item['Published'],item['cvss'],item['summary'],refs])
            else:
                csvoutput.writerow([item['id'],item['Published'],item['cvss'],item['summary'],refs,nl])
        elif htmlOutput:
            print("
"+item['id']+"

CVSS score: "+item['cvss']+"
"+""+item['Published']+"
"+item['summary']+"
")
            print("References:
")
            for entry in item['references']:
                print(entry+"
")
            print("

")
        # bson straight from the MongoDB db - converted to JSON default
        # representation
        elif jsonOutput:
            if not namelookup and not rankinglookup:
                print(json.dumps(item, sort_keys=True, default=json_util.default))
            else:
                if "vulnerable_configuration" in item:
                    vulconf = []
                    ranking = []
                    for conf in item['vulnerable_configuration']:
                        if namelookup:
                            vulconf.append(cves.getcpe(cpeid=conf))
                        if rankinglookup:
                            rank = cves.getranking(cpeid=conf)
                            if rank and rank not in ranking:
                                ranking.append(rank)
                    if namelookup:
                        item['vulnerable_configuration'] = vulconf
                    if rankinglookup:
                        item['ranking'] = ranking
                print(json.dumps(item, sort_keys=True, default=json_util.default))
        elif xmlOutput:
            c = SubElement(r,'id')
            c.text = item['id']
            c = SubElement(r,'Published')
            c.text = item['Published']
            c = SubElement(r,'cvss')
            c.text = item['cvss']
            c = SubElement(r,'summary')
            c.text = SaxEscape(item['summary'])
            for e in item['references']:
                c = SubElement(r,'references')
                c.text = SaxEscape(e)
            for e in item['vulnerable_configuration']:
                c = SubElement(r,'vulnerable_configuration')
                c.text = SaxEscape(e)
        elif cveidOutput:
            print(item['id'])
        else:
            print("CVE\t: " + item['id'])
            print("DATE\t: " + item['Published'])
            print("CVSS\t: " + str(item['cvss']))
            print(item['summary'])
            print("\nReferences:")
            print("-----------")
            for entry in item['references']:
                print(entry)
            print("\nVulnerable Configs:")
            print("-------------------")
            for entry in item['vulnerable_configuration']:
                if not namelookup:
                    print(entry)
                else:
                    print(cves.getcpe(cpeid=entry))
            print("\n\n")

if htmlOutput:
    print("")
if xmlOutput:
    # default encoding is UTF-8. Should this be detected on the terminal?
    s = tostring(r).decode("utf-8")
    print(s)


from skimage.feature import peak_local_max
from skimage.morphology import watershed
from scipy import ndimage
import numpy as np
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path da imagem com as moedas.")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
shifted = cv2.pyrMeanShiftFiltering(image, 21, 51)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

D = ndimage.distance_transform_edt(thresh)
localMax = peak_local_max(D, indices=False, min_distance=20, labels=thresh)

markers = ndimage.label(localMax, structure=np.ones((3,3)))[0]
labels = watershed(-D, markers, mask=thresh)

for label in np.unique(labels):
    if label == 0:
        continue

    mask = np.zeros(gray.shape, dtype="uint8")
    mask[labels == label] = 255

    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
    c = max(cnts, key = cv2.contourArea)

    ((x, y), r) =  cv2.minEnclosingCircle(c)
    cv2.circle(image, (int(x), int(y)), int(r), (0,255,00), 2)

    area = cv2.contourArea(c)
    perimeter = cv2.arcLength(c, True)

    value = 0

    if perimeter > 300: value = "1R$"
    elif perimeter > 200 and perimeter < 300: value = "0,25 R$"
    else : value = "??"

    cv2.putText(image, "#{}".format(value), (int(x) - 10, int(y)),
		cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)

cv2.imshow("Output", image)
cv2.waitKey()


import abc
import numpy as np
import scipy.ndimage.filters

from skimage.io import imread
from skimage import img_as_ubyte

from skimage.segmentation import slic
from cv2 import VideoWriter, VideoWriter_fourcc
from utilities import flow_to_middlebury, read_flownet2_flow, labels_to_falsecolor, max_width_height_bbox
from skimage.color import rgb2gray

class Volume:
    def __init__(self, data):
        self.data = data
        self.num_frames = data.shape[2]
        self.current_frame = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.current_frame >= self.num_frames:
            self.current_frame = 0
            raise StopIteration
        else:
            self.current_frame += 1
            return self.frame(self.current_frame - 1)

    @classmethod
    def arrange(cls, rows, cols, volume_list):
        """ useful to arrange before printing volume
            volume_list  list of Volumes.
            if 'e' will be substituted with a black volume
        """
        for i in volume_list:
            if i != 'e':
                tmp = i.data
                break

        height, width, t = tmp.shape[0], tmp.shape[1], tmp.shape[2]
        out = np.zeros(shape=(height*rows, width*cols, t, 3), dtype=np.ubyte)

        curr_col = 0
        curr_row = 0
        for vol in volume_list:
            if vol=='e':
                pass
            else:
                ystart = curr_row*height
                xstart = curr_col*width
                out[ystart:ystart+height, xstart:xstart+width] = vol.data

            curr_col += 1
            if curr_col % (cols) == 0:
                curr_row += 1
                curr_col = 0
        return ImageVolume(out)

    @abc.abstractmethod
    def rgb(self):
        return

    #TODO: super-pfusch fix.. the classtypes???
    def grayscale(self):
        tmp = np.zeros((self.data.shape[0],self.data.shape[1],self.data.shape[2]))
        rgb = self.rgb()
        for t,img in enumerate(self):
            tmp[:,:,t] = rgb2gray(rgb.data[:,:,t])
        return type(self)(tmp)

    def to_video(self, path):
        if not path.endswith(".avi"):
            path += ".avi"

        xdim, ydim, _, _ = self.rgb().data.shape
        vwrite = VideoWriter(path, VideoWriter_fourcc('M','J','P','G'), 30, (ydim, xdim))

        for img in self.rgb():
            vwrite.write(img[...,::-1])

        print("Video written to: " + str(path))
        vwrite.release()
        return

    def frame(self, t):
        return self.data[:,:,t]

    def insert_into(self, dst, rois):
        target_width, target_height = max_width_height_bbox(rois)

        padded = np.pad(dst.data.copy(), [(target_height, target_height),
                         (target_width , target_width),
                         (0,0), (0,0)], mode='mean')

        for idx, img in enumerate(self):
            roi = rois[idx]
            x1,y1,x2,y2 = roi[1],roi[0], roi[3],roi[2]

            bbox_width = int(np.ceil((x2-x1)/2)*2)
            bbox_height= int(np.ceil((y2-y1)/2)*2)

            dwidth  = int(target_width - bbox_width)
            dheight = int(target_height - bbox_height)

            xstart = int(x1 - dwidth/2) + target_width
            ystart = int(y1 - dheight/2) + target_height

            padded[ystart:ystart+target_height,xstart:xstart+target_width, idx] = img.copy()

        return type(self)(padded[target_height:-target_height, target_width:-target_width,:,:])

    def crop(self, rois, padmode='mean'):
        """ calulates the largest bounding box from rois
            creates a subvolume from them
        """
        target_width, target_height = max_width_height_bbox(rois)
        tdim = self.data.shape[2]

        bbox_vol = np.zeros((target_height, target_width)+(self.data.shape[2:]), self.data.dtype)

        # Pad image so we cannot index out of bounds with boundingbox
        padding_tuple = [(target_height, target_height),
                         (target_width , target_width),
                         (0,0),(0,0)]

        if self.data.ndim == 3:
            padding_tuple = padding_tuple[:-1]

        padded  = np.pad(self.data.copy(), padding_tuple, mode=padmode)

        for t in range(tdim):
            roi = rois[t]
            x1,y1,x2,y2 = roi[1],roi[0], roi[3],roi[2]

            bbox_width = int(np.ceil((x2-x1)/2)*2)
            bbox_height= int(np.ceil((y2-y1)/2)*2)

            dwidth  = int(target_width - bbox_width)
            dheight = int(target_height - bbox_height)

            xstart = int(x1 - dwidth/2) + target_width
            ystart = int(y1 - dheight/2) + target_height

            bbox_vol[:,:,t] = padded[ystart:ystart+target_height,
                                     xstart:xstart+target_width,t]
        return type(self)(bbox_vol)

    def project_onto(self, dst, rois):
        if len(np.unique(self.data)) == 1:
            print("NOTHING TO BACKPROJECT")
            return dst

        if self.data.ndim != dst.data.ndim:
            raise ValueError("Dimensions don't match!")

        if self.data.shape[-1] != dst.data.shape[-1]:
            raise ValueError("Depths don't match!")

        target_width, target_height = max_width_height_bbox(rois)

        padding_tuple = [(target_height, target_height),
                         (target_width , target_width),
                         (0,0), (0,0)]

        if self.data.ndim == 3: # todo make generic...
            padding_tuple = padding_tuple[:-1]

        padded = np.pad(dst.data.copy(), padding_tuple, mode='constant')

        for idx, img in enumerate(self):
            roi = rois[idx]
            x1,y1,x2,y2 = roi[1],roi[0], roi[3],roi[2]

            bbox_width = int(np.ceil((x2-x1)/2)*2)
            bbox_height= int(np.ceil((y2-y1)/2)*2)

            dwidth  = int(target_width - bbox_width)
            dheight = int(target_height - bbox_height)

            xstart = int(x1 - dwidth/2) + target_width
            ystart = int(y1 - dheight/2) + target_height

            padded[ystart:ystart+target_height,xstart:xstart+target_width, idx] = img.copy()

        unpadded = padded[target_height:-target_height, target_width:-target_width,...]
        return type(dst)(unpadded)

    def slic(self, n_segments=100, compactness=10.0, max_iter=10, sigma=0,
             spacing=None, multichannel=True, convert2lab=None,
             enforce_connectivity=True, min_size_factor=0.5,
             max_size_factor=3, slic_zero=False):

        tmp = slic(self.data, n_segments,compactness,max_iter, sigma, spacing, multichannel,
           convert2lab,enforce_connectivity, min_size_factor, max_size_factor, slic_zero)
        return LabelVolume(tmp+1)

    def framewise_slic(self, n_segments=100, compactness=10.0, max_iter=10, sigma=0,
             spacing=None, multichannel=True, convert2lab=None,
             enforce_connectivity=True, min_size_factor=0.5,
             max_size_factor=3, slic_zero=False):

        self.data.shape[0],
        tmp = np.zeros(self.data.shape[:3])
        for t, img in enumerate(self):
            lbls = slic(img, n_segments,compactness,max_iter, sigma, spacing, multichannel,
                        convert2lab,enforce_connectivity, min_size_factor, max_size_factor, slic_zero)
            tmp[...,t] = lbls
        return LabelVolume(tmp+1)

    def gaussian(self, sigma):
        if self.data.ndim != len(sigma):
          raise ValueError("sigma should be %d tuple" % self.data.ndim)
        return type(self)(scipy.ndimage.filters.gaussian_filter(self.data.copy(), sigma))

class ImageVolume(Volume):
    def __init__(self, data):
        Volume.__init__(self,data)

    def rgb(self):
        return self

    @classmethod
    def from_filelist(cls, files):
        xdim, ydim, cdim = imread(files[0]).shape
        tdim = len(files)

        if cdim != 3:
            raise ValueError("Depth must be 3(r,g,b)")

        volume = np.zeros((xdim, ydim, tdim, cdim), dtype=np.ubyte)

        for t, file in enumerate(files):
            volume[:,:,t,:] = img_as_ubyte(imread(file))
        return cls(volume)

class FlowVolume(Volume):
    def __init__(self, data):
        Volume.__init__(self, data)

    def rgb(self, step=0, normalize=True):
        return ImageVolume(flow_to_middlebury(self.data, step, normalize))

    @classmethod
    def from_filelist(cls, files):
        xdim, ydim, cdim = read_flownet2_flow(files[0]).shape
        tdim = len(files)

        if cdim != 2:
            raise ValueError("Depth must be 2(xflow, yflow)")

        volume = np.zeros((xdim, ydim, tdim, cdim))

        for t, file in enumerate(files):
            volume[:,:,t,:] = read_flownet2_flow(file)
        return cls(volume)

class LabelVolume(Volume):
    def __init__(self, data):
        Volume.__init__(self, data)

    def rgb(self, colormap='viridis'):
        return ImageVolume(labels_to_falsecolor(self.data, colormap))

    @classmethod
    def from_filelist(cls, files):
        xdim, ydim = imread(files[0]).shape
        tdim = len(files)

        volume = np.zeros((xdim, ydim, tdim), dtype=np.ubyte)

        for t, file in enumerate(files):
            volume[:,:,t,:] = img_as_ubyte(file)
        return cls(volume)

    def select(self, labels):
        tmp = np.zeros(self.data.shape, self.data.dtype)
        for lbl in labels:
            tmp += (self.data==lbl)*lbl
        return LabelVolume(tmp)

    def project_onto_imagevol(self, dst, rois, background=0):

        if len(np.unique(self.data)) == 1:
            print("NOTHING TO BACKPROJECT")
            return dst

        target_width, target_height = max_width_height_bbox(rois)
        ydim, xdim, tdim, _ =dst.data.shape

        empty = np.zeros((ydim, xdim, tdim), dst.data.dtype)

        padded = np.pad(empty, [(target_height, target_height),
                         (target_width , target_width),
                         (0,0)], mode='constant')

        for idx, img in enumerate(self):
            roi = rois[idx]
            x1,y1,x2,y2 = roi[1],roi[0], roi[3],roi[2]

            bbox_width = int(np.ceil((x2-x1)/2)*2)
            bbox_height= int(np.ceil((y2-y1)/2)*2)

            dwidth  = int(target_width - bbox_width)
            dheight = int(target_height - bbox_height)

            xstart = int(x1 - dwidth/2) + target_width
            ystart = int(y1 - dheight/2) + target_height

            padded[ystart:ystart+target_height,xstart:xstart+target_width, idx] = img.copy()

        masked = padded!=background
        masked = masked[target_height:-target_height, target_width:-target_width]
        labelvol = padded[target_height:-target_height, target_width:-target_width]

        imagevol = dst.data.copy()
        imagevol[masked] = labels_to_falsecolor(labelvol[masked])

        return ImageVolume(imagevol)


from datetime import datetime

from airflow import DAG
from airflow.contrib.sensors.file_sensor import FileSensor
from airflow.models import Variable
from airflow.operators.bash_operator import BashOperator
from airflow.operators.dagrun_operator import TriggerDagRunOperator
from airflow.operators.python_operator import PythonOperator
from airflow.operators.subdag_operator import SubDagOperator

default_args = {
    'owner': 'esoboliev',
    'start_date': datetime.utcnow(),
    'schedule_interval': '@once',
    'provide_context': True
}

dag = DAG('RemoveRunFile', default_args=default_args)

path = Variable.get('path_to_file', default_var='/tmp/test_file.txt')

file_sensor_oper = FileSensor(task_id='sensor_wait_run_file',
                              run_as_user='esoboliev',
                              filepath=path,
                              fs_conn_id='fs_default',
                              dag=dag)

trigger_dag_oper = TriggerDagRunOperator(task_id='trigger_dag',
                                         trigger_dag_id='dag_id_1',
                                         dag=dag)

def pull_function(**kwargs):
    kwargs['ti'].xcom_push(key='result_run_info',
                           value='{} ended'.format(kwargs.get('run_id')))
    print(kwargs['ti'].xcom_pull(key='result_run_info'))
    print(kwargs)

def generate_sub_dag(parent_dag_name, child_dag_name):
    inner_dag = DAG(dag_id="{}.{}".format(parent_dag_name, child_dag_name),
                    default_args=default_args)

    print_result = PythonOperator(task_id='print_results',
                                  python_callable=pull_function,
                                  default_args=default_args,
                                  dag=inner_dag)

    remove_file_oper = BashOperator(task_id='remove_run_file',
                                    run_as_user='esoboliev',
                                    bash_command='rm {}'.format(path),
                                    default_args=default_args,
                                    dag=inner_dag)

    create_timestamp = BashOperator(task_id='create_timestamp',
                                    run_as_user='esoboliev',
                                    bash_command='touch /tmp/finished_{}'.format(
                                        '{{ ts_nodash }}'),
                                    default_args=default_args,
                                    dag=inner_dag)

    print_result >> remove_file_oper >> create_timestamp

    return inner_dag

sub_dag_oper = SubDagOperator(
    task_id='process_results_sub_dag',
    subdag=generate_sub_dag(dag.dag_id,
                            'process_results_sub_dag'),
    default_args=default_args,
    dag=dag)

file_sensor_oper >> trigger_dag_oper >> sub_dag_oper