import re

from django.utils.cache import add_never_cache_headers

class NoCacheMiddleware(object):
    """Add No-cache headers to all responses if detect IE UA"""

    IE_DETECTION_RE = [
        re.compile('Trident/.*rv:(\d+\.\d*)'),
        re.compile('MSIE')
    ]

    @staticmethod
    def process_response(request, response):

        if 'HTTP_USER_AGENT' not in request.META:
            return response

        user_agent = request.META['HTTP_USER_AGENT']
        is_ie = False

        for expr in NoCacheMiddleware.IE_DETECTION_RE:
            if expr.search(user_agent):
                is_ie = True
                break

        if is_ie:
            add_never_cache_headers(response)

        return response


"""
This is the simplified version of the `py_arduino.py` script.
It reads serial data from arduino and prints it to console.
The arduino has to be connected to the computer via USB.
"""

import argparse
import time

import serial

def main():
    """reads serial data from arduino and prints it to console"""

    ap = argparse.ArgumentParser()
    ap.add_argument(
        "-c", "--commport", required=True, help="Arduino's communication port"
    )
    args = ap.parse_args()
    commport = args.commport

    ser = serial.Serial(commport, 9600, timeout=10)

    while True:
        if ser.in_waiting:
            line = ser.readline().decode("utf-8").rstrip()
            print(line)
        time.sleep(1)

if __name__ == "__main__":
    main()


from concurrent import futures
import time
import grpc
import os
import prometheus_client

import recognizer_pb2_grpc as grpc_proto
import recognizer_pb2 as proto
import recognizer
import metrics

class Servicer(grpc_proto.FaceRecognizerServicer):
    RECOGNIZE_HISTOGRAM = metrics.request_latency_histogram.labels(
        'recognize_faces')

    @RECOGNIZE_HISTOGRAM.time()
    def RecognizeFaces(self, request, context):

        faces = recognizer.recognize(request.url)
        proto_faces = []
        for face in faces:
            area = face['area']
            encoding = face['encoding']
            proto_face = proto.Face(x=area['x'], y=area['y'], width=area['width'],
                                    height=area['height'], encoding=encoding)
            proto_faces.append(proto_face)

        metrics.request_counter.labels('recognize_faces').inc()

        return proto.RegognizeResponse(faces=proto_faces)

def serve():

    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

    grpc_proto.add_FaceRecognizerServicer_to_server(
        Servicer(),
        server
    )

    server.add_insecure_port('[::]:' + os.environ['GRPC_PORT'])

    server.start()

    try:
        while True:
            time.sleep(60 * 60 * 24)
    except KeyboardInterrupt:
        server.stop(0)

if __name__ == '__main__':
    prometheus_client.start_http_server(int(os.environ['METRICS_PORT']))
    serve()


import cv2 as cv
import argparse
import matplotlib.pyplot as plt
import math
import numpy as np
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'

from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer, ColorMode
from detectron2.data import MetadataCatalog
from detectron2 import model_zoo
from detectron2.projects.deeplab import add_deeplab_config

import warnings
warnings.filterwarnings('ignore')
import torch
import imutils

parser = argparse.ArgumentParser('run video')
parser.add_argument('-v', '--video_dir', type = str, help ='video directory')
parser.add_argument('-yaml', '--config_yaml', type = str, help ='config yaml file')
args = parser.parse_args()

def run_video(video_dir, config_yaml):

    cfg = get_cfg()
    cfg.merge_from_file(model_zoo.get_config_file(config_yaml))
    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
    cfg.INPUT.CROP.ENABLED = False
    cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(config_yaml)
    cfg.MODEL.DEVICE = 'cuda'
    predictor = DefaultPredictor(cfg)

    # run video
    cap = cv.VideoCapture(video_dir)   #for webcam: 0 if single, if multiple: choose for e.g 1; if no rto -> video_dir

    width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
    size =(width, height)
    fourcc= cv.VideoWriter_fourcc(*"MP4V")

    # VideoWriter for output video
    video_writer = cv.VideoWriter(video_dir[:-4] +'_output.mp4', fourcc, 20.0, size)

    while True:
        isTrue, frame = cap.read()
        if isTrue:
#             frame = imutils.resize(frame, width=640, height=480)
            panoptic_seg, segments_info = predictor(frame)["panoptic_seg"]

            v = Visualizer(frame[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1)
            output = v.draw_panoptic_seg_predictions(panoptic_seg.to("cpu"), segments_info)

            # get labels for panoptic seg
            thing_classes = [i['category_id'] for i in segments_info if i['isthing'] == True]
            stuff_classes = [i['category_id'] for i in segments_info if i['isthing'] == False]

            thing_names = MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).thing_classes
            stuff_names = MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).stuff_classes
            pred_class_names = list(map(lambda x: thing_names[x], thing_classes)) + list(map(lambda x: stuff_names[x], stuff_classes))

            # display and save result
            cv.cvtColor(output.get_image()[:, :, ::-1], cv.COLOR_BGR2RGB)
            cv.imshow('result', output.get_image()[:, :, ::-1])
            video_writer.write(output.get_image()[:, :, ::-1])
            cv.waitKey(1)

        else:
            break

    cap.release()
    video_writer.release()
    cv.destroyAllWindows()

if __name__=='__main__':
    run_video(args.video_dir, args.config_yaml)


import sys, os, io, atexit
input = lambda : sys.stdin.readline().rstrip('\r\n')
stdout = io.BytesIO()
sys.stdout.write = lambda s : stdout.write(s.encode("ascii"))
atexit.register(lambda : os.write(1, stdout.getvalue()))
import math
N = int(input())
arr = list(map(int, input().split()))
money = int(input())

if sum(arr) <= money:
    print(max(arr))
else:
    start = 0
    end = money
    answer = 0
    while start <= end:
        mid = ( start + end ) // 2
        sum_money = 0
        for i in arr:
            if i < mid:
                sum_money += i
            else:
                sum_money += mid

        if sum_money <= money:
            start = mid + 1
            answer = mid
        else:
            end = mid - 1
    print(answer)


import pandas as pd
from constants import AVAILABLE_ASSETS
from player import Player
from utils import object_name

class Game(object):

    state = 'running'

    def __init__(self):
        self.player = None

    def create_player(self, name=None):
        player = Player(name=name)
        self.player = player

    def get_player(self):
        return self.player

    def get_assets(self):
        df = pd.DataFrame(list(self.player.get_assets()))
        df.columns = ["Owned Assets"]
        df.applymap(lambda x: object_name(x))
        print(df)
        return df

    def list_assets(self):
        df = pd.DataFrame(list(AVAILABLE_ASSETS.keys()))
        df.columns = ["Assets"]
        df.applymap(lambda x: object_name(x))
        print(df)
        return df

    def list_player_inventory(self):
        shop = self.player.get_inventory()
        if shop == {}:
            print('You have no ores mined')
            return

        print(f"{self.player.name}'s Shop")
        tildes = "~" * len(self.player.name) + "~~~~~"

        "Item Name", "Quantity"
        print(df)

        return len(shop)

    def display_available_commands(self):
        print('Available commands: ')
        print('1. mine')
        print('2. balance')
        print('3. close')
        print('4. buy')
        print('5. sell')

if __name__ == '__main__':
    g = Game()
    g.create_player("Cyber")
    player = g.get_player()

    while g.state == 'running':
        g.display_available_commands()
        command = input('Enter command: ')
        if command.lower() == 'mine':
            player.mine()
        elif command.lower() == 'balance':
            balance = player.get_account_balance()
            print(balance)
        elif command.lower() == 'close':
            g.state = 'not running'
        elif command.lower() == 'buy':
            g.list_assets()
            asset_to_buy = input("Enter the name of the asset you want to buy: ")
            player.buy_asset(asset_to_buy)
        elif command.lower() == 'sell':
            available_items = g.list_player_inventory()
            if not available_items or available_items == 0:
                continue
            ore = input("Enter the ore you want to sell: ")
            quantity = int(input(f"Enter the number of units of {ore} you want to sell: "))
            player.sell_from_shop(ore, quantity)
        else:
            print('Invalid input, please try again.')


import numpy as np
from skimage.color import rgb2gray

def extractFeatures(im, c, patch_radius):
    r = patch_radius
    img = rgb2gray(im)
    img = np.pad(img, pad_width = r,mode='constant')
    cx = c[1,:].astype(int)
    cy = c[0,:].astype(int)
    d = (2*r+1)
    d2 = d//2
    f = []
    for x in range(0,len(cx)):
        x1 = cx[x] - d2
        x2 = cx[x] + d2
        y1 = cy[x] - d2
        y2 = cy[x] + d2
        patch = img[x1:x2, y1:y2]
        feature = patch.reshape(-1)
        f.append(feature)
    return f


import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from tensorflow.keras import layers, Sequential, Input, Model, initializers
from tensorflow.keras.layers import Reshape, Dense, Flatten
import utilities.metrics
from models.utilities import custom_loss, custom_loss_UV
from utilities.metrics import MatrixSparsity, MatrixSparsity_SVD
from utilities.plot import plot_matrices

# #%%
# import sys

# sys.path.append('/Users/ericbrunner/git/denise/')
# print(sys.path)

# #%%

class NeuralNet:
    def __init__(self, dim, output_dim, lr=0.001, batch_size=64, n_epochs=30, valid_split=0.2,
                 loss='mean_absolute_error', metrics=None):
        self.dim = dim
        self.input_dim = dim * (dim + 1) / 2  # n * (n+1) / 2
        self.output_dim = output_dim  # tuple (n, k)

        self.batch_size = batch_size
        self.n_epochs = n_epochs
        self.valid_split = valid_split

        self.lr = lr
        self.optimizer = tf.keras.optimizers.Adam(learning_rate=self.lr)
        self.loss = loss
        self.metrics = metrics
        self._create_network()

        self.history = None

    def _create_network(self):
        self.model = Sequential()
        self.model.add(layers.Dense(self.dim * 2, activation="relu"))
        self.model.add(layers.Dense(self.dim * 2, activation="relu"))
        self.model.add(layers.Dense(self.dim * 2, activation="relu"))
        self.model.add(layers.Dense(self.output_dim[0] * self.output_dim[1]))
        self.model.add(layers.Reshape(self.output_dim))  # to matrix
        # self.model.add(Shrink(self.dim))

    def compile(self):
        self.model.compile(optimizer=self.optimizer,
                           loss=self.loss,
                           metrics=self.metrics
                           )

    def train(self, X, y):
        self.compile()

        self.history = self.model.fit(X, y,
                                      batch_size=self.batch_size,
                                      epochs=self.n_epochs,
                                      validation_split=self.valid_split,
                                      )
        self.model.summary()

    def save_weights(self, path):
        self.model.save_weights(filepath=path)

    def load_weights(self, path):
        x = np.zeros((1, int(self.input_dim)))
        self.model(x)
        self.model.load_weights(path)

    def plot_metrics(self, metrics):
        for metric in metrics:
            self._plot_one_metric(metric=metric)

    def _plot_one_metric(self, metric):
        train_metrics = self.history.history[metric]
        val_metrics = self.history.history['val_' + metric]
        epochs = range(1, len(train_metrics) + 1)
        plt.plot(epochs, train_metrics)
        plt.plot(epochs, val_metrics)
        plt.title('Training and validation ' + metric)
        plt.xlabel("Epochs")
        plt.ylabel(metric)
        plt.legend(["train_" + metric, 'val_' + metric])
        plt.savefig(f'plots/{metric}.pdf')
        plt.show()

    def evaluate(self, M_test, dim, path, color_code_min=None, color_code_max=None, i=0):
        if len(M_test.shape) == 2:
            M_test = M_test[None, :, :]

        # Convert
        M_tris = list()
        for m in M_test:
            tri_M = m[np.triu_indices(m.shape[0])]
            M_tris.append(tri_M)
        M_tris = np.array(M_tris)
        U_pred = self.predict(X=M_tris)

        matrix_sparsity = MatrixSparsity(dim=dim)
        matrix_sparsity.update_state(M_true=M_test, U_pred=U_pred)
        sparsity_test = matrix_sparsity.result()

        # Plot one example
        fig = plot_matrices(M_test[0], U_pred=U_pred[0], vmin=color_code_min, vmax=color_code_max)
        plt.savefig(f'{path}denise_output_{i}.pdf')
        fig.show()
        return sparsity_test, U_pred

    def predict(self, X):
        return self.model.predict(X)

class Shrink(tf.keras.layers.Layer):
    def __init__(self, dim):
        super(Shrink, self).__init__()
        self.dim = float(dim)

    def call(self, inputs, **kwargs):
        return tf.multiply(tf.abs(inputs) - 1/tf.sqrt(self.dim), tf.sign(inputs))

class NeuralNet_SVD:
    def __init__(self, dim, rank, lr=0.001, batch_size=64, n_epochs=30, iterations=10, valid_split=0.2,
                 loss='mean_absolute_error', metrics=None):
        self.dim = dim # tuple (n,m)
        self.n = dim[0]
        self.m = dim[1]
        self.rank = rank
        self.output_dim_U = (self.n,self.rank)  # tuple (n, k)
        self.output_dim_V = (self.m,self.rank)  # tuple (m, k)

        self.batch_size = batch_size
        self.n_epochs = n_epochs
        self.iterations = iterations
        self.valid_split = valid_split

        self.lr = lr
        self.optimizer = tf.keras.optimizers.Adam(learning_rate=self.lr)
        self.loss = custom_loss_UV(self.n)
        self.metrics = metrics
        self.model = self._create_network()

        self.history = None

    def _create_network(self):
        input_tensor = Input(shape=(self.n,self.m), name='Input')
        UV = Flatten()(input_tensor)
        # U = Dense(self.n // 2, activation='relu', name='U1')(UV)
        # U = Dense(self.n // 2, activation='relu',name='U2')(U)
        # U = Dense(self.n // 2, activation='relu',name='U3')(U)
        U = Dense(self.n*self.m, activation='relu', name='U1')(UV)
        U = Dense(self.n*self.m//2, activation='relu',name='U2')(U)
        U = Dense(self.n*self.m, activation='relu',name='U3')(U)
        U = Dense(self.output_dim_U[0] * self.output_dim_U[1], name='U4')(U)
        U = Reshape(self.output_dim_U)(U)

        # V = Dense(self.m // 2, bias_initializer="zeros", activation='relu', name='V1')(UV)
        # V = Dense(self.m // 2, activation='relu',name='V2')(V)
        # V = Dense(self.m // 2, activation='relu',name='V3')(V)
        V = Dense(self.n*self.m, bias_initializer="zeros", activation='relu', name='V1')(UV)
        V = Dense(self.n*self.m//2, activation='relu',name='V2')(V)
        V = Dense(self.n*self.m, activation='relu',name='V3')(V)
        V = Dense(self.output_dim_V[0] * self.output_dim_V[1], name='V4')(V)
        V = Reshape(self.output_dim_V)(V)
        UV = tf.concat([U,V], axis=-2)
        model = Model(inputs=input_tensor, outputs=UV)
        return model

    def compile(self):
        self.model.compile(optimizer=self.optimizer,
                           loss=self.loss,
                           metrics=self.metrics
                           )

    def train(self, X, y):
        self.compile()

        history = []
        for k in range(self.iterations):
            print('-----')
            print(f'Iteration {k}')
            print('-----')
            for x in ['U1','U2','U3','U4']:
                self.model.get_layer(x).trainable = True
            for x in ['V1','V2','V3','V4']:
                self.model.get_layer(x).trainable = False
            history.append(
                self.model.fit(X, y,
                    batch_size=self.batch_size,
                    epochs=self.n_epochs,
                    validation_split=self.valid_split,
                    verbose=1
                    )
                )
            for x in ['U1','U2','U3','U4']:
                self.model.get_layer(x).trainable = False
            for x in ['V1','V2','V3','V4']:
                self.model.get_layer(x).trainable = True
            history.append(
                self.model.fit(X, y,
                    batch_size=self.batch_size,
                    epochs=self.n_epochs,
                    validation_split=self.valid_split,
                    verbose=1
                    )
                )
        self.history = history
        self.model.summary()

    def save_weights(self, path):
        self.model.save_weights(filepath=path)

    def load_weights(self, path):
        # x = np.zeros((1, int(self.n), int(self.m)))
        # self.model(x)
        self.model.load_weights(path)

    def plot_metrics(self, metrics):
        for metric in metrics:
            self._plot_one_metric(metric=metric)

    def _plot_one_metric(self, metric):
        train_metrics = [x for hist in self.history for x in hist.history[metric]]
        val_metrics = [x for hist in self.history for x in hist.history['val_' + metric]]
        epochs = range(1, len(train_metrics) + 1)
        plt.plot(epochs, train_metrics)
        plt.plot(epochs, val_metrics)
        plt.title('Training and validation ' + metric)
        plt.xlabel("Epochs")
        plt.ylabel(metric)
        plt.legend(["train_" + metric, 'val_' + metric])
        plt.savefig(f'plots/SVD_{metric}.pdf')
        plt.show()

    def evaluate(self, M_test, dim):
        if len(M_test.shape) == 2:
            M_test = M_test[None, :, :]

        UV_pred = self.predict(X=M_test)

        matrix_sparsity = MatrixSparsity_SVD(dim=dim)
        matrix_sparsity.update_state(M_true=M_test, UV_pred=UV_pred)
        sparsity_test = matrix_sparsity.result()

        # Plot one example
        fig = plot_matrices(M_test[0], UV_pred=UV_pred[0])
        plt.savefig('plots/SVD_denise_output.pdf')
        fig.show()
        return sparsity_test, UV_pred

    def predict(self, X):
        return self.model.predict(X)


import requests
from bs4 import BeautifulSoup
if __name__ == '__main__':
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'
    }
    url = 'https://www.shicimingju.com/book/sanguoyanyi.html'
    page_text = requests.get(url=url,headers=headers).text
    soup = BeautifulSoup(page_text,'lxml')
    li_list = soup.select('.book-mulu > ul > li')
    fp = open('./sanguo.txt','w',encoding='utf-8')
    for li in li_list:
        title = li.a.string
        detail = 'https://www.shicimingju.com' + li.a['href']
        detail_page_text = requests.get(url = detail,headers=headers).content.decode('utf-8')
        detail_soup = BeautifulSoup(detail_page_text,'lxml')
        detail_li_list = detail_soup.find('div',class_ = 'chapter_content').text
        fp.write(title+':'+detail_li_list+'\n')
        print("{}已成功下载".format(title))

第三章/bs4案例1：三国演义小说.py

bs4案例1：三国演义小说.py


from typing import List

def exist(board: List[List[str]], word: str) -> bool:
    num_rows = len(board)
    num_cols = len(board[0])

    def dfs(r, c, i):
        if i == len(word):
            return True

        if (r < 0 or c < 0 or r >= len(num_rows) or c >= len(num_cols)
                or board[r][c] != word[i] or board[r][c] == 0):
            return False

        temp = board[r][c]
        board[r][c] = 0

        i += 1
        result = (dfs(r, c + 1, i) or
                  dfs(r, c - 1, i) or
                  dfs(r + 1, c, i) or
                  dfs(r - 1, c, i))

        board[r][c] = temp

        return result

    for r in range(num_rows):
        for c in range(num_cols):
            if dfs(r, c, 0):
                return True

    return False


import argparse
import humblerl as hrl
from humblerl import Callback, Mind
import numpy as np

class TabularQLearning(Mind, Callback):
    def __init__(self, nstates, nactions, learning_rate, decay_steps, discount_factor):
        # Store training parameters
        self._lr = learning_rate
        self._decay = decay_steps
        self._gamma = discount_factor
        self._episode_count = 1
        self._return = 0
        self.running_avg = 0

        # Initialize Q-table
        self.Q = np.zeros((nstates, nactions), dtype=np.float)

    def plan(self, state, train_mode, debug_mode):
        # Decaying over time random noise for exploration
        random_noise = np.random.randn(self.Q.shape[1]) * (1. / self._episode_count)
        return self.Q[state] + random_noise

    def on_episode_start(self, episode, train_mode):
        self._return = 0

    def on_step_taken(self, step, transition, info):
        # Add reward to return
        self._return += transition.reward

        # Exponentially decaying learning rate
        LR = pow(self._lr, self._episode_count / self._decay)

        # Update Q-table
        if transition.is_terminal:
            target = transition.reward
        else:
            target = transition.reward + self._gamma * \
                np.max(self.Q[transition.next_state])
        self.Q[transition.state, transition.action] += \
            LR * (target - self.Q[transition.state, transition.action])

        # Count episodes
        if transition.is_terminal:
            self._episode_count += 1

    def on_episode_end(self, episode, train_mode):
        self.running_avg = 0.01 * self._return + 0.99 * self.running_avg

    @property
    def metrics(self):
        return {"avg. return": self.running_avg}

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='HumbleRL tabular Q-Learning sample')
    parser.add_argument('--episodes', type=int, default=865, metavar='N',
                        help='number of episodes to train (default: 865)')
    parser.add_argument('--lr', type=float, default=0.75, metavar='LR',
                        help='learning rate (default: 0.75)')
    parser.add_argument('--decay', type=int, default=400, metavar='N',
                        help='exploration decay steps (default: 400)')
    parser.add_argument('--gamma', type=float, default=0.95, metavar='G',
                        help='discount factor (default: 0.95)')
    args = parser.parse_args()

    # Create environment and q-learning agent
    env = hrl.create_gym("FrozenLake-v0")
    mind = TabularQLearning(env.state_space, env.action_space.num,
                            learning_rate=args.lr,
                            decay_steps=args.decay,
                            discount_factor=args.gamma)

    # Seed env and numpy
    np.random.seed(7)
    env.env.seed(7)

    # Run training
    hrl.loop(env, mind, n_episodes=args.episodes, callbacks=[mind])


from http import HTTPStatus
from app.models.animies_models import Animes
from flask import jsonify, request
from psycopg2.errors import UndefinedColumn
from psycopg2.errors import UniqueViolation
from psycopg2.errors import UndefinedTable

def create():
    data = request.get_json()
    try:
        new_data = Animes(**data)
        result = Animes.create_anime(new_data.__dict__)
    except UniqueViolation:
        return {"error": "anime is already exists"}, HTTPStatus.UNPROCESSABLE_ENTITY
    except KeyError as e:
        return {"required_keys": ["anime", "released_date", "seasons"], "missing_keys": e.args}, HTTPStatus.UNPROCESSABLE_ENTITY

    new_result = Animes.serialized_animes(result)

    return new_result, HTTPStatus.CREATED

def get_all_animes():

    try:
        result = Animes.get_animes()
        data = Animes.serialized_animes(result, lista=True)
    except UndefinedTable:
        return jsonify([]), HTTPStatus.OK

    return {"data": data}, HTTPStatus.OK

def get_one_by_id_anime(id):

    try:
        data = Animes.get_animes_by_id(id)
        result = Animes.serialized_animes(data)
    except (TypeError, UndefinedTable):
        return {"error": "Not Found"}, HTTPStatus.NOT_FOUND

    return {"data": [result]}, HTTPStatus.OK

def update(id):
    get = request.get_json()
    try:
        data = Animes.update_animes(get, id)
        data_serialized = Animes.serialized_animes(data)
    except UniqueViolation:
        return {"error": "anime is already exists"}, HTTPStatus.UNPROCESSABLE_ENTITY
    except UndefinedColumn as e:
        text = str(e.args).split('"')[1]
        return {"available_keys": ["anime", "released_date", "seasons"], "wrong_keys_sended": [f"{text}"]}, HTTPStatus.UNPROCESSABLE_ENTITY
    except (UndefinedTable, TypeError):
        return {"error": "Not Found"}, HTTPStatus.NOT_FOUND
    except KeyError as e:
        text = str(e.args).replace("(", "").replace(")", "").replace(",", "").replace("'", "")
        return {"available_keys": ["anime", "released_date", "seasons"], "wrong_keys_sended": [f"{text}"]}, HTTPStatus.UNPROCESSABLE_ENTITY

    return data_serialized, HTTPStatus.OK

def delete(id):
    try:
        deleted = Animes.deleted_animes(id)
        if not deleted:
            return {"error": "Not Found"}, HTTPStatus.NOT_FOUND
    except UndefinedTable:
        return {"error": "Not Found"}, HTTPStatus.NOT_FOUND

    return "", HTTPStatus.NO_CONTENT


# -*- coding: utf-8 -*-
import pytest
from test.model.message import Message
from faker import Faker

fake = Faker()

parameters = [
    ("timofeyevadasha@gmail.com", fake.text(max_nb_chars=10), fake.text(max_nb_chars=50)),
    (fake.email(), "", fake.text(max_nb_chars=50)),
    (fake.email(), fake.text(max_nb_chars=10), "")
]

@pytest.mark.parametrize('addressee, theme, body', parameters)
def test_send_message(app, addressee, theme, body):
    app.message.create(Message(addressee, theme, body))
    app.session.logout()


from matplotlib import pyplot as plt
import numpy as np
from skimage import data, util, transform, feature, measure, filters, metrics
from skimage import io
from skimage.io import imsave as sk_imsave
import cv2

def match_locations(img0, img1, coords0, coords1, radius=5, sigma=8):
    """Match image locations using SSD minimization.

    Areas from `img0` are matched with areas from `img1`. These areas
    are defined as patches located around pixels with Gaussian
    weights.

    Parameters:
    -----------
    img0, img1 : 2D array
        Input images.
    coords0 : (2, m) array_like
        Centers of the reference patches in `img0`.
    coords1 : (2, n) array_like
        Centers of the candidate patches in `img1`.
    radius : int
        Radius of the considered patches.
    sigma : float
        Standard deviation of the Gaussian kernel centered over the patches.

    Returns:
    --------
    match_coords: (2, m) array
        The points in `coords1` that are the closest corresponding matches to
        those in `coords0` as determined by the (Gaussian weighted) sum of
        squared differences between patches surrounding each point.
    """
    y, x = np.mgrid[-radius:radius + 1, -radius:radius + 1]
    weights = np.exp(-0.5 * (x ** 2 + y ** 2) / sigma ** 2)
    weights /= 2 * np.pi * sigma * sigma

    match_list = []
    for r0, c0 in coords0:
        roi0 = img0[r0 - radius:r0 + radius + 1, c0 - radius:c0 + radius + 1]
        roi1_list = [img1[r1 - radius:r1 + radius + 1,
                          c1 - radius:c1 + radius + 1] for r1, c1 in coords1]
        # sum of squared differences
        ssd_list = [np.sum(weights * (roi0 - roi1) ** 2) for roi1 in roi1_list]
        match_list.append(coords1[np.argmin(ssd_list)])

    return np.array(match_list)

startNo = 0
endNo = 6

imageNumbers = np.arange(startNo + 1, endNo + 1)

differenceImages = [io.imread(r"rotatedCross\%s_D.tif" % str(startNo)).astype(np.float32)]
intensityImages = [io.imread(r"rotatedCross\%s_I.tif" % str(startNo)).astype(np.float32)]

for i in imageNumbers:

    differenceImg = io.imread(r"rotatedCross\%s_D.tif" % str(i))
    intensityImg = io.imread(r"rotatedCross\%s_I.tif" % str(i))

    differenceImages = differenceImages + [differenceImg]
    intensityImages = intensityImages + [intensityImg]

initialIntensityImages = intensityImages
initialDifferenceImages = differenceImages

imageStack = []

initialAvg = np.nanmean(differenceImages[0])
initialStd = np.nanstd(differenceImages[0])

for image in differenceImages:
    avg = np.nanmean(image)
    std = np.nanstd(image)

    newImage = initialAvg + ((image - avg) * (initialStd / std))
    np.clip(newImage, -1, 1)

    image = newImage
    image = (image + 1)/2
    image = image * 255
    image = np.array(image, dtype="uint8")
    #image = cv2.medianBlur(image, 5)
    #image = np.clip(image, 110, 150)
    #image = cv2.equalizeHist(image)
    #image = (image-np.nanmin(image))/(np.nanmax(image)-np.nanmin(image))
    #image = image * 255
    #image = np.array(image, dtype="uint8")

    imageStack = imageStack + [image]
#imageStack = standardizeStack(imageStack)

differenceImages = imageStack
print("no of images")
print(len(differenceImages))

initialAvg = np.nanmean(intensityImages[0])
initialStd = np.nanstd(intensityImages[0])
imageStack = []

for image in intensityImages:
    print("looping")
    avg = np.nanmean(image)
    std = np.nanstd(image)

    image = (image-np.nanmin(image))/(np.nanmax(image)-np.nanmin(image)) #renormalises - after contrast adjust so shows
    image = image * 255

    newImage = initialAvg + ((image - avg) * (initialStd / std))

    np.clip(newImage, 0, 255)
    image = np.array(newImage, dtype = "uint8")
    image = cv2.medianBlur(image, 5)

    image = np.array(image, dtype="uint8")

    imageStack = imageStack + [image]

intensityImages = imageStack

img_list = differenceImages

min_dist = 5
corner_list = [feature.corner_peaks(
    feature.corner_harris(img, sigma=7), threshold_rel=0.001, min_distance=min_dist, )
               for img in img_list]

img0 = img_list[0]
coords0 = corner_list[0]
matching_corners = [match_locations(img0, img1, coords0, coords1, min_dist)
                    for img1, coords1 in zip(img_list, corner_list)]

for dst in matching_corners:
    print("printing dst")
    print(dst)

src = np.array(coords0)
trfm_list = []
for dst in matching_corners:
    trfm_list = trfm_list + [measure.ransac((dst, src),
                            transform.EuclideanTransform, min_samples=3,
                            residual_threshold=50, max_trials=500)[0]]
print("trfm_list")
print(trfm_list)

fig, ax_list = plt.subplots(6, 2, figsize=(6, 9), sharex=True, sharey=True)
for idx, (im, trfm, (ax0, ax1)) in enumerate(zip(img_list, trfm_list,
                                                 ax_list)):
    ax0.imshow(im, cmap="gray")
    ax1.imshow(transform.warp(im, trfm), cmap="gray", vmin = 0.4, vmax = 0.55)

    if idx == 0:
        ax0.set_title("initial")
        #ax0.set_ylabel(f"Reference Image\n(PSNR={psnr_ref:.2f})")
        ax1.set_title("corrected")

    ax0.set(xticklabels=[], yticklabels=[], xticks=[], yticks=[])
    ax1.set_axis_off()

fig.tight_layout()
plt.show()

All Code - Everything Dumped/imageStitchingAttempt.py


from typing import Optional

import torch
from torch import nn
from einops import rearrange, einsum

class FeatureTokenizer(nn.Module):
    def __init__(self, categories: list[int], in_features_num: int, embedding_dim: int):
        super().__init__()
        assert all(map(lambda n: n > 0, categories)), 'number of each category must be positive'
        assert len(categories) + in_features_num > 0, 'input shape must not be empty'

        self.in_features_cat = len(categories)
        self.in_features_num = in_features_num
        self.cls_num_weight = nn.Parameter(torch.randn(1 + in_features_num, embedding_dim))

        if len(categories) > 0:
            self.register_buffer('category_offsets', torch.tensor([0] + categories[:-1], dtype=torch.int32).cumsum(0))
            self.cat_embedding = nn.Embedding(num_embeddings=sum(categories), embedding_dim=embedding_dim)

        self.bias = nn.Parameter(torch.randn(in_features_num + len(categories), embedding_dim))
        self.cls = nn.Parameter(torch.ones(1, embedding_dim))

    def forward(self, x_num: Optional[torch.Tensor] = None, x_cat: Optional[torch.Tensor] = None):
        """
        :param x_num: [batch_size x in_features_num]
        :param x_cat: [batch_size x in_features_cat]
        :return: [batch_size, 1 + in_features_num + in_features_cat, embedding_dim] the embeddings for the input with [CLS]
        """
        assert x_num is not None and x_cat is not None, 'Expected at least x_num or x_cat'
        x_nonnull = x_num if x_num is not None else x_cat
        batch_size, _ = x_nonnull.size()

        x_num = torch.cat([torch.ones(batch_size, 1, device=x_nonnull.device)] + ([] if x_num is None else [x_num]),
                          dim=-1)
        x_num = torch.einsum('bi,ij->bij', x_num, self.cls_num_weight)
        x_temp: list[torch.Tensor] = [x_num]

        if self.in_features_cat > 0:
            x_cat = self.cat_embedding(x_cat + self.get_buffer('category_offsets'))
            x_temp.append(x_cat)

        x = torch.cat(x_temp, dim=-2)

        _, embedding_dim = self.bias.size()
        bias = torch.cat([torch.zeros(1, embedding_dim, device=x_nonnull.device), self.bias])
        x += bias
        return x

class GEGLU(nn.Module):
    """https://github.com/lucidrains/tab-transformer-pytorch/blob/main/tab_transformer_pytorch/ft_transformer.py"""

    def forward(self, x):
        x, gates = x.chunk(2, dim=-1)
        return x * nn.functional.gelu(gates)

class FeedForward(nn.Module):
    def __init__(self, d_embedding: int, d_feedforward: int, dropout=0.):
        super().__init__()
        self.layer = nn.Sequential(
            nn.LayerNorm(d_embedding),
            nn.Linear(d_embedding, d_feedforward * 2),
            GEGLU(),
            nn.Dropout(dropout),
            nn.Linear(d_feedforward, d_embedding)
        )

    def forward(self, x: torch.Tensor):
        return self.layer(x)

class Attention(nn.Module):
    """https://github.com/lucidrains/tab-transformer-pytorch/blob/main/tab_transformer_pytorch/ft_transformer.py"""

    def __init__(
            self,
            d_embedding: int,
            n_heads=8,
            d_head=64,
            dropout=0.
    ):
        super().__init__()
        inner_dim = d_head * n_heads
        self.heads = n_heads
        self.scale = d_head ** -0.5

        self.norm = nn.LayerNorm(d_embedding)

        self.to_q = nn.Linear(d_embedding, inner_dim, bias=False)
        self.to_k = nn.Linear(d_embedding, inner_dim, bias=False)
        self.to_v = nn.Linear(d_embedding, inner_dim, bias=False)

        self.to_out = nn.Linear(inner_dim, d_embedding, bias=False)

        self.dropout = nn.Dropout(dropout)

    def forward(self, x: torch.Tensor, only_cls=False):
        h = self.heads

        x = self.norm(x)

        q = self.to_q(x[:, [0], :]) if only_cls else self.to_q(x)
        k, v = self.to_k(x), self.to_v(x)

        q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h=h), (q, k, v))
        q = q * self.scale

        sim = einsum(q, k, 'b h i d, b h j d -> b h i j')

        attn = sim.softmax(dim=-1)
        dropped_attn = self.dropout(attn)

        out = einsum(dropped_attn, v, 'b h i j, b h j d -> b h i d')
        out = rearrange(out, 'b h n d -> b n (h d)', h=h)
        out = self.to_out(out)

        return out, attn

class Transformer(nn.Module):
    """https://github.com/lucidrains/tab-transformer-pytorch/blob/main/tab_transformer_pytorch/ft_transformer.py"""

    def __init__(
            self,
            d_embedding: int,
            n_layers: int,
            n_heads: int,
            d_head: int,
            attn_dropout: float,
            ffn_dropout: float,
            d_feedforward: int,
    ):
        super().__init__()
        self.layers = nn.ModuleList([])

        for _ in range(n_layers):
            self.layers.append(nn.ModuleList([
                Attention(d_embedding, n_heads=n_heads, d_head=d_head, dropout=attn_dropout),
                FeedForward(d_embedding=d_embedding, d_feedforward=d_feedforward, dropout=ffn_dropout),
            ]))

    def forward(self, x, return_attn=False, only_cls_on_last=False):
        post_softmax_attns = []

        for i, (attn, ff) in enumerate(self.layers):
            is_last_layer = i + 1 == len(self.layers)
            attn_out, post_softmax_attn = attn(x, only_cls=only_cls_on_last and is_last_layer)
            post_softmax_attns.append(post_softmax_attn)

            x = attn_out + x
            x = ff(x) + x

        if not return_attn:
            return x

        return x, post_softmax_attns

class FTTransformer(nn.Module):
    def __init__(self,
                 categories: list[int],
                 d_numerical: int,
                 d_token: int,
                 d_out: int,
                 n_layers: int,
                 n_heads: int,
                 attention_dropout: float,
                 ffn_dropout: float,
                 d_ffn_factor: float,
                 # unused
                 token_bias: bool = True,
                 # transformer
                 residual_dropout: float = 0.,
                 activation: str = 'reglu',
                 prenormalization: bool = True,
                 initialization: str = 'kaiming',
                 kv_compression: Optional[float] = None,
                 kv_compression_sharing: Optional[str] = None,
                 ):
        super().__init__()
        assert d_token % n_heads == 0, 'embedding_dim must be a multiple of the number of heads'

        self.ft = FeatureTokenizer(categories=categories, in_features_num=d_numerical, embedding_dim=d_token)
        self.transformer = Transformer(d_embedding=d_token, n_layers=n_layers, n_heads=n_heads,
                                       d_head=d_token // n_heads,
                                       attn_dropout=attention_dropout, d_feedforward=int(d_ffn_factor * d_token),
                                       ffn_dropout=ffn_dropout)
        self.output = nn.Sequential(
            nn.LayerNorm(d_token),
            nn.ReLU(),
            nn.Linear(d_token, d_out)
        )

    def forward(self, x_num: torch.FloatTensor, x_cat: torch.IntTensor, only_cls_on_last=False):
        """
        :param x_num: [batch_size x n_num] a tensor containing numerical features
        :param x_cat: [batch_size x n_cat] a tensor containing categorical features
        :return:
        """
        x = self.ft(x_num, x_cat)
        x = self.transformer(x, only_cls_on_last=only_cls_on_last)

        # pick [CLS] embeddings
        x = x[:, 0, :]
        x = self.output(x)
        x = x.squeeze(dim=-1)

        return x


import pika
import sys
import os
import Object_Storage as Obj
import Image_Tagging as Tag
import database
import Email

class RabbitMQ_Receive():
    def __init__(self):
        self.AMQP_URL = "amqps://wqzfdbgv:H0k0rWOPf6vTBaFCgJ35vZgWn657nAQZ@albatross.rmq.cloudamqp.com/wqzfdbgv"
        self.connection = pika.BlockingConnection(
            pika.URLParameters(self.AMQP_URL))
        self.channel = self.connection.channel()
        self.queue = 'hello'
        print('start')
        self.db = database.Database()

    def receive(self):
        '''Receive a message from the RabbitMQ server.'''
        self.channel.queue_declare(queue=self.queue)

        def callback(ch, method, properties, body):
            print(" [x] Received %r" % body)
            body = body.decode('utf-8')
            id = int(os.path.splitext(body)[0])
            extention = os.path.splitext(body)[1]
            file_name = body
            file_name = Obj.S3().download_file(id, extention)
            tag, is_vehicle = Tag.ImageTagging().get_tags(file_name)
            os.remove(file_name)
            if is_vehicle:
                state = 2
                self.db.update_data(id=id, state=state, category=tag)
                email = self.db.get_email(id)
                text = f'Your Advertisement with id: {id} is approved and your category is {tag}'
                subject = 'Approve Advertisment'
                Email.SendEmail().send_message(email, subject, text)

            if is_vehicle is False:
                state = 1
                self.db.update_data(id=id, state=state, category=tag)
                email = self.db.get_email(id)
                text = f'Your Advertisement with id: {id} is rejected because it is not a vehicle'
                subject = 'Reject Advertisment'
                Email.SendEmail().send_message(email, subject, text)

            print('Email Sent!')

            # print(" [x] Received %r" % body)

        self.channel.basic_consume(
            queue=self.queue, on_message_callback=callback, auto_ack=True)
        print(' [*] Waiting for messages. To exit press CTRL+C')
        self.channel.start_consuming()

RabbitMQ_Receive().receive()


import h5py

import os
import numpy as np
from sortedcontainers import SortedList
from torch.utils.data import Dataset
import glob
import string

from tqdm import tqdm
import DALI as dali_code
from utils import load, write_wav, load_lyrics, ToolFreq2Midi

import soundfile as sf

import logging
# logging.basicConfig(level=logging.DEBUG)

def getDALI(database_path, level, lang, genre):
    dali_annot_path = os.path.join(database_path, 'annot_tismir')
    dali_audio_path = os.path.join(database_path, 'audio')
    dali_data = dali_code.get_the_DALI_dataset(dali_annot_path, skip=[], keep=[])

    # get audio list
    audio_list = os.listdir(os.path.join(dali_audio_path))

    subset = list()
    duration = list()
    total_line_num = 0
    discard_line_num = 0

    for file in audio_list:
        if file.endswith('.mp3') and os.path.exists(os.path.join(dali_annot_path, file[:-4] + '.gz')):
            # get annotation for the current song
            try:
                entry = dali_data[file[:-4]]
                entry_info = entry.info

                # language filter
                if lang is not None and entry_info['metadata']['language'] != lang:
                    continue
                # genre filter
                if genre is not None and genre not in entry_info['metadata']['genres']:
                    continue

                song = {"id": file[:-4], "annot": [], "path": os.path.join(dali_audio_path, file)}
                samples = entry.annotations['annot'][level]

                notes_raw = entry.annotations['annot']["notes"]
                notes = [{"freq": note_raw['freq'][0], "pitch": ToolFreq2Midi(note_raw['freq'][0]), "time": note_raw['time']} for note_raw in
                         notes_raw]
                song["notes"] = notes

                subset.append(song)

                for sample in samples:
                    sample["duration"] = sample["time"][1] - sample["time"][0]

                    if sample["duration"] > 10.22:
                        print(sample)
                        discard_line_num += 1

                    song["annot"].append(sample)
                    duration.append(sample["duration"])

                    total_line_num += 1

                logging.debug("Successfully loaded {} songs".format(len(subset)))
            except:
                logging.warning("Error loading annotation for song {}".format(file))
                pass

    logging.debug("Scanning {} songs.".format(len(subset)))
    logging.debug("Total line num: {} Discarded line num: {}".format(total_line_num,  discard_line_num))

    return np.array(subset, dtype=object)

def get_dali_folds(database_path, level, lang="english", genre=None):
    dataset = getDALI(database_path, level, lang, genre)

    total_len = len(dataset)
    train_len = np.int(0.8 * total_len)

    train_list = np.random.choice(dataset, train_len, replace=False)
    val_list = [elem for elem in dataset if elem not in train_list]
    logging.debug("First training song: " + str(train_list[0]["id"]) + " " + str(len(train_list[0]["annot"])) + " lines")
    logging.debug("train_list {} songs val_list {} songs".format(len(train_list), len(val_list)))
    return {"train" : train_list, "val" : val_list}

def crop(mix, targets, shapes):
    '''
    Crops target audio to the output shape required by the model given in "shapes"
    '''
    for key in targets.keys():
        if key != "mix":
            targets[key] = targets[key][:, shapes["output_start_frame"]:shapes["output_end_frame"]]
    return mix, targets

def random_amplify(mix, targets, shapes, min, max):
    '''
    Data augmentation by randomly amplifying sources before adding them to form a new mixture
    :param mix: Original mixture
    :param targets: Source targets
    :param shapes: Shape dict from model
    :param min: Minimum possible amplification
    :param max: Maximum possible amplification
    :return: New data point as tuple (mix, targets)
    '''
    residual = mix  # start with original mix
    for key in targets.keys():
        if key != "mix":
            residual -= targets[key]  # subtract all instruments (output is zero if all instruments add to mix)
    mix = residual * np.random.uniform(min, max)  # also apply gain data augmentation to residual
    for key in targets.keys():
        if key != "mix":
            targets[key] = targets[key] * np.random.uniform(min, max)
            mix += targets[key]  # add instrument with gain data augmentation to mix
    mix = np.clip(mix, -1.0, 1.0)
    return crop(mix, targets, shapes)

class LyricsAlignDataset(Dataset):
    def __init__(self, dataset, partition, sr, shapes, hdf_dir, in_memory=False, dummy=False, pad_length=150):
        '''

        :param dataset:     a list of song with line level annotation
        :param sr:          sampling rate
        :param shapes:      dict, keys: "output_frames", "output_start_frame", "input_frames"
        :param hdf_dir:     hdf5 file
        :param in_memory:   load in memory or not
        :param dummy:       use a subset
        '''

        super(LyricsAlignDataset, self).__init__()

        self.hdf_dataset = None
        os.makedirs(hdf_dir, exist_ok=True)
        if dummy == False:
            self.hdf_file = os.path.join(hdf_dir, partition + ".hdf5")
        else:
            self.hdf_file = os.path.join(hdf_dir, partition + "_dummy.hdf5")

        self.sr = sr
        self.shapes = shapes
        self.hop = (shapes["output_frames"] // 2)
        self.in_memory = in_memory
        self.pad_length = pad_length

        # PREPARE HDF FILE

        # Check if HDF file exists already
        if not os.path.exists(self.hdf_file):
            # Create folder if it did not exist before
            if not os.path.exists(hdf_dir):
                os.makedirs(hdf_dir)

            # Create HDF file
            with h5py.File(self.hdf_file, "w") as f:
                f.attrs["sr"] = sr

                print("Adding audio files to dataset (preprocessing)...")
                for idx, example in enumerate(tqdm(dataset[partition])):
                    # Load song
                    y, _ = load(example["path"], sr=self.sr, mono=True)

                    # Add to HDF5 file
                    grp = f.create_group(str(idx))
                    grp.create_dataset("inputs", shape=y.shape, dtype=y.dtype, data=y)

                    grp.attrs["audio_name"] = example["id"]
                    grp.attrs["input_length"] = y.shape[1]

                    # word level annotation
                    annot_num = len(example["annot"])
                    lyrics = [sample["text"].encode() for sample in example["annot"]]
                    times = np.array([sample["time"] for sample in example["annot"]])

                    # note level annotation
                    notes = np.array(example["notes"])
                    note_num = len(notes)
                    pitches = np.array([note["freq"] for note in notes])
                    note_times = np.array([np.array([note['time'][0], note['time'][1]]) for note in notes])

                    grp.attrs["annot_num"] = annot_num
                    grp.attrs["note_num"] = note_num

                    # words and corresponding times
                    grp.create_dataset("lyrics", shape=(annot_num, 1), dtype='S100', data=lyrics)
                    grp.create_dataset("times", shape=(annot_num, 2), dtype=times.dtype, data=times)

                    # notes and corresponding times
                    grp.create_dataset("freqs", shape=(note_num, 1), dtype=np.short, data=pitches)
                    grp.create_dataset("note_times", shape=(note_num, 2), dtype=note_times.dtype, data=note_times)

        # In that case, check whether sr and channels are complying with the audio in the HDF file, otherwise raise error
        with h5py.File(self.hdf_file, "r", libver='latest', swmr=True) as f:
            if f.attrs["sr"] != sr:
                raise ValueError(
                    "Tried to load existing HDF file, but sampling rate is not as expected. Did you load an out-dated HDF file?")

        # HDF FILE READY

        # SET SAMPLING POSITIONS

        # Go through HDF and collect lengths of all audio files
        with h5py.File(self.hdf_file, "r") as f:
            # length of song
            lengths = [f[str(song_idx)].attrs["input_length"] for song_idx in range(len(f))]

            # Subtract input_size from lengths and divide by hop size to determine number of starting positions
            lengths = [( (l - self.shapes["output_frames"]) // self.hop) + 1 for l in lengths]

        self.start_pos = SortedList(np.cumsum(lengths))
        self.length = self.start_pos[-1]

        self.shuffled_buffer = np.arange(self.length)
        self.shuffle_data_list()

    def shuffle_data_list(self):
        np.random.shuffle(self.shuffled_buffer)

    def __getitem__(self, index):

        # Open HDF5
        if self.hdf_dataset is None:
            driver = "core" if self.in_memory else None  # Load HDF5 fully into memory if desired
            self.hdf_dataset = h5py.File(self.hdf_file, 'r', driver=driver)

        while True:
            # Loop until it finds a valid sample

            # Find out which slice of targets we want to read
            song_idx = self.start_pos.bisect_right(index)
            if song_idx > 0:
                index = index - self.start_pos[song_idx - 1]

            # Check length of audio signal
            audio_length = self.hdf_dataset[str(song_idx)].attrs["input_length"]
            annot_num = self.hdf_dataset[str(song_idx)].attrs["annot_num"]
            target_length = self.shapes["output_frames"]

            # Determine position where to start targets
            start_target_pos = index * self.hop
            end_target_pos = start_target_pos + self.shapes["output_frames"]

            # READ INPUTS
            # Check front padding
            start_pos = start_target_pos - self.shapes["output_start_frame"]
            if start_pos < 0:
                # Pad manually since audio signal was too short
                pad_front = abs(start_pos)
                start_pos = 0
            else:
                pad_front = 0

            # Check back padding
            end_pos = start_target_pos - self.shapes["output_start_frame"] + self.shapes["input_frames"]
            if end_pos > audio_length:
                # Pad manually since audio signal was too short
                pad_back = end_pos - audio_length
                end_pos = audio_length
            else:
                pad_back = 0

            # read audio and zero padding
            audio = self.hdf_dataset[str(song_idx)]["inputs"][:, start_pos:end_pos].astype(np.float32)
            if pad_front > 0 or pad_back > 0:
                audio = np.pad(audio, [(0, 0), (pad_front, pad_back)], mode="constant", constant_values=0.0)

            # find the lyrics within (start_target_pos, end_target_pos)
            words_start_end_pos = self.hdf_dataset[str(song_idx)]["times"][:]
            try:
                first_word_to_include = next(x for x, val in enumerate(list(words_start_end_pos[:, 0]))
                                             if val > start_target_pos/self.sr)
            except StopIteration:
                first_word_to_include = np.Inf

            try:
                last_word_to_include = annot_num - 1 - next(x for x, val in enumerate(reversed(list(words_start_end_pos[:, 1])))
                                             if val < end_target_pos/self.sr)
            except StopIteration:
                last_word_to_include = -np.Inf

            targets = ""
            if first_word_to_include - 1 == last_word_to_include + 1: # the word covers the whole window
                # invalid sample, skip
                targets = None
                index = np.random.randint(self.length)
                continue
            if first_word_to_include <= last_word_to_include: # the window covers word[first:last+1]
                lyrics = self.hdf_dataset[str(song_idx)]["lyrics"][first_word_to_include:last_word_to_include+1]
                lyrics_list = [s[0].decode() for s in list(lyrics)]
                targets = " ".join(lyrics_list)
                targets = " ".join(targets.split())

            if len(targets) > 120:
                index = np.random.randint(self.length)
                continue

            seq = self.text2seq(targets)
            break

        return audio, targets, seq

    def text2seq(self, text):
        seq = []
        for c in text.lower():
            idx = string.ascii_lowercase.find(c)
            if idx == -1:
                if c == "'":
                    idx = 26
                elif c == " ":
                    idx = 27
                else:
                    continue # remove unknown characters
            seq.append(idx)
        # if len(seq) == 0:
        #     seq.append(28) # insert epsilon for instrumental segments
        return np.array(seq)

    def __len__(self):
        return self.length

class JamendoLyricsDataset(Dataset):
    def __init__(self, sr, shapes, hdf_dir, dataset, jamendo_dir, in_memory=False):
        super(JamendoLyricsDataset, self).__init__()
        self.hdf_dataset = None
        os.makedirs(hdf_dir, exist_ok=True)
        self.hdf_file = os.path.join(hdf_dir, dataset + ".hdf5")

        self.sr = sr
        self.shapes = shapes
        self.hop = shapes["output_frames"]
        self.in_memory = in_memory

        audio_dir = os.path.join(jamendo_dir, 'mp3')
        lyrics_dir = os.path.join(jamendo_dir, 'lyrics')
        self.audio_list = [file for file in os.listdir(audio_dir) if file.endswith('.mp3')]

        # PREPARE HDF FILE

        # Check if HDF file exists already
        if not os.path.exists(self.hdf_file):
            # Create folder if it did not exist before
            if not os.path.exists(hdf_dir):
                os.makedirs(hdf_dir)

            # Create HDF file
            with h5py.File(self.hdf_file, "w") as f:
                f.attrs["sr"] = sr

                print("Adding audio files to dataset (preprocessing)...")
                for idx, audio_name in enumerate(tqdm(self.audio_list)):

                    # Load song
                    y, _ = load(os.path.join(audio_dir, audio_name), sr=self.sr, mono=True)

                    lyrics, words, idx_in_full = load_lyrics(os.path.join(lyrics_dir, audio_name[:-4]))
                    annot_num = len(words)

                    # Add to HDF5 file
                    grp = f.create_group(str(idx))
                    grp.create_dataset("inputs", shape=y.shape, dtype=y.dtype, data=y)

                    grp.attrs["input_length"] = y.shape[1]
                    grp.attrs["audio_name"] = audio_name[:-4]
                    print(len(lyrics))

                    grp.create_dataset("lyrics", shape=(1, 1), dtype='S3000', data=np.array([lyrics.encode()]))
                    grp.create_dataset("idx", shape=(annot_num, 2), dtype=np.int, data=idx_in_full)

        # In that case, check whether sr and channels are complying with the audio in the HDF file, otherwise raise error
        with h5py.File(self.hdf_file, "r", libver='latest', swmr=True) as f:
            if f.attrs["sr"] != sr:
                raise ValueError(
                    "Tried to load existing HDF file, but sampling rate is not as expected. Did you load an out-dated HDF file?")

        # HDF FILE READY

        # SET SAMPLING POSITIONS

        # Go through HDF and collect lengths of all audio files
        with h5py.File(self.hdf_file, "r") as f:
            # length of song
            lengths = [f[str(song_idx)].attrs["input_length"] for song_idx in range(len(f))]

            # Subtract input_size from lengths and divide by hop size to determine number of starting positions
            lengths = [np.int(np.ceil(l / self.hop)) for l in lengths]

        self.lengths = lengths
        self.length = len(lengths)

    def __getitem__(self, index):

        # Open HDF5
        if self.hdf_dataset is None:
            driver = "core" if self.in_memory else None  # Load HDF5 fully into memory if desired
            self.hdf_dataset = h5py.File(self.hdf_file, 'r', driver=driver)

        # select song: index
        # Check length of audio signal
        audio_length = self.hdf_dataset[str(index)].attrs["input_length"]

        # number of chunks for that song
        num_chunk = self.lengths[index]

        chunks = []

        for i in np.arange(num_chunk):
            # Determine position where to start targets
            start_target_pos = i * self.hop
            end_target_pos = start_target_pos + self.shapes["output_frames"]

            # READ INPUTS
            # Check front padding
            start_pos = start_target_pos - self.shapes["output_start_frame"]
            if start_pos < 0:
                # Pad manually since audio signal was too short
                pad_front = abs(start_pos)
                start_pos = 0
            else:
                pad_front = 0

            # Check back padding
            end_pos = start_target_pos - self.shapes["output_start_frame"] + self.shapes["input_frames"]
            if end_pos > audio_length:
                # Pad manually since audio signal was too short
                pad_back = end_pos - audio_length
                end_pos = audio_length
            else:
                pad_back = 0

            # read audio and zero padding
            audio = self.hdf_dataset[str(index)]["inputs"][:, start_pos:end_pos].astype(np.float32)
            audio_name = self.hdf_dataset[str(index)].attrs["audio_name"]
            lyrics = self.hdf_dataset[str(index)]["lyrics"][0, 0].decode()
            align_idx = self.hdf_dataset[str(index)]["idx"]
            if pad_front > 0 or pad_back > 0:
                audio = np.pad(audio, [(0, 0), (pad_front, pad_back)], mode="constant", constant_values=0.0)

            chunks.append(audio)

        return chunks, align_idx, (lyrics, audio_name, audio_length)

    def __len__(self):
        return self.length


import fitz
import tkinter as tk
from tkinter import filedialog
def pdf_image(pdfPath, imgPath, zoom_x, zoom_y, rotation_angle):
    # 打开PDF文件
    pdf = fitz.open(pdfPath)
    # 逐页读取PDF
    for pg in range(0, pdf.page_count):
        page = pdf[pg]
        # 设置缩放和旋转系数
        trans = fitz.Matrix(zoom_x, zoom_y).prerotate(rotation_angle)
        pm = page.get_pixmap(matrix=trans, alpha=False)
        # 开始写图像
        pm._writeIMG(imgPath + str(pg) + ".png",format=1,jpg_quality=100)
    pdf.close()
def select_files():
    root = tk.Tk()
    root.withdraw()
    file_paths = filedialog.askopenfilenames(title="选择要提取的PDF文件", filetypes=[("PDF Files", "*.pdf")])
    return file_paths
input_file=select_files()
print("filename:{}".format(str(input_file)))
pdf_image(input_file[0]
          , r"images/", 10, 10, 0)


from django.shortcuts import render
from django.http import HttpResponse
from .models import Customers, Records

# Create your views here.

def index(request):
    return render(request,"index.html")

def customers(request):
    datas=Customers.objects.all()
    return render(request,"customers.html",{'datas':datas})

def transfer(request,id):
    costm=Customers.objects.get(id=id)
    recivers=Customers.objects.all().exclude(name=costm.name)
    return render(request,"transfer.html",{'costm':costm,'recivers':recivers})

def submission(request):
    vari = Records()
    vari.sender_name=request.POST.get('name')
    vari.resiver_name=request.POST.get('reciver')
    vari.transfer_balance=request.POST.get('balance')
    vari.save()
    send=Customers.objects.get(name=vari.sender_name)
    reci=Customers.objects.get(name=vari.resiver_name)
    send.balance=int(send.balance)-int(vari.transfer_balance)
    reci.balance=int(reci.balance)+int(vari.transfer_balance)
    send.save()
    reci.save()
    return HttpResponse("Transfer Successful")


import logging
import os
import torch
import numpy as np
import json
from tqdm import tqdm
from functools import reduce
import pickle
from abc import ABC, abstractmethod
import tqdm as tqdm

def intersect_2d(x1, x2):
    """
    Given two arrays [m1, n], [m2,n], returns a [m1, m2] array where each entry is True if those
    rows match.
    :param x1: [m1, n] numpy array
    :param x2: [m2, n] numpy array
    :return: [m1, m2] bool array of the intersections
    """
    if x1.shape[1] != x2.shape[1]:
        raise ValueError("Input arrays must have same #columns")

    # This performs a matrix multiplication-esque thing between the two arrays
    # Instead of summing, we want the equality, so we reduce in that way
    res = (x1[..., None] == x2.T[None, ...]).all(1)
    return res

def compute_iou(bboxes1, bboxes2):
    x11, y11, x12, y12 = np.split(bboxes1, 4, axis=1)
    x21, y21, x22, y22 = np.split(bboxes2, 4, axis=1)
    xA = np.maximum(x11, np.transpose(x21))
    yA = np.maximum(y11, np.transpose(y21))
    xB = np.minimum(x12, np.transpose(x22))
    yB = np.minimum(y12, np.transpose(y22))
    interArea = np.maximum((xB - xA + 1), 0) * np.maximum((yB - yA + 1), 0)
    boxAArea = (x12 - x11 + 1) * (y12 - y11 + 1)
    boxBArea = (x22 - x21 + 1) * (y22 - y21 + 1)
    iou = interArea / (boxAArea + np.transpose(boxBArea) - interArea)
    return iou

class SceneGraphEvaluation(ABC):
    def __init__(self, result_dict):
        super().__init__()
        self.result_dict = result_dict

    @abstractmethod
    def register_container(self, mode):
        print("Register Result Container")
        pass

    @abstractmethod
    def generate_print_string(self, mode):
        print("Generate Print String")
        pass

"""
Traditional Recall, implement based on:
https://github.com/rowanz/neural-motifs
"""
class SGRecall(SceneGraphEvaluation):
    def __init__(self, result_dict):
        super(SGRecall, self).__init__(result_dict)

    def register_container(self, mode):
        # self.result_dict[mode + '_recall'] = {20: [], 50: [], 100: []}

        self.result_dict[mode + '_recall'] = {}
        for key in [5, 10, 15, 20, 25, 30, 50, 100]:
            self.result_dict[mode + '_recall'][key] = []

    def generate_print_string(self, mode):
        result_str = 'SGG eval: '
        for k, v in self.result_dict[mode + '_recall'].items():
            result_str += '    R @ %d: %.4f; ' % (k, np.mean(v))
        result_str += ' for mode=%s, type=Recall(Main).' % mode
        result_str += '\n'
        return result_str

    def calculate_recall(self, global_container, local_container, mode):
        rel_pair_idx = local_container['rel_pair_idx']
        rel_probs = local_container['rel_probs']
        gt_rels = local_container['gt_rels']
        gt_classes = local_container['gt_classes']
        gt_boxes = local_container['gt_boxes']
        pred_classes = local_container['pred_classes']
        pred_boxes = local_container['pred_boxes']
        obj_probs = local_container['obj_probs']

        iou_thres = global_container['iou_thres']

        # print('rel_pair_idx', rel_pair_idx.shape, 'obj_probs', obj_probs.shape, 'rel_probs', rel_probs.shape)
        # Sort the predicted possible based on the (sub_score*pred_score*obj_score)
        pred_rel_inds, obj_scores, rel_scores, _ = sort_the_relations(rel_pair_idx, obj_probs, torch.from_numpy(rel_probs))
        # print('pred_rel_inds', pred_rel_inds.shape, 'obj_scores', obj_scores.shape, 'rel_scores', rel_scores.shape)

        pred_rels = np.column_stack((pred_rel_inds, 1+rel_scores[:,1:].argmax(1))) # (#predted relations, (sub_id, obj_id, pred_lab))
        # print('pred_rels', pred_rels.shape)
        pred_scores = rel_scores[:,1:].max(1) # #predted relations
        # print('pred_scores',pred_scores.shape)

        # print('\ngt_rels', gt_rels.shape, 'pred_rels', pred_rels.shape)
        # print('gt_boxes',gt_boxes.shape, 'pred_boxes',pred_boxes.shape)
        # Converts (m1, (sub_id, obj_id, pred_label)) to (m1, (sub_label, obj_label, pred_label))
        # Transforms (#entities, bbox) to (#triplets, bbox)
        gt_triplets, gt_triplet_boxes, _ = _triplet(gt_rels, gt_classes, gt_boxes)
        local_container['gt_triplets'] = gt_triplets
        local_container['gt_triplet_boxes'] = gt_triplet_boxes

        pred_triplets, pred_triplet_boxes, pred_triplet_scores = _triplet(
                pred_rels, pred_classes, pred_boxes, pred_scores, obj_scores)

        # print('\ngt_triplets',gt_triplets.shape, 'pred_triplets',pred_triplets.shape)
        # print('gt_triplet_boxes',gt_triplet_boxes.shape, 'pred_triplet_boxes',pred_triplet_boxes.shape)

        # Compute recall. It's most efficient to match once and then do recall after
        pred_to_gt = _compute_pred_matches(
            gt_triplets,
            pred_triplets,
            gt_triplet_boxes,
            pred_triplet_boxes,
            iou_thres,
            phrdet=mode=='phrdet',
        )
        local_container['pred_to_gt'] = pred_to_gt

        for k in self.result_dict[mode + '_recall']:
            # the following code are copied from Neural-MOTIFS
            match = reduce(np.union1d, pred_to_gt[:k])
            # print(len(pred_to_gt[:k]), len(match), gt_rels.shape[0])
            rec_i = float(len(match)) / float(gt_rels.shape[0])
            self.result_dict[mode + '_recall'][k].append(rec_i)

        return local_container

"""
Mean Recall: Proposed in:
https://arxiv.org/pdf/1812.01880.pdf CVPR, 2019
"""
class SGMeanRecall(SceneGraphEvaluation):
    def __init__(self, result_dict, num_rel, ind_to_predicates, print_detail=False):
        super(SGMeanRecall, self).__init__(result_dict)
        self.num_rel = num_rel
        self.print_detail = print_detail
        self.rel_name_list = ind_to_predicates[1:] # remove __background__

        self.rtypes = {
            'Head': ['on', 'has', 'of', 'wearing', 'in', 'near', 'with', 'behind', 'holding'],
            'Mid': ['wears', 'above', 'sitting on', 'under', 'riding', 'in front of', 'standing on'],
            'Tail': ['attached to', 'belonging to', 'at', 'walking on', 'carrying', 'over', 'watching',
                    'hanging from', 'for', 'looking at', 'parked on', 'eating', 'laying on', 'between',
                    'covering', 'and', 'covered in', 'using', 'along', 'on back of', 'to', 'mounted on',
                    'part of', 'lying on', 'walking in', 'from', 'painted on', 'growing on', 'across',
                    'against', 'made of', 'playing', 'flying in', 'says']
        }
    def register_container(self, mode):
        #self.result_dict[mode + '_recall_hit'] = {20: [0]*self.num_rel, 50: [0]*self.num_rel, 100: [0]*self.num_rel}
        #self.result_dict[mode + '_recall_count'] = {20: [0]*self.num_rel, 50: [0]*self.num_rel, 100: [0]*self.num_rel}
        # self.result_dict[mode + '_mean_recall'] = {20: 0.0, 50: 0.0, 100: 0.0}
        # self.result_dict[mode + '_mean_recall_collect'] = {20: [[] for i in range(self.num_rel)], 50: [[] for i in range(self.num_rel)], 100: [[] for i in range(self.num_rel)]}
        # self.result_dict[mode + '_mean_recall_list'] = {20: [], 50: [], 100: []}
        # self.result_dict[mode + '_mean_recall_HMT'] = {'Head':{20: 0.0, 50: 0.0, 100: 0.0},
        #                                                 'Mid': {20: 0.0, 50: 0.0, 100: 0.0},
        #                                                 'Tail': {20: 0.0, 50: 0.0, 100: 0.0}}

        self.result_dict[mode + '_recall_hit'] = {}
        self.result_dict[mode + '_recall_count'] = {}
        self.result_dict[mode + '_mean_recall'] = {}
        self.result_dict[mode + '_mean_recall_collect'] = {}
        self.result_dict[mode + '_mean_recall_list'] = {}
        self.result_dict[mode + '_mean_recall_HMT'] = {'Head':{}, 'Mid': {}, 'Tail': {}}
        for key in [5, 10, 15, 20, 25, 30, 50, 100]:
            self.result_dict[mode + '_mean_recall'][key] = 0.0
            self.result_dict[mode + '_mean_recall_collect'][key] = [[] for i in range(self.num_rel)]
            self.result_dict[mode + '_mean_recall_list'][key] = []
            self.result_dict[mode + '_mean_recall_HMT']['Head'][key] = 0.0
            self.result_dict[mode + '_mean_recall_HMT']['Mid'][key] = 0.0
            self.result_dict[mode + '_mean_recall_HMT']['Tail'][key] = 0.0

    def generate_print_string(self, mode):
        result_str = 'SGG eval: '
        for k, v in self.result_dict[mode + '_mean_recall'].items():
            result_str += '   mR @ %d: %.4f; ' % (k, float(v))
        result_str += ' for mode=%s, type=Mean Recall.' % mode
        result_str += '\n'
        if self.print_detail:

            result_str += '----------------------- HMT ------------------------\n'
            for rtype, recalls in self.result_dict[mode + '_mean_recall_HMT'].items():
                result_str += f'SGG eval {rtype}: '
                for k, v in recalls.items():
                    result_str += '   mR @ %d: %.4f; ' % (k, float(v))
                result_str += '\n'
            result_str += '\n'
            result_str += '----------------------- Details ------------------------\n'
            for n, r in zip(self.rel_name_list, self.result_dict[mode + '_mean_recall_list'][100]):
                result_str += '({}:{:.4f}) '.format(str(n), r)
            result_str += '\n'
            result_str += '--------------------------------------------------------\n'

        return result_str

    def collect_mean_recall_items(self, global_container, local_container, mode):
        pred_to_gt = local_container['pred_to_gt']
        gt_rels = local_container['gt_rels']

        for k in self.result_dict[mode + '_mean_recall_collect']:
            # the following code are copied from Neural-MOTIFS
            match = reduce(np.union1d, pred_to_gt[:k])
            # NOTE: by kaihua, calculate Mean Recall for each category independently
            # this metric is proposed by: CVPR 2019 oral paper "Learning to Compose Dynamic Tree Structures for Visual Contexts"
            recall_hit = [0] * self.num_rel
            recall_count = [0] * self.num_rel
            for idx in range(gt_rels.shape[0]):
                local_label = gt_rels[idx,2]
                recall_count[int(local_label)] += 1
                recall_count[0] += 1

            for idx in range(len(match)):
                local_label = gt_rels[int(match[idx]),2]
                recall_hit[int(local_label)] += 1
                recall_hit[0] += 1

            for n in range(self.num_rel):
                if recall_count[n] > 0:
                    self.result_dict[mode + '_mean_recall_collect'][k][n].append(float(recall_hit[n] / recall_count[n]))

    def calculate_mean_recall(self, mode):
        no_samples_rtype = {'Head': len(self.rtypes['Head']),
                            'Mid': len(self.rtypes['Mid']),
                            'Tail':len(self.rtypes['Tail'])}
        for k, v in self.result_dict[mode + '_mean_recall'].items():
            sum_recall = 0
            num_rel_no_bg = self.num_rel - 1
            for idx in range(num_rel_no_bg):
                if len(self.result_dict[mode + '_mean_recall_collect'][k][idx+1]) == 0:
                    tmp_recall = 0.0
                else:
                    tmp_recall = np.mean(self.result_dict[mode + '_mean_recall_collect'][k][idx+1])
                self.result_dict[mode + '_mean_recall_list'][k].append(tmp_recall)
                sum_recall += tmp_recall

                for rtype, relations in self.rtypes.items():
                    if self.rel_name_list[idx] in relations:
                        self.result_dict[mode + '_mean_recall_HMT'][rtype][k] += (tmp_recall/no_samples_rtype[rtype])

            self.result_dict[mode + '_mean_recall'][k] = sum_recall / float(num_rel_no_bg)
        return

"""
Accumulate Recall:
calculate recall on the whole dataset instead of each image
"""
class SGAccumulateRecall(SceneGraphEvaluation):
    def __init__(self, result_dict):
        super(SGAccumulateRecall, self).__init__(result_dict)

    def register_container(self, mode):
        # self.result_dict[mode + '_accumulate_recall'] = {20: 0.0, 50: 0.0, 100: 0.0}
        self.result_dict[mode + '_accumulate_recall'] = {}
        for key in [5, 10, 15, 20, 25, 30, 50, 100]:
            self.result_dict[mode + '_accumulate_recall'][key] = []

    def generate_print_string(self, mode):
        result_str = 'SGG eval: '
        for k, v in self.result_dict[mode + '_accumulate_recall'].items():
            result_str += '   aR @ %d: %.4f; ' % (k, float(v))
        result_str += ' for mode=%s, type=Accumulate Recall.' % mode
        result_str += '\n'
        return result_str

    def calculate_accumulate(self, mode):
        for k, v in self.result_dict[mode + '_accumulate_recall'].items():
            self.result_dict[mode + '_accumulate_recall'][k] = float(self.result_dict[mode + '_recall_hit'][k][0]) / float(self.result_dict[mode + '_recall_count'][k][0] + 1e-10)

        return

def _triplet(relations, classes, boxes, predicate_scores=None, class_scores=None):
    """
    format relations of (sub_id, ob_id, pred_label) into triplets of (sub_label, pred_label, ob_label)
    Parameters:
        relations (#rel, 3) : (sub_id, ob_id, pred_label)
        classes (#objs, ) : class labels of objects
        boxes (#objs, 4)
        predicate_scores (#rel, ) : scores for each predicate
        class_scores (#objs, ) : scores for each object
    Returns:
        triplets (#rel, 3) : (sub_label, pred_label, ob_label)
        triplets_boxes (#rel, 8) array of boxes for the parts
        triplets_scores (#rel, 3) : (sub_score, pred_score, ob_score)
    """
    sub_id, ob_id, pred_label = relations[:, 0], relations[:, 1], relations[:, 2]
    try:
        triplets = np.column_stack((classes[sub_id], pred_label, classes[ob_id]))
    except:
        print(relations)
        print(classes, sub_id, ob_id)

        print(classes.shape, sub_id.shape, ob_id.shape)
        assert False
    triplet_boxes = np.column_stack((boxes[sub_id], boxes[ob_id]))

    triplet_scores = None
    if predicate_scores is not None and class_scores is not None:
        triplet_scores = np.column_stack((
            class_scores[sub_id], predicate_scores, class_scores[ob_id],
        ))

    return triplets, triplet_boxes, triplet_scores

def _compute_pred_matches(gt_triplets, pred_triplets,
                 gt_boxes, pred_boxes, iou_thres, phrdet=False):
    """
    Given a set of predicted triplets, return the list of matching GT's for each of the
    given predictions
    Return:
        pred_to_gt [List of List]
    """
    # This performs a matrix multiplication-esque thing between the two arrays
    # Instead of summing, we want the equality, so we reduce in that way
    # The rows correspond to GT triplets, columns to pred triplets
    keeps = intersect_2d(gt_triplets, pred_triplets)
    gt_has_match = keeps.any(1)
    # print('\ngt_pred_triplet_CM ', keeps.shape)
    # print('#gt_with_pred_match ', np.where(gt_has_match)[0].shape, 'gt_idx_pred_match ', keeps[gt_has_match].shape)

    # Iterate over the GT triplets that matches with the predicted triplets
    pred_to_gt = [[] for x in range(pred_boxes.shape[0])]
    for gt_ind, gt_box, keep_inds in zip(np.where(gt_has_match)[0],
                                         gt_boxes[gt_has_match],
                                         keeps[gt_has_match],
                                         ):
        # Extract all the predicted boxes that match the current GT triplet
        boxes = pred_boxes[keep_inds]

        # Of these pred boxes filter the ones that satisfies the IOU condition the current GT bboxe
        if phrdet:
            # Evaluate where the union box > 0.5
            gt_box_union = gt_box.reshape((2, 4))
            gt_box_union = np.concatenate((gt_box_union.min(0)[:2], gt_box_union.max(0)[2:]), 0)

            box_union = boxes.reshape((-1, 2, 4))
            box_union = np.concatenate((box_union.min(1)[:,:2], box_union.max(1)[:,2:]), 1)

            inds = compute_iou(gt_box_union[None], box_union)[0] >= iou_thres
        else:

            sub_iou = compute_iou(gt_box[None,:4], boxes[:, :4])[0]
            obj_iou = compute_iou(gt_box[None,4:], boxes[:, 4:])[0]

            inds = (sub_iou >= iou_thres) & (obj_iou >= iou_thres)

        # print(np.where(keep_inds)[0], np.where(keep_inds)[0][inds])

        for i in np.where(keep_inds)[0][inds]:
            pred_to_gt[i].append(int(gt_ind))
    # print('pred_to_gt ', len(pred_to_gt))
    return pred_to_gt

def evaluate_relation_of_one_image(groundtruth, prediction, global_container, evaluator, mode):

    # Traditional Metric with Graph Constraint
    # NOTE: this is the MAIN evaluation function, it must be run first (several important variables need to be update)
    local_container = evaluator['eval_recall'].calculate_recall(global_container, local_container, mode)

    # GT Pair Accuracy
    evaluator['eval_pair_accuracy'].calculate_recall(global_container, local_container, mode)
    # Mean Recall
    evaluator['eval_mean_recall'].collect_mean_recall_items(global_container, local_container, mode)

    return

def sort_the_relations(rel_pair_idx, obj_class_prob, rel_class_prob):

    # print('rel_pair_idx ', rel_pair_idx.shape, 'obj_class_prob ', obj_class_prob.shape, 'rel_class_prob ', rel_class_prob.shape)
    # Getting the object ids from the object probabilities ignoring the background
    obj_scores, obj_pred = obj_class_prob[:, 1:].max(dim=1)
    obj_pred = obj_pred + 1 # adding 1 as the background was ignored in previous step

    # sorting triples according to score production
    # Splitting the obj_scores into sub and obj based on the rel_pair_idx
    obj_scores0 = obj_scores[rel_pair_idx[:, 0]]
    obj_scores1 = obj_scores[rel_pair_idx[:, 1]]
    # print('obj_scores0 ', obj_scores0.shape, 'obj_scores1 ', obj_scores1.shape)
    # Getting the relation ids from the relation probabilities ignoring the N/R
    rel_scores, rel_class = rel_class_prob[:, 1:].max(dim=1)
    rel_class = rel_class + 1 # adding 1 as the N/R was ignored in previous step
    # print('rel_scores ', rel_scores.shape, 'rel_class ', rel_class.shape)

    # TODO Kaihua: how about using weighted some here?  e.g. rel*1 + obj *0.8 + obj*0.8
    triple_scores = rel_scores * obj_scores0 * obj_scores1
    _, sorting_idx = torch.sort(triple_scores.view(-1), dim=0, descending=True)
    rel_pair_idx = rel_pair_idx[sorting_idx] # (#rel, 2)

    rel_class_prob = rel_class_prob[sorting_idx]# (#rel, #rel_class)
    rel_labels = rel_class[sorting_idx] # (#rel, )

    return rel_pair_idx, obj_scores.numpy(), rel_class_prob.numpy(), rel_labels.numpy()

if __name__ == "__main__":

    # with open('metrics/local_container.pkl', "rb") as input_file:
    #     local_container = pickle.load(input_file)
    with open('metrics/ind_to_predicates.pkl', "rb") as input_file:
        ind_to_predicates = pickle.load(input_file)
    # with open('metrics/groundtruths.pkl', "rb") as input_file:
    #     groundtruths = pickle.load(input_file)
    # with open('metrics/predictions.pkl', "rb") as input_file:
    #     predictions = pickle.load(input_file)

    modes = ['predcls', 'sgcls', 'sgdet']
    result_dict = {}

    # prepare all inputs
    global_container = {}
    global_container['zeroshot_triplet'] = None
    global_container['result_dict'] = result_dict
    global_container['mode'] = modes[0]
    global_container['multiple_preds'] = False
    global_container['num_rel_category'] = 51
    global_container['iou_thres'] = 0.5
    global_container['attribute_on'] = False
    global_container['num_attributes'] = 201

    num_rel_category = 51
    result_dict = {}
    # tradictional Recall@K
    eval_recall = SGRecall(result_dict)
    eval_recall.register_container(mode=modes[0])
    mean_recall = SGMeanRecall(result_dict, num_rel_category, ind_to_predicates, print_detail=True)
    mean_recall.register_container(mode=modes[0])

    for _ in range(10):

        # Keep creating local containers with the predictions
        local_container = eval_recall.calculate_recall(global_container, local_container, modes[0])

        mean_recall.collect_mean_recall_items(global_container, local_container, mode=modes[0])

    mean_recall.calculate_mean_recall(mode = modes[0])

    string = eval_recall.generate_print_string(mode = modes[0])
    string += mean_recall.generate_print_string(mode = modes[0])

    print(string)


import torch
import torch.nn as nn
import torch.nn.functional as F

def weights_init(m):

    classname = m.__class__.__name__

    if hasattr(m, 'weight') and classname.find('Conv') != -1:
        nn.init.normal_(m.weight.data, 0.0, 0.02)
        if hasattr(m, "bias") and m.bias is not None:
            nn.init.constant_(m.bias.data, 0.0)

    elif classname.find('BatchNorm2d') != -1:
        nn.init.normal_(m.weight.data, 1.0, 0.02)
        nn.init.constant_(m.bias.data, 0.0)

# ------
# ResNet
# ------
class ResidualBlock(nn.Module):

    def __init__(self, in_channels):
        super(ResidualBlock, self).__init__()

        self.block = nn.Sequential(
            nn.ReflectionPad2d(1),
            nn.Conv2d(in_channels, in_channels, 3),
            nn.InstanceNorm2d(in_channels),
            nn.ReLU(inplace=True),
            nn.ReflectionPad2d(1),
            nn.Conv2d(in_channels, in_channels, 3),
            nn.InstanceNorm2d(in_channels)
        )

    def forward(self, x):

        return x + self.block(x)

# ---------
# Generator
# ---------
class ResnetGenerator(nn.Module):

    def __init__(self, channels, num_residual_blocks):
        super(ResnetGenerator, self).__init__()

        # Initial convolution block
        out_channels = 64
        layers = [
            nn.ReflectionPad2d(channels),
            nn.Conv2d(channels, out_channels, 7),
            nn.InstanceNorm2d(out_channels),
            nn.ReLU(inplace=True)
        ]
        in_channels = out_channels

        # Downsampling
        for _ in range(2):

            out_channels *= 2
            layers += [
                nn.Conv2d(in_channels, out_channels, 3, 2, 1),
                nn.InstanceNorm2d(out_channels),
                nn.ReLU(inplace=True)
            ]
            in_channels = out_channels

        # Residual blocks
        for _ in range(num_residual_blocks):

            layers += [ResidualBlock(out_channels)]

        # Upsampling
        for _ in range(2):

            out_channels //= 2
            layers += [
                nn.Upsample(scale_factor=2),
                nn.Conv2d(in_channels, out_channels, 3, 1, 1),
                nn.InstanceNorm2d(out_channels),
                nn.ReLU(inplace=True)
            ]
            in_channels = out_channels

        # Output_layer
        layers += [
            nn.ReflectionPad2d(channels),
            nn.Conv2d(out_channels, channels, 7),
            nn.Tanh()
        ]

        self.block = nn.Sequential(*layers)

    def forward(self, x):
        return self.block(x)

# -------------
# Discriminator
# -------------
class Discriminator(nn.Module):

    def __init__(self, in_channels):
        super(Discriminator, self).__init__()

        def discriminator_block(in_chs, out_chs, IN=True):

            layers = [nn.Conv2d(in_chs, out_chs, 4, 2, 1)]
            if IN:
                layers.append(nn.InstanceNorm2d(out_chs))
            layers.append(nn.LeakyReLU(0.2, inplace=True))

            return layers

        self.blocks = nn.Sequential(
            *discriminator_block(in_channels, 64, IN=False),
            *discriminator_block(64, 128),
            *discriminator_block(128, 256),
            *discriminator_block(256, 512),
            nn.ZeroPad2d((1, 0, 1, 0)),
            nn.Conv2d(512, 1, 4, padding=1)
        )

    def forward(self, img):

        return self.blocks(img)


import re

from streamlink.plugin import Plugin
from streamlink.plugin.api import http

_url_re = re.compile(r'''https?://(?:www\.)?teamliquid\.net/video/streams/''')

class Teamliquid(Plugin):
    @classmethod
    def can_handle_url(cls, url):
        return _url_re.match(url)

    def _get_streams(self):
        res = http.get(self.url)

        stream_address_re = re.compile(r'''href\s*=\s*"([^"]+)"\s*>\s*View on''')

        stream_url_match = stream_address_re.search(res.text)
        if stream_url_match:
            stream_url = stream_url_match.group(1)
            self.logger.info("Attempting to play streams from {0}", stream_url)
            return self.session.streams(stream_url)

__plugin__ = Teamliquid


from apscheduler.schedulers.blocking import BlockingScheduler
import logging
import telebot
import tzlocal
from settings import BOT_TOKEN, KIDS_GROUP_ID, CONVERSATION_LINK

logging.basicConfig(
    format='%(asctime)s [%(levelname)s] %(message)s',
    level=logging.INFO
)
schedule = BlockingScheduler(timezone=str(tzlocal.get_localzone()))
bot = telebot.TeleBot(BOT_TOKEN)

def instant_message(msg, *args, **kwargs):
    bot.send_message(KIDS_GROUP_ID, msg, *args, **kwargs)
    logging.info(f"Sent instant message '{msg}'")

def first_reminder():
    bot.send_message(
        KIDS_GROUP_ID,
        "*Доброго ранку, друзі\!*\n\n"
        "За 15 хв почнеться наше заняття 👩💻",
        parse_mode="MarkdownV2"
    )
    logging.info("Sent first reminder")

def second_reminder():
    bot.send_message(
        KIDS_GROUP_ID,
        "🔔🔔🔔 Саме час приєднатися до уроку, за 5 хв починаємо\n\n"
        f"[Приєднатися до заняття]({CONVERSATION_LINK})",
        disable_web_page_preview=True,
        parse_mode="MarkdownV2"
    )
    logging.info("Sent second reminder")

def main():
    # instant_message("Доброго ранку! Сподіваюся у всіх все чудово "
    #                 "і побачимося з вами завтра на нашому занятті!")
    schedule.add_job(first_reminder, trigger="cron",
                     day_of_week="Sat", hour=11, minute=15)
    schedule.add_job(second_reminder, trigger="cron",
                     day_of_week="Sat", hour=11, minute=25)
    schedule.start()

if __name__ == "__main__":
    main()


import logging
from time import time

from mist.api.users.models import User
from mist.api.users.models import Organization
from mist.api.users.models import WhitelistIP

from mongoengine import ValidationError
from mongoengine import OperationError
from mongoengine import InvalidQueryError

from mist.api.auth.models import get_secure_rand_token

from mist.api.exceptions import BadRequestError
from mist.api.exceptions import MethodNotAllowedError
from mist.api.exceptions import OrganizationOperationError

from mist.api.portal.models import Portal

from mist.api.helpers import ip_from_request

from mist.api import config

log = logging.getLogger(__name__)

def get_users_count(mongo_uri=None, confirmed=False):
    # return the number of all users, optionally confirmed only users
    if confirmed:
        return User.objects(status="confirmed").count()
    else:
        return User.objects().count()

def register_user(email, first_name, last_name, registration_method,
                  selected_plan=None, promo_code=None, token=None,
                  status='pending', create_organization=True, request=None):
    # User does not exist so we have to add him/her to the database
    # First make sure that email is not banned
    # Then create the User objects and the Organization
    if email.split('@')[1] in config.BANNED_EMAIL_PROVIDERS:
        raise MethodNotAllowedError("Email provider is banned.")

    user = User()
    user.email = email
    user.first_name = first_name
    user.last_name = last_name
    user.registration_method = registration_method
    user.registration_date = time()
    user.status = status
    user.activation_key = get_secure_rand_token()
    user.can_create_org = True
    user.save()

    # For some users registering through sso it might not be necessary to
    # create an organization, hence the flag
    org = create_org_for_user(user, '', promo_code, token, selected_plan) \
        if create_organization else None

    log_event_args = {
        'owner_id': org and org.id or '',
        'user_id': user.id,
        'first_name': user.first_name,
        'last_name': user.last_name,
        'company': user.feedback.company_name,
        'event_type': 'request',
        'action': 'register',
        'authentication_provider': registration_method
    }

    if request:
        log_event_args.update({
            'request_method': request.method,
            'request_path': request.path,
            'request_ip': ip_from_request(request),
            'user_agent': request.user_agent,
        })

    if org:
        log_event_args.update({
            'org_id': org.id,
            'org_name': org.name
        })

    # Create log for the registration of a user and if an org has been created
    # add the id and name of the org
    from mist.api.logs.methods import log_event
    log_event(**log_event_args)

    return user, org

def create_org_for_user(user, org_name='', promo_code=None, token=None,
                        selected_plan=None):
    org = Organization(name=org_name, selected_plan=selected_plan)
    org.add_member_to_team('Owners', user)
    org.name = org_name
    try:
        org.save()
    except ValidationError as e:
        raise BadRequestError({"msg": str(e), "errors": e.to_dict()})
    except OperationError:
        raise OrganizationOperationError()

    # assign promo if applicable
    if promo_code or token:
        if config.HAS_BILLING:
            from mist.billing.methods import assign_promo
            assign_promo(org, promo_code, token)
    return org

def get_user_data(auth_context):
    """
    This function sends user's data to socket
    :param auth_context:
    :return: dict
    """
    user = auth_context.user

    orgs = []
    for org in Organization.objects(members=user):
        o_dict = {
            'id': org.id,
            'name': org.name,
            'avatar': org.avatar,
            'members': len(org.members),
            'isOwner': user in org.get_team('Owners').members,
            'super_org': org.super_org
        }

        if org.super_org and Organization.objects(parent=org.id):
            sub_orgs = Organization.objects(parent=org.id)
            for sub_org in sub_orgs:
                sub = {
                    'id': sub_org.id,
                    'parent_id': sub_org.parent.id,
                    'name': sub_org.name,
                    'members': len(sub_org.members),
                    'isOwner': user in org.get_team('Owners').members,
                }
                orgs.append(sub)

        orgs.append(o_dict)

    ret = {
        'id': user.id,
        'email': user.email,
        'first_name': user.first_name,
        'last_name': user.last_name,
        'username': user.username,
        'ips': [ip.as_dict() for ip in user.ips],
        'current_ip': auth_context.token.ip_address,
        'has_pass': user.password is not None and user.password != '',
        'orgs': orgs,
        'csrf_token': auth_context.token.csrf_token,
    }
    if user.role == 'Admin':
        upgrades = Portal.get_singleton().get_available_upgrades()
        ret['available_upgrades'] = upgrades
    if config.HAS_BILLING:
        ret.update({
            'stripe_public_apikey': config.STRIPE_PUBLIC_APIKEY,
        })
    return ret

def filter_org(auth_context):
    org_dict = auth_context.org.as_dict()
    owner_policy = config.OWNER_POLICY
    rules = []
    for resource in owner_policy.keys():
        for action in owner_policy[resource].keys():
            tags, constraints = owner_policy[resource][action]
            rule = {"operator": "ALLOW", "action": action, "rtype": resource,
                    "rtags": tags, "constraints": constraints}
            rules.append(rule)

    org_dict['owner_policy'] = {"rules": rules, "operator": "ALLOW"}
    org_dict['is_owner'] = auth_context.is_owner()

    # SEC return my teams + visible teams or all teams if owner
    teams = [team for team in org_dict['teams']
             if team['visible'] or
             auth_context.user.id in team['members'] or
             auth_context.is_owner()]

    # Get info about members in my teams or of all org if owner
    team_mates = set()
    for t in teams:
        for m in t['members']:
            team_mates.add(m)

    members = [m for m in org_dict['members']
               if auth_context.is_owner() or m['id'] in team_mates]
    org_dict['teams'] = teams
    org_dict['members'] = members
    # Billing info
    if config.HAS_BILLING:
        from mist.billing.methods import populate_billing_info
        org_dict.update(populate_billing_info(auth_context.org))

    return org_dict

def update_whitelist_ips(auth_context, ips):
    """
    This function takes a list of dicts in the form:
    [{cidr:'cidr1', 'description:'desc1'},
    {cidr:'cidr2', 'description:'desc2'}]
    and saves them in the User.ips field.
    """

    user = auth_context.user

    user.ips = []
    for ip_dict in ips:
        wip = WhitelistIP()
        wip.cidr = ip_dict['cidr']
        wip.description = ip_dict['description']
        user.ips.append(wip)

    try:
        user.save()
    except ValidationError as e:
        raise BadRequestError({"msg": str(e), "errors": e.to_dict()})

def purge_org(org):
    from mist.api.clouds.models import Cloud
    from mist.api.clouds.methods import purge_cloud
    from mist.api.keys.models import Key
    from mist.api.scripts.models import Script
    from mist.api.schedules.models import Schedule
    from mist.api.rules.models import Rule
    from mist.api.notifications.models import Notification
    from mist.api.notifications.models import UserNotificationPolicy
    from mist.api.poller.models import OwnerPollingSchedule
    rtypes = [
        Key, Script, Schedule, Rule, Notification, UserNotificationPolicy,
        OwnerPollingSchedule]
    try:
        from mist.orchestration.models import Stack, Template
        rtypes.append(Stack)
        rtypes.append(Template)
        from mist.vpn.models import Tunnel
        rtypes.append(Tunnel)
    except ImportError:
        pass
    clouds = Cloud.objects(owner=org)
    for cloud in clouds:
        print("Purging cloud %s" % cloud)
        purge_cloud(cloud.id)
        print("Done")
    for rtype in rtypes:
        try:
            rtype.objects(owner=org).delete()
        except InvalidQueryError:
            try:
                org_id = org.id
            except AttributeError:
                org_id = org
            rtype.objects(owner_id=org_id).delete()
    try:
        from mist.rbac.mappings import UserMapping, RBACMapping
        UserMapping.objects(org=org).delete()
        RBACMapping.objects(org=org).delete()
    except ImportError:
        pass
    org.delete()