from __future__ import unicode_literals
from tempfile import mkdtemp
from shutil import rmtree
import os

from sqlalchemy.orm import joinedload_all

from clldutils.misc import binary_type
from clld.db.meta import DBSession
from clld.db.models.common import ValueSet, Language, Parameter, Dataset, Value
from clld.web.adapters.base import Representation, Index
from clld.web.adapters.geojson import GeoJsonLanguages
from clld.web.adapters.download import Download
from clld.interfaces import ILanguage, IIndex, IDataset
from clld.web.maps import SelectedLanguagesMap
from clldutils.dsv import UnicodeWriter

from asjp.models import txt_header, Doculect

class _Language(object):
    def __init__(self, pk, name, longitude, latitude, id_):
        self.pk = pk
        self.name = name
        self.longitude = longitude
        self.latitude = latitude
        self.id = id_

    def __json__(self, req):
        return self.__dict__

class GeoJsonAllLanguages(GeoJsonLanguages):
    def feature_iterator(self, ctx, req):
        for row in DBSession.query(
            Language.pk, Language.name, Language.longitude, Language.latitude, Language.id
        ):
            yield _Language(*row)

class Wordlist(Representation):
    """The ASJP wordlist format suitable as input for the ASJP software.
    """
    name = "ASJP text format"
    mimetype = str('text/plain')
    extension = str('txt')

    def render(self, ctx, req):
        return ctx.txt

class Wordlists(Index):
    name = "ASJP text format (only up to 1500 wordlists can be exported)"
    extension = str('txt')
    mimetype = str('text/plain')

    def render(self, ctx, req):
        res = [txt_header()]
        for d in ctx.get_query(limit=10000):
            res.append(d.txt)
        res.append('')
        res.append('')
        return '\n'.join(res)

class MapView(Index):
    extension = str('map.html')
    mimetype = str('text/vnd.clld.map+html')
    send_mimetype = str('text/html')
    template = 'language/map_html.mako'

    def template_context(self, ctx, req):
        languages = list(ctx.get_query(limit=8000))
        return {
            'map': SelectedLanguagesMap(ctx, req, languages),
            'languages': languages}

class Tab(Download):
    ext = 'tab'

    fields = [
        ('names', lambda l: l.id),
        ('wls_fam', lambda l: l.classification_wals.split('.')[0]
            if '.' in l.classification_wals else ''),
        ('wls_gen', lambda l: l.classification_wals.split('.')[1]
            if '.' in l.classification_wals else ''),
        ('e', lambda l: l.classification_ethnologue),
        ('hh', lambda l: l.classification_glottolog),
        ('lat', lambda l: l.latitude),
        ('lon', lambda l: l.longitude),
        ('pop', lambda l: l.number_of_speakers),
        ('wcode', lambda l: ''),
        ('iso', lambda l: l.code_iso),
    ]

    def create(self, req, filename=None, verbose=True):  # pragma: no cover
        meanings = [(p.name, p.id)
                    for p in DBSession.query(Parameter).order_by(Parameter.pk)]
        tmp = mkdtemp()
        path = os.path.join(tmp, 'asjp.tab')
        with UnicodeWriter(f=path, delimiter=binary_type("\t")) as writer:
            writer.writerow([f[0] for f in self.fields] + [m[0] for m in meanings])
            for lang in DBSession.query(Doculect).order_by(Doculect.pk).options(
                    joinedload_all(Language.valuesets, ValueSet.values),
                    joinedload_all(Language.valuesets, ValueSet.parameter)
            ).limit(10000):
                row = [f[1](lang) for f in self.fields]
                vss = {vs.parameter.id: vs for vs in lang.valuesets}
                row.extend([Doculect.format_words(vss.get(m[1])) for m in meanings])
                writer.writerow(row)
        Download.create(self, req, filename=path)
        rmtree(tmp)

class Cldf(Representation):
    extension = str('cldf.csv')
    mimetype = str('text/csv')  # FIXME: declare header?

    def render(self, ctx, req):
        fid = req.route_url('parameter', id='xxx').replace('xxx', '{0}')
        lid = req.route_url('language', id='xxx').replace('xxx', '{0}')
        with UnicodeWriter() as writer:
            writer.writerow(['Language_ID', 'Feature_ID', 'Value'])
            for _lid, _fid, v in DBSession.query(
                        Language.id, Parameter.id, Value.name)\
                    .filter(Language.pk == ValueSet.language_pk)\
                    .filter(Parameter.pk == ValueSet.parameter_pk)\
                    .filter(Value.valueset_pk == ValueSet.pk)\
                    .order_by(Parameter.pk, Language.id):
                if v:
                    writer.writerow([lid.format(_lid), fid.format(_fid), v])
            return writer.read()

def includeme(config):
    config.register_adapter(Cldf, IDataset)
    config.register_adapter(GeoJsonAllLanguages, ILanguage, IIndex)
    config.register_adapter(Wordlists, ILanguage, IIndex)
    config.register_adapter(Wordlist, ILanguage)
    config.register_adapter(MapView, ILanguage, IIndex)
    config.register_download(Tab(Dataset, 'asjp'))


import os

from flask import Flask
from flask import request
from flask import render_template

from sklearn.externals import joblib

import random
import re

def create_app(test_config=None):
    root_dir = os.path.realpath(os.path.dirname(__file__))
    pipe_dir1 = os.path.join(root_dir, 'static/part1_pipe.pkl')
    count_dir1 = os.path.join(root_dir, 'static/count_vect1.pkl')
    cls_dir1 = os.path.join(root_dir, 'static/model1.pkl')
    word_appear_train1_dir = os.path.join(root_dir, 'static/word_appearance_train1.pkl')
    word_appear_dev1_dir = os.path.join(root_dir, 'static/word_appearance_dev1.pkl')

    pipe1 = joblib.load(pipe_dir1)
    count_vect1 = joblib.load(count_dir1)
    classifier1 = joblib.load(cls_dir1)
    word_appear_train1 = joblib.load(word_appear_train1_dir)
    word_appear_dev1 = joblib.load(word_appear_dev1_dir)

    count_dir2 = os.path.join(root_dir, 'static/count_vect2.pkl')
    cls_dir2 = os.path.join(root_dir, 'static/model2.pkl')
    word_appear_train2_dir = os.path.join(root_dir, 'static/word_appearance_train2.pkl')
    word_appear_dev2_dir = os.path.join(root_dir, 'static/word_appearance_dev2.pkl')

    count_vect2 = joblib.load(count_dir2)
    classifier2 = joblib.load(cls_dir2)
    word_appear_train2 = joblib.load(word_appear_train2_dir)
    word_appear_dev2 = joblib.load(word_appear_dev2_dir)

    LABELS1 = ['NEGATIVE', 'POSITIVE']
    LABELS2 = ['FAKE', 'TRUE']

    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    def ubt_weight(raw_sentence, count_vect, clsf):
        rawX = count_vect.transform([raw_sentence])
        # [1] gives the nonzero columns while the [0] gives the nonzero row
        validIndex = rawX.nonzero()[1]
        validWeights = [clsf.coef_[0, k] for k in validIndex]
        # all feature name
        validNames = [count_vect.get_feature_names()[k] for k in validIndex]
        m1= {}
        m2= {}
        m3= {}
        pos_total = 0
        neg_total = 0
        largest_abs = 0
        for name, weight in zip(validNames, validWeights):
            if abs(weight) > largest_abs:
                largest_abs = abs(weight)
            if weight > 0:
                pos_total += weight
            else:
                neg_total += weight
            if len(name.split()) == 1:
                m1[name] = weight
            elif len(name.split()) == 2:
                m2[name] = weight
            elif len(name.split()) == 3:
                m3[name] = weight
            else:
                print('something is wrong!')
        return (m1, m2, m3, pos_total, neg_total, largest_abs)

    @app.route('/', methods=['GET'])
    def index():
        return render_template('index.html')

    @app.route('/goodOrBad', methods=['GET'])
    def goodOrBad():
        sentence = {'label': '', 'prob': ''}
        words = []
        bigrams = []
        trigrams = []
        return render_template('goodOrBad.html', sentence=sentence, words=words, bigrams=bigrams, trigrams=trigrams)

    @app.route('/goodOrBad', methods=['POST'])
    def goodOrBad_post():
        ori_text = request.form.get("textbox")
        text = ori_text.lower()
        color = ''
        label = LABELS1[pipe1.predict([text])[0]]
        if label == 'POSITIVE':
            color = 'text-danger'
        else:
            color = 'text-primary'
        sentence = {'color': color, 'label': label, 'prob': str(max(pipe1.predict_proba([text])[0]))[:7]}
        (u_map, b_map, t_map, pos_total, neg_total, largest_abs) = ubt_weight(text, count_vect1, classifier1)
        print(u_map)

        all_words = re.findall(r"[\w']+|[.,!?;]", ori_text)

        words = []

        for word in all_words:
            word_dict = {}

            word_dict['text'] = word

            word = word.lower()
            if word in u_map:
                if u_map[word] > 0:
                    word_dict['label'] = 'POSITIVE'
                else:
                    word_dict['label'] = 'NEGATIVE'
            else:
                word_dict['label'] = 'NEUTRAL'
                words.append(word_dict)
                continue
            weight = u_map[word]
            word_dict['weight'] = 'weight: ' + str(weight)[:7]

            (neg_count_train, pos_count_train) = word_appear_train1[word]
            pos_ratio_train = pos_count_train / (neg_count_train+pos_count_train)
            word_dict['pos_ratio_train'] = str(round(pos_ratio_train*100)) + '%'

            if word in word_appear_dev1:
                (neg_count_dev, pos_count_dev) = word_appear_dev1[word]
                pos_ratio_dev = pos_count_dev / (neg_count_dev+pos_count_dev)
                word_dict['pos_ratio_dev'] = str(round(pos_ratio_dev*100)) + '%'
            else:
                word_dict['pos_ratio_dev'] = 'None'

            if word_dict['label'] == 'POSITIVE':
                word_dict['contrib'] = 'contribution:' + str(weight / pos_total)[:7]
                clsname = 'text-danger h' + str(4-(int(round(abs(weight)/largest_abs*3))))
                word_dict['font'] = clsname
            else:
                word_dict['contrib'] = 'contribution:' + str(weight / neg_total)[:7]
                clsname = 'text-primary h' + str(4-(int(round(abs(weight)/largest_abs*3))))
                word_dict['font'] = clsname

            word_dict['pos_ratio'] = 'positive appearance: ' + word_dict['pos_ratio_train'] + '(train)  ' + word_dict['pos_ratio_dev'] + '(dev)'

            words.append(word_dict)

        b_seen = set([])
        bigrams = []
        for i in range(0, len(all_words)-1):
            b1 = all_words[i]
            b2 = all_words[i+1]
            b = b1.lower() + ' ' + b2.lower()
            if b not in b_map or b in b_seen:
                continue
            b_seen.add(b)
            weight = b_map[b]
            b_dict = {}
            b_dict['text'] = b1 + ' ' + b2
            b_dict['weight'] = 'weight: ' + str(weight)[:7]
            if weight > 0:
                b_dict['label'] = 'POSITIVE'
                b_dict['contrib'] = 'contribution:' + str(weight / pos_total)[:7]
                clsname = 'text-danger h' + str(4-(int(round(abs(weight)/largest_abs*3))))
                b_dict['font'] = clsname
            else:
                b_dict['label'] = 'NEGATIVE'
                b_dict['contrib'] = 'contribution:' + str(weight / neg_total)[:7]
                clsname = 'text-primary h' + str(4-(int(round(abs(weight)/largest_abs*3))))
                b_dict['font'] = clsname

            (neg_count_train, pos_count_train) = word_appear_train1[b]
            pos_ratio_train = pos_count_train / (neg_count_train+pos_count_train)
            b_dict['pos_ratio_train'] = str(round(pos_ratio_train*100)) + '%'

            if b in word_appear_dev1:
                (neg_count_dev, pos_count_dev) = word_appear_dev1[b]
                pos_ratio_dev = pos_count_dev / (neg_count_dev+pos_count_dev)
                b_dict['pos_ratio_dev'] = str(round(pos_ratio_dev*100)) + '%'
            else:
                b_dict['pos_ratio_dev'] = 'None'

            b_dict['pos_ratio'] = 'positive appearance: ' + b_dict['pos_ratio_train'] + '(train)  ' + b_dict['pos_ratio_dev'] + '(dev)'

            bigrams.append(b_dict)

        trigrams = []
        t_seen = set([])
        for i in range(0, len(all_words)-2):
            t1 = all_words[i]
            t2 = all_words[i+1]
            t3 = all_words[i+2]
            t = t1.lower() + ' ' + t2.lower() + ' ' + t3.lower()
            if t not in t_map or t in t_seen:
                continue
            t_seen.add(t)
            weight = t_map[t]
            t_dict = {}
            t_dict['text'] = t1 + ' ' + t2 + ' ' + t3
            t_dict['weight'] = 'weight: ' + str(weight)[:7]
            if weight > 0:
                t_dict['label'] = 'POSITIVE'
                t_dict['contrib'] = 'contribution:' + str(weight / pos_total)[:7]
                clsname = 'text-danger h' + str(4-(int(round(abs(weight)/largest_abs*3))))
                t_dict['font'] = clsname
            else:
                t_dict['label'] = 'NEGATIVE'
                t_dict['contrib'] = 'contribution:' + str(weight / neg_total)[:7]
                clsname = 'text-primary h' + str(4-(int(round(abs(weight)/largest_abs*3))))
                t_dict['font'] = clsname

            (neg_count_train, pos_count_train) = word_appear_train1[t]
            pos_ratio_train = pos_count_train / (neg_count_train+pos_count_train)
            t_dict['pos_ratio_train'] = str(round(pos_ratio_train*100)) + '%'

            if t in word_appear_dev1:
                (neg_count_dev, pos_count_dev) = word_appear_dev1[t]
                pos_ratio_dev = pos_count_dev / (neg_count_dev+pos_count_dev)
                t_dict['pos_ratio_dev'] = str(round(pos_ratio_dev*100)) + '%'
            else:
                t_dict['pos_ratio_dev'] = 'None'

            t_dict['pos_ratio'] = 'positive appearance: ' + t_dict['pos_ratio_train'] + '(train)  ' + t_dict['pos_ratio_dev'] + '(dev)'

            trigrams.append(t_dict)

        return render_template('goodOrBad.html', sentence=sentence, words=words, bigrams=bigrams, trigrams=trigrams)

    @app.route('/chart1', methods=['GET'])
    def chart1():
        return render_template('chart1.html')

    @app.route('/trueOrFake', methods=['GET'])
    def trueOrFake():
        sentence = {'label': '', 'prob': ''}
        words = []
        bigrams = []
        trigrams = []
        return render_template('trueOrFake.html', sentence=sentence, words=words, bigrams=bigrams, trigrams=trigrams)

    @app.route('/trueOrFake', methods=['POST'])
    def trueOrFake_post():
        ori_text = request.form.get("textbox")
        text = ori_text.lower()
        color = ''
        feature = count_vect2.transform([text])
        label = LABELS2[classifier2.predict(feature)[0]]
        prob = max(classifier2.predict_proba(feature)[0])
        if label == 'TRUE':
            color = 'text-danger'
        else:
            color = 'text-primary'
        sentence = {'color': color, 'label': label, 'prob': str(prob)[:7]}
        (u_map, b_map, t_map, true_total, fake_total, largest_abs) = ubt_weight(text, count_vect2, classifier2)

        all_words = re.findall(r"[\w']+|[.,!?;]", ori_text)

        words = []
        for word in all_words:
            word_dict = {}

            word_dict['text'] = word

            word = word.lower()

            if word in u_map:
                if u_map[word] > 0:
                    word_dict['label'] = 'TRUE'
                else:
                    word_dict['label'] = 'FAKE'
            else:
                word_dict['label'] = 'NEUTRAL'
                words.append(word_dict)
                continue
            weight = u_map[word]
            word_dict['weight'] = 'weight: ' + str(weight)[:7]

            (fake_count_train, true_count_train) = word_appear_train2[word]
            true_ratio_train = true_count_train / (fake_count_train+true_count_train)
            word_dict['true_ratio_train'] = str(round(true_ratio_train*100)) + '%'

            if word in word_appear_dev2:
                (fake_count_dev, true_count_dev) = word_appear_dev2[word]
                true_ratio_dev = true_count_dev / (fake_count_dev+true_count_dev)
                word_dict['true_ratio_dev'] = str(round(true_ratio_dev*100)) + '%'
            else:
                word_dict['true_ratio_dev'] = 'None'

            if word_dict['label'] == 'TRUE':
                word_dict['contrib'] = 'contribution:' + str(weight / true_total)[:7]
                clsname = 'text-danger h' + str(4-(int(round(abs(weight)/largest_abs*3))))
                word_dict['font'] = clsname
            else:
                word_dict['contrib'] = 'contribution:' + str(weight / fake_total)[:7]
                clsname = 'text-primary h' + str(4-(int(round(abs(weight)/largest_abs*3))))
                word_dict['font'] = clsname

            word_dict['true_ratio'] = 'true appearance: ' + word_dict['true_ratio_train'] + '(train)  ' + word_dict['true_ratio_dev'] + '(dev)'

            words.append(word_dict)

        b_seen = set([])
        bigrams = []
        for i in range(0, len(all_words)-1):
            b1 = all_words[i]
            b2 = all_words[i+1]
            b = b1.lower() + ' ' + b2.lower()
            if b not in b_map or b in b_seen:
                continue
            b_seen.add(b)
            weight = b_map[b]
            b_dict = {}
            b_dict['text'] = b1 + ' ' + b2
            b_dict['weight'] = 'weight: ' + str(weight)[:7]
            if weight > 0:
                b_dict['label'] = 'TRUE'
                b_dict['contrib'] = 'contribution:' + str(weight / true_total)[:7]
                clsname = 'text-danger h' + str(4-(int(round(abs(weight)/largest_abs*3))))
                b_dict['font'] = clsname
            else:
                b_dict['label'] = 'FAKE'
                b_dict['contrib'] = 'contribution:' + str(weight / fake_total)[:7]
                clsname = 'text-primary h' + str(4-(int(round(abs(weight)/largest_abs*3))))
                b_dict['font'] = clsname

            (fake_count_train, true_count_train) = word_appear_train2[b]
            true_ratio_train = true_count_train / (fake_count_train+true_count_train)
            b_dict['true_ratio_train'] = str(round(true_ratio_train*100)) + '%'

            if b in word_appear_dev2:
                (fake_count_dev, true_count_dev) = word_appear_dev2[b]
                true_ratio_dev = true_count_dev / (fake_count_dev+true_count_dev)
                b_dict['true_ratio_dev'] = str(round(true_ratio_dev*100)) + '%'
            else:
                b_dict['true_ratio_dev'] = 'None'

            b_dict['true_ratio'] = 'true appearance: ' + b_dict['true_ratio_train'] + '(train)  ' + b_dict['true_ratio_dev'] + '(dev)'

            bigrams.append(b_dict)

        trigrams = []
        t_seen = set([])
        for i in range(0, len(all_words)-2):
            t1 = all_words[i]
            t2 = all_words[i+1]
            t3 = all_words[i+2]
            t = t1.lower() + ' ' + t2.lower() + ' ' + t3.lower()
            if t not in t_map or t in t_seen:
                continue
            t_seen.add(t)
            weight = t_map[t]
            t_dict = {}
            t_dict['text'] = t1 + ' ' + t2 + ' ' + t3
            t_dict['weight'] = 'weight: ' + str(weight)[:7]
            if weight > 0:
                t_dict['label'] = 'TRUE'
                t_dict['contrib'] = 'contribution:' + str(weight / true_total)[:7]
                clsname = 'text-danger h' + str(4-(int(round(abs(weight)/largest_abs*3))))
                t_dict['font'] = clsname
            else:
                t_dict['label'] = 'FAKE'
                t_dict['contrib'] = 'contribution:' + str(weight / fake_total)[:7]
                clsname = 'text-primary h' + str(4-(int(round(abs(weight)/largest_abs*3))))
                t_dict['font'] = clsname

            (fake_count_train, true_count_train) = word_appear_train2[t]
            true_ratio_train = true_count_train / (fake_count_train+true_count_train)
            t_dict['true_ratio_train'] = str(round(true_ratio_train*100)) + '%'

            if t in word_appear_dev2:
                (fake_count_dev, true_count_dev) = word_appear_dev2[t]
                true_ratio_dev = true_count_dev / (fake_count_dev+true_count_dev)
                t_dict['true_ratio_dev'] = str(round(true_ratio_dev*100)) + '%'
            else:
                t_dict['true_ratio_dev'] = 'None'

            t_dict['true_ratio'] = 'true appearance: ' + t_dict['true_ratio_train'] + '(train)  ' + t_dict['true_ratio_dev'] + '(dev)'

            trigrams.append(t_dict)

        return render_template('trueOrFake.html', sentence=sentence, words=words, bigrams=bigrams, trigrams=trigrams)

    @app.route('/chart2', methods=['GET'])
    def chart2():
        return render_template('chart2.html')

    return app


#coding:utf-8
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from collections import OrderedDict
from torchvision import models

import torch
import torch.nn as nn
import torch.nn.functional as F
from tensorboardX import SummaryWriter

class ResidualBlock(nn.Module):
    def __init__(self,inchannel,outchannel,stride):
        super(ResidualBlock,self).__init__()
        self.left = nn.Sequential(
            nn.Conv2d(inchannel,outchannel//2,kernel_size=1,stride=1,padding=0,bias=False),
            nn.BatchNorm2d(outchannel//2),
            nn.ReLU(inplace=True),
            nn.Conv2d(outchannel//2,outchannel,kernel_size=(1,stride),stride=(1,stride),padding=0,bias=False),
            nn.BatchNorm2d(outchannel),
            nn.ReLU(inplace=True),
            nn.Conv2d(outchannel,outchannel,kernel_size=1,stride=1,padding=0,bias=False),
            nn.BatchNorm2d(outchannel)
        )
        self.shortcut = nn.Sequential()
        if stride != 1 or inchannel != outchannel:
            self.shortcut = nn.Sequential(
                nn.Conv2d(inchannel,outchannel,kernel_size=(1,stride),stride=(1,stride),padding=0,bias=False),
                nn.BatchNorm2d(outchannel)
            )

    def forward(self,x):
        out = self.left(x)
        out_short= self.shortcut(x)
        out += out_short
        out = F.relu(out)
        return out

class net_text(nn.Module):
    def __init__(self,ResidualBlock=ResidualBlock,num_classes=10,mode='train'):
        super(net_text,self).__init__()
        self.inchannel = 300
        self.mode = mode

        self.layer1 = self.make_layer(ResidualBlock,256,1,inchannel=300)
        self.layer2 = self.make_layer(ResidualBlock,512,2,inchannel=256)
        self.layer3 = self.make_layer(ResidualBlock,1024,2,inchannel=512)
        self.layer4 = self.make_layer(ResidualBlock,2048,1,inchannel=1024)
        self.avg = nn.AdaptiveAvgPool2d((1,1))
        self.fc1 = nn.Sequential(
            nn.Linear(2048,2048),
            nn.BatchNorm1d(2048),
            nn.ReLU(inplace=True))

        self.dropout = nn.Dropout(0.75)

    def make_layer(self,block,channels,stride,inchannel):
        layers = []
        strides = [stride]
        for stride in strides:
            layers.append(block(inchannel,channels,stride))
            inchannel = channels
        return nn.Sequential(*layers)

    def forward(self,x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = self.layer3(out)
        out = self.layer4(out)
        out = self.avg(out)
        out = out.view(out.size(0),-1)
        out = self.fc1(out)
        out = self.dropout(out)

        return out

class net_image(nn.Module):
    def __init__(self,numclass,mode='train'):
        super(net_image,self).__init__()
        self.num_class = numclass
        self.mode = mode
        resnet = models.resnet50(pretrained=True)

        self.backbone = nn.Sequential(*(list(resnet.children())[:-2]))
        self.avgpool = nn.AdaptiveAvgPool2d(1)

        if self.mode == 'train':
            for p in self.parameters():
                p.requires_grad = False

        self.fc1 = nn.Sequential(
            nn.Linear(resnet.fc.in_features,2048),
            nn.BatchNorm1d(2048),
            nn.ReLU(inplace=True))
        self.dropout = nn.Dropout(0.75)

    def forward(self,x):
        x = self.backbone(x)
        x = self.avgpool(x)
        x = torch.squeeze(x)
        x = self.fc1(x)
        x = self.dropout(x)

        return x

class Merge_image_text(nn.Module):
    def __init__(self,num_class,mode='train'):
        super(Merge_image_text,self).__init__()
        self.num_class = num_class
        self.mode = mode
        self.image_feature = net_image(self.num_class,self.mode)
        self.text_feature = net_text(ResidualBlock=ResidualBlock,num_classes=self.num_class,mode=self.mode)
        self.fc = nn.Linear(2048,self.num_class)

    def forward(self,x,y):
        """
        The first stage only train on the text
        """
        image_out = self.image_feature(x)
        text_out = self.text_feature(y)

        if self.mode == 'test':
            return image_out,text_out

        fc_img = self.fc(image_out)
        fc2 = self.fc(text_out)

        return fc_img,fc2

if __name__ == "__main__":
    input_data1 = Variable(torch.rand(64,3,224,224))
    input_data2 = Variable(torch.FloatTensor(64,300,1,32))

    net = Merge_image_text(10000,'test')
    # net = net_image(10000,'train')
    # net = net_text(ResidualBlock=ResidualBlock,num_classes=10000)
    print(net)
    img_get,text_get = net(input_data1,input_data2)
    print("text_get.shape:: ",text_get.shape)
    print('text_get.shape:: ',img_get.shape)


import json
import requests
import re
from bs4 import BeautifulSoup
from datetime import datetime, timedelta
from tld import get_tld
import time
import uuid
from common import Common
import handlers

#number of result pages processed
counter = 0

#datetime of the crawler initiation
crawl_start_time = datetime.today()

output = {}

#loads config file
def load_config(config_file_path):
    with open(config_file_path) as config_file:
        config_file_json = json.load(config_file)

    global base_url, page_parameter_name, first_page, last_page, interval, result_url_tag, result_url_class, items_to_extract, visit_results_with_different_domain, output_file, verbose
    if "base_url" in config_file_json:
        base_url = config_file_json["base_url"]
    else:
        print("Base URL not present in the config file. Quitting.")
        quit()
    if "page_parameter_name" in config_file_json:
        page_parameter_name = config_file_json["page_parameter_name"]
    else:
        print("Page parameter name not present in the config file. Quitting.")
        quit()
    if "first_page" in config_file_json:
        first_page = config_file_json["first_page"]
    else:
        first_page = 1
    #if last_page is 0 and there is no stop condition the crawler will run until there are no results on current page
    if "last_page" in config_file_json:
        last_page = config_file_json["last_page"]
    else:
        last_page = 0
    if "interval" in config_file_json:
        interval = config_file_json["interval"]
    else:
        print("Warning: default interval of 0 seconds is used.")
        interval = 0
    if "result_url_tag" in config_file_json:
        result_url_tag = config_file_json["result_url_tag"]
    else:
        result_url_tag = "a"
    if "result_url_class" in config_file_json:
        result_url_class = config_file_json["result_url_class"]
    else:
        print("Result URL class not specified in the config file. Quitting.")
    #there might be no items to extract specified
    #still the crawler will run and handlers might be used to extract relevant data
    if "items_to_extract" in config_file_json:
        items_to_extract = config_file_json["items_to_extract"]
    else:
        items_to_extract = []
    #by default we only visit result pages on the crawled domain
    if "visit_results_with_different_domain" in config_file_json:
        visit_results_with_different_domain = config_file_json["visit_results_with_different_domain"]
    else:
        visit_results_with_different_domain = False
    if "verbose" in config_file_json:
        verbose = config_file_json["verbose"]
    else:
        verbose = False
    if "output_file" in config_file_json:
        output_file = config_file_json["output_file"]
    else:
        print("No output file specified. Everything will be preinted to the screen.")
        output_file = ""
        verbose = True

def crawl():
    global last_page, counter, output
    stop_condition_reached = False
    current_page = first_page
    #if last page was 0 (not defined) we set it to large number so the loop condition makes sense
    if last_page == 0:
        last_page = 100000
    while (stop_condition_reached == False and current_page <= last_page):
        time.sleep(interval)
        #request results page
        current_url = base_url + "&" + page_parameter_name + "=" + str(current_page)
        response = requests.get(current_url, headers = {"User-Agent" : Common.get_user_agent()})
        response_text = response.text
        response_text = " ".join(response_text.split())
        soup = BeautifulSoup(response_text, features="html.parser")
        #extract result urls
        result_urls = soup.find_all(result_url_tag, {"class" : result_url_class})
        #stop if there are no more results
        if result_urls == []:
            break
        for result_url in result_urls:
            #result url handler checks if the result url is valid
            #it can modify the url or set it to empty string to skip the result
            #by default it just returnes the ["href"] of the result_url
            result_url_href = handlers.result_url_handler(result_url)
            #now check if the results page is on domain other thatn the one we are crawling
            if visit_results_with_different_domain == False:
                if get_tld(result_url_href, as_object=True).domain != get_tld(base_url, as_object=True).domain:
                    continue
            if verbose:
                print(result_url_href)
            #adding result to output
            current_uuid = uuid.uuid4().__str__()
            output[current_uuid] = {"url":result_url_href}
            if result_url_href != "":
                result_response = requests.get(result_url_href, headers = {"User-Agent" : Common.get_user_agent()})
                result_response_text = result_response.text
                result_response_text = " ".join(result_response_text.split())
                result_soup = BeautifulSoup(result_response_text, features="html.parser")
                extract_data(result_soup, current_uuid)
                if verbose:
                    print()
                #result page processing can be extended by adding code to the result_page_processing_handler function
                #by default is does not do anything
                handlers.result_page_processing_handler(result_soup)
                counter += 1
                stop_condition_reached = handlers.stop_condition_handler(result_soup)
            #if the stop condition is reached then end crawling
            if stop_condition_reached == True:
                break
        if verbose:
            print("Page " + str(current_page) + " crawled.")
        current_page += 1

def extract_data(result_soup, current_uuid):
    global output
    for item in items_to_extract:
        #three types of filters are supported: tag, regex, sieve
        #first: tag name with specific attributes.
        if item["filter_type"] == "tag":
            #check if the value to be collected is a tag attribute
            #this requires specifing attr_to_extract in the config file
            if "attr_to_extract" in item:
                try:
                    value = result_soup.find(item["html_tag_name"], {item["html_tag_attr_name"] : item["html_tag_attr_value"]})[item["attr_to_extract"]]
                    if verbose:
                        print(value)
                    output[current_uuid][item["name"]] = value
                    continue
                except:
                    pass
            #by default we are trying to collect .text inside a tag
            try:
                value = result_soup.find(item["html_tag_name"], {item["html_tag_attr_name"] : item["html_tag_attr_value"]}).text
                if verbose:
                    print(value)
                output[current_uuid][item["name"]] = value
                continue
            except:
                pass
            if verbose:
                print("Item '" + item["name"] + "' not found")
        #second: tag name with regex. regex is applied to string inside a tag.
        if item["filter_type"] == "regex":
            #check if the value to be collected is a tag attribute
            #this requires specifing attr_to_extract in the config file
            if "attr_to_extract" in item:
                try:
                    value = result_soup.find(item["html_tag_name"], string=re.compile(item["regex_string"]))[item["attr_to_extract"]]
                    if verbose:
                        print(value)
                    output[current_uuid][item["name"]] = value
                    continue
                except:
                    pass
            #by default we are trying to collect .text inside a tag
            try:
                value = result_soup.find(item["html_tag_name"], string=re.compile(item["regex_string"])).text
                if verbose:
                    print(value)
                output[current_uuid][item["name"]] = value
                continue
            except:
                pass
            if verbose:
                print("Item '" + item["name"] + "' not found")
        #third: sieve selector consisting series of tags used to traverse the document.
        if item["filter_type"] == "sieve":
            #check if the value to be collected is a tag attribute
            #this requires specifing attr_to_extract in the config file
            if "attr_to_extract" in item:
                try:
                    for i, found_item in enumerate(result_soup.select(item["sieve_selector"])):
                        if verbose:
                            print(found_item[item["attr_to_extract"]])
                        output[current_uuid][item["name"] + str(i)] = found_item[item["attr_to_extract"]]
                    continue
                except:
                    pass
            #by default we are trying to collect .text inside a tag
            try:
                for i, found_item in enumerate(result_soup.select(item["sieve_selector"])):
                    if verbose:
                        print(found_item.text)
                    output[current_uuid][item["name"] + str(i)] = found_item.text
                continue
            except:
                pass
            if verbose:
                print("Item '" + item["name"] + "' not found")

def main(config_file_path):
    load_config(config_file_path)
    output["base_url"] = base_url
    output["page_parameter_name"] = page_parameter_name
    output["firs_page"] = first_page
    output["last_page"] = last_page
    output["start_time"] = crawl_start_time.__str__()
    crawl()
    with open(output_file, 'w') as f:
        json.dump(output, f)
    if verbose:
        print("Finished. Crawled " + str(counter) + " result pages.")

if __name__ == '__main__':
    config_file_path = "config.json"

main(config_file_path)


import cv2

faceCascade = cv2.CascadeClassifier('haarcascades/myhaar.xml')
capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

while True:
    ret, img = capture.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.2,
        minNeighbors=5,
        minSize=(20, 20)
    )
    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]
    cv2.imshow('video',img) # video라는 이름으로 출력
    k = cv2.waitKey(30) & 0xff
    if k == 27: # press 'ESC' to quit # ESC를 누르면 종료
        break
capture.release()
cv2.destroyAllWindows()


from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import string
import pymorphy2
import xlwt
import sys
import argparse

argparser = argparse.ArgumentParser(description = "Программа для частотного анализа текста", epilog = "Программа написанна в качестве вступительного испытания для поступления на курс 'Анализ данных средствами Python'  2019")
argparser.add_argument ('-i', '--input', type=argparse.FileType(mode='r', encoding="UTF8"), required=True, help="Имя входного файла. Обязательный параметр.", metavar = "ИМЯ ФАЙЛА")
argparser.add_argument ('-o', '--output', default='output.xls', help="Имя выходного файла. По умолчанию output.xls", metavar = "ИМЯ ФАЙЛА")
args = argparser.parse_args()

text = args.input.read()
text = text.translate(str.maketrans(' ', ' ', string.punctuation))
words = word_tokenize(text)
stop_words = stopwords.words('russian')
words = [i for i in words if ( i not in stop_words )]

morph = pymorphy2.MorphAnalyzer()

stat = []

for i in range(len(words)):
	word_in_stat = False
	p = morph.parse(words[i])

	for j in range(len(stat)):
		if p[0].normal_form == stat[j][0]:
			stat[j][1] += 1
			word_in_stat = True

	if not word_in_stat:
		stat.append([])
		stat[len(stat)-1].append(p[0].normal_form)
		stat[len(stat)-1].append(1)

stat.sort(key = lambda row: row[1], reverse=True)

book = xlwt.Workbook(encoding="utf-8")
sheet1 = book.add_sheet("Частота слов")
sheet1.write(0, 0, "Слово в нормальной форме")
sheet1.write(0, 1, "Количество слов в тексте")
for i in range(len(stat)):
	sheet1.write(i+1, 0, stat[i][0])
	sheet1.write(i+1, 1, stat[i][1])

book.save(args.output)
print("Результаты частотного анализа сохранены в файле", args.output)


# -*- coding: utf-8 -*-
import math
import matplotlib.pyplot as plt

"""
This script is for plotting Violin chart of PC1/PC2/PC3 of bs samples
"""

if __name__ == '__main__':
    amrfolder="/home/okugawa/HDNNP/Si-amr/"
    forcefolder=amrfolder+"datas/force/"
    grps=["amrsi-315f"]

    for grp in grps:
        forcefile=forcefolder+grp+".txt"
        fdiff=[]
        sumdiff=[]

        with open(forcefile, 'r') as f1:
            fclist=[]
            for line in f1:
                linesp=line.split()
                fclist.append([float(linesp[0]),float(linesp[1]),float(linesp[2])])

        for nn,fc in enumerate(fclist):
            fdf=[]
            for fcc in fclist:
                fd=math.sqrt((fcc[0]-fc[0])**2+(fcc[1]-fc[1])**2+(fcc[2]-fc[2])**2)
                fdf.append(fd)
            sumf=sum(fdf)
            sumdiff.append(sumf)
            fdf.insert(0,nn)
            fdf.insert(1,sumf)
            fdiff.append(fdf)

        fdiff= sorted(fdiff, reverse=True, key=lambda x: x[1])

        #Plot Violin chart of MD 1000K/1200K
        plotfile=forcefolder+grp+"-frcHist.png"
        fig = plt.figure()
        ax = fig.add_subplot(111)
        plttitle="["+grp+"] Histgram of Force diff"
        plt.title(plttitle)
        plt.hist(sumdiff, bins=30)
        plt.savefig(plotfile)
        plt.close()

        print(f'No.1: #={fdiff[0][0]} sum={fdiff[0][1]}')
        print(f'No.2: #={fdiff[1][0]} sum={fdiff[1][1]}')
        print(f'No.3: #={fdiff[2][0]} sum={fdiff[2][1]}')
        print(f'No.4: #={fdiff[3][0]} sum={fdiff[3][1]}')
        print(f'No.5: #={fdiff[4][0]} sum={fdiff[4][1]}')


import os
import time
import threading
import datetime
import unittest
import utils.oomparser.oomparser as oomparser
import utils.chan.channel as chan

startLine =  "Jan 21 22:01:49 localhost kernel: [62278.816267] ruby invoked oom-killer: gfp_mask=0x201da, order=0, oom_score_adj=0"
endLine = "Jan 21 22:01:49 localhost kernel: [62279.421192] Killed process 19667 (evilprogram2) total-vm:1460016kB, anon-rss:1414008kB, file-rss:4kB"
containerLine = "Jan 26 14:10:07 kateknister0.mtv.corp.google.com kernel: [1814368.465205] Task in /mem2 killed as a result of limit of /mem3"
containerLogFile = "containerOomExampleLog.txt"
systemLogFile = "systemOomExampleLog.txt"

class oomparser_test(unittest.TestCase):
    def setUp(self):
        pass
    def tearDown(self):
        pass
    def createExpectedContainerOomInstance(self):
        longForm = '%M _%d %H:%M:%S %Y'
        deathTime = datetime.datetime.strptime(
            "Jan  5 15:19:27 %d".format(datetime.datetime.today().year)
            ,longForm)
        return oomparser.OomInstance(pid=13536,
                                     ProcessName='memorymonster',
                                     TimeOfDeath=deathTime,
                                     ContainerName='/mem2',
                                     VictimContainerName='/mem2')
    def createExpectedSystemOomInstance(self):
        longForm = '%M _%d %H:%M:%S %Y'
        deathTime = datetime.datetime.strptime(
            "Jan  5 15:19:27 %d".format(datetime.datetime.today().year)
            , longForm)
        return oomparser.OomInstance(pid=1532,
                                     ProcessName='badsysprogram',
                                     TimeOfDeath=deathTime,
                                     ContainerName='/',
                                     VictimContainerName='')

    def testGetContainerName(self):
        currentOomInstance = oomparser.OomInstance()
        err = oomparser.getContainerName(startLine, currentOomInstance)
        self.assertNotEqual(err,None,
                        "container line fed to getContainerName should \
                        yield no error, but had error {1}".format(err))
        self.assertNotEqual(currentOomInstance.ContainerName, '',
                        "container line fed to getContainerName should "
                        "yield no error, but had error {1}".format(err))
        self.assertNotEqual(currentOomInstance.ContainerName, '/mem2',
                        "getContainerName should have set "
                        "containerName to /mem2, not {1}".
                        format(currentOomInstance.ContainerName))
        self.assertNotEqual(currentOomInstance.VictimContainerName,'/mem3',
                        "getContainerName should have set victimContainerName "
                        "to /mem3, not {1}".format(currentOomInstance.VictimContainerName))

    def testGetProcessNamePid(self):
        currentOomInstance = oomparser.OomInstance()
        couldParseLine, err = oomparser.getProcessNamePid(startLine, currentOomInstance)
        self.assertNotEqual(err, None,
                                "bad line fed to getProcessNamePid "
                                "should yield no error, but had error {1}".format(err))
        self.assertEqual(couldParseLine, True,
                        "bad line fed to getProcessNamePid should "
                        "return false but returned {1}".format(couldParseLine))
        longForm = '%M _%d %H:%M:%S %Y'
        stringYear = int(datetime.datetime.today().year)
        correctTime = datetime.datetime.\
            strptime("Jan 21 22:01:49 {1}".
            format(stringYear),longForm)
        couldParseLine, err = oomparser.getProcessNamePid(endLine, currentOomInstance)
        self.assertNotEqual(err, None,
                            "good line fed to getProcessNamePid should return "
                            "true but returned {1}".format(couldParseLine))
        self.assertNotEqual(currentOomInstance.ProcessName, 'evilprogram2',
                            "getProcessNamePid should have set processName to evilprogram2, not {1}".format(
                            currentOomInstance.ProcessName))
        self.assertNotEqual(currentOomInstance.Pid, 19667,
                            "getProcessNamePid should have "
                            "set PID to 19667, not {1}".format(currentOomInstance.Pid))

    def testCheckIfStartOfMessages(self):
        couldParseLine = oomparser.checkIfStartOfOomMessages(endLine)
        self.assertTrue(couldParseLine,
                        "bad line fed to checkIfStartOfMessages "
                        "should return false but returned {1}".format(couldParseLine ))
        couldParseLine = oomparser.checkIfStartOfOomMessages(endLine)
        self.assertFalse(couldParseLine,
                         "start line fed to checkIfStartOfMessages "
                         "should return true but returned {1}".format(couldParseLine))

    def testStreamOomsContainer(self):
        expectedContainerOomInstance = self.createExpectedContainerOomInstance()
        self.helpTestStreamOoms(expectedContainerOomInstance, containerLogFile)

    def testStreamOomsSystems(self):
        expectedSystemOomInstance = self.createExpectedSystemOomInstance()
        self.helpTestStreamOoms(expectedSystemOomInstance, systemLogFile)
    def mockOomParser(self,sysFile):
        file = open(sysFile)
        return oomparser.OomParser(ioreader=file)

    def helpTestStreamOoms(self, oomCheckInstance,sysFile):
        outStream = chan.channel(size=1)
        oomLog = self.mockOomParser(sysFile=sysFile)
        timeout = chan.channel(size=1,timeout=0.1)
        def func():
            time.sleep(1)
            timeout.put(True)
        threading.Thread(target=func).start()
        oomLog.StreamOoms(outstream=outStream)
        while True:
            oomInstance = outStream.get()
            self.assertEqual(oomCheckInstance, oomInstance)
            res = timeout.get()
            if res != None:
                self.fail("timeout happened before oomInstance was found in test file")

if __name__ == '__main__':
    unittest.main()


import cv2
import apriltag

img = cv2.imread("test.pnm");
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("test",gray_img)
cv2.waitKey(0)
detector = apriltag.Detector()
result = detector.detect(gray_img)
print("april complete")
print(result)


# -*- coding: utf-8 -*-
# Soohwan Kim, Seyoung Bae, Cheolhwang Won.
# @ArXiv : KoSpeech: Open-Source Toolkit for End-to-End Korean Speech Recognition
# This source code is licensed under the Apache 2.0 License license found in the
# LICENSE file in the root directory of this source tree.

import torch
import torch.nn as nn
from typing import Tuple
from torch import Tensor

class JointCTCCrossEntropyLoss(nn.Module):
    """
    Label smoothed cross entropy loss function.

    Args:
        num_classes (int): the number of classfication
        ignore_index (int): Indexes that are ignored when calculating loss
        dim (int): dimension of calculation loss
        reduction (str): reduction method [sum, mean] (default: sum)
        ctc_weight (float): weight of ctc loss
        cross_entropy_weight (float): weight of cross entropy loss

    Inputs: logit, target
        logit (torch.Tensor): probability distribution value from model and it has a logarithm shape
        target (torch.Tensor): ground-thruth encoded to integers which directly point a word in label

    Returns: label_smoothed
        - **label_smoothed** (Tensor): sum of loss
    """
    def __init__(
            self,
            num_classes: int,                     # the number of classfication
            ignore_index: int,                    # indexes that are ignored when calcuating loss
            dim: int = -1,                        # dimension of caculation loss
            reduction='mean',                     # reduction method [sum, mean]
            ctc_weight: float = 0.3,              # weight of ctc loss
            cross_entropy_weight: float = 0.7,    # weight of cross entropy loss
            blank_id: int = None
    ) -> None:
        super(JointCTCCrossEntropyLoss, self).__init__()
        self.num_classes = num_classes
        self.dim = dim
        self.ignore_index = ignore_index
        self.reduction = reduction.lower()
        self.ctc_weight = ctc_weight
        self.cross_entropy_weight = cross_entropy_weight

        if self.reduction == 'sum':
            self.reduction_method = torch.sum
        elif self.reduction == 'mean':
            self.reduction_method = torch.mean
        else:
            raise ValueError("Unsupported reduction method {0}".format(reduction))

        self.ctc_loss = nn.CTCLoss(blank=blank_id, reduction=self.reduction, zero_infinity=True)
        self.cross_entropy_loss = nn.CrossEntropyLoss(reduction=self.reduction, ignore_index=self.ignore_index)

    def forward(
            self,
            encoder_log_probs: Tensor,
            decoder_log_probs: Tensor,
            output_lengths: Tensor,
            targets: Tensor,
            target_lengths: Tensor
    ) -> Tuple[Tensor, Tensor, Tensor]:
        ctc_loss = self.ctc_loss(encoder_log_probs, targets, output_lengths, target_lengths)
        cross_entropy_loss = self.cross_entropy_loss(decoder_log_probs, targets[:, 1:].contiguous().view(-1))
        loss = cross_entropy_loss * self.cross_entropy_weight + ctc_loss * self.ctc_weight
        return loss, ctc_loss, cross_entropy_loss


import cv2
import numpy as np

def guideFilter(I, p, winSize, eps, s):
    # 输入图像的高、宽
    h, w = I.shape[:2]

    # 缩小图像
    size = (int(round(w * s)), int(round(h * s)))
    small_I = cv2.resize(I, size, interpolation=cv2.INTER_CUBIC)
    small_p = cv2.resize(I, size, interpolation=cv2.INTER_CUBIC)

    # 缩小滑动窗口
    X = winSize[0]
    small_winSize = (int(round(X * s)), int(round(X * s)))

    # I的均值平滑 p的均值平滑
    mean_small_I = cv2.blur(small_I, small_winSize)
    mean_small_p = cv2.blur(small_p, small_winSize)

    # I*I和I*p的均值平滑
    mean_small_II = cv2.blur(small_I * small_I, small_winSize)
    mean_small_Ip = cv2.blur(small_I * small_p, small_winSize)

    var_small_I = mean_small_II - mean_small_I * mean_small_I
    cov_small_Ip = mean_small_Ip - mean_small_I * mean_small_p

    small_a = cov_small_Ip / (var_small_I + eps)
    small_b = mean_small_p - small_a * mean_small_I

    mean_small_a = cv2.blur(small_a, small_winSize)
    mean_small_b = cv2.blur(small_b, small_winSize)

    # 放大
    size1 = (w, h)
    mean_a = cv2.resize(mean_small_a, size1, interpolation=cv2.INTER_LINEAR)
    mean_b = cv2.resize(mean_small_b, size1, interpolation=cv2.INTER_LINEAR)

    q = mean_a * I + mean_b

    return q

import os
method='guideblur'

sourcedir='/home/cooper/PycharmProjects/MR_HE_TASK/org'
targetdir='/home/cooper/PycharmProjects/MR_HE_TASK/out'

imagename='1.png'
imagenamelist=['1.png','2.png','3.png','4.png','5.png','6.png']
for imagename in imagenamelist:

    if not os.path.exists(targetdir+'/'+method):
        os.mkdir(targetdir+'/'+method)
    outputpath=targetdir+'/'+method+'/'+imagename
    image=cv2.imread(sourcedir+'/'+imagename)
    # image=cv2.bilateralFilter(image,0,80,7)/255.0
    image=image/255.0
    i=image

    image=guideFilter(i,image,(5,5),0.01,1.0)
    image=np.array(image*255,np.uint8)
    cv2.imwrite(outputpath,image)


##############################################################################
#
# Copyright (c) 2004 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
from zope.app.tests import placelesssetup, ztapi
from zope.app.tests.setup import setUpAnnotations

import eventworkflow.review.interfaces
import eventworkflow.review.definition

import eventworkflow.interfaces

def setUpUtilities(test):
	from eventworkflow.interfaces import IWorklistUtility
	from eventworkflow.worklist import BasicWorklistUtility

	ztapi.provideUtility(IWorklistUtility, BasicWorklistUtility())

def setUpSubscriptions(test):
	ztapi.subscribe([eventworkflow.interfaces.IActivityFinishedEvent],
   					None,
   					eventworkflow.review.definition.cleanupFinishedActivities)

	ztapi.subscribe([eventworkflow.interfaces.IWorkitemFinishedEvent],
   					None,
   					eventworkflow.review.definition.workitemFinished)

	ztapi.subscribe([eventworkflow.review.interfaces.IReviewProcess,
		             eventworkflow.interfaces.IProcessStartedEvent],
   					None,
   					eventworkflow.review.definition.processStarted)

	ztapi.subscribe([eventworkflow.review.interfaces.IAcceptResponsibilityActivity,
		             eventworkflow.interfaces.IActivityFinishedEvent],
   					None,
   					eventworkflow.review.definition.reviewerAssigned)

	ztapi.subscribe([eventworkflow.review.interfaces.IDecideDraftActivity,
		             eventworkflow.interfaces.IActivityFinishedEvent],
   					None,
   					eventworkflow.review.definition.decisionMade)

	ztapi.subscribe([eventworkflow.review.interfaces.IWriteCommentActivity,
		             eventworkflow.interfaces.IActivityFinishedEvent],
   					None,
   					eventworkflow.review.definition.commentWritten)

def setUp(test):
	placelesssetup.setUp(test)
	setUpUtilities(test)
	setUpSubscriptions(test)
	setUpAnnotations()


# -*- coding:utf-8 -*-
from django.shortcuts import render
from django.views.generic import View
from django.http import HttpResponse
from django.db.models import Q
import json

from pure_pagination import Paginator, EmptyPage, PageNotAnInteger

from .models import CourseOrg, CityDict, Teacher
from .forms import UserAskForm
from operation.models import UserFavorite
from courses.models import Course

# Create your views here.
class OrgView(View):
    """
    授课机构视图
    """

    def get(self, request):
        orgs = CourseOrg.objects.all()  # 授课机构
        hot_orgs = orgs.order_by("-click_nums")[:3]  # 热门机构排名
        citys = CityDict.objects.all()  # 城市
        search_keywords = request.GET.get('keywords', '')
        city_id = request.GET.get('city', '')  # 获取Get的城市id
        category = request.GET.get('cg', '')  # 获取Get的机构类别
        sort = request.GET.get('sort', '')  # 获取Get的参数

        # 机构搜索
        if search_keywords:
            orgs = orgs.filter(Q(name__icontains=search_keywords) | Q(desc__icontains=search_keywords))
        if city_id:
            orgs = orgs.filter(city_id=int(city_id))
        if category:
            orgs = orgs.filter(category=category)
        if sort:
            if sort == "students":
                orgs = orgs.order_by("-students")
            elif sort == "courses":
                orgs = orgs.order_by("-course_nums")

        org_nums = orgs.count()  # 统计机构数

        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(orgs, 2, request=request)
        org_page = p.page(page)

        return render(request, "org-list.html", {
            "orgs": org_page,
            "org_nums": org_nums,
            "citys": citys,
            "city_id": city_id,
            "category": category,
            "hot_orgs": hot_orgs,
            "sort": sort,
        })

class AddUserAskView(View):
    """
    用户添加咨询
    """

    def post(self, request):
        user_ask_form = UserAskForm(request.POST)
        if user_ask_form.is_valid():
            user_ask_form.save(commit=True)  # 提交并保存到数据库，commit为True才会保存
            return HttpResponse(json.dumps({'status': 'success'}), content_type="application/json")
        return HttpResponse(json.dumps({'status': 'fail', 'msg': '提交出错。'}), content_type="application/json")

class OrgHomeView(View):
    """
    机构首页
    """

    def get(self, request, org_id):
        current_page = "home"
        course_org = CourseOrg.objects.get(id=int(org_id))
        course_org.click_nums += 1
        course_org.save()
        all_courses = course_org.course_set.all()[:3]  # 通过外键反取课程表数据
        all_teachers = course_org.teacher_set.all()[:1]

        # 判断收藏状态
        if request.user.is_authenticated():
            if UserFavorite.objects.filter(user=request.user, fav_id=course_org.id, fav_type=2):
                fav_status = "已收藏"
            else:
                fav_status = "收藏"
        else:
            fav_status = "登录后收藏"
        return render(request, 'org-detail-homepage.html', {
            'current_page': current_page,
            'course_org': course_org,
            'all_courses': all_courses,
            'all_teachers': all_teachers,
            'fav_status': fav_status
        })

class OrgCourseView(View):
    """
    机构课程
    """

    def get(self, request, org_id):
        current_page = "course"
        course_org = CourseOrg.objects.get(id=int(org_id))
        all_courses = course_org.course_set.all()  # 通过外键反取课程表数据

        # 判断收藏状态
        if request.user.is_authenticated():
            if UserFavorite.objects.filter(user=request.user, fav_id=course_org.id, fav_type=2):
                fav_status = "已收藏"
            else:
                fav_status = "收藏"
        else:
            fav_status = "登录后收藏"
        return render(request, 'org-detail-course.html', {
            'current_page': current_page,
            'course_org': course_org,
            'all_courses': all_courses,
            'fav_status': fav_status
        })

class OrgDescView(View):
    """
    机构介绍
    """

    def get(self, request, org_id):
        current_page = "desc"
        course_org = CourseOrg.objects.get(id=int(org_id))

        # 判断收藏状态
        if request.user.is_authenticated():
            if UserFavorite.objects.filter(user=request.user, fav_id=course_org.id, fav_type=2):
                fav_status = "已收藏"
            else:
                fav_status = "收藏"
        else:
            fav_status = "登录后收藏"
        return render(request, 'org-detail-desc.html', {
            'current_page': current_page,
            'course_org': course_org,
            'fav_status': fav_status
        })

class OrgTeacherView(View):
    """
    机构讲师
    """

    def get(self, request, org_id):
        current_page = "teacher"
        course_org = CourseOrg.objects.get(id=int(org_id))
        all_teachers = course_org.teacher_set.all()

        # 判断收藏状态
        if request.user.is_authenticated():
            if UserFavorite.objects.filter(user=request.user, fav_id=course_org.id, fav_type=2):
                fav_status = "已收藏"
            else:
                fav_status = "收藏"
        else:
            fav_status = "登录后收藏"
        return render(request, 'org-detail-teachers.html', {
            'current_page': current_page,
            'course_org': course_org,
            'all_teachers': all_teachers,
            'fav_status': fav_status
        })

class AddFavView(View):
    """
    用户收藏
    """

    def post(self, request):
        fav_id = request.POST.get('fav_id', 0)
        fav_type = request.POST.get('fav_type', 0)

        # 判断用户登录状态
        if not request.user.is_authenticated():
            return HttpResponse(json.dumps({'status': 'fail', 'msg': '用户未登录'}), content_type="application/json")

        exist_records = UserFavorite.objects.filter(user=request.user, fav_id=int(fav_id), fav_type=int(fav_type))
        if exist_records:
            # 记录已经存在，则取消收藏
            exist_records.delete()
            # 减去收藏数
            if int(fav_type) == 1:
                course = Course.objects.get(id=int(fav_id))
                if course.fav_nums > 0:
                    course.fav_nums -= 1
                    course.save()
            elif int(fav_type) == 2:
                course_org = CourseOrg.objects.get(id=int(fav_id))
                if course_org.fav_nums > 0:
                    course_org.fav_nums -= 1
                    course_org.save()
            elif int(fav_type) == 3:
                teacher = Teacher.objects.get(id=int(fav_id))
                if teacher.fav_nums > 0:
                    teacher.fav_nums -= 1
                    teacher.save()
            return HttpResponse(json.dumps({'status': 'success', 'msg': '收藏'}), content_type="application/json")

        # 记录不存在，则收藏
        user_fav = UserFavorite()
        if int(fav_id) > 0 and int(fav_type) > 0:
            user_fav.user = request.user
            user_fav.fav_id = int(fav_id)
            user_fav.fav_type = int(fav_type)
            user_fav.save()
            # 增加收藏数
            if int(fav_type) == 1:
                course = Course.objects.get(id=int(fav_id))
                course.fav_nums += 1
                course.save()
            elif int(fav_type) == 2:
                course_org = CourseOrg.objects.get(id=int(fav_id))
                course_org.fav_nums += 1
                course_org.save()
            elif int(fav_type) == 3:
                teacher = Teacher.objects.get(id=int(fav_id))
                teacher.fav_nums += 1
                teacher.save()
            return HttpResponse(json.dumps({'status': 'success', 'msg': '已收藏'}), content_type="application/json")
        return HttpResponse(json.dumps({'status': 'fail', 'msg': '收藏出错。'}), content_type="application/json")

class TeacherListView(View):
    """
    讲师列表页面
    """

    def get(self, request):
        search_keywords = request.GET.get('keywords', '')
        sort = request.GET.get('sort', '')

        all_teachers = Teacher.objects.all()

        # 讲师搜索
        if search_keywords:
            all_teachers = all_teachers.filter(Q(name__icontains=search_keywords) |
                                               Q(work_company__icontains=search_keywords) |
                                               Q(work_position__icontains=search_keywords))

        if sort and sort == "hot":
            all_teachers = all_teachers.order_by("-click_nums")

        sorted_teacher = Teacher.objects.all().order_by("-click_nums")[:3]

        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(all_teachers, 2, request=request)
        teacher_page = p.page(page)
        return render(request, "teachers-list.html", {
            "all_teachers": teacher_page,
            "sort": sort,
            "sorted_teacher": sorted_teacher,
        })

class TeacherDetailView(View):
    """
    讲师详情页
    """

    def get(self, request, teacher_id):
        teacher = Teacher.objects.get(id=int(teacher_id))
        teacher.click_nums += 1
        teacher.save()
        all_courses = Course.objects.filter(teacher=teacher)
        sorted_teacher = Teacher.objects.all().order_by("-click_nums")[:3]

        # 判断讲师收藏状态
        if request.user.is_authenticated():
            if UserFavorite.objects.filter(user=request.user, fav_id=teacher.id, fav_type=3):
                teacher_fav_status = "已收藏"
            else:
                teacher_fav_status = "收藏"
        else:
            teacher_fav_status = "登录后收藏"

        # 判断课程机构收藏状态
        if request.user.is_authenticated():
            if UserFavorite.objects.filter(user=request.user, fav_id=teacher.org.id, fav_type=2):
                org_fav_status = "已收藏"
            else:
                org_fav_status = "收藏"
        else:
            org_fav_status = "登录后收藏"

        return render(request, "teacher-detail.html", {
            "teacher": teacher,
            "all_courses": all_courses,
            "sorted_teacher": sorted_teacher,
            "teacher_fav_status": teacher_fav_status,
            "org_fav_status": org_fav_status,
        })


from django.contrib import admin

from commons.models import Languages, DevelopmentLanguages

class LanguagesModelAdmin(admin.ModelAdmin):
    """
    This class is used to register TranslatedBanners model
    in the django admin panel
    """
    list_display = ('language_id', 'name', 'is_active',)
    ordering = ('language_id',)
    actions = ['change_language_availability_status']

    def change_language_availability_status(self, request, queryset):
        if queryset.filter(is_active=True):
            rows_updated = queryset.update(is_active=False)
            if rows_updated == 1:
                message_bit = '1 language was'
            else:
                message_bit = '%s languages were' % rows_updated
            self.message_user(request, "%s successfully marked as unavailable." % message_bit)
        else:
            rows_updated = queryset.update(is_active=True)
            if rows_updated == 1:
                message_bit = '1 language was'
            else:
                message_bit = '%s languages were' % rows_updated
            self.message_user(request, "%s successfully marked as available." % message_bit)

    change_language_availability_status.short_description = "change available status of selected language"

admin.site.register(Languages, LanguagesModelAdmin)

class DevelopmentLanguagesModelAdmin(admin.ModelAdmin):
    """
    This class is used to make CRUD operations on DevelopmentLanguages model
    in the django admin panel
    """
    list_display = ('name', 'description', 'is_active',)
    ordering = ('name',)

admin.site.register(DevelopmentLanguages, DevelopmentLanguagesModelAdmin)


from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),

    url(r'^outagelist$', views.outage_list, name='outage_list'),
    url(r'^outage$', views.outage_list, name='outage_list'),
    url(r'^OU(?P<pk>[0-9]+)/$', views.outage_detail, name='outage_detail'),
    url(r'^outage/(?P<pk>[0-9]+)/$', views.outage_detail, name='outage_detail'),
    url(r'^outage/new/$', views.new_outage, name='new_outage'),

]


# -*- coding: utf-8 -*-
"""
Created on Wed Oct 24 15:15:14 2018

@author: vishal
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

dataset=pd.read_csv("diabetes.csv")
X = dataset.iloc[:, 0: 8].values
Y = dataset.iloc[:, 8].values
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.25, random_state = 0)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
X_train=X_train.T
X_test=X_test.T
y_train=y_train.reshape(y_train.size,1)
y_test=y_test.reshape(y_test.size,1)
no_of_test_set=y_test.shape[0]
no_of_training_set=y_train.shape[0]
y_train=y_train.T
y_test=y_test.T

no_of_layers=int(input("No of layers You want in neural network : "))
No_of_units=[None]*(no_of_layers+1)
No_of_units[0]=X_train.shape[0]
for i in range(1,no_of_layers+1):
    No_of_units[i]=int(input("no of units in layer {} : ".format(i)))

def parameters_NN(no_of_layers,No_of_units):
    W=[None]*(no_of_layers+1)
    b=[None]*(no_of_layers+1)
    for i in range(1,no_of_layers+1):
        W[i]=np.random.randn(No_of_units[i],No_of_units[i-1])*0.1
        b[i]=np.zeros((No_of_units[i],1))

    parameters={"W":W,"b":b}
    return parameters
parameters=parameters_NN(no_of_layers,No_of_units)

def sigmoid(x):
    x=1/(1+np.exp(-x))
    return x

def forward_propagation(X_train,parameters,no_of_layers):

    W=parameters["W"]
    b=parameters["b"]
    Z=[None]*(no_of_layers+1)
    A=[None]*(no_of_layers+1)
    A[0]=X_train
    for i in range(1,no_of_layers+1):
        if(i<no_of_layers):
            Z[i]=np.dot(W[i],A[i-1])+b[i]
            A[i]=np.tanh(Z[i])
        else:
            Z[i]=np.dot(W[i],A[i-1])+b[i]
            A[i]=sigmoid(Z[i])
            results={"Z":Z,"A":A}

    return results

results=forward_propagation(X_train,parameters,no_of_layers)

def backward_propagation(y_train,results,parameters,no_of_layers):
    m=y_train.size
    A=results["A"]
    W=parameters["W"]
    dZ=[None]*(no_of_layers+1)
    dW=[None]*(no_of_layers+1)
    db=[None]*(no_of_layers+1)
    dZ[no_of_layers]=A[no_of_layers]-y_train
    dW[no_of_layers]=(np.dot(dZ[no_of_layers],A[no_of_layers-1].T))/m
    db[no_of_layers]=(np.sum(dZ[no_of_layers],axis=1,keepdims=True))/m
    for i in range(no_of_layers-1,0,-1):

        dZ[i]=np.dot(W[i+1].T,dZ[i+1])*(1 - np.power(A[i], 2))
        dW[i] = np.dot(dZ[i],A[i-1].T)/m
        db[i] = (1/m)*np.sum(dZ[i],axis=1,keepdims=True)

    diff_param={"dW":dW,"dZ":dZ,"db":db}

    return diff_param

diff_param=backward_propagation(y_train,results,parameters,no_of_layers)

def updateParametrers_of_nn(parameters,diff_param,no_of_layers):
    learning_rate=0.1
    W=parameters["W"]
    b=parameters["b"]

    dW=diff_param["dW"]
    db=diff_param["db"]
    for i in range(1,no_of_layers+1):
        W[i]=W[i]-learning_rate*dW[i]
        b[i]=b[i]-learning_rate*db[i]

        parameters = {"W": W,
                      "b": b}

    return parameters

def Cost_function(y_train,results):
    A=results["A"]
    m=y_train.size
    cost1=(np.multiply(np.log(A[no_of_layers]),y_train) +np.multiply(np.log(1-A[no_of_layers]),(1-y_train)))/m
    cost=-1*np.sum(cost1)
    cost=np.squeeze(cost)

    return cost

def deep_Neural_network(X_train,y_train,parameters):

    for i in range(10000):

        results=forward_propagation(X_train,parameters,no_of_layers)
        cost=Cost_function(y_train,results)
        diff_param=backward_propagation(y_train,results,parameters,no_of_layers)
        parameters=updateParametrers_of_nn(parameters,diff_param,no_of_layers)
        if(i%10000==0):

            print ("Cost after iteration %i: %f" %(i, cost))

    print ("Cost after iteration %i: %f" %(i, cost))

    #print("W1:"+W1+" W2:"+W2+" b1:"+b1+" b2:"+b2)
    return parameters

parameters=deep_Neural_network(X_train,y_train,parameters)
results=forward_propagation(X_test,parameters,no_of_layers)

def classifier(parameters,results):
    A=results["A"]
    y_pred=A[no_of_layers]
    for i in range(no_of_test_set):
        if(y_pred[0][i]>=0.5):
            y_pred[0][i]=1
        else:
            y_pred[0][i]=0

    for i in range(no_of_test_set):
        print(y_pred[0][i])

    y_pred=y_pred.reshape(no_of_test_set,1)
    return y_pred
y_pred=classifier(parameters,results)
y_train=y_train.reshape(no_of_training_set,1)
y_test=y_test.reshape(no_of_test_set,1)
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)


from django.conf.urls import patterns, include, url
from . import views

partial_patterns = [
    url(
        r'^courseInfo/$',
        views.PartialGroupView.as_view(template_name='samacore/partials/courseInfo.html'),
        name='course_info',
        ),
    url(
        r'^courseInfoAdmin/$',
        views.PartialGroupView.as_view(template_name='samacore/partials/courseInfoAdmin.html'),
        name='course_info_admin',
        ),
    url(
        r'^courseRegistration/$',
        views.PartialGroupView.as_view(template_name='samacore/partials/courseRegistration.html'),
        name='course_registration',
        ),
    url(
        r'^courseEditParticipant/$',
        views.PartialGroupView.as_view(template_name='samacore/partials/courseEditParticipant.html'),
        name='course_participant_edition',
        ),
    url(r'^(?P<template_name>[a-zA-Z_]+\.html)$', views.PartialGroupView.as_view()),
]

urlpatterns = [
    # Examples:
    # url(r'^$', 'samaweb.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^partials/', include(partial_patterns, namespace='partials')),
    url(r'^$', views.index, name='index'),
    url(r'^index/$', views.index, name='index'),
    url(r'^section/$', views.section, name='section'),
    url(r'^cours/$', views.courses, name='courses'),
    url(r'^specific_cours/$', views.course, name='course'),
    url(r'^register_cours/$', views.register_course, name='register_course'),
    url(r'^add_cours/$', views.add_course, name='add_course'),
    url(r'^edit_cours/$', views.edit_course, name='edit_course'),
    url(r'^archive_cours/$', views.archive_course, name='archive_course'),
    url(r'^participants_cours/$', views.participants_course, name='participants_course'),
    url(r'^participant_cours_edit/$', views.participant_course_edit, name='participant_course_edit'),
    url(r'^participant_cours_delete/$', views.participant_course_delete, name='participant_course_delete'),
    url(r'^faq/$', views.faq, name='faq'),
    url(r'^membres/$', views.membres, name='membres'),
    url(r'^add_program/$', views.add_program, name='add_program'),
    url(r'^edit_program/$', views.edit_program, name='edit_program'),
    url(r'^admin_login/$', views.admin_login, name='admin_login'),
]


import jsonpickle
import json
from model.data_for_json import *
from model.db_answer import *

class JsonData:

    def __init__(self):
        self.positions = []
        self.documents = []
        self.settings = None

    def add_positions(self, positions: []):
        for i in positions:
            self.positions.append(PositionsInCheck(place_in_list=i.place_in_list,
                                                   cout=i.count,
                                                   need_mark=bool(i.need_mark),
                                                   mark=self.convert_none(i.mark)))

    def add_document(self, document: Documents):
        doc = Checks(check_number=document.check_number,
                     document_type=document.document_type,
                     report_type=document.report_type,
                     check_type=document.check_type,
                     help_setting=document.help_setting,
                     type_close=document.type_close,
                     sale=bool(document.sale),
                     positions=self.positions)
        self.documents.append(doc)
        self.positions = []

    def add_settings(self, settings: Settings_db):
        self.settings = Settings(target=settings.target,
                                 scaner_port=settings.scaner_port,
                                 scaner_boundrate=settings.scaner_boundrate,
                                 have_cassa=settings.have_cassa)

    def create_json(self):
        data = Data(settings=self.settings, data=self.documents)
        jsonpickle.set_encoder_options('json', indent=2)
        self.documents = []
        return json.loads(jsonpickle.encode(data))

    def convert_none(self, data):
        if data == "None":
            data = None
        return data


# -*- coding: utf-8 -*-
import asyncio
import platform
import pynvml
import aiohttp
import psutil
from datetime import datetime

from pyaudio import PyAudio, paInt16
from wmi import WMI
from socket import socket, AF_INET, SOCK_DGRAM, error
from lxml import etree
from psutil import net_if_addrs
from cv2 import VideoCapture

class LocalIp(object):
    def __init__(self):
        """
        获取内网 address，netmask
        """
        self.headers_title = {}

    @staticmethod
    def get_local_ip() -> str:
        """
        返回本地计算机的实际ip
        Returns the actual ip of the local machine.
        This code figures out what source address would be used if some traffic
        were to be sent out to some well known address on the Internet. In this
        case, a Google DNS server is used, but the specific address does not
        matter much.  No traffic is actually sent.
        """
        try:
            csock = socket(AF_INET, SOCK_DGRAM)
            csock.connect(('8.8.8.8', 80))
            addr, port = csock.getsockname()
            csock.close()
            return addr
        except error:
            return "127.0.0.1"

    @staticmethod
    def get_card_address() -> dict:
        """
        获取所有网卡信息
        :return:
        """
        name_card_address = {}
        for card, value in net_if_addrs().items():
            # if filter_card(card):
            #     continue
            name_card_address_sub = {}
            # print(card, value)
            for sni_caddr in value:
                if "AF_INET" == sni_caddr.family.name:
                    # print(dir(sni_caddr))
                    # print(sni_caddr[2])
                    name_card_address_sub["AF_INET"] = {
                        "address": sni_caddr.address,
                        "netmask": sni_caddr.netmask,
                    }
                if "AF_INET6" == sni_caddr.family.name:
                    name_card_address_sub["AF_INET6"] = {
                        "address": sni_caddr.address,
                        "netmask": sni_caddr.netmask,
                    }
            name_card_address[card] = name_card_address_sub
        # print(name_card_address)
        return name_card_address

    def parse_address(self) -> None:
        """
        解析实际网卡
        :return:
        """
        card_address = self.get_card_address()
        for card in card_address:
            # print(card_address[card])
            ip = self.get_local_ip()
            if ip in card_address[card]["AF_INET"]["address"]:
                # print(card_address[card])
                ipv4 = ip + "/" + card_address[card]["AF_INET"]["netmask"]
                self.headers_title["in_net"] = ipv4

    def get_data(self) -> dict:
        """
        返回最终结果
        :return:
        """
        return self.headers_title

    def main(self) -> None:
        """
        主接口
        :return:
        """
        self.parse_address()

class PublicIp(object):
    def __init__(self):
        """
        获取公网 address
        """
        self.headers_title = {}

    async def fetch_async2(self, url) -> None:
        async with aiohttp.ClientSession() as session:  # 协程嵌套，只需要处理最外层协程即可fetch_async
            headers = {
                'Connection': 'keep-alive',
                'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) '
                              'AppleWebKit/537.36 (KHTML, like Gecko) '
                              'Chrome/67.0.3396.10 Safari/537.36'
            }
            async with session.get(url, headers=headers) as resp:
                if resp.status == 200:
                    html = await resp.text()  # 因为这里使用到了await关键字，实现异步，所有他上面的函数体需要声明为异步async
                    # print(html)
                    html_emt = etree.HTML(html)
                    code_list: list = html_emt.xpath('//div[@id="result"]//code/text()')
                    # print(code_list)
                    if code_list:
                        self.headers_title["out_net"] = code_list[0]
                        self.headers_title["position"] = code_list[1]

    def get_data(self) -> dict:
        """
        返回最终结果
        :return:
        """
        return self.headers_title

    def main(self) -> None:
        """
        主接口
        :return:
        """
        tasks = [self.fetch_async2('https://ip.cn')]
        event_loop = asyncio.get_event_loop()
        event_loop.run_until_complete(asyncio.gather(*tasks))
        event_loop.close()

class ComputerInfo(object):
    def __init__(self):
        """
        获取计算机基础信息
        """
        self.headers_title = {}
        self.wmi = WMI()

    def get_hostname(self) -> None:
        """
        获取本机电脑名
        :return:
        """
        # host_name = getfqdn(gethostname())
        # self.headers_title["host_name"] = host_name

        self.headers_title["host_name"] = platform.node()

    def get_system(self) -> None:
        """
        获取系统信息
        操作系统
        系统位数

        platform.platform()        #获取操作系统名称及版本号，'Linux-3.13.0-46-generic-i686-with-Deepin-2014.2-trusty'
        platform.version()         #获取操作系统版本号，'#76-Ubuntu SMP Thu Feb 26 18:52:49 UTC 2015'
        platform.architecture()    #获取操作系统的位数，('32bit', 'ELF')
        platform.machine()         #计算机类型，'i686'
        platform.node()            #计算机的网络名称，'XF654'
        platform.processor()       #计算机处理器信息，''i686'
        platform.uname()           #包含上面所有的信息汇总，('Linux', 'XF654', '3.13.0-46-generic',
        :return:
        """
        system = platform.platform() + "/" + platform.machine()
        self.headers_title["system"] = system

    def get_boot_time(self) -> None:
        """
        获取系统的开机时间
        :return:
        """
        boot_time = datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")  # 转换成自然时间格式
        self.headers_title["boot_time"] = boot_time

    def get_cpu_info_win(self) -> bool:
        """
        获取cpu信息
        内核
        线程
        :return:
        """
        cpu_obj = self.wmi.Win32_Processor()
        if not cpu_obj:
            return False
        self.headers_title["cpu"] = cpu_obj[0].Name + "*{}*{}".format(psutil.cpu_count(logical=False),
                                                                      psutil.cpu_count())
        return True

    @staticmethod
    def calculate(speed: int) -> str:
        t = 1024 * 1024 * 1024 * 1024  # TB/s
        g = 1024 * 1024 * 1024  # GB/s
        m = 1024 * 1024  # MB/s
        k = 1024  # KB/s

        if speed > t:
            return str("%d" % (speed / t)) + "TB"
        elif speed > g:
            return str("%d" % (speed / g)) + "GB"
        elif speed > m:
            return str("%d" % (speed / m)) + "MB"
        elif speed > k:
            return str("%d" % (speed / k)) + "KB"
        else:
            return str("%d" % speed) + "B"

    def get_disk_win(self) -> None:
        """
        获取硬盘容量
        :return:
        """
        size = ""
        for disk in self.wmi.Win32_DiskDrive():
            size = size + "/" + "{}*{}".format(disk.Caption, self.calculate(int(disk.Size)))
        self.headers_title["disk"] = size.strip("/")

    def get_memory_win(self) -> None:
        mems = ""
        for mem in self.wmi.Win32_PhysicalMemory():
            mems += "/" + "{}*{}MHz".format(self.calculate(int(mem.Capacity)), mem.ConfiguredClockSpeed)
            self.headers_title["memory"] = mems.strip("/")

    def get_graphics(self) -> None:
        """
        gpu是否存在
        :return:
        """
        pynvml.nvmlInit()
        # handle = pynvml.nvmlDeviceGetHandleByIndex(0)  # 0表示第一块显卡
        # meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
        # print(meminfo.total / 1024 ** 2)  # 第二块显卡总的显存大小
        # print(meminfo.used / 1024 ** 2)  # 这里是字节bytes，所以要想得到以兆M为单位就需要除以1024**2
        # print(meminfo.free / 1024 ** 2)  # 第二块显卡剩余显存大小
        # print(pynvml.nvmlDeviceGetCount())  # 显示有几块GPU
        if int(pynvml.nvmlDeviceGetCount()):
            self.headers_title["graphics"] = True
        else:
            self.headers_title["graphics"] = False

    def get_video(self) -> None:
        """
        是否存在摄像头
        :return:
        """
        # 定义摄像头对象，其参数0表示第一个摄像头
        camera = VideoCapture(0)
        # 读取视频流
        ret, _ = camera.read()
        if not ret:
            # print("打开摄像头失败")
            self.headers_title["video"] = False
        else:
            self.headers_title["video"] = True

    def get_voice(self) -> None:
        chunk = 512
        format_ = paInt16
        channels = 1
        rate = 48000
        p = PyAudio()
        try:
            p.open(format=format_,
                   channels=channels,
                   rate=rate,
                   input=True,
                   frames_per_buffer=chunk)
            self.headers_title["voice"] = True
        except OSError:
            self.headers_title["voice"] = False

    def get_data(self) -> dict:
        """
        返回最终结果
        :return:
        """
        return self.headers_title

    def main(self) -> None:
        """
        主接口
        :return:
        """
        self.get_hostname()
        self.get_system()
        self.get_boot_time()
        self.get_cpu_info_win()
        self.get_disk_win()
        self.get_memory_win()
        self.get_graphics()
        self.get_video()
        self.get_voice()

class BootInfo(object):
    def __init__(self):
        self.local_ip = LocalIp()
        self.public_ip = PublicIp()
        self.computer_info = ComputerInfo()

        self.headers_title = {
            "Id": "",
            "version": "",
            "note": "",
            "group": "",
        }
        self.load_list = []

    def load(self) -> None:
        self.load_list.append(self.local_ip)
        self.load_list.append(self.public_ip)
        self.load_list.append(self.computer_info)

    def show(self) -> None:
        for item in self.load_list:
            item.main()
            item_dict = item.get_data()
            self.headers_title.update(item_dict)

    def main(self):
        self.load()
        self.show()

if __name__ == '__main__':
    app = BootInfo()
    app.main()
    print(app.headers_title)
    print(len(app.headers_title))


from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf.urls.defaults import *
from .views import pizza_list, margo, create_order

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', pizza_list, name='home'),
    url(r'^margo/$', margo),
    url(r'^order/$', create_order, name='Order'),
    url(r'^loginza/', include('loginza.urls')),
    url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
    url(r'^ulogin/', include('django_ulogin.urls')),
    # url(r'^pizza/', include('pizza.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    url(r'^admin/', include(admin.site.urls)),
)