'''
将一个知识图谱中的数据导入elastic search,须提前新建index和type
'''
import requests

def bulk_insert(base_url, data):
    response = requests.put(base_url, headers={"Content-Type":"application/x-ndjson"}, data=data)
    print(response)

def begin_insert_job(index_name, type_name, json_filepath, bulk_size=1000):
    base_url = "http://localhost:9200/" + index_name + "/" + type_name + "/_bulk"
    f = open(json_filepath, 'r', encoding='utf-8')
    cnt, es_id = 0, 1
    data = ""
    for line in f.readlines():
        action_meta = '{"index": {"_id":"' + str(es_id) + '"}}'
        data = data + action_meta + "\n" + line
        es_id += 1
        cnt += 1
        if cnt >= bulk_size:
            bulk_insert(base_url, data)
            cnt, data = 0, ""
        if not (es_id % bulk_size):
            print(es_id)
    if cnt:
        bulk_insert(base_url, data)
    f.close()

if __name__ == '__main__':
    begin_insert_job("person", "_doc", "Person.json")


import discord
from discord.ext import commands
import random

@commands.command()
async def lovecalc(ctx, user1="Reggie", user2="Reggie"):

	try: user1 = ctx.message.guild.get_member(int(user1[2:-1])).name
	except: user1 = user1

	try: user2 = ctx.message.guild.get_member(int(user2[2:-1])).name
	except: user2 = user2

	i = random.randint(1,100)
	embed = discord.Embed(
		title = f"{user1} and {user2} : ",
		description = f"Your compatibility is {i}%!!!",
		colour = discord.Colour.magenta()
	)
	embed.set_image(url = "https://i.kym-cdn.com/photos/images/original/001/461/337/f96.gif")
	await ctx.send(embed=embed)

async def setup(bot):
	bot.add_command(lovecalc)


# Escreva uma função que aceite Strings e calcule a quantidade
# de letras em mauisculas e minúsculas que a String possui.

def qtdeUpperLow(x):
    listax = list(tuple(x))
    low = 0
    upper = 0
    for a in listax:
        if a.islower():
            low += 1
        elif a.isupper():
            upper += 1

    print("O Total de Letras minúsculas é de", low, "e o total de letras maiúsculas é de", upper)
    return

frase = input("Digite uma frase: ")
qtdeUpperLow(frase)


import sys, os, re
from _mathtext_data import uni2type1

#Convert string the a python type
_to_int = int
_to_float = float
_to_str =  str

def _to_list_of_ints(s):
    s = s.replace(',', ' ')
    return [_to_int(val) for val in s.split()]

def _to_list_of_floats(s):
    return [_to_float(val) for val in s.split()]
def _to_bool(s):
    if s.lower().strip() in ('false', '0', 'no'): return False
    else: return True

def _sanity_check(fh):
    """
    Check if the file at least looks like AFM.
    If not, raise :exc:`RuntimeError`.
    """

    # Remember the file position in case the caller wants to
    # do something else with the file.
    pos = fh.tell()
    try:
        line = fh.readline()
    finally:
        fh.seek(pos, 0)

    # AFM spec, Section 4: The StartFontMetrics keyword [followed by a
    # version number] must be the first line in the file, and the
    # EndFontMetrics keyword must be the last non-empty line in the
    # file. We just check the first line.
    if not line.startswith('StartFontMetrics'):
        raise RuntimeError('Not an AFM file')

def _parse_header(fh):
    """
    Reads the font metrics header (up to the char metrics) and returns
    a dictionary mapping *key* to *val*.  *val* will be converted to the
    appropriate python type as necessary; eg:

        * 'False'->False
        * '0'->0
        * '-168 -218 1000 898'-> [-168, -218, 1000, 898]

    Dictionary keys are

      StartFontMetrics, FontName, FullName, FamilyName, Weight,
      ItalicAngle, IsFixedPitch, FontBBox, UnderlinePosition,
      UnderlineThickness, Version, Notice, EncodingScheme, CapHeight,
      XHeight, Ascender, Descender, StartCharMetrics

    """
    headerConverters = {
        'StartFontMetrics': _to_float,
        'FontName': _to_str,
        'FullName': _to_str,
        'FamilyName': _to_str,
        'Weight': _to_str,
        'ItalicAngle': _to_float,
        'IsFixedPitch': _to_bool,
        'FontBBox': _to_list_of_ints,
        'UnderlinePosition': _to_int,
        'UnderlineThickness': _to_int,
        'Version': _to_str,
        'Notice': _to_str,
        'EncodingScheme': _to_str,
        'CapHeight': _to_float, # Is the second version a mistake, or
        'Capheight': _to_float, # do some AFM files contain 'Capheight'? -JKS
        'XHeight': _to_float,
        'Ascender': _to_float,
        'Descender': _to_float,
        'StdHW': _to_float,
        'StdVW': _to_float,
        'StartCharMetrics': _to_int,
        'CharacterSet': _to_str,
        'Characters': _to_int,
        }

    d = {}
    while 1:
        line = fh.readline()
        if not line: break
        line = line.rstrip()
        if line.startswith('Comment'): continue
        lst = line.split( ' ', 1 )
        #print '%-s\t%-d line :: %-s' % ( fh.name, len(lst), line )
        key = lst[0]
        if len( lst ) == 2:
            val = lst[1]
        else:
            val = ''
        #key, val = line.split(' ', 1)
        try: d[key] = headerConverters[key](val)
        except ValueError:
            print >>sys.stderr, 'Value error parsing header in AFM:', key, val
            continue
        except KeyError:
            print >>sys.stderr, 'Found an unknown keyword in AFM header (was %s)' % key
            continue
        if key=='StartCharMetrics': return d
    raise RuntimeError('Bad parse')

def _parse_char_metrics(fh):
    """
    Return a character metric dictionary.  Keys are the ASCII num of
    the character, values are a (*wx*, *name*, *bbox*) tuple, where
    *wx* is the character width, *name* is the postscript language
    name, and *bbox* is a (*llx*, *lly*, *urx*, *ury*) tuple.

    This function is incomplete per the standard, but thus far parses
    all the sample afm files tried.
    """

    ascii_d = {}
    name_d  = {}
    while 1:
        line = fh.readline()
        if not line: break
        line = line.rstrip()
        if line.startswith('EndCharMetrics'): return ascii_d, name_d
        vals = line.split(';')[:4]
        if len(vals) !=4 : raise RuntimeError('Bad char metrics line: %s' % line)
        num = _to_int(vals[0].split()[1])
        wx = _to_float(vals[1].split()[1])
        name = vals[2].split()[1]
        bbox = _to_list_of_ints(vals[3][2:])
        # Workaround: If the character name is 'Euro', give it the corresponding
        # character code, according to WinAnsiEncoding (see PDF Reference).
        if name == 'Euro':
            num = 128
        if num != -1:
            ascii_d[num] = (wx, name, bbox)
        name_d[name] = (wx, bbox)
    raise RuntimeError('Bad parse')

def _parse_kern_pairs(fh):
    """
    Return a kern pairs dictionary; keys are (*char1*, *char2*) tuples and
    values are the kern pair value.  For example, a kern pairs line like
    ``KPX A y -50``

    will be represented as::

      d[ ('A', 'y') ] = -50

    """

    line = fh.readline()
    if not line.startswith('StartKernPairs'):
        raise RuntimeError('Bad start of kern pairs data: %s'%line)

    d = {}
    while 1:
        line = fh.readline()
        if not line: break
        line = line.rstrip()
        if len(line)==0: continue
        if line.startswith('EndKernPairs'):
            fh.readline()  # EndKernData
            return d
        vals = line.split()
        if len(vals)!=4 or vals[0]!='KPX':
            raise RuntimeError('Bad kern pairs line: %s'%line)
        c1, c2, val = vals[1], vals[2], _to_float(vals[3])
        d[(c1,c2)] = val
    raise RuntimeError('Bad kern pairs parse')

def _parse_composites(fh):
    """
    Return a composites dictionary.  Keys are the names of the
    composites.  Values are a num parts list of composite information,
    with each element being a (*name*, *dx*, *dy*) tuple.  Thus a
    composites line reading:

      CC Aacute 2 ; PCC A 0 0 ; PCC acute 160 170 ;

    will be represented as::

      d['Aacute'] = [ ('A', 0, 0), ('acute', 160, 170) ]

    """
    d = {}
    while 1:
        line = fh.readline()
        if not line: break
        line = line.rstrip()
        if len(line)==0: continue
        if line.startswith('EndComposites'):
            return d
        vals = line.split(';')
        cc = vals[0].split()
        name, numParts = cc[1], _to_int(cc[2])
        pccParts = []
        for s in vals[1:-1]:
            pcc = s.split()
            name, dx, dy = pcc[1], _to_float(pcc[2]), _to_float(pcc[3])
            pccParts.append( (name, dx, dy) )
        d[name] = pccParts

    raise RuntimeError('Bad composites parse')

def _parse_optional(fh):
    """
    Parse the optional fields for kern pair data and composites

    return value is a (*kernDict*, *compositeDict*) which are the
    return values from :func:`_parse_kern_pairs`, and
    :func:`_parse_composites` if the data exists, or empty dicts
    otherwise
    """
    optional = {
        'StartKernData' : _parse_kern_pairs,
        'StartComposites' :  _parse_composites,
        }

    d = {'StartKernData':{}, 'StartComposites':{}}
    while 1:
        line = fh.readline()
        if not line: break
        line = line.rstrip()
        if len(line)==0: continue
        key = line.split()[0]

        if key in optional: d[key] = optional[key](fh)

    l = ( d['StartKernData'], d['StartComposites'] )
    return l

def parse_afm(fh):
    """
    Parse the Adobe Font Metics file in file handle *fh*. Return value
    is a (*dhead*, *dcmetrics*, *dkernpairs*, *dcomposite*) tuple where
    *dhead* is a :func:`_parse_header` dict, *dcmetrics* is a
    :func:`_parse_composites` dict, *dkernpairs* is a
    :func:`_parse_kern_pairs` dict (possibly {}), and *dcomposite* is a
    :func:`_parse_composites` dict (possibly {})
    """
    _sanity_check(fh)
    dhead =  _parse_header(fh)
    dcmetrics_ascii, dcmetrics_name = _parse_char_metrics(fh)
    doptional = _parse_optional(fh)
    return dhead, dcmetrics_ascii, dcmetrics_name, doptional[0], doptional[1]

class AFM:

    def __init__(self, fh):
        """
        Parse the AFM file in file object *fh*
        """
        (dhead, dcmetrics_ascii, dcmetrics_name, dkernpairs, dcomposite) = \
            parse_afm(fh)
        self._header = dhead
        self._kern = dkernpairs
        self._metrics = dcmetrics_ascii
        self._metrics_by_name = dcmetrics_name
        self._composite = dcomposite

    def get_bbox_char(self, c, isord=False):
        if not isord: c=ord(c)
        wx, name, bbox = self._metrics[c]
        return bbox

    def string_width_height(self, s):
        """
        Return the string width (including kerning) and string height
        as a (*w*, *h*) tuple.
        """
        if not len(s): return 0,0
        totalw = 0
        namelast = None
        miny = 1e9
        maxy = 0
        for c in s:
            if c == '\n': continue
            wx, name, bbox = self._metrics[ord(c)]
            l,b,w,h = bbox

            # find the width with kerning
            try: kp = self._kern[ (namelast, name) ]
            except KeyError: kp = 0
            totalw += wx + kp

            # find the max y
            thismax = b+h
            if thismax>maxy: maxy = thismax

            # find the min y
            thismin = b
            if thismin<miny: miny = thismin

        return totalw, maxy-miny

    def get_str_bbox_and_descent(self, s):
        """
        Return the string bounding box
        """
        if not len(s): return 0,0,0,0
        totalw = 0
        namelast = None
        miny = 1e9
        maxy = 0
        left = 0
        if not isinstance(s, unicode):
            s = s.decode()
        for c in s:
            if c == '\n': continue
            name = uni2type1.get(ord(c), 'question')
            try:
                wx, bbox = self._metrics_by_name[name]
            except KeyError:
                name = 'question'
                wx, bbox = self._metrics_by_name[name]
            l,b,w,h = bbox
            if l<left: left = l
            # find the width with kerning
            try: kp = self._kern[ (namelast, name) ]
            except KeyError: kp = 0
            totalw += wx + kp

            # find the max y
            thismax = b+h
            if thismax>maxy: maxy = thismax

            # find the min y
            thismin = b
            if thismin<miny: miny = thismin

        return left, miny, totalw, maxy-miny, -miny

    def get_str_bbox(self, s):
        """
        Return the string bounding box
        """
        return self.get_str_bbox_and_descent(s)[:4]

    def get_name_char(self, c, isord=False):
        """
        Get the name of the character, ie, ';' is 'semicolon'
        """
        if not isord: c=ord(c)
        wx, name, bbox = self._metrics[c]
        return name

    def get_width_char(self, c, isord=False):
        """
        Get the width of the character from the character metric WX
        field
        """
        if not isord: c=ord(c)
        wx, name, bbox = self._metrics[c]
        return wx

    def get_width_from_char_name(self, name):
        """
        Get the width of the character from a type1 character name
        """
        wx, bbox = self._metrics_by_name[name]
        return wx

    def get_height_char(self, c, isord=False):
        """
        Get the height of character *c* from the bounding box.  This
        is the ink height (space is 0)
        """
        if not isord: c=ord(c)
        wx, name, bbox = self._metrics[c]
        return bbox[-1]

    def get_kern_dist(self, c1, c2):
        """
        Return the kerning pair distance (possibly 0) for chars *c1*
        and *c2*
        """
        name1, name2 = self.get_name_char(c1), self.get_name_char(c2)
        return self.get_kern_dist_from_name(name1, name2)

    def get_kern_dist_from_name(self, name1, name2):
        """
        Return the kerning pair distance (possibly 0) for chars
        *name1* and *name2*
        """
        try: return self._kern[ (name1, name2) ]
        except: return 0

    def get_fontname(self):
        "Return the font name, eg, 'Times-Roman'"
        return self._header['FontName']

    def get_fullname(self):
        "Return the font full name, eg, 'Times-Roman'"
        name = self._header.get('FullName')
        if name is None: # use FontName as a substitute
            name = self._header['FontName']
        return name

    def get_familyname(self):
        "Return the font family name, eg, 'Times'"
        name = self._header.get('FamilyName')
        if name is not None:
            return name

        # FamilyName not specified so we'll make a guess
        name = self.get_fullname()
        extras = r'(?i)([ -](regular|plain|italic|oblique|bold|semibold|light|ultralight|extra|condensed))+$'
        return re.sub(extras, '', name)

    def get_weight(self):
        "Return the font weight, eg, 'Bold' or 'Roman'"
        return self._header['Weight']

    def get_angle(self):
        "Return the fontangle as float"
        return self._header['ItalicAngle']

    def get_capheight(self):
        "Return the cap height as float"
        return self._header['CapHeight']

    def get_xheight(self):
        "Return the xheight as float"
        return self._header['XHeight']

    def get_underline_thickness(self):
        "Return the underline thickness as float"
        return self._header['UnderlineThickness']

    def get_horizontal_stem_width(self):
        """
        Return the standard horizontal stem width as float, or *None* if
        not specified in AFM file.
        """
        return self._header.get('StdHW', None)

    def get_vertical_stem_width(self):
        """
        Return the standard vertical stem width as float, or *None* if
        not specified in AFM file.
        """
        return self._header.get('StdVW', None)

if __name__=='__main__':
    #pathname = '/usr/local/lib/R/afm/'
    pathname = '/usr/local/share/fonts/afms/adobe'

    for fname in os.listdir(pathname):
        fh = file(os.path.join(pathname,fname))
        afm = AFM(fh)
        w,h =  afm.string_width_height('John Hunter is the Man!')


#um professor quer escolher um de seus 4 alunos para apagar o quadro
#desemvolva um programa que leia o nome dos 4 e sortei um

from random import choice
print('--'*30)
n1 = input('Digite o Primeiro Nome: ')
n2 = input('Digite o Segundo Nome: ')
n3 = input('Digite o Terceiro Nome: ')
n4 = input('Digite o Quarto Nome: ')

nomes = [n1, n2, n3, n4]
es = choice(nomes)
print('--'*30)
print(f'O Aluno(a) Escolhido Foi O(a) {es}')
print('--'*30)


from datetime import datetime
import uuid
from enum import Enum
import boto3
import os
from pynamodb.attributes import ListAttribute, UnicodeAttribute, UTCDateTimeAttribute
from pynamodb.models import Model
from log_cfg import logger

BUCKET = os.environ['S3_BUCKET']
KEY_BASE = os.environ['S3_KEY_BASE']

class State(Enum):
    """
    Manage asset states in dynamo with a string field
    Could have used an int as well, or used a custom serializer which is a bit cleaner.
    """
    CREATED = 1
    UPLOADED = 2
    PROCESSED = 3
    PROCESSED_WITH_CALLBACK = 4

class BlobModel(Model):
    class Meta:
        table_name = os.environ['DYNAMODB_TABLE']
        if 'ENV' in os.environ:
            host = 'http://localhost:8000'
        else:
            region = os.environ['REGION']
            host = os.environ['DYNAMODB_HOST']
            # 'https://dynamodb.us-east-1.amazonaws.com'

    blob_id = UnicodeAttribute(hash_key=True)
    callback_url = UnicodeAttribute(null=True)
    labels = ListAttribute(null=False, default=[])
    state = UnicodeAttribute(null=False, default=State.CREATED.name)
    created_time = UTCDateTimeAttribute(null=False, default=datetime.now().astimezone())
    updated_time = UTCDateTimeAttribute(null=False, default=datetime.now().astimezone())
    rekognition_error = UnicodeAttribute(null=True)

    def __str__(self):
        return 'blob_id:{}, callback_url:{}, labels'.format(self.blob_id, self.callback_url, self.labels)

    def get_key(self):
        return u'{}/{}'.format(KEY_BASE, self.blob_id)

    def save(self, conditional_operator=None, **expected_values):
        try:
            self.updated_time = datetime.now().astimezone()
            logger.debug('saving: {}'.format(self))
            super(BlobModel, self).save()
        except Exception as e:
            logger.error('save {} failed: {}'.format(self.blob_id, e), exc_info=True)
            raise e

    def __iter__(self):
        for name, attr in self._get_attributes().items():
            yield name, attr.serialize(getattr(self, name))

    def get_upload_url(self, ttl=60):
        """
        :param ttl: url duration in seconds
        :return: a temporary presigned PUT url
        """
        s3 = boto3.client('s3')
        put_url = s3.generate_presigned_url(
            'put_object',
            Params={
                'Bucket': BUCKET,
                'Key': self.get_key()
            },
            ExpiresIn=ttl,
            HttpMethod='PUT'
        )
        logger.debug('upload URL: {}'.format(put_url))
        return put_url

    def generate_new_item(self, callback_url=None):
        self.blob_id = uuid.uuid4().__str__()
        if callback_url:
            self.callback_url = callback_url
        self.save()

    def update_state_to_processed_and_add_labels(self, labels):
        """
        Mark asset as having been processed after rekognition has run and change labels
        """
        if self.state != State.UPLOADED.name:
            raise AssertionError('State: \"{}\" must be {}'.format(self.state, State.UPLOADED.name))
        self.state = State.PROCESSED.name
        logger.debug('mark asset processed: {}'.format(self.blob_id))
        self.labels = labels
        self.save()

    def mark_uploaded(self):
        """
        Mark assest as been having uploaded by client
        """
        self.state = State.UPLOADED.name
        logger.debug('mark asset uploaded: {}'.format(self.blob_id))
        self.save()

    def mark_processed_with_callback(self):
        """
        Mark asset as having been received via the s3 objectCreated:Put event
        """
        if self.state != State.PROCESSED.name:
            raise AssertionError('State: \"{}\" must be one of {}'.format(self.state, State.PROCESSED.name))
        self.state = State.PROCESSED_WITH_CALLBACK.name
        logger.debug('mark asset processed and callback complete: {}'.format(self.blob_id))
        self.save()

    def set_rekognition_error_and_mark_processed(self, error):
        self.rekognition_error = error
        logger.debug('rekognition returned with errors: {}'.format(self.rekognition_error))
        self.state = State.PROCESSED.name
        self.save()

    def set_labels(self, labels):
        self.labels = labels
        logger.debug('saved {} labels'.format(len(self.labels)))
        self.save()


"""
Problem 10

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.

Takes a while for large numbers
"""
below = 10
sum = 0
for x in range(2, below+1):
    incounter = 0
    for y in range (2, x+1):
        if (x%y == 0):
            incounter += 1

    if (incounter == 1):
        sum += x

print (sum)


#!venv/bin/python

import cv2
import numpy as np
import os
import argparse
from keras.applications.mobilenet_v2 import preprocess_input as preprocess_mobilenet
import tensorflow as tf
# Constants.
INPUT_WIDTH = 416
INPUT_HEIGHT = 416
SCORE_THRESHOLD = 0.5  # cls score
NMS_THRESHOLD = 0.45
CONFIDENCE_THRESHOLD = 0.45  # obj confidence

# Text parameters.
FONT_FACE = cv2.FONT_HERSHEY_SIMPLEX
FONT_SCALE = 0.7
THICKNESS = 1

MODEL_JACKET = tf.keras.models.load_model("model_finetune0.88.h5")
MODEL_PANTS = tf.keras.models.load_model("mobilenetV2_full_trainable_pants_0.7638376355171204.h5")

CLASSES_JACKET = {0: "Invisible Jacket", 1: "With Jacket", 2: "No Jacket"}
CLASSES_PANTS = {0: "Invisible Pants", 1: "With Pants", 2: "No Pants"}

# Colors
BLACK = (0, 0, 0)
BLUE = (255, 178, 50)
YELLOW = (0, 255, 255)
RED = (0, 0, 255)
GEEN = (0, 255, 0)

def draw_label(input_image, label, left, top, color):
    """Draw text onto image at location."""

    # Get text size.
    dy = 25
    # Use text size to create a BLACK rectangle.
    for line, text in enumerate(label.split("\n")):
        text_size = cv2.getTextSize(text, FONT_FACE, FONT_SCALE, THICKNESS)
        dim, baseline = text_size[0], text_size[1]
        cv2.rectangle(input_image, (left,top - dim[1] - 2*line*dy ), (left + dim[0], top - 2*dim[1] - baseline), color, cv2.FILLED)
    # Display text inside the rectangle.
        cv2.putText(input_image, text, (left, top - dim[1] - line*dy), FONT_FACE, FONT_SCALE, BLACK, THICKNESS, cv2.LINE_AA)

def pre_process(input_image, net):
    # Create a 4D blob from a frame.
    blob = cv2.dnn.blobFromImage(input_image, 1 / 255, (INPUT_WIDTH, INPUT_HEIGHT), [0, 0, 0], 1, crop=False)

    # Sets the input to the network.
    net.setInput(blob)

    # Runs the forward pass to get output of the output layers.
    output_layers = net.getUnconnectedOutLayersNames()
    outputs = net.forward(output_layers)
    # print(outputs[0].shape)

    return outputs

def get_color(jck, pnts):
    if jck == 2 and pnts == 2:
        return RED
    elif jck == 1 and pnts == 1:
        return GEEN
    else:
        return YELLOW

def getGuassianValue(W, H):
    x, y = np.meshgrid(np.linspace(-1, 1, W), np.linspace(-1, 1, H))
    d = np.sqrt(x * x + y * y)
    sigma, mu = 1.0, 0.0
    g = np.exp(-((d - mu) ** 2 / (2.0 * sigma ** 2)))
    return g.T

def post_process(input_image, outputs):
    # Lists to hold respective values while unwrapping.
    class_ids = []
    confidences = []
    boxes = []
    heatmp = np.zeros(input_image.shape[:2], dtype=np.float64)
    # Rows.
    rows = outputs[0].shape[1]

    image_height, image_width = input_image.shape[:2]

    # Resizing factor.
    x_factor = image_width / INPUT_WIDTH
    y_factor = image_height / INPUT_HEIGHT

    # Iterate through 25200 detections.
    for r in range(rows):
        row = outputs[0][0][r]
        confidence = row[4]

        # Discard bad detections and continue.
        if confidence >= CONFIDENCE_THRESHOLD:
            classes_scores = row[5:]

            # Get the index of max class score.
            class_id = np.argmax(classes_scores)

            #  Continue if the class score is above threshold.
            if (classes_scores[class_id] > SCORE_THRESHOLD):
                confidences.append(confidence)
                class_ids.append(class_id)

                cx, cy, w, h = row[0], row[1], row[2], row[3]

                left = int((cx - w / 2) * x_factor)
                top = int((cy - h / 2) * y_factor)
                width = int(w * x_factor)
                height = int(h * y_factor)

                box = np.array([left, top, width, height])
                boxes.append(box)

    # Perform non maximum suppression to eliminate redundant overlapping boxes with
    # lower confidences.
    indices = cv2.dnn.NMSBoxes(boxes, confidences, CONFIDENCE_THRESHOLD, NMS_THRESHOLD)
    for i in indices:
        box = boxes[i]
        left = box[0]
        top = box[1]
        width = box[2]
        height = box[3]
        img = input_image[top:top + height, left:left + width]
        w, h = np.shape(heatmp[top:top + height, left:left + width])
        heatmp[top:top + height, left:left + width] += getGuassianValue(w, h)
        img = tf.image.resize(img, (100, 100))
        img = preprocess_mobilenet(img)
        pred_jacket = MODEL_JACKET.predict(img[None, ...])[0]
        pred_pants = MODEL_PANTS.predict(img[None, ...])[0]
        arg_jacket = np.argmax(pred_jacket)
        arg_pants = np.argmax(pred_pants)
        color = get_color(arg_jacket, arg_pants)
        cv2.rectangle(input_image, (left, top), (left + width, top + height), color, 3 * THICKNESS)
        label = "{}:{:.2f}\n{}:{:.2f}".format(CLASSES_JACKET[arg_jacket],
                                              pred_jacket[arg_jacket],
                                              CLASSES_PANTS[arg_pants],
                                              pred_pants[arg_pants])
        draw_label(input_image, label, left, top, color)

    return input_image, heatmp

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--video', default=None, help="Путь к вашему видео")
    parser.add_argument('--img', default=None, help="Путь к вашему изображению")
    parser.add_argument('--speed', default=1, help="Скорость обработки видео")
    args = parser.parse_args()

    video_path, img_path, speed = args.video, args.img, int(args.speed)

    # Load class names.
    model_path = "modelV2.onnx"
    # Give the weight files to the model and load the network using them.
    net = cv2.dnn.readNet(model_path)
    window_name = os.path.splitext(os.path.basename(model_path))[0]
    classes = ["person"]
    # newVideo = cv2.VideoWriter('detectVideo.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 30, (1920,1080))
    # newVideoHeatMap = cv2.VideoWriter('HeatMap.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 30, (1920,1080))
    # Load image.
    if video_path != None:
        rec = cv2.VideoCapture(video_path)

        heatmp = []
        iter = 0
        while rec.isOpened:
            ret, frame = rec.read()
            if iter % speed == 0:
                detections = pre_process(frame.copy(), net)
                img, htmp = post_process(frame.copy(), detections)
                heatmp.append(htmp)
                heatmapshow = None
                heatmapshow = cv2.normalize(np.sum(heatmp, axis=0), heatmapshow, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
                heatmapshow = cv2.applyColorMap(heatmapshow, cv2.COLORMAP_JET)
                super_imposed_img = cv2.addWeighted(heatmapshow, 0.3, frame, 0.5, 0)
            # newVideo.write(img)
            # newVideoHeatMap.write(super_imposed_img)
            cv2.imshow("HeatMap", super_imposed_img)
            cv2.waitKey(1)
            cv2.imshow(window_name, img)
            cv2.waitKey(1)
        rec.release()
        # newVideo.release()
        # newVideoHeatMap.release()
        cv2.destroyAllWindows()

    elif img_path != None:
        frame = cv2.imread(img_path)
        detections = pre_process(frame.copy(), net)
        img, htmp = post_process(frame.copy(), detections)
        heatmapshow = None
        heatmapshow = cv2.normalize(htmp, heatmapshow, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX,
                                    dtype=cv2.CV_8U)
        heatmapshow = cv2.applyColorMap(heatmapshow, cv2.COLORMAP_JET)
        super_imposed_img = cv2.addWeighted(heatmapshow, 0.3, frame, 0.5, 0)
        cv2.imshow("HeatMap", super_imposed_img)
        cv2.imshow(window_name, img)
        cv2.waitKey(0)


## below code is expected to start 2 processes on different gpu's and with a parameter server placed in the cpu.
###### The code trains the neural network with 2 hidden layers of size 4096 and ann output layer of 256

import multiprocessing
import time
import tensorflow as tf
from tensorflow.contrib.training import HParams
import os
import psutil
import numpy as np
from tensorflow.python.client import device_lib

cluster_spec ={"ps": ["localhost:2223"
                      ],
    "worker": [
        "localhost:2224",
        "localhost:2225"]}

cluster = tf.train.ClusterSpec(cluster_spec)
#loss = tf.random_normal([1],dtype=tf.float32)
#print(cluster.task_address())
#X = tf.constant(np.random.random_sample(size=(128,256)), dtype=tf.float32)
#labls=tf.constant([1,0,0,1,0,1,0,1,0,1],shape=[10,1],dtype=tf.float32)

def dumb_input_fn():

    x = tf.random_normal([128,256], dtype=tf.float32)
    y = tf.random_normal([128,256], dtype=tf.float32)

    return [x,y]

output_dir = 'tfprojects/output_dir_debug'
#tf.logging.set_verbosity(tf.logging.INFO)  # enables training error print out during training
def model_fn(features,labels):
    # This is is reeeeally slow way to square numbers.
    outputs = tf.layers.dense(
                    inputs = features,
                    units = 4096)
    outputs = tf.layers.dense(
                    inputs = outputs,
                    units = 4096)
    outputs = tf.layers.dense(
                    inputs = outputs,
                    units = 256)

    loss = tf.losses.mean_squared_error(outputs, labels)

    train_op = tf.contrib.layers.optimize_loss(
              loss, None, optimizer='Adam',
                        learning_rate = .0001)

    predictions = {"predictions":tf.identity(outputs,name = 'predictions')}
    return predictions, loss, train_op

def worker(device):
    params = HParams(cluster=cluster,
                     job_name = device[0],
                     task_index = device[1])

    if device[0]=='worker':
        # allow each worker to see only 1 of the 4 GPUS
        os.environ["CUDA_VISIBLE_DEVICES"]=str(params.task_index)

    else:
        # hide all 4 GPUS from ps
        os.environ["CUDA_VISIBLE_DEVICES"]=''

    Workrs(output_dir, params)

class _LoggerHook():
    ## create before run and after run functions that could be used to call certain parameters
    def begin(self):
        self._time=time.time()

    def before_run(self, run_context):
        self._time = time.time()
        return tf.train.SessionRunArgs(loss)  # Asks for loss value.

    def after_run(self, run_context, run_values):
        self._time = time.time()
        duration = time.time() - self._start_time
        loss_value = run_values.results

        format_str = ('loss = %.2f')
        print (format_str % (loss_value))

## The Workrs processes is triggered from the main function. T
def Workrs(output_dir,d={}):
    cluster = d.cluster
    job_name = d.job_name
    task_index = d.task_index
    # tf.train.Server creates a server cluster with the local host address as mentioned in cluster_Spec in tensorflow
    server= tf.train.Server(d.cluster,job_name = d.job_name,task_index=d.task_index)
    print("Task IndeX:",d.task_index)

    if job_name == "ps":
        ##try and wait for all the wokers to finish their tasks.
        ## Tensorflow does not stop the server process on PS to stop. This needs to be manually done by the code
        ## Have to setup a Tensorflow queue which is written to by Worker process once they finish training and read by PS to stop its session from running.
        server.join()
    elif job_name == "worker":
        #tf.train.replica_device_setter is used to automatically assign available devices for the specific task index
        with tf.device(tf.train.replica_device_setter(
                worker_device="/job:worker/replica:0/task:%d" % task_index,
                cluster=cluster)):

            X, labls = dumb_input_fn()
            _,_,train_op=model_fn(X,labls,None,d)

        global_step = tf.contrib.framework.get_or_create_global_step()
        step = 0
        start_time = time.time()
        # These are hooks that are to be used to stop training after a certain global step value has been reached.
        hooks = tf.train.StopAtStepHook(last_step=10)
        # MonitoredTrainingSession sets up the tensorflow threads and queues.
        with tf.train.MonitoredTrainingSession(master=server.target,config=tf.ConfigProto(log_device_placement=True),
                                                   is_chief=(d.task_index == 0),
                                                   checkpoint_dir=output_dir,
                                                   hooks=[hooks]) as mon_sess:

            #ckpt = tf.train.get_checkpoint_state(output_dir)

            while not mon_sess.should_stop():

                _ = mon_sess.run(train_op)
                if step % 5 == 0:
                    print("Step:", step, 10 / (time.time() - start_time), 'steps/sec')
                    print("Proc:",)
                    start_time = time.time()

                step += 1

#def get_available_gpus():
#    local_device_protos = device_lib.list_local_devices()
#    return [x.name for x in local_device_protos if x.device_type == 'GPU']
## Test the multiprocessing on a single cpu vs 4 on the local machine
#print("There are %d CPUs on this machine" % multiprocessing.cpu_count())
#print("There are  CPUs on this machine:" , get_available_gpus())
#pool = multiprocessing.Pool(processes=4)
#start_time = time.time()
#results = [pool.apply_async(cube, args=(x,)) for x in range(17,25)]
#out = [p.get() for p in results]
#print(out)
#start_time = time.time()
#pool.close()
#pool.join()

if __name__ == '__main__':

    logger = multiprocessing.log_to_stderr()
    result = multiprocessing.Queue()
    processes = []
    devices = [['ps', 0],
               ['worker', 0],
               ['worker', 1],
               ]

    for i in (devices):
        start_time = time.time()
        proc = multiprocessing.Process(target=worker,args=(i,))
        processes.append(proc)
        proc.start()

    for p in processes:
        p.join()

    print("time taken = %d" % (start_time - time.time()))

    results = [result.get() for p in processes]
#process_names = [proc.get_cpu_times() for proc in psutil.Process()]


import os
import time
import warnings

import cv2 as cv
import numpy as np
from numba.core.errors import (NumbaDeprecationWarning,
                               NumbaPendingDeprecationWarning, NumbaWarning)
from tqdm import tqdm

from char_carve import *
from DOG import *
from log_images import Logger
from medial_seam import *
from persistence.use_persistence import get_persistence
from project import *
from seam_carve import *
from segment import *
from utils import easify_persistence, ceil_profile_with_threshold, clearout_0_shapes
from word_carve import *

warnings.simplefilter('ignore', category=NumbaDeprecationWarning)
warnings.simplefilter('ignore', category=NumbaPendingDeprecationWarning)
warnings.simplefilter('ignore', category=NumbaWarning)

# @jit
def sukalpameth(img: np.ndarray, write_path: str, log_intermediates=True):

    log = Logger(write_path)

    # DOG image processing
    # gray scale image
    gray = grayscale(img)

    # get gaussian smoothed images
    gaus5 = get_gaus5(gray)
    gaus3 = get_gaus3(gray)

    # edge image by subtracting images from each other
    dog = get_dog(gaus3, gaus5)

    # histogram equalization
    dog_eq = hist_equalize(dog)

    # smooth histogram eq image
    dog_eq_gaus = get_smooth_equalized_dog(dog_eq)

    # write dog images
    if log_intermediates:
        log.create_subdir('dog')
        log.set_subdir('dog')

        log.write_img(gray, 'gray')
        log.write_img(gaus5, 'gaus5')
        log.write_img(gaus3, 'gaus3')
        log.write_img(dog, 'dog')
        log.write_img(dog_eq, 'dog_hist_eq')
        log.write_img(dog_eq_gaus, 'dog_hist_eq_smooth')

    # Medial line computations
    strips = 4
    strip_width = get_strip_width(dog_eq_gaus, strips)

    # get strips
    strips = get_strips(dog_eq_gaus, strips)

    # get horizontal profiles per strip
    strip_hpps = get_strips_hpp(strips)

    # smooth projection profile with 1-dim gaussian
    smooth_strip_hpps = one_dim_gausblur(strip_hpps, 3, 3)

    # collect extremas from projection profile in strip (both min and max), with over persistence
    persistences = []
    persistence_level = 10

    for i in range(len(smooth_strip_hpps)):
        persistence = get_persistence(smooth_strip_hpps[i], persistence_level
                                      )
        persistence = easify_persistence(persistence)

        persistences.append(persistence)

    persistences_maximas = []

    # collect maximas from extremas in strip
    for per in persistences:
        persistences_maximas.append(get_maximas(per))

    # get the threshold which helps in binding maximas in different strips
    threshold = get_appropriate_thresh(smooth_strip_hpps)
    print('text line width threshold', threshold, '\n')

    # print(persistences_maximas)

    current_peaks = persistences_maximas
    prev_peaks = None

    """
        Algorithm for determining peaks.

        Check if the current maximas changed from previous iteration.
        If not; run another iteration.

        Check if there is a histogram row which has a higher value then the current value within the threshold of rows.
        If yes; set this as a new maxima and remove previous.
        If no; continue.
        Do this for all rows.

        This removes redundant maximas and converges the greatest peaks of the histogram.
    """
    counter = 0
    while has_peak_change(current_peaks, prev_peaks) and counter < 20:
        # print(counter)
        counter += 1
        prev_peaks = current_peaks
        strip_peaks = []

        for i in range(len(persistences)):
            strip_peaks.append(find_peaks(
                current_peaks[i], smooth_strip_hpps[i], threshold))

        current_peaks = strip_peaks

    persistences_peaks = current_peaks

    # print(counter)
    # print(persistences_peaks)

    # connect peaks in strips
    medial_lines = bind_peaks(current_peaks, threshold)
    # print(medial_lines)

    # Seam carve compuation
    # get sub-images between seam lines
    betweens = []
    betweens_for_proj = []
    row_intervals = []

    for s in range(1, len(medial_lines)):
        between, interval = between_medial_seams(
            dog_eq_gaus, medial_lines[s-1], medial_lines[s])

        between_for_proj, _ = between_medial_seams(
            gray, medial_lines[s-1], medial_lines[s])

        betweens.append(between)
        betweens_for_proj.append(between_for_proj)
        row_intervals.append(interval)

    # Seam carving
    # find carve in each sub-image, and proj to image
    carve_imgs = []
    for i in range(len(betweens)):
        between = cv.rotate(betweens[i], cv.ROTATE_90_CLOCKWISE)
        between_for_proj = cv.rotate(
            betweens_for_proj[i], cv.ROTATE_90_CLOCKWISE)

        # get mask which marks pixel path as least energy
        mask = seam_carve(between)

        # projections are used to for concatenation and to get segmented lines
        carve_img = project_carves(between_for_proj, mask)
        carve_img = cv.rotate(carve_img, cv.ROTATE_90_COUNTERCLOCKWISE)
        carve_imgs.append(carve_img)

    # concatenates betweens with projected carve to the original image with seam carves
    total_carved_img = get_total_carved(img, carve_imgs, row_intervals)

    # get line segments
    lineseg_lst = line_segment(total_carved_img)

    # Projections
    if log_intermediates:
        strip_hpp_projected_on_originalimg = project_hpp_strips_to_img(
            img, strip_hpps, strip_width)
        smooth_strip_hpp_projected_on_originalimg = project_hpp_strips_to_img(
            img, smooth_strip_hpps, strip_width)

        smooth_strip_hpp_maxi_projected_on_originalimg = project_hpp_strips_to_img(
            img, smooth_strip_hpps, strip_width, persistences_maximas)

        smooth_strip_hpp_peaks_projected_on_originalimg = project_hpp_strips_to_img(
            img, smooth_strip_hpps, strip_width, persistences_peaks)

        projected_connected_peaks = project_connected_peaks(img,
                                                            medial_lines, strip_width)

    # write line segment related images images
    if log_intermediates:
        log.create_subdir('medial_line')
        log.set_subdir('medial_line')

        log.write_img(strip_hpp_projected_on_originalimg, 'strip_hpp')
        log.write_img(smooth_strip_hpp_projected_on_originalimg,
                    'smooth_strip_hpp')
        log.write_img(smooth_strip_hpp_maxi_projected_on_originalimg, 'hpp_maxi')
        log.write_img(smooth_strip_hpp_peaks_projected_on_originalimg, 'hpp_peaks')

        log.write_img(projected_connected_peaks, 'connected_peaks')

        log.create_subdir('seam_carves/betweens')
        log.set_subdir('seam_carves/betweens')

        for i in range(len(betweens_for_proj)):
            log.write_img(betweens_for_proj[i], f'between{i}')

        log.create_subdir('seam_carves/carves')
        log.set_subdir('seam_carves/carves')

        for i in range(len(carve_imgs)):
            log.write_img(carve_imgs[i], f'carve{i}')

        log.write_img(total_carved_img, 'total_carved_img')

    log.create_subdir('line_segments')
    log.set_subdir('line_segments')

    for i in range(len(lineseg_lst)):
        log.write_img(lineseg_lst[i], f'line{i}')

    # Word segmentation
    """
        Word segmentation could definitively use a revisit.
        Use DOG before thresholding.
        Revisit the filtering method.
    """

    per_line_word_segs = []
    per_line_whitespace_segs = [] # NOTE: can maybe be remove later

    word_segments_per_line = []

    # carve words from lines
    for i in range(len(lineseg_lst)):
        word_segs, whitespace_segs = carve_word_whitespace_segs(lineseg_lst[i])

        per_line_word_segs.append(word_segs)
        per_line_whitespace_segs.append(whitespace_segs)

        word_seg_for_line = carve_word_img_with_seg(lineseg_lst[i], word_segs)
        word_segments_per_line.append(word_seg_for_line)

    # crop with horizontal fill and connected component analysis
    word_segments_cropped_per_line = []
    for line in range(len(word_segments_per_line)):
        word_segments_cropped_per_line.append([])

        for word in range(len(word_segments_per_line[line])):
            # trouble in this function
            cropped, _, __ = crop_with_cca(word_segments_per_line[line][word])

            word_segments_cropped_per_line[line].append(cropped)

            if word_segments_cropped_per_line[line][word].shape == (0, 6, 3):
                print('present')
                print('made from:', word_segments_per_line[line][word].shape)
                print('line', line, 'word', word)
                cv.imwrite('test.png', word_segments_per_line[line][word])

                quit()

    word_segments_cropped_per_line = clearout_0_shapes(word_segments_cropped_per_line)

    # busy zone generation
    hori_multiplier = 10

    # most_occ_gap_hori = find_most_occuring_gap_horizontal(img) * hori_multiplier

    busy_zones = []
    hori_filled_lst = []
    hori_filled_hpp_lst = []

    for line in range(len(per_line_word_segs)):
        busy_zones.append([])
        hori_filled_lst.append([])
        hori_filled_hpp_lst.append([])

        for word in range(len(per_line_word_segs[line])):
            most_occ_gap_hori = find_most_occuring_text_width_hori(
                word_segments_cropped_per_line[line][word]) * hori_multiplier

            # horizontal fill
            hori_filled = fill_this_gapsize_hori(
                word_segments_cropped_per_line[line][word], most_occ_gap_hori)
            hori_filled_lst[line].append(hori_filled)

            hori_filled_hpp = get_horizontal_projection_profile(hori_filled)
            hori_filled_hpp_lst[line].append(hori_filled_hpp)

            busy_zone, high, low = carve_busy_zone_hp(
                word_segments_cropped_per_line[line][word], hori_filled_hpp)

            busy_zones[line].append(busy_zone)

    # Projections
    if log_intermediates:
        # line image projected with the splitting points
        segs_projected_on_line = []
        for i in range(len(lineseg_lst)):
            projected_seg = project_segments_on_line(lineseg_lst[i], per_line_word_segs[i])
            segs_projected_on_line.append(projected_seg)

        # hori filled with hpp projection
        hori_filled_hpp_on_hori_filled_lst = []
        for line in range(len(hori_filled_lst)):
            hori_filled_hpp_on_hori_filled_lst.append([])
            for word in range(len(hori_filled_lst[line])):
                hori_filled_hpp_on_hori_filled_lst[line].append(
                    project_horizontal_projection_profile(hori_filled_lst[line][word], hori_filled_hpp_lst[line][word]))

        # side by side hpp image for busy and cropped
        side_by_side_busy_hpp_lst = []
        side_by_side_crop_hpp_lst = []
        for line in range(len(busy_zones)):
            side_by_side_busy_hpp_lst.append([])
            side_by_side_crop_hpp_lst.append([])
            for word in range(len(busy_zones[line])):

                # some preprocess before hpp
                busy_zone = busy_zones[line][word].astype(np.uint8)
                busy_zone = cv.cvtColor(busy_zone, cv.COLOR_BGR2GRAY)

                crop = word_segments_cropped_per_line[line][word].astype(np.uint8)
                crop = cv.cvtColor(crop, cv.COLOR_BGR2GRAY)

                # threshold then get hpp
                ret, thresh_busy = cv.threshold(busy_zone, 150, 255, cv.THRESH_BINARY_INV)
                hpp_busy = get_horizontal_projection_profile(thresh_busy)

                ret, thresh_crop = cv.threshold(crop, 150, 255, cv.THRESH_BINARY_INV)
                hpp_crop = get_horizontal_projection_profile(thresh_crop)

                sbs_busy = project_hpp_side_by_side_image(busy_zone, hpp_busy)
                sbs_crop = project_hpp_side_by_side_image(crop, hpp_crop)

                # add to list
                side_by_side_busy_hpp_lst[line].append(sbs_busy)
                side_by_side_crop_hpp_lst[line].append(sbs_crop)

    # write word image related words
    log.create_subdir('word_segments/projected_segs_on_line')
    log.set_subdir('word_segments/projected_segs_on_line')
    for i in range(len(segs_projected_on_line)):
        log.write_img(segs_projected_on_line[i], f'projected_segs{i}')

    log.create_subdir('word_segments/uncropped_wordsegments')
    log.set_subdir('word_segments/uncropped_wordsegments')
    for line in range(len(word_segments_per_line)):
        for word in range(len(word_segments_per_line[line])):
            log.write_img(word_segments_per_line[line][word], f'line{line}word{word}')

    log.create_subdir('word_segments/cropped_wordsegments')
    log.set_subdir('word_segments/cropped_wordsegments')
    for line in range(len(word_segments_cropped_per_line)):
        for word in range(len(word_segments_cropped_per_line[line])):
            log.write_img(word_segments_cropped_per_line[line][word], f'line{line}word{word}')

    log.create_subdir('word_segments/hori_fill_for_busy_zone')
    log.set_subdir('word_segments/hori_fill_for_busy_zone')
    for line in range(len(hori_filled_lst)):
        for word in range(len(hori_filled_lst[line])):
            log.write_img(hori_filled_lst[line][word], f'line{line}word{word}')

    log.create_subdir('word_segments/hori_fill_for_busy_zone_with_hpp')
    log.set_subdir('word_segments/hori_fill_for_busy_zone_with_hpp')
    for line in range(len(hori_filled_hpp_on_hori_filled_lst)):
        for word in range(len(hori_filled_hpp_on_hori_filled_lst[line])):
            log.write_img(hori_filled_hpp_on_hori_filled_lst[line][word], f'line{line}word{word}')

    log.create_subdir('word_segments/busy_zones')
    log.set_subdir('word_segments/busy_zones')
    for line in range(len(busy_zones)):
        for word in range(len(busy_zones[line])):
            log.write_img(busy_zones[line][word], f'line{line}word{word}')

    log.create_subdir('word_segments/busy_zone_sbs_hpp')
    log.set_subdir('word_segments/busy_zone_sbs_hpp')
    for line in range(len(side_by_side_busy_hpp_lst)):
        for word in range(len(side_by_side_busy_hpp_lst[line])):
            log.write_img(side_by_side_busy_hpp_lst[line][word], f'line{line}word{word}')

    log.create_subdir('word_segments/cropped_sbs_hpp')
    log.set_subdir('word_segments/cropped_sbs_hpp')

    for line in range(len(side_by_side_crop_hpp_lst)):
        for word in range(len(side_by_side_crop_hpp_lst[line])):
            log.write_img(side_by_side_crop_hpp_lst[line][word], f'line{line}word{word}')

    return

    # character segmentation

    # character segmentation with vertical projection profile
    vertical_multiplier = 7

    verti_filled_lst = []
    vpp_lst = []

    for i in range(len(busy_zones)):

        # most_occ_gap_verti = find_most_occuring_gap_vertically(
        #     busy_zones[i]) * vertical_multiplier

        # fill with most occuring text width vertically
        # most_occ_gap_verti = find_most_occuring_text_width_verti(
        #     busy_zones[i]) * vertical_multiplier

        # verti_filled = fill_this_gapsize_verti(
        #     busy_zones[i], most_occ_gap_verti)
        # verti_filled_lst.append(verti_filled)

        # fill connected component gaps
        # verti_filled = verti_fill_with_ccmap(busy_zones[i])
        # verti_filled_lst.append(verti_filled)

        # vertical_profile = get_vertical_projection_profile(verti_filled)

        # vertical_profile = ceil_profile_with_threshold(vertical_profile, int(busy_zones[i].shape[0] * 0.60), busy_zones[i].shape[0])

        # vpp_lst.append(vertical_profile)

        # fill with most occuring text width and thereafter height
        most_occ_text_width_hori = find_most_occuring_text_width_hori(busy_zones[i]) * hori_multiplier

        print(most_occ_text_width_hori)

        hori_filled = fill_this_gapsize_hori(
                busy_zones[i], most_occ_text_width_hori)

        cv.imwrite('horifill.png', hori_filled)

        most_occ_text_width_verti = find_most_occuring_text_width_verti(hori_filled, True) * 2

        print(most_occ_text_width_verti)

        verti_filled = fill_this_gapsize_verti(busy_zones[i], most_occ_text_width_verti)
        verti_filled_lst.append(verti_filled)

        vertical_profile = get_vertical_projection_profile(verti_filled)
        vertical_profile = ceil_profile_with_threshold(vertical_profile, int(busy_zones[i].shape[0] * 0.05), busy_zones[i].shape[0])

        vpp_lst.append(vertical_profile)

    # smooth projection profile with 1-dim gaussian
    # vpp_lst = one_dim_gausblur(vpp_lst, 3, 3)

    # create minimas from the ceiled profile
    current_minimas = []
    for i in range(len(vpp_lst)):
        minimas = get_minimas_from_ceiled_pp(vpp_lst[i], busy_zones[i].shape[0])

        current_minimas.append(minimas)

        # quit()

    # # # Find peaks in strips histogram projection
    # persistences_vertical = []
    # persistence_vertical = 5

    # collect extremas from projection profile in strip (both min and max), with over persistence
    # for i in range(len(vpp_lst)):
    #     persistence = get_persistence(
    #         vpp_lst[i], persistence_vertical)

    #     persistence = easify_persistence(persistence)

    #     persistences_vertical.append(persistence)

    # # print(persistences_vertical[0])

    # # print(smooth_vpp_lst[0].shape)

    # persistences_vertical_minimas = []
    # # collect maximas from extremas in strip
    # for per in persistences_vertical:
    #     persistences_vertical_minimas.append(get_minimas(per))

    # # find mean gap
    # mean_gaps = []
    # for i in range(len(verti_filled_lst)):
    #     mean_gap = find_mean_gap_vertically(verti_filled_lst[i])
    #     mean_gaps.append(mean_gap)

    # # print('Mean gaps:', mean_gaps)

    # current_minimas = persistences_vertical_minimas
    # prev_minimas = None
    # counter = 0
    # while has_peak_change(current_minimas, prev_minimas) and counter < 20:
    #     # print(counter)
    #     counter += 1
    #     prev_minimas = current_minimas
    #     bases = []

    #     # print('counter', counter)

    #     for i in range(len(current_minimas)):
    #         bases.append(find_bases(
    #             current_minimas[i], vpp_lst[i], mean_gaps[i]))

    #     current_minimas = bases

    # print(current_minimas)

    # Find peaks in strips histogram projection, 10% of image height sets
    # current_minimas = []

    # for i in range(len(busy_zones)):
    #     minimas = get_splitting_points_with_vpp_percentage(busy_zones[i], vpp_lst[i])
    #     current_minimas.append(minimas)

    # print(current_minimas)

    # carve the chars from the minimas
    carved_chars_per_word = []
    for i in range(len(current_minimas)):
        chars = carve_chars(busy_zones[i], current_minimas[i], 0) # set threshold to 0 (mean_gaps[i])

        carved_chars_per_word.append(chars)

    # Projections
    if log_intermediates:
        # hori_filled_hpp_on_hori_filled_lst = []
        # for i in range(len(hori_filled_lst)):
        #     hori_filled_hpp_on_hori_filled_lst.append(
        #         project_horizontal_projection_profile(hori_filled_lst[i], hori_filled_hpp_lst[i]))

        verti_filled_vpp_on_verti_filled_lst = []
        for i in range(len(verti_filled_lst)):
            verti_filled_vpp_on_verti_filled_lst.append(
                project_vertical_projection_profile(verti_filled_lst[i], vpp_lst[i]))

        # project vpp with minimas
        vpps_with_minimas = []
        for i in range(len(busy_zones)):
            vpps_with_minimas.append(
                project_minimas(project_vertical_projection_profile(
                    busy_zones[i], vpp_lst[i]), current_minimas[i])
            )

    # Writing char related images.
    if log_intermediates:
        log.create_subdir('character_segments/hori_fill')
        log.set_subdir('character_segments/hori_fill')

        for i in range(len(hori_filled_lst)):
            log.write_img(hori_filled_lst[i], f'hori_fill{i}')

        for i in range(len(hori_filled_hpp_on_hori_filled_lst)):
            log.write_img(hori_filled_hpp_on_hori_filled_lst[i], f'with_hpp{i}')

        log.create_subdir('character_segments/busy_zones')
        log.set_subdir('character_segments/busy_zones')

        for i in range(len(busy_zones)):
            log.write_img(busy_zones[i], f'busy_zone{i}')

        log.create_subdir('character_segments/verti_filled')
        log.set_subdir('character_segments/verti_filled')

        for i in range(len(verti_filled_lst)):
            log.write_img(verti_filled_lst[i], f'verti_filled{i}')

        for i in range(len(verti_filled_vpp_on_verti_filled_lst)):
            log.write_img(verti_filled_vpp_on_verti_filled_lst[i], f'with_vpp{i}')

        log.create_subdir('character_segments/vpps_with_minimas')
        log.set_subdir('character_segments/vpps_with_minimas')

        for i in range(len(vpps_with_minimas)):
            log.write_img(vpps_with_minimas[i], f'vpp{i}')

    log.create_subdir('character_segments/character_segments')
    log.set_subdir('character_segments/character_segments')

    for word_index in range(len(carved_chars_per_word)):
        for char_index in range(len(carved_chars_per_word[word_index])):
            log.write_img(
                carved_chars_per_word[word_index][char_index], f'character_w{word_index}_c{char_index}')

    # quit()

    # # cv.imwrite(f'{write_path}/profile_projection.png', strip_hpp_projected_on_originalimg)
    # # cv.imwrite(f'{write_path}/profile_projection_presence.png',
    # #            projected_img_presence)

    # # smooth_projected_img = project_horizontal_profiles_to_image(
    # #     smooth_strip_projections, strip_width, img.copy())

    # # print(*smooth_strip_projections[0])
    # # print(smooth_strip_projections[0].shape)

    # # cv.imwrite(f'{write_path}/smooth_profile_projection.png',
    # #            smooth_projected_img)

    # # Find peaks in strips histogram projection

    # # print(persistences)
    # # print()

    # projected_img_persistences = project_hpp_strips_to_img(
    #     smooth_strip_projections.copy(), strip_width, img.copy(), persistences)

    # cv.imwrite(f'{write_path}/smooth_profile_projection_persistences.png',
    #            projected_img_persistences)

    # persistences_maximas = []

    # # collect maximas from extremas in strip
    # for per in persistences:
    #     persistences_maximas.append(get_maximas(per))

    # # print('maximas\n', persistences_maximas)
    # # print()

    # projected_img_persistences_max = project_hpp_strips_to_img(
    #     smooth_strip_projections.copy(), strip_width, img.copy(), persistences_maximas)

    # cv.imwrite(f'{write_path}/smooth_profile_projection_persistences_max.png',
    #            projected_img_persistences_max)

    # # get the threshold which helps in binding maximas in different strips
    # threshold = get_appropriate_thresh(
    #     img.copy(), smooth_strip_projections.copy())
    # print('text line width threshold', threshold, '\n')

    # current_peaks = persistences_maximas
    # prev_peaks = None

    # """
    #     Algorithm for determining peaks need needs rebrush.

    #     Check if the current maximas changed from previous iteration.
    #     If not; run another iteration.

    #     Check if there is a histogram row which has a higher value then the current value within the threshold of rows.
    #     If yes; set this as a new maxima and remove previous.
    #     If no; continue.
    #     Do this for all rows.

    #     This removes redundant maximas and converges the greatest peaks of the histogram.
    # """
    # counter = 0
    # while has_peak_change(current_peaks, prev_peaks) and counter < 20:
    #     # print(counter)
    #     counter += 1
    #     prev_peaks = current_peaks
    #     strip_peaks = []

    #     for i in range(len(persistences)):
    #         strip_peaks.append(find_peaks(
    #             current_peaks[i], smooth_strip_projections[i], threshold))

    #     current_peaks = strip_peaks

    # projected_img_peaks = project_hpp_strips_to_img(
    #     smooth_strip_projections, strip_width, img.copy(), current_peaks)

    # cv.imwrite(f'{write_path}/smooth_profile_projection_peaks.png',
    #            projected_img_peaks)

    # # bind maximas in each strip to the next within the threshold of rows.
    # medial_lines = bind_peaks(current_peaks, threshold)
    # # print(chained_peaks)
    # # print()

    # projected_connected_peaks = project_connected_peaks(
    #     medial_lines, strip_width, img.copy())
    # cv.imwrite(f'{write_path}/projected_connected_peaks.png',
    #            projected_connected_peaks)

    # # get the simpel line segmentation / we use more advanced method
    # # line_segs = get_simpel_horizontal_segmenting(medial_seams, threshold, strip_width)
    # # print(line_segs)

    # # projected_line_segmentation = project_line_seg(line_segs, projected_connected_peaks.copy())
    # # cv.imwrite(f'{write_path}/projected_line_segmentation.png',
    # #            projected_line_segmentation)

    # # print(medial_seams)

    # # get sub-images between seam lines
    # betweens = []
    # betweens_for_proj = []
    # row_intervals = []

    # for s in range(1, len(medial_lines)):
    #     between, interval = between_medial_seams(
    #         dog_eq_gaus.copy(), medial_lines[s-1], medial_lines[s])
    #     between_for_proj, _ = between_medial_seams(
    #         gray.copy(), medial_lines[s-1], medial_lines[s])

    #     betweens.append(between)
    #     betweens_for_proj.append(between_for_proj)
    #     row_intervals.append(interval)

    # # print(row_intervals)

    # cv.imwrite(f'{write_path}/between.png', betweens_for_proj[0])

    # # Seam carving
    # # find carve in each sub-image, and proj to image
    # carve_imgs = []
    # for i in range(len(betweens)):
    #     between = cv.rotate(betweens[i], cv.ROTATE_90_CLOCKWISE)
    #     between_for_proj = cv.rotate(
    #         betweens_for_proj[i], cv.ROTATE_90_CLOCKWISE)

    #     carve_img, mask = seam_carve(between, between_for_proj)
    #     carve_img = cv.rotate(carve_img, cv.ROTATE_90_COUNTERCLOCKWISE)

    #     carve_imgs.append(carve_img)

    # # print(carve_img[0].shape)
    # cv.imwrite(f'{write_path}/projected_carve.png', carve_imgs[0])

    # # concatenate subimages with carve projection to the original image
    # bgr = cv.cvtColor(gray.copy(), cv.COLOR_GRAY2BGR)
    # # print(row_intervals[0][0])
    # total_carved_img = bgr[0:row_intervals[0][0], :]

    # for i in range(0, len(carve_imgs)):
    #     total_carved_img = cv.vconcat([total_carved_img, carve_imgs[i]])

    # total_carved_img = cv.vconcat(
    #     [total_carved_img, bgr[row_intervals[-1][1]:bgr.shape[0], :]])

    # cv.imwrite(f'{write_path}/total_carved.png', total_carved_img)

    # # get line segments
    # lineseg_lst = line_segment(total_carved_img)

    # for i in range(len(lineseg_lst)):
    #     cv.imwrite(f'{write_path}/linesegment{i}.png', lineseg_lst[i])

    # global number_of_words

    # words_per_line_lst = []

    # # carve words from lines
    # for i in range(len(lineseg_lst)):
    #     words_in_line = carve_word_img(lineseg_lst[i])

    #     for img in words_in_line:
    #         words_per_line_lst.append(img)

    #     number_of_words += len(words_in_line)

    #     for k in range(len(words_in_line)):
    #         cv.imwrite(
    #             f'{write_path}/wordseg_line{i}_word{k}.png', words_in_line[k])

    # print('words segmented', number_of_words)

    # cv.imwrite('word_img.png', words_per_line_lst[0])

    # # character segmentation

    # # busy zone

    # hori_multiplier = 10
    # vertical_multiplier = 5

    # most_occ_gap_hori = find_most_occuring_gap_horizontal(
    #     img) * hori_multiplier
    # most_occ_gap_verti = find_most_occuring_gap_vertically(
    #     img) * vertical_multiplier

    # busy_zones = []

    # for word_img in range(len(words_per_line_lst)):
    #     #

    #     # busy_zone_cc, high, low = carve_busy_zone_cc(total_word_img_lst[word_img])

    #     # cv.imwrite(f'{write_path}/wordseg_busy_zone{word_img}.png', project_busy_zone(total_word_img_lst[word_img], high, low))

    #     # cv.imwrite(f'{write_path}/busy_zone{word_img}.png', busy_zone_cc)

    #     # hpp busy zones

    #     # horizontal fill
    #     hori_filled = fill_this_gapsize_hori(
    #         words_per_line_lst[word_img], most_occ_gap_hori)
    #     cv.imwrite(f'{write_path}/hori_fill{word_img}.png', hori_filled)

    #     hori_filled_hpp = get_horizontal_projection_profile(hori_filled.copy())
    #     cv.imwrite(f'{write_path}/hori_fill{word_img}_hpp.png',
    #                project_horizontal_projection_profile(hori_filled, hori_filled_hpp))

    #     # vertical fill
    #     verti_filled = cv.rotate(fill_this_gapsize_hori(cv.rotate(
    #         words_per_line_lst[word_img], cv.ROTATE_90_CLOCKWISE), most_occ_gap_verti), cv.ROTATE_90_COUNTERCLOCKWISE)
    #     cv.imwrite(f'{write_path}/verti_fill{word_img}.png', verti_filled)

    #     verti_filled_hpp = get_horizontal_projection_profile(
    #         verti_filled.copy())
    #     cv.imwrite(f'{write_path}/verti_fill{word_img}_hpp.png',
    #                project_horizontal_projection_profile(verti_filled, verti_filled_hpp))

    #     # fill in both directions
    #     total = hori_filled + verti_filled
    #     cv.imwrite(f'{write_path}/total{word_img}.png', total)

    #     # horizontal projection profile of total
    #     total_hpp = get_horizontal_projection_profile(total.copy())
    #     cv.imwrite(f'{write_path}/total{word_img}_hpp.png',
    #                project_horizontal_projection_profile(total.copy(), total_hpp))

    #     busy_zone, high, low = carve_busy_zone_hp(
    #         words_per_line_lst[word_img], total_hpp)
    #     cv.imwrite(f'{write_path}/busy_zone{word_img}.png', busy_zone)

    #     busy_zones.append(busy_zone)

    # quit()

    # vpp_lst = []

    # for i in range(len(busy_zones)):
    #     vertical_profile = get_vertical_projection_profile(
    #         fill_this_gapsize_verti(busy_zones[i]))
    #     vpp_lst.append(vertical_profile)

    #     cv.imwrite(f'{write_path}/projected_vertical_profile{i}.png',
    #                project_vertical_projection_profile(busy_zones[i], vertical_profile))

    # # smooth projection profile with 1-dim gaussian
    # vpp_lst = one_dim_gausblur(vpp_lst, 3, 3)

    # for i in range(len(vpp_lst)):
    #     cv.imwrite(f'{write_path}/smooth_projected_vertical_profile{i}.png',
    #                project_vertical_projection_profile(busy_zones[i], vpp_lst[i]))

    # # Find peaks in strips histogram projection
    # persistences_vertical = []
    # persistence_vertical = 5

    # # collect extremas from projection profile in strip (both min and max), with over persistence
    # for i in range(len(vpp_lst)):
    #     persistences_vertical.append(get_persistence(
    #         vpp_lst[i].copy(), persistence_vertical))

    # # print(persistences_vertical[0])

    # # print(smooth_vpp_lst[0].shape)

    # persistences_vertical_minimas = []
    # # collect maximas from extremas in strip
    # for per in persistences_vertical:
    #     persistences_vertical_minimas.append(get_minimas(per))

    # # print(persistences_vertical_minimas[0])

    # # project vpp with minimas
    # for i in range(len(busy_zones)):
    #     cv.imwrite(f'{write_path}/smooth_projected_vertical_profile_minimas{i}.png', project_minimas(
    #         project_vertical_projection_profile(busy_zones[i], vpp_lst[i]), persistences_vertical_minimas[i]))

    # # mean_gap = find_mean_gap_vertically(busy_zones[0])

    # # for i in range(len(persistences_vertical)):
    # #     del persistences_vertical[i][0]
    # #     del persistences_vertical[i][-1]

    # mean_gaps = []
    # for i in range(len(busy_zones)):
    #     mean_gap = find_mean_gap_vertically(busy_zones[i])
    #     mean_gaps.append(mean_gap)

    # print('Mean gaps:', mean_gaps)

    # current_minimas = persistences_vertical_minimas
    # prev_minimas = None
    # counter = 0
    # while has_peak_change(current_minimas, prev_minimas) and counter < 20:
    #     # print(counter)
    #     counter += 1
    #     prev_minimas = current_minimas
    #     bases = []

    #     for i in range(len(current_minimas)):
    #         mean_gap = find_mean_gap_vertically(busy_zones[i])

    #         bases.append(find_bases(
    #             current_minimas[i], vpp_lst[i], mean_gaps[i]))

    #     current_minimas = bases

    # # print(len(busy_zones), len(smooth_vpp_lst), len(current_minimas))

    # #   project vpp with minimas
    # for i in range(len(busy_zones)):
    #     cv.imwrite(f'{write_path}/smooth_projected_vertical_profile_reduced_minimas{i}.png', project_minimas(
    #         project_vertical_projection_profile(busy_zones[i], vpp_lst[i]), current_minimas[i]))

    # # carve the chars from the minimas
    # carved_chars_per_word = []
    # for i in range(len(current_minimas)):
    #     carved_chars_per_word.append(
    #         carve_chars(busy_zones[i], current_minimas[i], mean_gaps[i])
    #     )

    # # write the carved chars
    # for word_index in range(len(carved_chars_per_word)):
    #     for char_index in range(len(carved_chars_per_word[word_index])):
    #         cv.imwrite(f'{write_path}/char_seg_word{word_index}_char{char_index}.png',
    #                    carved_chars_per_word[word_index][char_index])

def test_on_all_images(img_folder: str, write_path: str):
    for img_name in tqdm(os.listdir(img_folder)):
        # print(img_name)
        out_path = f'{write_path}/{img_name[:img_name.index(".png")]}'

        os.makedirs(out_path, exist_ok=True)

        img = cv.imread(f'{img_folder}/{img_name}')

        # sukalpameth(img, out_path)

        try:
            print(f'{img_folder}/{img_name}')
            sukalpameth(img, out_path)
        except:
            print(f'SOMETHING went wrong with:\n {img_folder}/{img_name}')
            break

def main():
    # img = cv.imread('data/Bangla-writer-test-convert/test_001.png')
    img = cv.imread('data/Bangla-writer-test-convert/test_235.png')
    # img = cv.imread('data/Bangla-writer-train-convert/train_145.png')
    # img = cv.imread('data/Bangla-writer-test-convert/test_322.png')
    # img = cv.imread('data/Bangla-writer-train-convert/train_153.png')
    # img = cv.imread('data/Bangla-writer-test-convert/test_346.png')
    # img = cv.imread('data/Bangla-writer-test-convert/test_228.png')
    # img = cv.imread('data/Bangla-writer-train-convert/train_088.png')
    # img = cv.imread('data/Bangla-writer-train-convert/train_231.png')

    # sukalpameth(img, 'hpp_busyzones')
    sukalpameth(img, 'sukalpameth')

    # start = time.time()
    # sukalpameth(img, 'sukalpameth', True)
    # end = time.time()
    # print("Elapsed (with compilation) = %s" % (end - start))

    # start = time.time()
    # sukalpameth(img, 'sukalpameth', True)
    # end = time.time()
    # print("Elapsed (after compilation) = %s" % (end - start))

    # test_on_all_images('data/Bangla-writer-train-convert', 'segmented_data_wordlevel/train')
    # test_on_all_images('data/Bangla-writer-test-convert', 'segmented_data_wordlevel/test')
    # test_on_all_images('data/Bangla-writer-test-convert')

if __name__ == '__main__':
    main()


from flask import request

from src.config import MESSAGE_LOCAL_NOT_FOUND, MESSAGE_PARAMETERS_REQUIRED, MESSAGE_LOCAL_WRONG_ID, \
    MESSAGE_LOCAL_WRONG_POSTAL_ADDRESS, MESSAGE_USER_WRONG_ID, MESSAGE_USER_NOT_FOUND, MESSAGE_CATEGORY_NOT_FOUND, \
    MESSAGE_LOCAL_POST_ERROR
from src.helper import response, maps
from src.service import category as category_service
from src.service import local as local_service
from src.service import user as user_service
from src.service import opening_hours_item as opening_hours_service
from src.service import review_local as review_local_service

def get(local_id):
    try:
        # Check input
        if not local_id or local_id <= 0:
            return response.make(error=True, message=MESSAGE_LOCAL_WRONG_ID)
        # Get instance
        local = local_service.get(local_id)
        if not local:
            return response.make(error=True, message=MESSAGE_LOCAL_NOT_FOUND)
        # Return instance object
        tags = local_service.get_tags(local_id)
        average = review_local_service.get_average(local_id)
        return response.make(error=False, response=dict(local=local.serialize(), tags=tags, average=average))
    except Exception as e:
        return response.raise_exception(e)

def get_all():
    try:
        local_list = [local.serialize() for local in local_service.get_all()]
        return response.make(error=False, response=dict(local_list=local_list))
    except Exception as e:
        return response.raise_exception(e)

def post():
    try:
        # Check input
        body = request.json
        required_parameters = ['name', 'postal_address', 'user_id']
        if not all(x in body for x in required_parameters):
            return response.make(error=True, message=MESSAGE_PARAMETERS_REQUIRED)
        if not body.get('user_id') or body.get('user_id') <= 0:
            return response.make(error=True, message=MESSAGE_USER_WRONG_ID)

        # Check user
        user = user_service.get(body.get('user_id'))
        if not user:
            return response.make(error=True, message=MESSAGE_USER_NOT_FOUND)

        # Check category
        if body.get('category_id', None):
            category = category_service.get(body.get('category_id'))
            if not category:
                return response.make(error=True, message=MESSAGE_CATEGORY_NOT_FOUND)

        # Compute coordinates
        coordinates = maps.compute_coordinates(body.get('postal_address'))
        if not coordinates:
            return response.make(error=True, message=MESSAGE_LOCAL_WRONG_POSTAL_ADDRESS)

        local_id, error_message = local_service.create(
            name=body.get('name'),
            postal_address=body.get('postal_address'),
            user_id=body.get('user_id'),
            latitude=coordinates[0],
            longitude=coordinates[1],
            website=body.get('website', None),
            description=body.get('description', None),
            category_id=body.get('category_id', None),
            phone_number=body.get('phone_number', None),
            image=body.get('image', None),
            pick_up=body.get('pick_up', True),
            delivery=body.get('delivery', False)
        )
        if local_id:
            return response.make(error=False, response=dict(local_id=local_id))
        else:
            return response.make(error=True, message=f'{MESSAGE_LOCAL_POST_ERROR} - {error_message}')
    except Exception as e:
        return response.raise_exception(e)

def put(local_id):
    try:
        # Check input
        if not local_id or local_id <= 0:
            return response.make(error=True, message=MESSAGE_LOCAL_WRONG_ID)
        # Check local
        local = local_service.get(local_id)
        if not local:
            return response.make(error=True, message=MESSAGE_LOCAL_NOT_FOUND)
        # Get input
        request_json = request.json
        name = request_json.get('name', None)
        postal_address = request_json.get('postal_address', None)
        coordinates = (None, None)
        website = request_json.get('website', None)
        description = request_json.get('description', None)
        category = request_json.get('category', None)
        phone_number = request_json.get('phone_number', None)
        image = request_json.get('image', None)
        pick_up = request_json.get('pick_up', None)
        delivery = request_json.get('delivery', None)
        # Compute coordinates
        if postal_address:
            coordinates = maps.compute_coordinates(postal_address)
            if not coordinates:
                return response.make(error=True, message=MESSAGE_LOCAL_WRONG_POSTAL_ADDRESS)
        # Process
        edited = local_service.edit(
            local_id,
            name=name,
            description=description,
            postal_address=postal_address,
            latitude=coordinates[0],
            longitude=coordinates[1],
            website=website,
            phone_number=phone_number,
            pick_up=pick_up,
            delivery=delivery,
            category=category,
            image=image
        )
        return response.make(error=False, response=dict(edited=edited))
    except Exception as e:
        return response.raise_exception(e)

def search(latitude, longitude):
    try:
        # Get all local coordinates
        coordinates_dict = local_service.get_all_coordinates()
        local_id_list = maps.filter_by_around(coordinates_dict, latitude, longitude)
        local_list = local_service.get_from_id_list(local_id_list)
        return response.make(error=False, response=dict(local_list=local_list))
    except Exception as e:
        return response.raise_exception(e)

def get_opening_hours(local_id):
    try:
        # Check input
        if not local_id or local_id <= 0:
            return response.make(error=True, message=MESSAGE_LOCAL_WRONG_ID)
        # Get instance
        local = local_service.get(local_id)
        if not local:
            return response.make(error=True, message=MESSAGE_LOCAL_NOT_FOUND)
        # Get all reviews
        opening_hours = opening_hours_service.get(local_id)
        # Return list
        return response.make(error=False, response=dict(opening_hours=opening_hours))
    except Exception as e:
        return response.raise_exception(e)

def post_opening_hours(local_id):
    body = request.json
    try:
        # Check input
        if not local_id or local_id <= 0:
            return response.make(error=True, message=MESSAGE_LOCAL_WRONG_ID)
        # Get instance
        local = local_service.get(local_id)
        if not local:
            return response.make(error=True, message=MESSAGE_LOCAL_NOT_FOUND)
        # Post reviews
        created = opening_hours_service.post(local_id, body)
        return response.make(error=False, response=dict(created=created))
    except Exception as e:
        return response.raise_exception(e)

def get_tags(local_id):
    try:
        # Check input
        if not local_id or local_id <= 0:
            return response.make(error=True, message=MESSAGE_LOCAL_WRONG_ID)
        # Get instance
        local = local_service.get(local_id)
        if not local:
            return response.make(error=True, message=MESSAGE_LOCAL_NOT_FOUND)
        # Get all reviews
        tags = local_service.get_tags(local_id)
        # Return list
        return response.make(error=False, response=dict(tags=tags))
    except Exception as e:
        return response.raise_exception(e)


import os
import re
import argparse

from typing import NamedTuple, Tuple, List

EXCLUDED = ['./_data', './_site', '.vscode', 'scripts', './README.md']
ROOT = os.getcwd()

BEGIN_SORT_ALPHABETICAL = ''
END_SORT_ALPHABETICAL = ''

BEGIN_SORT_CATEGORIES = ''
END_SORT_CATEGORIES = ''

class Path(NamedTuple):
    file: str
    path: str
    lines: Tuple[str]
    headers: List['Header']
    links: List['Link']

    def has_header(self, path: str) -> bool:
        return any(h.path == path for h in self.headers)

    def save(self):
        with open(self.file, 'w', encoding='utf-8') as f:
            f.write('\n'.join(self.lines))

class Header(NamedTuple):
    src: Path
    line: int
    raw: str
    path: str

class Link(NamedTuple):
    """ A [name](path#section) link embedded in a markdown file. """
    src: Path
    line: int
    raw: str
    name: str
    path: str
    section: str

    def follow(self) -> str:
        """ Follows the link, returning a path for the new destination """
        return '%s' % abs_path(os.path.join(self.src.path, self.path))

    def format(self) -> str:
        """ Formats the link in a standardized format """
        path = os.path.normpath(self.path)
        link = '[%s](' % self.name
        if path == '.':
            link += '#%s)' % self.section
        elif self.has_section():
            link += '%s/#%s)' % (path, self.section)
        else:
            link += '%s/)' % path
        return link

    def has_section(self) -> bool:
        return self.section != ''

class BatchedPrint:
    def __init__(self, header: str):
        self.header = header

    def print(self, *args):
        if self.header is not None:
            print(self.header)
            self.header = None
        print(*args)

def main():
    parser = argparse.ArgumentParser('Tools for manipulating links in markdown')
    subparser = parser.add_subparsers(dest='command')

    _ = subparser.add_parser('validate', help='Validates all existing links, reporting broken ones')
    _ = subparser.add_parser('sanitize', help='Sanitizes and reformats all existing links')

    args = parser.parse_args()

    if args.command == 'validate':
        do_validate()
    elif args.command == 'sanitize':
        do_sanitize()

def do_validate():
    tree = build_tree()
    index = {p.path: p for p in tree}

    for path in tree:
        batch = BatchedPrint('Found broken links in %s' % path.file)
        for link in path.links:
            target = link.follow()
            if target not in index:
                batch.print('  L%-5d: \'%s\' -> \'%s\'' % (link.line, link.raw, target))
            elif link.has_section():
                target_path = index[target]
                if not target_path.has_header(link.section):
                    target = '#' + link.section
                    batch.print('  L%-5d: \'%s\' -> \'%s\'' % (link.line, link.raw, target))

def do_sanitize():
    tree = build_tree()

    for path in tree:
        modified = False

        replacements = {}
        batch = BatchedPrint('Found incorrectly formatted links in %s' % path.file)
        for link in path.links:
            clean = link.format()
            if link.raw != clean:
                batch.print('  L%-3d: \'%s\' -> \'%s\'' % (link.line, link.raw, clean))
                replacements[link.raw] = clean

        if replacements:
            for i, line in enumerate(path.lines):
                for key, val in replacements.items():
                    if key in line:
                        path.lines[i] = line.replace(key, val)
                        modified = True

        # Alphabetical Sorting
        sort_ranges = []
        sort_range = None

        # Category Sorting
        sort_category_start = None
        sort_category_end = None
        sort_categories = []
        sort_category = None
        in_sort_category = False

        for i, line in enumerate(path.lines):
            if sort_range is not None:
                if line == END_SORT_ALPHABETICAL:
                    sort_ranges.append(sort_range)
                    sort_range = None
                elif line != '':  # Don't include empty lines in sort
                    sort_range.append((i, line))
            elif line == BEGIN_SORT_ALPHABETICAL:
                sort_range = []

            if in_sort_category:
                if line == END_SORT_CATEGORIES:
                    sort_category = None
                    sort_category_end = i
                    in_sort_category = False
                elif line.startswith('## '):
                    sort_category = []
                    sort_categories.append((line, sort_category))
                elif sort_category is not None:
                    if line != '
' and (line != '' or not sort_category or sort_category[-1] != ''):  # Don't include
 separator, or double empty lines
                        sort_category.append(line)
            elif line == BEGIN_SORT_CATEGORIES:
                assert sort_category_start is None, 'Can only have one category sort per page'
                sort_category = None
                sort_category_start = i
                in_sort_category = True

        if sort_range is not None:
            print('ERROR: Finished iterating for sort ranges but did not find an end tag?')
            continue

        if in_sort_category:
            print('ERROR: Finished iterating for sort categories but did not find an end tag?')

        batch = BatchedPrint('Sorting ranges in %s' % path.file)
        for sort_range in sort_ranges:
            sorted_lines = sorted([line for _, line in sort_range])
            sorted_indexes = sorted([i for i, _ in sort_range])
            sorting_done = False

            for i, line in zip(sorted_indexes, sorted_lines):
                if path.lines[i] != line:
                    modified = True
                    sorting_done = True
                path.lines[i] = line

            if sorting_done:
                batch.print('  L%d-%d (%d lines)' % (sorted_indexes[0], sorted_indexes[-1], len(sorted_indexes)))

        # Sort categories
        if sort_categories:
            new_lines = ['']
            for header, cat_lines in sorted(sort_categories):
                new_lines.append(header)
                new_lines += cat_lines
                new_lines.append('
')
                new_lines.append('')

            # Splice the sorted joined section onto the new section
            old_lines = path.lines[sort_category_start + 1:sort_category_end]

            if old_lines != new_lines:
                path.lines[sort_category_start + 1:sort_category_end] = new_lines
                batch.print('  L%d-%d (%d -> %d lines)' % (sort_category_start + 1, sort_category_end + 1, sort_category_end - sort_category_start - 1, len(new_lines)))
                modified = True

        if modified:  # Modified
            path.save()

def do_mv(old: str, new: str):
    tree = build_tree()
    index = {p.path : p for p in tree}

    if old not in tree:
        return print('No path: %s' % old)
    if new not in tree:
        return print('No path: %s' % new)

    old_path = index[old]
    new_path = index[new]

    # Update all links to the old path
    for path in tree:
        if path != old_path:
            replacements = {}
            for link in path:
                if link.follow() == old_path.path:  # Link that links to the old file
                    pass  # Need to update the old link to point at the new file

def build_tree() -> Tuple[Path, ...]:
    """ Builds the set of all Path objects in the web view """
    tree = []
    for path in walk('.'):
        if all(not path.startswith(exc) for exc in EXCLUDED) and path.endswith('.md'):
            path = abs_path(path)

            with open(path, 'r', encoding='utf-8') as f:
                text = f.read()

            lines = text.split('\n')
            path_obj = Path(path, sanitize_path(path), lines, [], [])

            for i, line in enumerate(lines):
                for match in re.finditer(r'\[([^\(\)\[\]\n\r]+)\]\(([./A-Za-z0-9_-]*)\/?\#?([./A-Za-z0-9_-]*)\)', line):
                    name, root, section = match.groups()
                    start, end = match.span()
                    path_obj.links.append(Link(path_obj, 1 + i, line[start:end], name, './%s/' % root, section))

                match = re.match(r'#+ (.+)', line)
                if match:
                    raw, *_ = match.groups()
                    link = re.sub(r'[^A-Za-z0-9]', '-', raw.lower())
                    path_obj.headers.append(Header(path_obj, line, raw, link))

            tree.append(path_obj)
    return tuple(tree)

def walk(path: str):
    """ Walks a directory, yielding each path found """
    for sub in os.listdir(path):
        sub_path = os.path.join(path, sub)
        if os.path.isfile(sub_path):
            yield sub_path
        elif os.path.isdir(sub_path):
            yield from walk(sub_path)

def sanitize_path(path: str) -> str:
    if '\\' in path:
        path = path.replace('\\', '/')
    if '.md' in path:
        path = path.replace('.md', '')
    if path.endswith('index'):
        path = path[:-len('/index')]
    return os.path.normpath(path)

def abs_path(path: str) -> str:
    """ Returns the absolute path relative to the root of the web directory """
    return os.path.relpath(os.path.abspath(path), ROOT)

if __name__ == '__main__':
    main()


from django.urls import path
from . import views

urlpatterns = [
    path('', view=views.getRoutes, name='routes'),
    path('products', view=views.getProducts, name='products'),
    path('products-db', view=views.getProductsListFromDB, name='products-db'),
    path('products/<str:pk>', view=views.getParticularProduct, name='particular-product'),
    path('products-db/<str:pk>', view=views.getProductsByIDFromDB, name='particular-product-db'),
]


import os
from concurrent.futures import ProcessPoolExecutor

import numpy as np
from tqdm import tqdm

from utils.load_yaml import load_root_dir
from utils.util_loc import is_cont_meas

root_dir = load_root_dir()
subdir, dirs, files = next(os.walk(os.path.join(root_dir)))

def compute(d):
    if is_cont_meas(d):
        return
    # [snapshots x freq points x BS antennas x users]
    H = np.load(os.path.join(root_dir, d, "small-channel.npy"))

    num_time = H.shape[0]
    num_freq = H.shape[1]

    avg_H = np.sum(H, axis=(0, 1)) / (num_time * num_freq)

    if not os.path.isfile(os.path.join(root_dir, d, "avg_H.npy")):
        np.save(os.path.join(root_dir, d, "avg_H.npy"), avg_H)

if __name__ == '__main__':
    pbar = tqdm(total=len(dirs))
    with ProcessPoolExecutor(max_workers=10) as executor:
        for d in dirs:
            future = executor.submit(compute, d)
            future.add_done_callback(lambda p: pbar.update())


import time
import numpy as np
import cvxopt

def loadData(fileName):             # 预处理数据，分为-1和1的二分类
    features = []
    labels = []
    fr = open(fileName, 'r')
    for line in fr.readlines():
        curLine = line.strip().split(',')
        if int(curLine[0]) == 0:
            labels.append(1)
        else:
            labels.append(-1)
        features.append([int(num) / 255 for num in curLine[1:]])
    return features, labels

class SVM(object):
    def __init__(self):
        pass

    def _init_parameters_(self, features, labels):              # 初始化参数
        self.m = len(features[0])   # 获取样本的维度(784)
        self.P = np.identity(self.m + 1)   # 目标函数中的二次部分，我们在最后把b也算作一个变量
        self.P[self.m][self.m] = 0            # 将变量b初始化为0
        self.P = cvxopt.matrix(self.P)   # 转换为矩阵形式
        self.q = cvxopt.matrix([0.0] * (self.m + 1))   # 目标函数中的一次部分

    def train(self, features, labels):
        # 约束条件中的不等式
        self._init_parameters_(features, labels)
        G = []
        for i in range(self.m):
            G.append([-features[j][i] * labels[j] for j in range(len(features))])
        G.append([-labels[i] * 1.0 for i in range(len(labels))])
        G = cvxopt.matrix(G)
        h = cvxopt.matrix([-1.0] * (len(labels)))

        # 将参数传递给solvers.qp，返回最优解
        cs = cvxopt.solvers
        result = cs.qp(self.P, self.q, G, h)
        results = result['x']
        w = results[:-1]
        b = results[-1]
        return w, b

    def _predict_(self, w, b, features):
        if (np.dot(w.T, features) + b) >= 0:
            return 1
        else:
            return -1

    def predict(self, w, b, features, labels):
        # 错误值计数
        errorCnt = 0
        # 循环遍历测试集中的每一个样本
        for i in range(len(features)):
            # 获取预测值
            predict = self._predict_(w, b, features[i])
            # 与答案进行比较
            if predict != labels[i]:
                # 若错误  错误值计数加1
                errorCnt += 1
        # 返回准确率
        return 1 - (errorCnt / len(features))

if __name__ == '__main__':
    time_1 = time.time()
    # 获取训练集
    print('start read train data')
    train_features, train_labels = loadData('../data/mnist_train.csv')
    time_2 = time.time()
    print('read train data cost ', time_2 - time_1, ' seconds', '\n')

    # 获取测试集
    print('start read test data')
    test_features, test_labels = loadData('../data/mnist_test.csv')
    time_3 = time.time()
    print('read test data cost ', time_3 - time_2, ' seconds', '\n')

    # 计算最优解w*,b*3
    print('start train')
    svm = SVM()
    w, b = svm.train(train_features[:6000], train_labels[:6000])
    time_4 = time.time()
    print('train cost ', time_4 - time_3, ' seconds', '\n')

    # 对训练得到的最优解进行测试
    print('start predict')
    accuracy = svm.predict(w, b, test_features[:3000], test_labels[:3000])
    time_5 = time.time()
    # 显示正确率
    print ("The accruacy socre is ", accuracy)
    print('predicting cost ', time_5 - time_4, ' seconds', '\n')
    # 显示用时时长
    print('All cost', time_5 - time_1, ' seconds')


import re
import MeCab
import glob

def main():
  m = MeCab.Tagger('-Owakati')

  for name in glob.glob('/home/ubuntu/posi-nega-twt/*'):
    try:
      f = open(name,'r')
    except Exception as e:
      print(e)
      continue

    tweet = f.read()
    tweet = m.parse(tweet).strip()
    emojis = re.findall(r'[\U0001F600-\U0001F64F|\U0001F300-\U0001F5FF|\U0001F680-\U0001F6FF|\U0001F1E0-\U0001F1FF]', tweet)

    for e in emojis:
      tweet = tweet.replace(e, '')

    for e in emojis:
      tweet += ' %s '%e

    tweet = re.sub(r'\s{1,}', ' ', tweet)
    nico = 'ðŸ˜ƒ'
    namida = 'ðŸ˜¢'

    if nico in tweet:
      file_name = name.replace('posi-nega-twt','posi-nega-twt-wakati')
      file_name = file_name.replace('_p.txt', '_p_wakati.txt')
      f = open(file_name, 'w')
    elif namida in tweet:
      file_name = name.replace('posi-nega-twt','posi-nega-twt-wakati')
      file_name = file_name.replace('_n.txt', '_n_wakati.txt')
      f = open(file_name, 'w')
    else:
      continue

    tweet = tweet.replace(nico, '')
    tweet = tweet.replace(namida, '')
    tweet = re.sub(r'\s{1,}', ' ', tweet)
    f.write(tweet)

if __name__ == '__main__':
  main()


from pk.widgets import Button
from PyQt4.QtCore import QObject, SIGNAL, Qt
from PyQt4.QtGui import QFrame, QVBoxLayout, QGridLayout, QPainter, QColor, QHBoxLayout, QButtonGroup
import pk.widgets
import pk.widgets.utils
import rtplayer
import controlpanel
import keyboard
import toolbox
import spec
import slots
import parts

SYNTHS = 3

class ChannelControls(QFrame):
    """ Volume, knobs. """
    def __init__(self, parent=None):
        QFrame.__init__(self, parent)
        self.setAutoFillBackground(True)
        self.setFrameShape(spec.FRAME_SHAPE)
        self.setFrameShadow(QFrame.Sunken)
        self.palette().setColor(self.backgroundRole(),
                                pk.widgets.utils.POV_COLOR)

        self.knobs = pk.widgets.Knobs(knobs=3, parent=self)
        self.panKnob = self.knobs.knobs[0]
        self.send1Knob = self.knobs.knobs[1]
        self.send2Knob = self.knobs.knobs[2]
        self.volumeSlider = pk.widgets.MixerSlider(self)

        Layout = QVBoxLayout(self)
        Layout.setSpacing(0)
        Layout.setMargin(0)
        pk.widgets.utils.h_centered(Layout, self.knobs)
        pk.widgets.utils.h_centered(Layout, self.volumeSlider)

class MainWindow(QFrame):
    def __init__(self, engine, parent=None, channels=4):
        QFrame.__init__(self, parent)
        self.setAutoFillBackground(True)
        self.palette().setColor(self.backgroundRole(),
                                spec.BACKGROUND)

        self.controlPanel = controlpanel.ControlPanel(self)

        self.keyboard = keyboard.Keyboard(self, orientation=Qt.Vertical)
        self.keyboard.keywidth = 25
        self.keyboard.setHalfSteps(127)
        self.keyboard.setMinimumWidth(50)

        self.controls = []
        self.synthzones = []
        MixerLayout = QHBoxLayout()
        for i in range(channels):
            ChannelLayout = QVBoxLayout()
            for j in range(SYNTHS):
                synthzone = slots.SynthSlot(self)
                QObject.connect(synthzone, SIGNAL('selected(QWidget *)'),
                                self.clicked)
                synthzone.channel = i
                ChannelLayout.addWidget(synthzone)
            controls = ChannelControls(self)
            controls.channel = i
            ChannelLayout.addWidget(controls)
            MixerLayout.addLayout(ChannelLayout)

        self.toolbox = toolbox.ToolBox(self)
        self.toolbox.setFrameShape(spec.FRAME_SHAPE)
        self.toolbox.setFixedWidth(spec.PART_WIDTH + spec.SCROLLPAD_WIDTH + 10)

        self.engine = engine
        self.rtplayer = rtplayer.RTPlayer(engine.server)
        self.rt_note = None
        self.current_synth = None
        self.mode = None
        self.set_mode('select')
        self.notes = {}

        QObject.connect(self.controlPanel.modeGroup,
                        SIGNAL('buttonClicked(QAbstractButton *)'),
                        self.mode_clicked)
        QObject.connect(self.keyboard,
                        SIGNAL('noteon(int, int)'),
                        self.noteon)
        QObject.connect(self.keyboard,
                        SIGNAL('noteoff(int, int)'),
                        self.noteoff)

        Layout = QHBoxLayout(self)
        Layout.addWidget(self.controlPanel)
        Layout.addWidget(self.keyboard)
        Layout.addLayout(MixerLayout)
        Layout.addWidget(self.toolbox)

    def slotVolume(self, chan, value):
        self.emit(SIGNAL('volume(int, float)'),
                  self.channels.index(chan), value)

    def mode_clicked(self, button):
        self.set_mode(str(button.text()))

    def set_mode(self, mode):
        self.mode = mode
        getattr(self.controlPanel, mode+'Button').setChecked(True)

    def clicked(self, part):
        if self.current_synth:
            self.current_synth.set_selected(False)

        if self.mode == 'select':
            self.select(part)
        elif self.mode == 'delete':
            part.parent().delete()
            self.current_mode = None
        elif self.mode == 'play':
            self.select(part)
            if part.stream:
                self.engine.deregister(part.stream)
                part.activate(False)
                part.stream = None
            elif part.pattern:
                part.stream = self.engine.register(part.data, part.pattern)
                part.activate(True)

    def select(self, part):
        part.set_selected(True)
        self.current_synth = part
        if isinstance(part, parts.SynthPart):
            self.rtplayer.synth = part.data
        else:
            self.rtplayer.synth = None

    def noteon(self, note, vel):
        """ from editor keyboard """
        note -= 12 * 4
        if self.current_synth:
            synth = self.current_synth.data.copy()
            synth.pitch(note)
            self.rt_note = self.engine.player.play_rt(synth)

    def noteoff(self, note, vel):
        """ from editor keyboard """
        if self.rt_note:
            self.engine.player.stop_rt(self.rt_note)
            self.rt_note = None

    def midi_note(self, msg):
        device, key, vel, timestamp = msg
        if vel:
            self.rtplayer.note_on(key, vel)
        else:
            self.rtplayer.note_off(key, vel)