from .models import MatchModel, PlayerLeagueModel, GameModel
from django.db.models import F
from collections import OrderedDict, Counter
from django.db.models import Q

def table(player_list, game_list):
    player_point_dict = {}
    player_games_played_dict = {}
    player_wins_dict = {}
    player_draw_dict = {}
    player_defeat_dict = {}
    player_goal_scored = {}
    player_goal_conceded = {}
    player_goal_difference = {}
    for p in player_list:
        player_point_dict[str(p.pk)] = 0
        player_games_played_dict[str(p.pk)] = 0
        player_wins_dict[str(p.pk)] = 0
        player_draw_dict[str(p.pk)] = 0
        player_defeat_dict[str(p.pk)] = 0
        player_goal_scored[str(p.pk)] = 0
        player_goal_conceded[str(p.pk)] = 0
        player_goal_difference[str(p.pk)] = 1

    # Calculating the points of players. Every victory is 3 points, every draw is 1.
    for m in game_list:
        if m.h_score != None and m.a_score != None:
            player_games_played_dict[str(m.home_player.pk)] += 1
            player_games_played_dict[str(m.away_player.pk)] += 1
            player_goal_scored[str(m.home_player.pk)] += m.h_score
            player_goal_conceded[str(m.home_player.pk)] += m.a_score
            player_goal_scored[str(m.away_player.pk)] += m.a_score
            player_goal_conceded[str(m.away_player.pk)] += m.h_score

            if m.h_score > m.a_score:
                player_point_dict[str(m.home_player.pk)] += 3
                player_wins_dict[str(m.home_player.pk)] += 1
                player_defeat_dict[str(m.away_player.pk)] += 1
            elif m.h_score < m.a_score:
                player_point_dict[str(m.away_player.pk)] += 3
                player_wins_dict[str(m.away_player.pk)] += 1
                player_defeat_dict[str(m.home_player.pk)] += 1
            elif m.h_score == m.a_score:
                player_point_dict[str(m.home_player.pk)] += 1
                player_point_dict[str(m.away_player.pk)] += 1
                player_draw_dict[str(m.home_player.pk)] += 1
                player_draw_dict[str(m.away_player.pk)] += 1

    for k, v in player_point_dict.items():
        PlayerLeagueModel.objects.filter(pk=k).update(points=v)

    for k, v in player_wins_dict.items():
        PlayerLeagueModel.objects.filter(pk=k).update(wins=v)

    for k, v in player_draw_dict.items():
        PlayerLeagueModel.objects.filter(pk=k).update(draws=v)

    for k, v in player_defeat_dict.items():
        PlayerLeagueModel.objects.filter(pk=k).update(defeats=v)

    for k, v in player_games_played_dict.items():
        PlayerLeagueModel.objects.filter(pk=k).update(games_played=v)

    for k, v in player_goal_scored.items():
        PlayerLeagueModel.objects.filter(pk=k).update(goals_scored=v)

    for k, v in player_goal_conceded.items():
        PlayerLeagueModel.objects.filter(pk=k).update(goals_conceded=v)

    for k, v, in player_goal_scored.items():
        for ck, cv in player_goal_conceded.items():
            if k == ck:
                v -= cv
                player_goal_difference[k] = v

    for k, v in player_goal_difference.items():
        PlayerLeagueModel.objects.filter(pk=k).update(goal_difference=v)

def match_calc(game):
    match_dict = {}
    for g in game:
        match_dict[g.match.pk] = {g.match.player1.pk:0, g.match.player2.pk:0}

    for g in game:
        if g.h_score != None and g.a_score != None:
            match_dict[g.match.pk][g.home_player.pk] += g.h_score
            match_dict[g.match.pk][g.away_player.pk] += g.a_score

    for match_pk, match in match_dict.items():
        this_match = MatchModel.objects.get(pk=match_pk)
        MatchModel.objects.filter(pk=match_pk).update(score1=match[this_match.player1.pk],
                                          score2=match[this_match.player2.pk])

        if match[this_match.player1.pk] > match[this_match.player2.pk]:
            MatchModel.objects.filter(pk=this_match.pk).update(winner=this_match.player1)
            MatchModel.objects.filter(pk=this_match.pk).update(loser=this_match.player2)
        elif match[this_match.player1.pk] < match[this_match.player2.pk]:
            MatchModel.objects.filter(pk=this_match.pk).update(winner=this_match.player2)
            MatchModel.objects.filter(pk=this_match.pk).update(loser=this_match.player1)
        else:
            MatchModel.objects.filter(pk=this_match.pk).update(winner=None)
            MatchModel.objects.filter(pk=this_match.pk).update(loser=None)

def head_to_head_winner(player1, player2):
    try:
        match = MatchModel.objects.get(
            Q(player1=player1, player2=player2) |
            Q(player1=player2, player2=player1)
        )
        return match.winner
    except:
        match = None

def head_to_head_loser(player1, player2):
    match = MatchModel.objects.get(
        Q(player1=player1, player2=player2) |
        Q(player1=player2, player2=player1)
    )
    return match.loser

def sort_table(player_list):
    points_list = []
    for p in player_list:
        points_list.append(p)
    points_list.sort(key=lambda x: x.points, reverse=True)
    equal_point_players = []
    couples_list = []
    no_dub_couples = []
    sorted_by_head_to_head = []
    i = 0
    while i < len(points_list):
        j = 0
        while j < len(points_list):
            if points_list[i] != points_list[j] and points_list[i].points == points_list[j].points:
                if points_list[j] not in equal_point_players:
                    equal_point_players.append(points_list[j])
                    x = 0
                    while x < len(equal_point_players):
                        z = 0
                        while z < len(equal_point_players):
                            if equal_point_players[x] != equal_point_players[z] and equal_point_players[x].points == equal_point_players[z].points:
                                couples_list.append([equal_point_players[x], equal_point_players[z]])
                            z +=1
                        x +=1
                    for c in couples_list:
                        c.sort(key=lambda x: x.pk, reverse=True)
                    for nd in couples_list:
                        if nd not in no_dub_couples:
                            no_dub_couples.append(nd)
            j +=1
        i +=1
    for m in no_dub_couples:
        sorted_by_head_to_head.append(head_to_head_winner(m[0], m[1]))

    for p in player_list:
        if p.tournament.position_priority == 'Head to Head Matches':
            points_list.sort(key=lambda x: (x.points, sorted_by_head_to_head.count(x), x.goal_difference, x.goals_scored, x.wins), reverse=True)
        else:
            points_list.sort(key=lambda x: (x.points, x.goal_difference, sorted_by_head_to_head.count(x), x.goals_scored, x.wins), reverse=True)

    my_dict = {i: sorted_by_head_to_head.count(i) for i in sorted_by_head_to_head}
    return points_list


from collections import namedtuple

Point = namedtuple("Point", "x y height")

class HeightMap:
    def __init__(self, height_map):
        self.height_map = height_map
        self.y_count = len(self.height_map)
        self.x_count = len(self.height_map[0])

    def is_in_bounds(self, x, y):
        return x >= 0 and x < self.x_count and y >= 0 and y < self.y_count

    def get_height(self, x, y):
        if not self.is_in_bounds(x, y):
            return None
        return self.height_map[y][x]

    def calc_neighbor_points(self, x, y):
        neighbors = []

        new_points = [(x, y - 1), (x, y + 1), (x + 1, y), (x - 1, y)]
        for new_point in new_points:
            new_x, new_y = new_point
            height = self.get_height(new_x, new_y)
            if height != None:
                neighbors.append(Point(new_x, new_y, height))
        return neighbors

    def calc_neighbor_heights(self, x, y):
        return [point.height for point in self.calc_neighbor_points(x, y)]

    def is_lowpoint(self, point):
        return self.is_lowpoint_xy(point.x, point.y)

    def is_lowpoint_xy(self, x, y):
        height = self.get_height(x, y)
        if height is None:
            return None

        larger_neighbors = filter(
            lambda v: v <= height, self.calc_neighbor_heights(x, y)
        )
        return len(list(larger_neighbors)) == 0

    def calc_lowpoints(self):
        return list(filter(lambda point: self.is_lowpoint(point), self))

    def calc_total_risk(self):
        return sum([point.height + 1 for point in self.calc_lowpoints()])

    def calc_basin(self, lowpoint):
        basin = set()
        to_expand = [lowpoint]
        while len(to_expand) > 0:
            point = to_expand.pop()
            basin.add(point)
            neighbors = self.calc_neighbor_points(point.x, point.y)
            neighbors = filter(lambda point: point.height < 9, neighbors)
            to_expand.extend(set(neighbors).difference(basin))
        return basin

    def calc_basins(self):
        return [self.calc_basin(lowpoint) for lowpoint in self.calc_lowpoints()]

    def __iter__(self):
        for x in range(self.x_count):
            for y in range(self.y_count):
                yield Point(x, y, self.get_height(x, y))

    def __str__(self):
        rows_for_printing = []
        for row in self.height_map:
            rows_for_printing.append("".join([str(h) for h in row]))
        return "\n".join(rows_for_printing)

def read_height_map(filename="test_advent09.txt"):
    """return test data if no file is given"""
    height_map = []
    with open(filename) as file:
        for line in file:
            height_map.append([int(h) for h in line.strip()])
    return HeightMap(height_map)

if __name__ == "__main__":
    height_map = read_height_map("advent09.txt")
    total_risk = height_map.calc_total_risk()
    print(f"part1: total risk level is {total_risk}")

    m = read_height_map("advent09.txt")
    basin_sizes = sorted([len(basin) for basin in m.calc_basins()], reverse=True)
    multi = basin_sizes[0] * basin_sizes[1] * basin_sizes[2]
    print(
        f"part2: the largest basins are of size {basin_sizes[0:3]}, multiplied that's {multi}"
    )


name = "brain"
age = 10

def is_num(input):
    try:
        int(input)
        it_is = True
    except ValueError:
        it_is = False
    return it_is

def is_str(input):
    try:
        str(input)
        it_is = True
    except ValueError:
        it_is = False
    return it_is

def print_user_data(name, age):
    print(name, age)

def ask_for_name():
    return input("what is your name: ")

def ask_for_age():
    return input("what is your age :")

def pull_data():
    global name, age
    name = ask_for_name()
    age = ask_for_age()

def calc_user_decades():
    global age, name
    decades = str(float(age) / float(10))
    text = "Okay {} looks like you are {} and have lived for {} decades"
    print(text.format(name, age, decades))

def run():
    pull_data()
    if is_num(age) & is_str(name):
        calc_user_decades()
    else:
        print(
            "oops, looks like either your try again. make sure age is a number and name is text."
        )

run()


from keras import backend as K
import cv2
import os
import re
import csv
import numpy as np
import pandas as pd
from keras.utils import multi_gpu_model
from keras.layers import Input, Dropout, BatchNormalization, LeakyReLU, concatenate
from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, Conv2DTranspose, Dense
from keras.models import save_model, load_model, Model
from keras import applications
from keras.layers.pooling import GlobalAveragePooling2D
from keras.layers.core import Reshape, Masking, Lambda, Permute
from PIL import Image, ImageDraw, ImageFont
from yolo3.model_vgg16 import yolo_eval, yolo_body, tiny_yolo_body
from yolo3.utils import letterbox_image
# from ctpn.model import bekenctpn as ctpn
from prepare.image_rectification import jiaozheng, rotate_bound
from crnn import densenet

class YOLO(object):
    """
    YOLO模型
    """
    def __init__(self):
        self.model_path = 'best_model/trained_weights_final.h5' # model path or trained weights path
        self.anchors_path = 'best_model/yolo_anchors.txt'
        self.classes_path = 'best_model/coco_classes.txt'
        self.score = 0.3
        self.iou = 0.45
        self.gpu_num = 1
        self.class_names = self._get_class()
        self.anchors = self._get_anchors()
        self.sess = K.get_session()
        self.model_image_size = (416, 416) # fixed size or (None, None), hw
        self.boxes, self.scores, self.classes = self.generate()

    def _get_class(self):
        classes_path = os.path.expanduser(self.classes_path)
        with open(classes_path) as f:
            class_names = f.readlines()
        class_names = [c.strip() for c in class_names]
        return class_names

    def _get_anchors(self):
        anchors_path = os.path.expanduser(self.anchors_path)
        with open(anchors_path) as f:
            anchors = f.readline()
        anchors = [float(x) for x in anchors.split(',')]
        return np.array(anchors).reshape(-1, 2)

    def generate(self):
        '''to generate the bounding boxes'''
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.'

        # Load model, or construct model and load weights.
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        is_tiny_version = num_anchors==6 # default setting
        try:
            self.yolo_model = load_model(model_path, compile=False)
        except:
            self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors//2, num_classes) \
                if is_tiny_version else yolo_body(Input(shape=(None,None,3)), num_anchors//3, num_classes)
            self.yolo_model.load_weights(self.model_path) # make sure model, anchors and classes match
        else:
            assert self.yolo_model.layers[-1].output_shape[-1] == \
                num_anchors/len(self.yolo_model.output) * (num_classes + 5), \
                'Mismatch between model and given anchor and class sizes'

        print('{} model, anchors, and classes loaded.'.format(model_path))

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        if self.gpu_num>=2:
            self.yolo_model = multi_gpu_model(self.yolo_model, gpus=self.gpu_num)
        boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors,
                len(self.class_names), self.input_image_shape,
                score_threshold=self.score, iou_threshold=self.iou)
        # default arg
        # self.yolo_model->'model_data/yolo.h5'
        # self.anchors->'model_data/yolo_anchors.txt'-> 9 scales for anchors
        return boxes, scores, classes

    def detect_image(self, image):
        # start = timer()
        rects = []
        if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        # print(image_data.shape)
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.

        # tf.Session.run(fetches, feed_dict=None)
        # Runs the operations and evaluates the tensors in fetches.
        #
        # Args:
        # fetches: A single graph element, or a list of graph elements(described above).
        #
        # feed_dict: A dictionary that maps graph elements to values(described above).
        #
        # Returns:Either a single value if fetches is a single graph element, or a
        # list of values if fetches is a list(described above).
        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                K.learning_phase(): 0
            })

        print('Found {} boxes for {}'.format(len(out_boxes), 'img'))

        # font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
        #             size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
        # thickness = (image.size[0] + image.size[1]) // 500

        classes = []
        for i, c in reversed(list(enumerate(out_classes))):
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            score = out_scores[i]

            # label = '{} {:.2f}'.format(predicted_class, score)
            # draw = ImageDraw.Draw(image)
            # label_size = draw.textsize(label, font)

            y1, x1, y2, x2 = box
            y1 = max(0, np.floor(y1 + 0.5).astype('float32'))
            x1 = max(0, np.floor(x1 + 0.5).astype('float32'))
            y2 = min(image.size[1], np.floor(y2 + 0.5).astype('float32'))
            x2 = min(image.size[0], np.floor(x2 + 0.5).astype('float32'))
            # print(label, (x1, y1), (x2, y2))
            bbox = dict([("score",str(score)),("x1",str(x1)),("y1", str(y1)),("x2", str(x2)),("y2", str(y2))])
            rects.append(bbox)
            classes.append(predicted_class)

        return rects, classes

    def close_session(self):
        self.sess.close()

class Unet(object):
    """
    Unet模型
    """
    def __init__(self):
        self.model_path = 'best_model/unet_weights.h5' # model path or trained weights path
        self.input_size_1 = 1024
        self.input_size_2 = 1024
        self.model = self.create_model()
        self.model.load_weights(self.model_path)
        print('{} model loaded.'.format(self.model_path))

    def Conv2d_BN(self, x, nb_filter, kernel_size, strides=(1, 1), padding='same'):
        x = Conv2D(nb_filter, kernel_size, strides=strides, padding=padding)(x)
        x = BatchNormalization(axis=3)(x)
        x = LeakyReLU(alpha=0.1)(x)
        return x

    def Conv2dT_BN(self, x, filters, kernel_size, strides=(2, 2), padding='same'):
        x = Conv2DTranspose(filters, kernel_size, strides=strides, padding=padding)(x)
        x = BatchNormalization(axis=3)(x)
        x = LeakyReLU(alpha=0.1)(x)
        return x

    def create_model(self):
        inpt = Input(shape=(self.input_size_1, self.input_size_2, 3))

        conv1 = self.Conv2d_BN(inpt, 8, (3, 3))
        conv1 = self.Conv2d_BN(conv1, 8, (3, 3))
        pool1 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')(conv1)

        conv2 = self.Conv2d_BN(pool1, 16, (3, 3))
        conv2 = self.Conv2d_BN(conv2, 16, (3, 3))
        pool2 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')(conv2)

        conv3 = self.Conv2d_BN(pool2, 32, (3, 3))
        conv3 = self.Conv2d_BN(conv3, 32, (3, 3))
        pool3 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')(conv3)

        conv4 = self.Conv2d_BN(pool3, 64, (3, 3))
        conv4 = self.Conv2d_BN(conv4, 64, (3, 3))
        pool4 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same')(conv4)

        conv5 = self.Conv2d_BN(pool4, 128, (3, 3))
        # conv5 = Dropout(0.1)(conv5)
        conv5 = self.Conv2d_BN(conv5, 128, (3, 3))
        # conv5 = Dropout(0.1)(conv5)

        convt1 = self.Conv2dT_BN(conv5, 64, (3, 3))
        concat1 = concatenate([conv4, convt1], axis=3)
        # concat1 = Dropout(0.1)(concat1)
        conv6 = self.Conv2d_BN(concat1, 64, (3, 3))
        conv6 = self.Conv2d_BN(conv6, 64, (3, 3))

        convt2 = self.Conv2dT_BN(conv6, 32, (3, 3))
        concat2 = concatenate([conv3, convt2], axis=3)
        # concat2 = Dropout(0.1)(concat2)
        conv7 = self.Conv2d_BN(concat2, 32, (3, 3))
        conv7 = self.Conv2d_BN(conv7, 32, (3, 3))

        convt3 = self.Conv2dT_BN(conv7, 16, (3, 3))
        concat3 = concatenate([conv2, convt3], axis=3)
        # concat3 = Dropout(0.1)(concat3)
        conv8 = self.Conv2d_BN(concat3, 16, (3, 3))
        conv8 = self.Conv2d_BN(conv8, 16, (3, 3))

        convt4 = self.Conv2dT_BN(conv8, 8, (3, 3))
        concat4 = concatenate([conv1, convt4], axis=3)
        # concat4 = Dropout(0.1)(concat4)
        conv9 = self.Conv2d_BN(concat4, 8, (3, 3))
        conv9 = self.Conv2d_BN(conv9, 8, (3, 3))
        # conv9 = Dropout(0.1)(conv9)
        outpt = Conv2D(filters=3, kernel_size=(1, 1), strides=(1, 1), padding='same', activation='sigmoid')(conv9)

        model = Model(inpt, outpt)
        model.compile(loss='mean_squared_error', optimizer='Nadam', metrics=['mae'])
        # model.summary()
        return model

    def predict(self, path):
        imgorg = cv2.imread(path)
        imgorg = cv2.resize(imgorg, (self.input_size_1, self.input_size_2))
        imgorg = np.array(imgorg, 'f') / 255.0
        x = np.array([imgorg])
        y = self.model.predict(x)[0] * 255.0
        return y

# class CPTN(object):
#     """
#     cptn模型
#     """
#     def __init__(self):
#         self.VGG_PATH = 'model/vgg16_weights.h5'
#         self.CTPN_MODEL_PATH = 'model/ctpn_weights.hdf5'
#         self.fold_path = 'output_imgs'
#         self.model = ctpn.Ctpn()
#         self.model.bulid_model(self.VGG_PATH)
#         self.model.load_model(self.CTPN_MODEL_PATH)
#
#     def predict(self, img_org, img_name):
#         """
#         cptn内部定位
#         :param imgorg:
#         :param ctpn_model:
#         :param savepath:
#         :return:
#         """
#         img, text, scale, angle = self.model.predict(img_org)
#
#         img_copy = img_org.copy()
#
#         rec_list = []   # 全部定位框的数组
#         for box in text:
#             dict = {}
#             w = int((box[2] - box[0]) / scale)
#             h = int((box[5] - box[1]) / scale)
#             POINT_y = int(box[1] / scale)
#             POINT_x = int(box[0] / scale)
#
#             cv2.rectangle(img_copy, (POINT_x, POINT_y), (POINT_x + w, POINT_y + h), (0, 255, 0), 2)
#             rec_list.append([POINT_x, POINT_y, POINT_x + w, POINT_y + h])
#
#         save_path = self.fold_path + '/' + img_name
#         cv2.imwrite(save_path, img_copy)
#         return rec_list

class Classify(object):
    """
    分类模型
    """
    def __init__(self, model_path):
        self.sizew = 480
        self.sizeh = 350
        self.n_class = 2
        self.model_path = model_path
        self.model = self.create_model()
        self.model.load_weights(self.model_path)
        print('{} model loaded.'.format(self.model_path))

    def create_model(self):
        # Transfer learning with Inception V3
        # base_model = applications.MobileNetV2(include_top=False, input_shape=(self.sizew, self.sizeh, 3), weights=None)
        base_model = applications.MobileNetV2(include_top=False, weights=None,
                                              input_shape=(self.sizeh, self.sizew, 3))  # (高，宽，通道数)

        x = base_model.output
        # 每个特征图平均成一个特征 也是展平的效果
        x = GlobalAveragePooling2D()(x)

        # 全连接网络
        x = Dense(64, activation='relu')(x)
        x = Dense(32, activation='relu')(x)
        # x = Dropout(0.1)(x)
        x = Dense(self.n_class, activation='softmax')(x)

        model = Model(inputs=base_model.input, outputs=x)
        model.compile(loss='categorical_crossentropy', optimizer='adam',
                      metrics=['accuracy'])
        # model.summary()
        return model

    def predict(self, img):
        x = img
        x = cv2.resize(x, (self.sizew, self.sizeh))  # (width, height)
        X = [x]
        X = np.array(X)
        X = X / 255.0
        y = self.model.predict(X)  # 格式为[[0.96, 0.04]]
        y_label = np.argmax(y)  # 转化为0或1，0为倒，1为正
        return y_label

class CRNN(object):
    """
    CRNN模型
    """
    def __init__(self, model_path, charfile_path):
        self.img_h = 32
        self.img_w = 240
        self.batch_size = 2
        self.maxlabellength = 10
        self.model_path = model_path
        self.charfile_path = charfile_path
        self.n_class, self.char_set = self.get_n_class()
        self.basemodel = self.creat_model()
        print('{} model loaded.'.format(self.model_path))

    def creat_model(self):
        input = Input(shape=(self.img_h, None, 1), name='the_input')
        y_pred = densenet.crnn2(input, self.n_class)

        basemodel = Model(inputs=input, outputs=y_pred)
        # basemodel.summary()

        labels = Input(name='the_labels', shape=[None], dtype='float32')
        input_length = Input(name='input_length', shape=[1], dtype='int64')
        label_length = Input(name='label_length', shape=[1], dtype='int64')

        loss_out = Lambda(self.ctc_lambda_func, output_shape=(1,), name='ctc')(
            [y_pred, labels, input_length, label_length])

        model = Model(inputs=[input, labels, input_length, label_length], outputs=loss_out)
        model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer='adam', metrics=['accuracy'])
        # model.summary()
        model.load_weights(self.model_path)
        return basemodel

    def ctc_lambda_func(self, args):
        y_pred, labels, input_length, label_length = args
        return K.ctc_batch_cost(labels, y_pred, input_length, label_length)

    def get_n_class(self):
        char_set = open(self.charfile_path, 'r', encoding='utf-8').readlines()
        char_set = ''.join([ch.strip('\n') for ch in char_set][1:] + ['卍'])  #
        return len(char_set), char_set

    def decode(self, pred):
        '''
        ctc模型输出decode编码为最终输出
        :param pred: 模型输出
        :return:
        '''
        char_list = []
        pred_text = pred.argmax(axis=2)[0]
        for i in range(len(pred_text)):
            if pred_text[i] != self.n_class - 1 and (
                    (not (i > 0 and pred_text[i] == pred_text[i - 1])) or (i > 1 and pred_text[i] == pred_text[i - 2])):
                char_list.append(self.char_set[pred_text[i]])
        return u''.join(char_list)

    def predict(self, img):
        out = ''
        try:
            # cv2.imshow('',img)
            # cv2.waitKey(0)
            img = Image.fromarray(img).convert('L')
            width, height = img.size[0], img.size[1]
            # if height < 20:
            #     return ''
            scale = height * 1.0 / self.img_h
            width = int(width / scale)
            img = img.resize([width, self.img_h], Image.ANTIALIAS)
            # cv2.imwrite('save_2_' + img_name, np.array(img))
            img = np.array(img).astype(np.float32) / 255.0 - 0.5
            x = img.reshape([1, self.img_h, width, 1])
            if width > 15:
                y_pred = self.basemodel.predict(x)
                # print(y_pred)
                y_pred = y_pred[:, :, :]  # batch_size,time_step,num_classes
                out = self.decode(y_pred)
            # print(out)
        except:
            pass
        return out

    def crop_predict(self, img, rec):
        crop = img[rec[1]:rec[3], rec[0]:rec[2]]
        out = self.predict(crop)
        return out

if __name__ == '__main__':
    # data_path = 'imgs'      # 数据集路径
    data_path = 'E:/projects/python/DF_idcard/data/Test'
    # data_path = 'E:/projects/python/DF_idcard/data/train_data'

    output_path = 'output_imgs'  # 保存中间文件的路径
    notrect_file = output_path + "/notrec_imgs.csv"  # 保存为进行识别的图片名

    yolo = YOLO()    # 加载yolo模型
    unet = Unet()    # 加载Unet模型
    # ctpn = CPTN()     # 加载CPTN模型

    # 加载分类模型
    # front_weights_path = 'model/front_classify_weights.h5'     # 正面分类权值文件
    front_weights_path = 'model/0904_zf_front_weights-net-0010-0.000008.h5'  # 正面分类权值文件

    back_weights_path = 'model/back_classify_weights.h5'    # 背面分类权值文件
    front_classify = Classify(front_weights_path)
    back_classify = Classify(back_weights_path)

    # 加载CRNN模型
    # char_weighs_path = 'model/char.h5'
    # letternum_weights_path = 'model/letternum.h5'
    # char_file_path = 'model_data/char.txt'
    # letternum_file_path = 'model_data/letternum.txt'
    # char_crnn = CRNN(char_weighs_path, char_file_path)
    # letternum_crnn = CRNN(letternum_weights_path, letternum_file_path)

    path_list = os.listdir(data_path)  # 列出文件夹下所有的目录与文件

    # 建立result.csv提交文件
    path = output_path + '/result.csv'
    with open(path, 'wb') as f:
        csv_write = csv.writer(f)
        pass

    yolo_list = []

    # 循环进行识别
    for i in range(0, len(path_list)):
        name, ext = os.path.splitext(path_list[i])
        if ext == '.png' or ext == '.PNG' or ext == '.JPG' or ext == '.jpg':
            print(path_list[i])
            img_path = data_path + '/' + path_list[i]
            img_name = img_path.split('/')[-1]

            # yolo框出正面背面图片
            image = Image.open(img_path)
            rects, classes = yolo.detect_image(image)  # <class 'list'>: [{'score': '0.064195335', 'x1': '94.0', 'y1': '492.0', 'x2': '635.0', 'y2': '815.0'}, {'score': '0.061926477', 'x1': '78.0', 'y1': '82.0', 'x2': '660.0', 'y2': '390.0'}], #  class 'list'>: ['fan', 'zheng']

            if (len(classes)!=2 or 'zheng' not in classes or 'fan' not in classes):
                with open(notrect_file, 'a+') as f:
                    print('error =========> ',img_path,len(classes))
                    f.write(img_name +","+ "+".join(classes) + '\n')
                # continue

            if len(set(classes)) != 2:
                continue

            card_info = {}
            # 抠图：用yolo得到的坐标在unet还原后的图片上抠图
            box_list = []
            merge_blank = 0  # 两幅图接近时有问题，暂时删除padding

            for idx, rec in enumerate(rects):

                if classes[idx] == 'zheng':

                    if card_info.__contains__('front_score') and float(card_info['front_score']) > float(rec['score']):
                        continue

                    card_info['file'] = name
                    card_info['front_score'] = float(rec['score'])
                    card_info['front_x1'] = int(float(rec['x1']))
                    card_info['front_y1'] = int(float(rec['y1']))
                    card_info['front_x2'] = int(float(rec['x2']))
                    card_info['front_y2'] = int(float(rec['y2']))

                    box_list.append(
                        [int(float(rec['x1'])), max(int(float(rec['y1'])) - merge_blank, 0),
                         int(float(rec['x2'])), min(int(float(rec['y2'])) + merge_blank, 1000)])  # objBoxSet <class 'list'>: [['94.0', '492.0', '635.0', '815.0'], ['78.0', '82.0', '660.0', '390.0']]

                if classes[idx] == 'fan':

                    if card_info.__contains__('back_score') and float(card_info['back_score']) > float(rec['score']):
                        continue

                    card_info['file'] = name
                    card_info['back_score'] = float(rec['score'])
                    card_info['back_x1'] = int(float(rec['x1']))
                    card_info['back_y1'] = int(float(rec['y1']))
                    card_info['back_x2'] = int(float(rec['x2']))
                    card_info['back_y2'] = int(float(rec['y2']))

                    box_list.append(
                        [int(float(rec['x1'])), max(int(float(rec['y1'])) - merge_blank, 0),
                         int(float(rec['x2'])), min(int(float(rec['y2'])) + merge_blank, 1000)])  # objBoxSet <class 'list'>: [['94.0', '492.0', '635.0', '815.0'], ['78.0', '82.0', '660.0', '390.0']]

            # 新增加 计算两幅接近程度 1017
            min_space = min(abs(float(card_info['back_y1']) - float(card_info['front_y2'])),abs(float(card_info['front_y1']) - float(card_info['back_y2'])))

            # 寻找图片边界
            back_front_flag = 0
            merge_blank_min_h = 15 # 最小边界
            merge_blank_nor_h = 15 # 正常边界
            if min_space < 30:
                merge_blank_min_h = int(min_space * 0.5)  # 收缩图片

                if min_space < 15:
                    # merge_blank_min_h = -3 # 收缩图片,防止重叠
                    merge_blank_min_h = 0  # 收缩图片,防止重叠

                # back - front
                if abs(float(card_info['back_y1']) - float(card_info['front_y2'])) < abs(float(card_info['front_y1']) - float(card_info['back_y2'])):
                    back_front_flag = 1 # 背面在上，正面在下
            else:
                merge_blank_min_h = 15  # 增加边界

            # # --------------------原图（保存在不同的文件夹） 尝试 会导致（f940a20e64dd42ddaf157ae830d9e1d0）颠倒（71efe9b12bb44f2f96e3e0adb80304a7）倾斜
            # img_leave = cv2.imread(img_path)
            img_src = cv2.imread(img_path)

            # unet对原图去水印
            y = unet.predict(img_path)
            cv2.imwrite(output_path + '/unet_img.jpg', y)
            img_leave = cv2.imread(output_path + '/unet_img.jpg')

            croped_dict = {}  # 正面图片为img_crops[0], 背面图片为img_crops[1]
            src_croped_dict = {}

            for idx, box in enumerate(box_list):
                # 遍历每种类型的锚点框
                img_class = classes[idx]

                top_blank = merge_blank_nor_h
                bottom_blank = merge_blank_nor_h

                # 存在不同的边界高度(两幅图 中间部分
                if merge_blank_min_h != merge_blank_nor_h :

                    if back_front_flag == 1 and img_class == "zheng" :
                        top_blank = merge_blank_nor_h
                        bottom_blank = merge_blank_min_h
                    else:
                        top_blank = merge_blank_min_h
                        bottom_blank = merge_blank_nor_h

                cropped = img_leave[(box[1] - top_blank):(box[3] + bottom_blank), (box[0] - merge_blank_nor_h):(box[2] + merge_blank_nor_h)]  # 裁剪坐标为[y0:y1, x0:x1]
                croped_dict[img_class] = cropped

                cropped = img_src[(box[1] - top_blank):(box[3] + bottom_blank), (box[0]- merge_blank_nor_h):(box[2] + merge_blank_nor_h)]  # 裁剪坐标为[y0:y1, x0:x1]
                src_croped_dict[img_class] = cropped

                # 调整后坐标保存
                if classes[idx] == 'zheng':
                    card_info['front_x1'] = int(card_info['front_x1']) - merge_blank_nor_h
                    card_info['front_y1'] = int(card_info['front_y1']) - top_blank
                    card_info['front_x2'] = int(card_info['front_x2']) + merge_blank_nor_h
                    card_info['front_y2'] = int(card_info['front_y2']) + bottom_blank

                if classes[idx] == 'fan':
                    card_info['back_x1'] = int(card_info['back_x1']) - merge_blank_nor_h
                    card_info['back_y1'] = int(card_info['back_y1']) - top_blank
                    card_info['back_x2'] = int(card_info['back_x2']) + merge_blank_nor_h
                    card_info['back_y2'] = int(card_info['back_y2']) + bottom_blank

            # front_img = croped_dict['zheng']
            # back_img = croped_dict['fan']
            front_img = src_croped_dict['zheng']
            back_img = src_croped_dict['fan']

            cv2.imwrite(output_path + '/front.jpg', front_img)
            cv2.imwrite(output_path + '/back.jpg', back_img)

            # 图像正面反面的正倒分类
            front_y = front_classify.predict(src_croped_dict['zheng'])    # 分类标签：0为倒，1为正
            back_y = back_classify.predict(croped_dict['fan'])

            front_rotate_angle = 0
            back_rotate_angle = 0

            # 图像旋转+校正
            front_process_img = np.ones((1000, 1000, 3))
            back_process_img = np.ones((1000, 1000, 3))
            if (front_y == 0):
                front_img_step1,front_rotate_angle = jiaozheng(front_img)
                cv2.imwrite(output_path+'/front_img_step1.jpg', front_img_step1)
                front_img_step2 = rotate_bound(front_img_step1, 180)
                front_process_img = front_img_step2
            else:
                cv2.imwrite(output_path+'/front_img_step1.jpg', front_img)
                front_img_step2, front_rotate_angle = jiaozheng(front_img)
                front_process_img = front_img_step2
            if (back_y == 0):
                # back_img_step1, back_rotate_angle = jiaozheng(back_img, 150)
                back_img_step1,back_rotate_angle = jiaozheng(back_img,120) # 训练集有错误图 150->120
                cv2.imwrite(output_path+'/back_img_step1.jpg', back_img_step1)
                back_img_step2 = rotate_bound(back_img_step1, 180)
                back_process_img = back_img_step2
            else:
                cv2.imwrite(output_path+'/back_img_step1.jpg', back_img)
                # back_img_step2, back_rotate_angle = jiaozheng(back_img,150)
                back_img_step2, back_rotate_angle = jiaozheng(back_img, 120) # 训练集有错误图 150->120
                back_process_img = back_img_step2

            cv2.imwrite(output_path + '/jz_front.jpg', front_process_img)
            cv2.imwrite(output_path + '/jz_back.jpg', back_process_img)

            card_info['front_rotate_180'] = front_y
            card_info['back_rotate_180'] = back_y

            card_info['front_rotate_angle'] = front_rotate_angle
            card_info['back_rotate_angle'] = back_rotate_angle

            yolo_list.extend([card_info])

            print('i=',i)

    detect_pd = pd.DataFrame(yolo_list)
    # detect_pd.to_csv(output_path +'/yolo_rect_train_1017.csv',index= False)
    detect_pd.to_csv(output_path + '/yolo_rect_test_1017.csv', index=False)

    # yolo.close_session()


from contact import Contact

def add_contact(name, number, email):
    return Contact(name, number, email)

def see_contact(schedule, name):
    for num in schedule:
        if num.getName() == name:
            print(f"{num.getName()} | {num.getNumber()} | {num.getEmail()}")
            break
    else:
        print('name not found!')

def delete_contact(schedule, name):
    if len(schedule) != 0:
        cont = 0
        for num in schedule:
            if num.getName() == name:
                schedule.pop(cont)
                print(f'contact {num.getName} successfully removed!')
                break
            else:
                print('name not found!')
            cont += 1
    else:
        print('empty list')

def all_contact(agenda):
    for num in agenda:
        print(f'{num.getName()} | {num.getNumber()} | {num.getEmail()}')


from __future__ import print_function
from imutils.object_detection import non_max_suppression
from imutils import paths
import numpy as np
import argparse
import imutils
import cv2

hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

cap = cv2.VideoCapture("burglar.mp4")

while(True):
    # Capture frame-by-frame
    ret, image = cap.read()
    image = imutils.resize(image, width=min(400, image.shape[1]))
    orig = image.copy()
    (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),padding=(8, 8), scale=1.05)
    if len(rects) > 0:
        for (x, y, w, h) in rects:
            cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)
    rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
    pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
    for (xA, yA, xB, yB) in pick:
        cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
    # Display the resulting frame
    cv2.imshow('frame',image)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()


#!/usr/bin/env python
# coding: utf-8

# In[34]:

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
import pandas as pd
from collections import OrderedDict
import sqlite3
import sys

# In[35]:

conn = sqlite3.connect('Hello.db')
curr = conn.cursor()
playerName = "Rohit Sharma"

# In[36]:

curr.execute("SELECT * FROM Players WHERE Player_Name=?", ("Rohit Sharma",))
rows = curr.fetchall()
if (len(rows) != 0):
    print("Player already exists")
    sys.exit(0)

# In[37]:

curr.execute('INSERT INTO Players (Player_Name) VALUES (?)',
             (playerName,))
player_Id = curr.lastrowid

print(player_Id)
conn.commit()
conn.close()

# In[38]:

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

# In[39]:

url = "http://www.espncricinfo.com/india/content/player/34102.html"
html = urllib.request.urlopen(url, context=ctx).read()

# In[40]:

temp_data = OrderedDict()
list_of_dict = []
bs = BeautifulSoup(html, "lxml")
table_body = bs.find_all('tbody')
rows = table_body[0].find_all('tr')
headings = bs.findAll('tr', {"class", "head"})

heads = headings[:2]
vatting = heads[0]
bowling = heads[1]

batText = vatting.find_all("th")
bowlText = bowling.find_all("th")
bat = []
for i in batText:
    bat.append(i.text)

bowl = []
for i in bowlText:
    bowl.append(i.text)

print(len(bat))

for row in rows:
    cols = row.find_all('td')
    cols = [x.text.strip() for x in cols]
    temp_data = OrderedDict()
    {el: 0 for el in bat}
    #     print(len(cols))
    #     print(cols)
    #     print(bat)
    for col in range(len(cols)):
        temp_data[bat[col]] = cols[col]

    list_of_dict.append(temp_data)

# In[41]:

batDf = pd.DataFrame(list_of_dict)
batDf
batDf.set_index('', inplace=True)
batDf

# In[ ]:

# In[42]:

temp_data = OrderedDict()
list_of_dict = []
bs = BeautifulSoup(html, "lxml")
table_body = bs.find_all('tbody')
rows = table_body[1].find_all('tr')
headings = bs.findAll('tr', {"class", "head"})

heads = headings[:2]
vatting = heads[0]
bowling = heads[1]

batText = vatting.find_all("th")
bowlText = bowling.find_all("th")
bat = []
for i in batText:
    bat.append(i.text)

bowl = []
for i in bowlText:
    bowl.append(i.text)

# print(bat[1:])
# print(bowl[1:])

# bat = bat[1:]
# bowl = bowl[1:]
print(len(bat))

for row in rows:
    cols = row.find_all('td')
    cols = [x.text.strip() for x in cols]
    temp_data = OrderedDict()
    {el: 0 for el in bat}
    #     print(len(cols))
    #     print(cols)
    #     print(bat)
    for col in range(len(cols)):
        temp_data[bowl[col]] = cols[col]

    list_of_dict.append(temp_data)

# In[43]:

bowl_df = pd.DataFrame(list_of_dict)
bowl_df
bowl_df.set_index('', inplace=True)
bowl_df

# In[44]:

conn = sqlite3.connect('Hello.db')
c = conn.cursor()

# In[45]:

for index, row in batDf.iterrows():
    query = """INSERT INTO `{}`
                          ('Player_ID','Mat', 'Inns', 'NO', 'Runs', 'HS', 'Ave', 'BF', 'SR', '100', '50', '4s', '6s', 'Ct', 'St')
                          VALUES (?, ?, ?, ?,?,?, ?, ?,?,?, ?, ?,?,?,?);""".format(index)
    values = list(row)
    values.insert(0, player_Id)
    print((values))
    c.execute(query, values)

conn.commit()
conn.close()


Stage0 += baseimage(image='ubuntu:20.04', _as='build')

# get optional user arguments
mxofed= USERARG.get('mellanox', 'False')
psm = USERARG.get('psm', 'False')
pmi0 = USERARG.get('pmi0', 'False')

# update apt keys
bs = shell(commands=['apt-get update -y',
     'DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends -o=Dpkg::Use-Pty=0 build-essential pkg-config ca-certificates gnupg apt-utils',
     'rm -rf /var/lib/apt/lists/*'])
Stage0 += bs

# useful system tools
# libexpat is required by udunits
baselibs = packages(apt=['tcsh','csh','ksh', 'openssh-server','libncurses-dev',
                              'libssl-dev','libx11-dev','less','man-db','tk','tcl','swig',
                              'bc','file','flex','bison','libexpat1-dev', 'git','vim',
                              'libxml2-dev','unzip','wish','curl','wget','time','emacs',
                              'libcurl4-openssl-dev','nano','screen','lsb-release',
                              'libgmp-dev','libmpfr-dev','libboost-thread-dev',
                              'autoconf','pkg-config','clang-tidy','tar'])
Stage0 += baselibs

# get an up-to-date version of CMake
Stage0 += cmake(eula=True,version="3.19.2")
Stage0 += shell(commands=['rm -f /usr/bin/gmake','ln -s /usr/bin/make /usr/bin/gmake'])

# git-lfs
lfs = shell(commands=
            ['curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash',
             'apt-get update','DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends git-lfs', 'git lfs install --skip-repo'])
Stage0 += lfs

# python3
pyth = packages(apt=['python3-pip','python3-dev','python3-yaml',
                              'python3-scipy'])
Stage0 += pyth
Stage0 += shell(commands=['ln -s /usr/bin/python3 /usr/bin/python'])

# Mellanox or inbox OFED
if (mxofed.lower() == "true"):
    o = mlnx_ofed(version='5.2-1.0.4.0')
    hp = hpcx(version='2.8.0',mlnx_ofed='5.2-1.0.4.0',multi_thread=True)
    with_hpcx = True
else:
    o = ofed()
    # omit this for now because hpccm isn't up to date with ubuntu 20.04
    # If you really want it, you can get it to work if you manually edit the Dockerfile and
    # replace x86_64 with aarch64 in the file name.  So, the correct filename would be
    # http://www.mellanox.com/downloads/hpc/hpc-x/v2.8/hpcx-v2.8.0-gcc-inbox-ubuntu20.04-aarch64.tbz
    #Stage0 += hpcx(version='2.8.0',inbox=True)
    with_hpcx = False
Stage0 += o
if (with_hpcx):
    Stage0 += hp

# PSM library
if (psm.lower() == "true"):
    psm = packages(apt=['libpsm-infinipath1','libpsm-infinipath1-dev'])
    Stage0 += psm
    with_psm=True
else:
    with_psm=False

# UCX and components
kn = knem()
Stage0 += kn

x = xpmem()
Stage0 += x

u = ucx(ofed=True,knem=True,xpmem=True,cuda=False)
Stage0 += u

pmilibs=packages(apt=['default-libmysqlclient-dev',
        'libfreeipmi-dev',
        'freeipmi-tools',
        'libglib2.0-0',
        'libglib2.0-dev',
        'libgtk-3-0',
        'libgtk-3-dev',
        'libhwloc-dev',
        'libjson-c-dev',
        'liblua5.2-0',
        'liblua5.2-dev',
        'libmunge-dev',
        'libmunge2',
        'libpam0g-dev',
        'libyaml-dev',
        'hwloc',
        'file',
        'libevent-dev'])
Stage0 += pmilibs

#--------------------------------------------------------------
# install Intel OneAPI compilers and MPI

intelenv = environment(variables={'APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE':'1'})
Stage0 += intelenv

# configure oneapi repo
intelrepo = shell(commands=['wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB',
    'apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB','rm GPG-PUB-KEY-INTEL-SW-PRODUCTS-2023.PUB',
    'echo "deb https://apt.repos.intel.com/oneapi all main" > /etc/apt/sources.list.d/oneAPI.list'])
Stage0 += intelrepo

Stage0 += shell(commands=['apt-get update -y',
     'DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends -o=Dpkg::Use-Pty=0'+
     ' intel-hpckit-getting-started'+
     ' intel-oneapi-clck'+
     ' intel-oneapi-common-licensing'+
     ' intel-oneapi-common-vars'+
     ' intel-oneapi-dev-utilities'+
     ' intel-oneapi-dpcpp-cpp-compiler-pro'+
     ' intel-oneapi-ifort'+
     ' intel-oneapi-inspector'+
     ' intel-oneapi-itac'+
     ' intel-oneapi-mpi-devel'+
     ' intel-oneapi-mkl-devel',
     'rm -rf /var/lib/apt/lists/*'])

inp = shell(commands=['echo "if [ -z $INTEL_SH_GUARD ]; then" > /etc/profile.d/intel.sh',
     'echo "    source /opt/intel/oneapi/setvars.sh -i_mpi_library_kind=release_mt" >> /etc/profile.d/intel.sh',
     'echo "fi" >> /etc/profile.d/intel.sh',
     'echo "export INTEL_SH_GUARD=1" >> /etc/profile.d/intel.sh',
     'chmod a+x /etc/profile.d/intel.sh'])
Stage0 += inp

#--------------------------------------------------------------
# Build jedi-stack

Stage0 += shell(commands=['DOCKERSHELL BASH'])

ev = environment(variables={'I_MPI_THREAD_SPLIT':'1',
                                 'I_MPI_LIBRARY_KIND':'release_mt',
                                 'CMAKE_C_COMPILER':'mpiicc',
                                 'CMAKE_CXX_COMPILER':'mpiicpc',
                                 'CMAKE_Fortran_COMPILER':'mpiifort',
                                 'CMAKE_Platform':'linux.intel',
                                 'CMAKE_PREFIX_PATH':'/opt/jedistack',
                                 'NETCDF':'/opt/jedistack',
                                 'CC':'mpiicc',
                                 'CXX':'mpiicpc',
                                 'FC':'mpiifort',
                                 'MPI_CC':'mpiicc',
                                 'MPI_CXX':'mpiicpc',
                                 'MPI_FC':'mpiifort',
                                 'SERIAL_CC':'icc',
                                 'SERIAL_CXX':'icpc',
                                 'SERIAL_FC':'ifort',
                                 'PNETCDF':'/opt/jedistack',
                                 'HDF5_ROOT':'/opt/jedistack',
                                 'PIO':'/opt/jedistack',
                                 'BOOST_ROOT':'/opt/jedistack',
                                 'EIGEN3_INCLUDE_DIR':'/opt/jedistack',
                                 'PATH':'/opt/jedistack/bin:/usr/local/bin:/usr/local/pmix/bin:$PATH',
                                 'LD_LIBRARY_PATH':'/opt/jedistack/lib:/usr/local/lib:/usr/lib:/usr/lib/x86_64-linux-gnu:'
                                 +'/opt/intel/oneapi/compiler/2021.2.0/linux/compiler/lib/intel64_lin/'
                                 +'/usr/local/pmix/lib:$LD_LIBRARY_PATH',
                                 'LIBRARY_PATH':'/opt/jedistack/lib:/usr/local/lib:/usr/lib:/usr/lib/x86_64-linux-gnu:$LIBRARY_PATH',
                                 'CPATH':'/opt/jedistack/include:/usr/local/include:/usr/include:/usr/local/pmix/include:$CPATH',
                                 'PYTHONPATH':'/opt/jedistack/lib:/usr/local/lib:$PYTHONPATH',
                                 'PKG_CONFIG_PATH':'/usr/lib/x86_64-linux-gnu/pkgconfig'})
Stage0 += ev

Stage0 += shell(commands=['source /etc/profile','cd /root',
    'git clone https://github.com/jcsda/jedi-stack.git',
    'cd jedi-stack/buildscripts',
    'git checkout develop',
    './build_stack.sh "container-intel-impi-app"',
    'mv ../jedi-stack-contents.log /etc',
    'chmod a+r /etc/jedi-stack-contents.log',
    'cd /root/jedi-stack/pkg/NCEPLIBS-bufr/build/python',
    'python3 setup.py build',
    'python3 setup.py install --prefix=/opt/jedistack --install-lib=/opt/jedistack/lib/python3.8/site-packages',
    'rm -rf /root/jedi-stack',
    'mkdir /worktmp'])

#------------------------------------------------------------------------------
# Install JEDI

Stage0 += copy(src='hello_world_mpi.c',dest='/root/hello_world_mpi.c')

Stage0 += shell(commands=[
    'source /etc/profile',
    'mkdir -p /opt/jedi',
    'cd /opt/jedi',
    'git clone https://github.com/jcsda/fv3-bundle.git',
    'cd /opt/jedi/fv3-bundle',
    'git checkout develop',
    'git clone https://github.com/jcsda/crtm.git -b v2.3-jedi',
    'mkdir -p /opt/jedi/build','cd /opt/jedi/build',
    'ecbuild --build=Release ../fv3-bundle',
    'make -j4',
    'ctest -R get_',
    'cd /opt/jedi/build/test_data',
    'find . -type f -name "*.tar.gz" -delete',
    'chmod -R 777 /opt/jedi/fv3-bundle',
    'cd /root',
    'mpiicc /root/hello_world_mpi.c -o /opt/jedistack/bin/hello_world_mpi -lstdc++'])

#------------------------------------------------------------------------------
# install PMI after the stack so it can take advantage of hdf5

# compile and install pmix
Stage0 += shell(commands=['source /etc/profile',
          'mkdir -p /var/tmp',
          'wget -q -nc --no-check-certificate -P /var/tmp https://github.com/openpmix/openpmix/releases/download/v3.1.5/pmix-3.1.5.tar.gz',
    'tar -x -f /var/tmp/pmix-3.1.5.tar.gz -C /var/tmp -z',
    'cd /var/tmp/pmix-3.1.5',
    './configure --prefix=/opt/jedistack/pmix',
    'make -j$(nproc)',
    'make -j$(nproc) install',
    'rm -rf /var/tmp/pmix-3.1.5 /var/tmp/pmix-3.1.5.tar.gz'])

# compile and install slurm-pmi2
Stage0 += shell(commands=['source /etc/profile',
    'mkdir -p /var/tmp',
    'wget -q -nc --no-check-certificate -P /var/tmp https://download.schedmd.com/slurm/slurm-19.05.4.tar.bz2',
    'tar -x -f /var/tmp/slurm-19.05.4.tar.bz2 -C /var/tmp -j',
    'cd /var/tmp/slurm-19.05.4',
    './configure --prefix=/opt/jedistack/slurm-pmi2 --with-hdf5=/opt/jedistack/bin',
    'cd /var/tmp/slurm-19.05.4',
    'make -C contribs/pmi2 install',
    'rm -rf /var/tmp/slurm-19.05.4 /var/tmp/slurm-19.05.4.tar.bz2'])

#==============================================================================
# Second stage: Runtime
#==============================================================================
Stage1 += baseimage(image='ubuntu:20.04', _as='runtime')

Stage1 += copy(_from='build', src='/opt/jedistack', dest='/opt/jedistack')
Stage1 += copy(_from='build', src='/opt/jedi', dest='/opt/jedi')

Stage1 += bs
Stage1 += baselibs
Stage1 += lfs
Stage1 += pyth
Stage1 += shell(commands=['ln -s /usr/bin/python3 /usr/bin/python'])

Stage1 += o.runtime()
if (with_hpcx):
    Stage1 += hp.runtime()

if (with_psm):
    Stage1 += psm

Stage1 += kn.runtime()
Stage1 += x.runtime()
Stage1 += u.runtime()

#----------------------------------------------------
#Stage1 += pmilibs

## optionally install an older pmi - may be needed for some platforms
if (pmi0.lower() == "true"):
    pm0 = packages(apt=['libpmi0','libpmi0-dev'])
    Stage1 += pm0

#----------------------------------------------------
# intel runtime
Stage1 += intelenv
Stage1 += intelrepo

Stage1 += shell(commands=['DOCKERSHELL BASH'])

Stage1 += shell(commands=['source /etc/profile','apt-get update -y',
     'DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends -o=Dpkg::Use-Pty=0'
      +' kmod linux-headers-5.4.0-1041-aws libnuma-dev','rm -rf /var/lib/apt/lists/*'])

Stage1 += shell(commands=['apt-get update -y',
     'DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends -o=Dpkg::Use-Pty=0'
     +' intel-oneapi-runtime-ccl'
     +' intel-oneapi-runtime-compilers'
     +' intel-oneapi-runtime-dnnl'
     +' intel-oneapi-runtime-dpcpp-cpp'
     +' intel-oneapi-runtime-dpcpp-library'
     +' intel-oneapi-runtime-fortran'
     +' intel-oneapi-runtime-libs'
     +' intel-oneapi-runtime-mkl'
     +' intel-oneapi-runtime-mpi'
     +' intel-oneapi-runtime-opencl'
     +' intel-oneapi-runtime-openmp'
     +' intel-oneapi-runtime-tbb'
     +' intel-oneapi-runtime-vpl'
     +' intel-hpckit-runtime',
     'rm -rf /var/lib/apt/lists/*'])

#----------------------------------------------------
# set some environment variables
Stage1 += ev
Stage1 += inp


# -*- coding: utf-8 -*-
"""
Created on Sat Dec  1 10:17:57 2018

@author: gary.allison

This code is used to translate an APINumber string from the ODNR injection
files into a valid API number for further processing.

There are various problems with these input strings:
    - they often include extraneous and information
    - they are occasionally a group of API
    - they have no API number but are just descriptive
    - typos

    We handle this by using a hand-checked translation table.  This table
    has the given APIstring, Year and Quarter and returns a valid API with
    ten digits.

"""
import pandas as pd
import os, sys
#from shutil import copyfile
import time

xlatefn = 'xlateAPI.txt'
xlatefnbak = 'xlateAPI_backup.txt'

def fetchXlateDict(fn=xlatefn):
    xdict = {}
    with open(fn) as f:
        f.readline() # ignore header
        for ln in f.readlines():
            lst = ln.split('|')
            xdict[(lst[0],int(lst[1]),int(lst[2]))] = lst[3]
    return xdict

xdict = fetchXlateDict()

def saveSortedXlateDict(fn=xlatefn,bfn=xlatefnbak):
    timestr = time.strftime("%Y%m%d-%H%M%S_")
    bufn = 'tmp/'+timestr+fn
    try:
        os.rename(bfn,bufn)
    except:
        print('couldnt move backup into tmp!')
    d = pd.read_csv(fn,sep='|')
    os.rename(fn,bfn)
    out = d.sort_values(by=['APIstr','year','quarter'])
    out.to_csv(fn,sep='|',index=False)

def is_apistr_numeric(astr):
    # returns true if the first 10 chars are numeric
    if len(astr) <10:
        return False
    try:
        int(astr[0:10])
        # verify that it is not MORE than 10 digits
        if astr[10].isdigit():
            return False
        return True
    except:
        return False

def is_potential_multiple(astr):
    try:
        if ('&' in astr) or (',' in astr):
            return True
        else:
            return False
    except:
        print(f'is_potential_multiple error with APIstr: {astr}')
        #sys.exit()

def getAPI10(astr, yr, q,flag_problems=True):
    # First check if we've already identified the API10 to use
    if (astr,yr,q) in xdict.keys():
        #print(f'{astr} {yr} {q} : {xdict[(astr,yr,q)]}')
        return xdict[(astr,yr,q)]
    if is_potential_multiple(astr):
        if flag_problems:
            print(f'{astr}|{yr}|{q}|API_HERE| Multiple?')
        return f'{astr}|{yr}|{q}|API_HERE| Multiple?'
    if is_apistr_numeric(astr):
        return astr[0:10]
    else:
        if flag_problems:
            print(f'{astr}|{yr}|{q}|API_HERE|Comment ')
        return f'{astr}|{yr}|{q}|API_HERE|Comment '

if __name__ == '__main__':
    print(getAPI10('another test',2009,1))
    saveSortedXlateDict()


# -*- coding: utf-8 -*-
"""
Created on Tue Oct 18 17:53:18 2022

@author: R I B
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import glob
from math import *
import csv
import PIL
from PIL import ImageOps, Image
import os
import xml.etree.cElementTree as et
Zone=[570, 500, 717, 584]#513
def get_img_files(path):
    ''' fonction pour extraire les fichiers xml dans un path donné.'''
    xml_list = []
    for filename in os.listdir(path):
        if filename.endswith(".jpg"):
            xml_list.append(os.path.join('', filename))
    return xml_list
# Il faut penser à changer le path selon votre pc
L=get_img_files("C:\\Users\\R I B\\Desktop\\wooow\\annoted data")
''' extraire les ficihers jpg '''
for i in range(len(L)):
    A=[]
    xmin,ymin,xmax,ymax=0,0,0,0
    image=Image.open(L[i])

    box=Zone

    area=image.crop(box)
    area=ImageOps.grayscale(area)
    M=np.array(area)
    N,P=M.shape
    pas=31
    for k in range((N//pas)):
        for m in range((P//pas)):
            a=M[(k+1)*pas-pas//2][(m+1)*pas-pas//2] # pixels du centre de gravité
            T=M[((k+1)*pas-pas//2)-pas//4:((k+1)*pas-pas//2)+pas//4,((m+1)*pas-pas//2)-pas//4:((m+1)*pas-pas//2)+pas//4]
            test=True
            s1,s2=T.shape
            for p in range(s1):
                for s in range(s2):
                    if T[s][p]>110:
                        test=False
                        break
            if test==True:
                   xmin=((k+1)*pas-pas//2)-pas//2
                   xmax=((k+1)*pas-pas//2)+pas//2
                   ymax=((m+1)*pas-pas//2)+pas//2
                   ymin=((m+1)*pas-pas//2)-pas//2
                   A.append([xmin,ymin,xmax,ymax])
                   break
    area=image.crop(box)
    M=np.array(area)
    if len(A)>0:
        xmin,ymin,xmax,ymax=A[-1][0],A[-1][1],A[-1][2],A[-1][3]
        for l in range(xmin,xmax):
            for t in range(ymin,ymax):
                M[xmax-1][t]=[0,255,0]
                M[l][ymin]=[0,255,0]
                M[xmin][t]=[0,255,0]
                M[l][ymax-1]=[0,255,0]
    plt.imshow(M,interpolation='nearest')
    plt.show()


"""Implement the aggregate operations"""
import os
import constants
import matplotlib.pyplot as plt
import numpy as np
import collections
import datetime
import configurations
from BaseOperations import BaseOperations

class AggregateOperations(BaseOperations):
    """Contains the aggregate operations"""

    def __init__(self, input_file, output_file):
        super(AggregateOperations, self).__init__(
            input_file, output_file, None)

    def generate_output_file(self, search_text=None):
        separated_contents = self.get_separated_contents_from_message_lines()
        sender_counter = collections.Counter()
        send_time_counter = collections.Counter()
        for item in separated_contents:
            timestamp, sender, _ = item
            if sender:
                sender_counter[sender] += 1

            datetime_object = self.get_datetime_from_timestamp(timestamp)
            if datetime_object:
                send_time_counter[datetime_object.time().hour] += 1

        most_common_senders = sender_counter.most_common(configurations.NUMBER_MOST_COMMON_SENDERS)
        print("The {0} most common message senders in the group are: ".format(
            configurations.NUMBER_MOST_COMMON_SENDERS))
        print(most_common_senders)

        most_active_hours = send_time_counter.most_common(configurations.NUMBER_MOST_ACTIVE_HOURS)
        print("The {0} most common hours for sending message in the group is: ".format(
            configurations.NUMBER_MOST_ACTIVE_HOURS))
        print(most_active_hours)

        # plot the most active members
        self.plot_barchart_from_list_of_tuples(most_common_senders, "Most active members")

        # plot the most active hours
        self.plot_barchart_from_list_of_tuples(most_active_hours, "Most active hours")

        if os.environ.get("show_graphs") == "y":
            plt.show()

    def get_datetime_from_timestamp(self, timestamp):
        """Get the date time object from time stamp string
        Sample date time format - 03/11/2017, 12:06
        """
        if timestamp:
            timestamp = timestamp.strip()
            if len(timestamp) == constants.DATETIME_LENGTH:
                if timestamp[0].isdigit() and timestamp[-1].isdigit():
                    try:
                        return datetime.datetime.strptime(timestamp, constants.DATETIME_FORMAT)
                    except Exception as e:
                        print("Error parsing date: {0} with error: {1}".format(timestamp, str(e)))
        return None

    def plot_barchart_from_list_of_tuples(self, data_with_labels, plot_name):
        """Plot barcharts from the list of tuples"""
        labels = [item[0] for item in data_with_labels]
        frequencies = [int(item[1]) for item in data_with_labels]
        indexes = np.arange(len(labels))
        width = 1
        plt.figure(figsize=(20, 3))
        plt.bar(indexes, frequencies, width=0.3,align='center')
        plt.xticks(indexes, labels)
        plt.title(plot_name)
        output_file_location = os.path.join(constants.OUTPUT_FILES_FOLDER_NAME, plot_name + constants.IMAGE_EXTENSION)
        plt.savefig(output_file_location)


from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django_proxy_users import forms as ProxyUsersForms
from django.contrib import messages as DjangoMessages
from django.utils.translation import ugettext as _

LOGIN_URL = "/django/proxy/users/login/"

@login_required(login_url=LOGIN_URL)
def home(request):
    """
    Home page.

    **template**

    :template:`django_proxy_users/home.html`
    """
    return render(request, 'django_proxy_users/home.html')

def login(request):
    """
    Interface to log users in.

    **POST Arguments**

    username  String.
    password  String.

    **template**

    :template:`django_proxy_users/login.html`
    """
    POST = request.POST if request.POST else None
    login_form = ProxyUsersForms.AuthenticationForm(data=POST)
    if login_form.is_valid():
        DjangoMessages.success(request, _("Access granted, Welcome!"))
        login_form.login(request)
        return redirect('home')

    return render(request, 'django_proxy_users/login.html', {
        'login_form': login_form
    })

def logout(request):
    """
    Log users out.
    """
    DjangoMessages.success(request, _("Bye Bye"))
    ProxyUsersForms.AuthenticationForm.logout(request)
    return redirect('home')

@login_required(login_url=LOGIN_URL)
def login_as(request):
    """
    Display a list of non-superusers to login as.

    **GET Arguments**

    user  int  User id to login as.

    **POST Arguments**

    find_user  String  Find user to login as.

    **template**

    :template:`django_proxy_users/login_as.html`
    """

    POST = {}
    PAGE = request.GET.get('page', 1)

    if request.POST.get('find_user', False):
        POST.update({'find_user': request.POST.get('find_user')})

    if request.GET.get('user', False):
        POST.update({'user': request.GET.get('user')})

    POST = POST if POST else None

    login_as_form = ProxyUsersForms.AuthenticateAsForm(data=POST, page=PAGE)

    if login_as_form.is_valid():
        if login_as_form.login_as(request):
            DjangoMessages.success(request, _(u"Access granted!"))
            return redirect('home')

    return render(request, 'django_proxy_users/login_as.html', {
        'login_as_form': login_as_form
    })

@login_required(login_url=LOGIN_URL)
def log_back_in_as(request):
    """
    Log super-users back into their original session.
    """

    DjangoMessages.success(request, _(u"Welcome back!"))
    ProxyUsersForms.AuthenticateAsForm.log_back_in_as(request)
    return redirect('home')


print("found me!")

import pygame
from classes import *
import scenes

pygame.init()

background_modes = BackgroundModes

class Engine():
    game_running = False
    screen = None
    scenemanager = None
    timemanager = None
    fontmanager = None
    bg = None
    framerate = 60
    clock = pygame.time.Clock()
    deltaTime = 0

class MonitorText(GameObject):
    valuetomonitor = None
    position = (0, 0)

    def update(self):
        render_text(self.valuetomonitor(), self.getposition(), 0)

    def getposition(self):
        return (Engine.screen.get_width()*self.position[0], Engine.screen.get_height()*self.position[1])

def init():
    Engine.bg = Background()
    Engine.scenemanager = SceneManager(scenes.scenes)
    Engine.scenemanager.go_to("default")
    Engine.timemanager = TimeManager()
    Engine.fontmanager = FontManager()

def start_game(width, height):
    Engine.screen = pygame.display.set_mode((width, height), pygame.NOFRAME)
    Engine.game_running = True
    main_loop(Engine.screen)

def tick(fps):
    Engine.deltaTime = Engine.clock.tick(60)/1000

def render_background():
    Engine.bg.render(Engine.screen)

def display():
    pygame.display.flip()

def instantiate(obj, x=0, y=0):
    if Engine.scenemanager.scene != None:
        return Engine.scenemanager.scene.add(obj, x, y)
    else:
        print("No scene is loaded. Cannot instantiate objects into an empty scene.")

def update_scene():
    if Engine.scenemanager.scene != None:
        Engine.scenemanager.scene.update()
    else:
        print("No scene is loaded. Game closed.")
        Engine.game_running = False

def get_time_12hour():
    return Engine.timemanager.get_time_12hour()

def add_monitor_text(function, position, fontsize, fontnumber):
    mt = instantiate(MonitorText)
    mt.valuetomonitor = function
    mt.position = position

def draw_surface(surface, xy = (0, 0), centered = True):
    size = surface.get_size()
    nx = xy[0]
    ny = xy[1]
    if centered:
        nx -= (size[0]/2)
        ny -= (size[1]/2)
    Engine.screen.blit(surface, (nx, ny))

def render_text(text, xy=(0,0), fontnumber=0):
    s = Engine.fontmanager.fonts[fontnumber].render(str(text), True, (255, 255, 255))
    draw_surface(s, xy)

def main_loop(screen):
    while Engine.game_running:
        Engine.timemanager.update()
        tick(Engine.framerate)
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                Engine.game_running = False
        render_background()
        update_scene()
        display()
    pygame.quit()

init()


import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ex_cond

PATH = "C:\Program Files (x86)\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(PATH)

test_url = "file:///C:/Users/Yuki/PycharmProjects/COEBadmintonReserve/webpage/Cart%20-%20move.learn.play.html"

before_XPath_1 = "//*[@class='table']/tbody/tr["
before_XPath_2 = "/td["
after_XPath = "]"

search_text = "The class is at or exceeding the maximum number of participants"

driver.get(test_url)

# time.sleep(30)
WebDriverWait(driver, 6).until(ex_cond.presence_of_element_located((By.CLASS_NAME, "table")))

try:
    print(driver.find_element_by_class_name("table").text)
except Exception as e:
    print("Oops!", e.__class__, "occurred.")
    print("not able to print cart table")

num_rows = len(driver.find_elements_by_xpath("//*[@class='table']/tbody/tr"))
num_columns = len(driver.find_elements_by_xpath("//*[@class = 'table']/tbody/tr[2]/td"))

elem_found = False

error_list = []

for t_row in range(2, (num_rows + 1), 2):
    FinalXPath = before_XPath_1 + str(t_row) + after_XPath + before_XPath_2 + str(1) + after_XPath
    cell_text = driver.find_element_by_xpath(FinalXPath).text
    if "Error" in cell_text:
        error_list.append(int(t_row/2))

print(error_list)

for rm in reversed(error_list):
    remove_buttons = driver.find_elements_by_class_name("BasketItemRemoveLink")
    try:
        ActionChains(driver).move_to_element(remove_buttons[rm-1]).perform()
    except Exception as e:
        print(str(e))
        print("scroll stuff")
    time.sleep(1)
    remove_buttons[rm-1].click()
    driver.back()

driver.quit()


#User function Template for python3
from itertools import permutations
class Solution:
    def permutation(self,s):
        l = len(s)
        string = list(s)
        permu = permutations(string, l)
        per = []
        for i in list(permu):
            per.append("".join(i))
        per.sort()
        return per

#{
 # Driver Code Starts
#Initial Template for Python 3

if __name__=="__main__":
    T=int(input())
    while(T>0):
        s=input()
        ob=Solution()
        ans=ob.permutation(s)
        for i in ans:
            print(i,end=" ")
        print()

        T-=1
# } Driver Code Ends

GFG/Problem of the day/String Permutations.py


"""
BackFlow.py
---------------

Main Function for performing video segmentatino as described in the paper

"""

import cv2
import glob
from tracker import Tracker
from segmentation_maskrcnn import MaskRCNN
import sys
import os
from config.segmentationParameters import SEG_METHOD

def print_usage():

    print( "Usage: segmentation.py <path>")
    print( "path: [data_path]/all or name of the folder")
    print( "e.g., segmentation.py Data/all, segmentation.py Data_new/all, "
           "segmentation.py Data/Cheezit")

def visualize(path):
    global out
    resultIDs = []

    for file in os.listdir(path +SEG_METHOD+"_results"):
        if file.endswith(".png"):
            resultIDs.append(int(file[:-4]))

    resultIDs.sort()

    for id in resultIDs:
        filename_cad = path + 'cad/%s.jpg' % id
        cad = cv2.imread(filename_cad)
        filename_result = path + SEG_METHOD+'_results/%s.png' % id
        overlay = cv2.imread(filename_result)
        cv2.addWeighted(overlay, 0.4, cad, 0.6, 0, cad)
        out.write(cad)
        cv2.imshow("Segment", cad)

if __name__ == "__main__":

    try:
        if sys.argv[1][-3:] == "all":
            folders = glob.glob(sys.argv[1][:-3]+"*/")
        elif sys.argv[1]+"/" in glob.glob("Data_new/*/") \
                or sys.argv[1]+"/" in glob.glob("Data/*/")\
                or sys.argv[1]+"/" in glob.glob("Data_stuck/*/"):
            folders = [sys.argv[1]+"/"]
        else:
            print_usage()
            exit()
    except:
        print_usage()
        exit()
    # folders=["Data/YcbGelatinBox/"]
    # folders = ["Data/cheezit/"]
    for path in folders:
        if not os.path.exists(path+SEG_METHOD+"_results/"):
            os.makedirs(path+SEG_METHOD+"_results/")

        cap = cv2.VideoCapture(0)

        # Define the codec and create VideoWriter object
        fourcc = cv2.VideoWriter_fourcc(*"XVID")
        out = cv2.VideoWriter(path +SEG_METHOD+"_result.avi",fourcc, 20.0, (640,480))

        print(path)

        if SEG_METHOD == "BackFlow":
            tracker = Tracker(path, debug = True)

            tracker.run()
        else:
            maskrcnn = MaskRCNN(path)
            maskrcnn.run()

        visualize(path)

        # Release everything if job is finished
        cap.release()
        out.release()
        cv2.destroyAllWindows()

        print( "Results saved in " + path + SEG_METHOD+"_results")


my_tuple = (1, 2, 3, 4, 5)
slice_tuple = my_tuple[1:4]

print(slice_tuple)

my_tuple = (1, 2, 3, 4, 5)
slice_from_beginning = my_tuple[:3]  # Start from the beginning up to index 3 (exclusive)
slice_to_end = my_tuple[2:]        # Start from index 2 (inclusive) to the end

print(slice_from_beginning)  # Output: (1, 2, 3)
print(slice_to_end)          # Output: (3, 4, 5)


import numpy as np
import matplotlib.pyplot as plt

gamma=0
w=np.zeros(10);
Z=np.zeros((100,6)); rhatRec=np.zeros(100)
x=np.random.rand(10,6)
r=np.array([0,0,1,0,0.5,0])

for trial in range(100):
    V=0;
    for t in range(1,6):
        Vlast=V
        V=w@x[:,t]
        rhat=r[t-1]+gamma*V-Vlast
        w=w+0.1*rhat*x[:,t-1]
        Z[trial,t-1]=Vlast
        rhatRec[trial]=rhatRec[trial]+rhat
plt.figure(); plt.plot(Z[-1,:])
plt.figure(); plt.plot(rhatRec/5)


import uuid

from django.conf import settings
from django.contrib.postgres.fields import ArrayField
from django.db import models
from django.db.models.signals import pre_save
from django.dispatch import receiver

# from records.models import Record

def scramble_uploaded_image(instance, filename):
    extension = filename.split(".")[-1]
    return "item/{}.{}".format(uuid.uuid4(), extension)

class TagManager(models.Manager):

    def search_by_tag(self, tag):
        items = Item.objects.filter(tags__contains=[tag])
        images = Image.objects.filter(tags__contains=[tag])
        return {'items': items, 'images': images}

class Item(models.Model):
    name = models.CharField(max_length=100, blank=False)
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             on_delete=models.CASCADE,
                             related_name='items')
    description = models.TextField(blank=True)
    tags = ArrayField(models.CharField(max_length=200),
                                       null=True,
                                       blank=True,
                                       default=[''])
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    objects = TagManager()

    def __str__(self):
        return '{} - {}'.format(self.name, self.user.username)

    # @property
    # def record_history(self):
    #     record_history = []
    #     print('item/property')
    #     for record in self.records.all():
    #         history = {}
    #         history['record_number'] = record.record_number
    #         history['owner'] = record.owner.username
    #         history['purchased_price'] = record.purchased_price
    #         history['message'] = record.message
    #         history['auction_price_history'] = record.auction_price_history
    #         history['timestamp'] = record.timestamp
    #         record_history.append(history)
    #     return record_history

    @property
    def main_image(self):
        image = None

        # check if main image exists
        # if it exists, set image to that main image
        main_image = self.images.filter(status_main=1)
        main_image_exists = main_image.exists()
        if main_image_exists:
            image = main_image.first()

        # check if images exist at all
        # if it exists, get the image uploaded first
        images_exist = (self.images.count() != 0)
        if not main_image_exists and images_exist:
            image = self.images.order_by('created').first()

        if image != None:
            image_url = image.image.url
            image_desc = image.description
            image_created = image.created

        # but if no images exist, set all attributes to None
        if not main_image_exists and not images_exist:
            image_url = None
            image_desc = None
            image_created = None

        main_image = {
            'image': image_url,
            'description': image_desc,
            'created': image_created
        }
        return main_image

class Image(models.Model):
    item = models.ForeignKey(Item,
                             on_delete=models.CASCADE,
                             related_name='images')
    image = models.ImageField(upload_to=scramble_uploaded_image, blank=True)
    description = models.TextField(blank=True)
    tags = ArrayField(models.CharField(max_length=200),
                                       null=True,
                                       blank=True,
                                       default=[''])
    status_main = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    objects = TagManager()

    def __str__(self):
        return '[{}]\'s image - {}'.format(self.item.name, self.item.user.username)


## Stores the schemas for each relation.
## Enables users to define a schema.
## Note: defining a schema does not add a
## relation to the database. A user must define
## all schemas they wish to add to a database
## before creating the database. Once the database
## is created they can execute queries over the respective
## schemas.

## The order the types are specified in the schema must be
## the same as the order in the CSV or TSV file. The annotations
## (if specified) must come last.

import numpy as np
from sets import Set

#map from dataframe type to EH accepted types
#check in the QC occurs in Schema.scala
#we also check in DFMap when we pass the array
#difference is here we figure out the annotations
map_types = {
  "int32":np.int32,
  "int64":np.int64,
  "uint32":np.uint32,
  "uint64":np.uint64,
  "float32":np.float32,
  "float64":np.float64
}

def strip_unicode(values):
  return [str(x.name) for x in values]

def wrap_strings(values):
  return [np.dtype(map_types[str(x)]) for x in values]

def wrap_types(values):
  return [np.dtype(x) for x in values]

class Schema:
  def python2java(self,duncecap):
    return duncecap.QueryCompiler.buildSchema(
      strip_unicode(self.attributes),
      strip_unicode(self.annotations),
      self.attribute_names,
      self.annotation_names)

  @staticmethod
  def java2python(jobject):
    attribute_types = wrap_types(jobject.getAttributeTypes())
    annotation_types = wrap_types(jobject.getAnnotationTypes())
    return Schema(attribute_types,annotation_types)

  def __init__(self,attributes,annotations=[],attribute_names=[],annotation_names=[]):
    self.attributes = wrap_types(attributes)
    self.annotations = wrap_types(annotations)
    self.attribute_names = attribute_names
    self.annotation_names = annotation_names

  @staticmethod
  def fromDF(df, attribute_names=[], annotation_names=[]):
    #dataframe names for annnotations must start with "a_"
    pattern = r'^a_.*'
    typesSeries = df.dtypes

    found = typesSeries.index.str.contains(pattern)
    notfound = map(lambda x: not x, found)

    attribute_types = []
    for a in typesSeries[notfound]:
      if str(a.name) not in map_types:
        raise Exception("DataFrame attribute type error. Type " + str(a.name) + " not accepted.")
      attribute_types.append(a)

    annotation_types = []
    for a in typesSeries[found]:
      if str(a.name) not in map_types:
        raise Exception("DataFrame annotation type error. Type " + str(a.name) + " not accepted.")
      annotation_types.append(a)

    return Schema(attribute_types,annotation_types,attribute_names,annotation_names)

  #for printing purposes
  def __repr__(self):
    return """(%s,%s)""" % (self.attributes,self.annotations)


from MemeDetector.MLModels.resnet152memes import ResNet152Memes

def process_new_image(sender, instance, **kwargs):

    # Need to check was_processed because it calls instance.save(), triggering itself recursively again

    if not instance.was_processed:
        print("processing new image")
        resnet = ResNet152Memes()
        (meme_confidence, no_meme_confidence) = resnet.predict(instance.image.path)
        instance.meme_confidence = round(meme_confidence, 3)
        instance.no_meme_confidence = round(no_meme_confidence, 3)
        instance.was_processed = True
        if (instance.meme_confidence > 0.5 and instance.submitted_as_meme) or\
                (instance.no_meme_confidence > 0.5 and not instance.submitted_as_meme):
            instance.correct_prediction = True
        instance.save()
    else:
        print("image processed")


from math import sqrt

def fnn(n):
    ans = (((1 + sqrt(5)) / 2)**(n+1) - ((1 - sqrt(5)) / 2)**(n+1)) / sqrt(5)
    return int(ans)

t = int(input())
for i in range(t):
    n = int(input())
    l = []
    for k in range(1, n):
        temp = fnn(k)
        if temp > n:
            break
        else:
            if k%3 == 2:
                l.append(temp)
    res = sum(l)
    print(res)

HackerRank/ProjectEuler/#2: Even Fibonacci numbers/#2: Even Fibonacci numbers.py


import requests
from bs4 import BeautifulSoup
import re
import json

# 字符串处理函数，排除字符串中间的换行符和空格并生成数组
def str_list(string):
    # '\s+'来匹配一个或多个连续的空格或换行符
    pattern = r'\s+'
    result = re.split(pattern, string)
    # 移除空字符串
    result = [s for s in result if s]
    return result

# 天气信息爬虫
def weather_get(url):
    # 发起请求
    req = requests.get(url)
    # 设置编码
    req.encoding = 'utf-8'
    # 获取网页内容
    html = req.text
    # 创建BeautifulSoup对象
    soup = BeautifulSoup(html, 'html.parser')

    # 获取七天的粗略数据
    text_list = []
    # 获取外层类名为day的div标签
    day_div = soup.find_all('div', class_='day')
    for day in day_div:
        text = ''
        for i in day:
            text += i.text
        text_list.append(str_list(text))
    # print(text_list)

    # 获取七天的详细数据
    detail_list = []
    # 获取外层类名为hour-table的table标签
    hour_table = soup.find_all('table', class_='hour-table')
    for i in hour_table:
        current_str = str_list(i.text)
        # 由于天气是以图片的形式呈现的，这里把无法获取的天气元素的表头去除
        current_str.pop(9)
        detail_list.append(current_str)
    if detail_list[0].index('05:00') != 8:
        for i in range(8):
            del detail_list[0][4 * i + detail_list[0].index('05:00') + 1:4 * i + 9]
    # print(detail_list)

    return text_list, detail_list

general_data, detail_data = weather_get('https://weather.cma.cn/web/weather/58433.html')
data = {}
for i in range(7):
    # 添加某天粗略数据的键值对
    key = general_data[i].pop(0)
    general_value = general_data[i]
    data[key] = {
        'general': general_value,
        'detail': {}
    }

    # 添加某天详细数据的键值对
    increas_num = int(len(detail_data[i]) / 8)
    # print(detail_data[i])
    # print(len(detail_data[i]))
    # print(increas_num)
    for j in range(8):
        detail_key = detail_data[i][increas_num * j]
        detail_value = detail_data[i][increas_num * j + 1: increas_num * j + increas_num]
        data[key]['detail'].update({detail_key: detail_value})

# print(data)

# 转换为 JSON 格式的字符串
json_str = json.dumps(data, ensure_ascii=False)
print(json_str)

with open('data.json', "w") as file:
    json.dump(data, file, ensure_ascii=False)

with open('data.json', 'r') as file:
    json_data = json.load(file)

print(json_data)


import sys
input = sys.stdin.readline

N = int(input())

max_dp = [0, 0, 0]
min_dp = [0, 0, 0]

for i in range(N):
    a, b, c = map(int, input().split())
    for j in range(3):
        if j == 0:
            max_tmp1 = max(max_dp[j], max_dp[j+1]) + a
            min_tmp1 = min(min_dp[j], min_dp[j+1]) + a
        elif j == 1:
            max_tmp2 = max(max_dp[j-1], max_dp[j], max_dp[j+1]) + b
            min_tmp2 = min(min_dp[j-1], min_dp[j], min_dp[j+1]) + b
        elif j == 2:
            max_tmp3 = max(max_dp[j-1], max_dp[j]) + c
            min_tmp3 = min(min_dp[j-1], min_dp[j]) + c

    max_dp = [max_tmp1, max_tmp2, max_tmp3]
    min_dp = [min_tmp1, min_tmp2, min_tmp3]

print(max(max_dp), min(min_dp))


import streamlit as st
import streamlit.components.v1 as html
import pandas as pd
import numpy as np
import pickle
import string
import string
import io
import re
import nltk
from Sastrawi.Stemmer.StemmerFactory import StemmerFactory
from nltk.corpus import stopwords
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn import metrics
from streamlit_option_menu import option_menu
from PIL import Image

with st.container():
    st.markdown('
 Analisis Sentimen - Anteraja
', unsafe_allow_html = True)
    st.markdown('
 Analisis sentimen adalah proses menganalisis teks digital untuk menentukan apakah nada emosional pesan tersebut positif, negatif, atau netral. Anteraja adalah jasa pengiriman online yang mengedepankan nilai SATRIA. Sigap, Aman, Terpercaya, Ramah, Integritas, dan Amanah. Aplikasi ini digunakan untuk mengetahui analisis sentimen komentar tentang anteraja.
', unsafe_allow_html = True)

    logo = Image.open('anteraja.png')
    st.image(logo, caption='')

    df = pd.read_csv('https://raw.githubusercontent.com/AriAndiM/dataset/main/anteraja-balanced.csv')
    # hasil_tfidf1

    # mengambil data
    x = df['Filter By Length']
    # x
    # mengambil label
    y = df['Label'].values
    # y

    # splitting data
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, shuffle=False)
    # st.write(y_train.shape)
    # st.write(y_test.shape)

    with open('nb_pickle.pkl','rb') as r:
        nbp = pickle.load(r)

    # pred = nbp.predict(x_test)
    # st.write('Akurasi',round(accuracy_score(y_test, pred)*100,2),'%')

    teks_inputan = st.text_input('Masukkan Teks')
    cek = st.button('cek', type='primary')
    if cek:
        # preprocesing
        # remove punctuation, emot
        def remove_punctuation(text):
            # untuk mengganti username menjadi spasi
            teks_inputan=re.sub('@[^\s]+', ' ', text)
            # untuk mengganti link dengan spasi
            teks_inputan = re.sub(r'http\S*', ' ', teks_inputan)
            # menghapus tanda baca seperti #,^ dll
            teks_inputan=teks_inputan.translate(str.maketrans(' ',' ',string.punctuation))
            # mengapus semua karakter lain selain a-z atau A-Z seperti emot
            teks_inputan=re.sub('[^a-zA-Z]',' ',teks_inputan)
            # mengganti newline dengan spasi
            teks_inputan=re.sub("\n"," ",teks_inputan)
            # menghapus kata yang hanya memiliki 1 huruf saja
            teks_inputan=re.sub(r"\b[a-zA-z]\b"," ",teks_inputan)
            # menghilangkan spasi berlebihan pada kalimat
            teks_inputan=' '.join(teks_inputan.split())
            return teks_inputan

        # lower case
        lower = remove_punctuation(teks_inputan).lower()
        # st.write(lower)

        # proses tokenize berdasarkan spasi menggunakan WhitespaceTokenizer() dari hasil lower tweet
        tokenize = nltk.tokenize.WhitespaceTokenizer().tokenize(lower)
        # st.write(tokenize)

        # stemming
        # create stemmer
        Fact = StemmerFactory()
        Stemmer = Fact.create_stemmer()

        def steming(x):
            # proses stemming
            b = []
            for kata in x:
                # proses stemming setiap kata
                a = Stemmer.stem(kata)
                # memasukkan kata yang sudah di stemming ke array
                b.append(a)
            # menggabungkan array dengan pemisal ','
            return ','.join(b)

        data_steming = steming(tokenize)
        # st.write(data_steming)
        # Stopword Removal

        # Initialize the stopwords
        nltk.download('stopwords')
        stoplist = stopwords.words('indonesian')
        def remove_stopwords(x):
            a = []
            # memisahkan antar kata berdasarkan spasi
            for i in x.split(','):
                # dilakukan pengecekan pada stoplist
                if i not in stoplist:
                    # memasukkan kata yang tidak ada di stoplist ke array
                    a.append(i)
            return ','.join(a)

        stopword = remove_stopwords(data_steming)
        # st.write(stopword)

        # Filter Token By Length
        def filter_by_length(kata):
            a = []
            # memisahkan antar kata berdasarkan koma
            for i in kata.split(','):
                # dilakukan pengecekan jumlah huruf dalam kata
                if len(i) >= 4 and len(i) <= 25:
                    # memasukkan kata yang memiliki jumlah huruf >=4 dan <= 25 ke list
                    a.append(i)
            return ','.join(a)

        #pickle
        import pickle
        # wb - write binary
        with open('tfidf_pickle.pkl','rb') as f:
            vectorizer = pickle.load(f)

        # x_train, x_test, y_train, y_test = train_test_split(dt, label, test_size=0.4, shuffle=False)
        with open('nb_pickle.pkl','rb') as r:
            nbp = pickle.load(r)

        filter = filter_by_length(stopword)
        # st.write(filter)
        # transform = vectorizer.transform([" ".join(a)])
        transform = vectorizer.transform([filter])
        # st.write(transform)

        # pred = nbp.predict(np.asarray(transform.todense()))
        pred = nbp.predict(transform)
        st.write('Teks ','"',teks_inputan,'"',' memiliki sentimen :',pred[0])


import os
import subprocess
import tarfile
from os.path import exists, dirname
from threading import Thread

_running_downloads = {}

def _get_download_tmp(dest):
    tmp_base = dest + '.part'
    if not exists(tmp_base):
        return tmp_base
    else:
        i = 1
        while (True):
            tmp = tmp_base + '.' + str(i)
            if not exists(tmp):
                return tmp
            else:
                i += 1

class Downloader(Thread):
    """
        Downloader is a thread based downloader instance when instanciated
        it will download the provided url to a file on disk.

        When the download is complete or failed the `.done` property will
        be set to true and the `.status` will indicate the status code.
        200 = Success.

        Args:
            url:            Url to download
            dest:           Path to save data to
            complet_action: Function to run when download is complete.
                            `func(dest)`
    """

    def __init__(self, url, dest, complete_action=None, header=None):
        super(Downloader, self).__init__()
        self.url = url
        self.dest = dest
        self.complete_action = complete_action
        self.status = None
        self.done = False
        self._abort = False
        self.header = header

        # Create directories as needed
        if not exists(dirname(dest)):
            os.makedirs(dirname(dest))

        #  Start thread
        self.daemon = True
        self.start()

    def perform_download(self, dest):

        cmd = ['wget', '-c', self.url, '-O', dest,
               '--tries=20', '--read-timeout=5']
        if self.header:
            cmd += ['--header={}'.format(self.header)]
        return subprocess.call(cmd)

    def run(self):
        """
            Does the actual download.
        """
        tmp = _get_download_tmp(self.dest)
        self.status = self.perform_download(tmp)

        if not self._abort and self.status == 0:
            self.finalize(tmp)
        else:
            self.cleanup(tmp)
        self.done = True
        arg_hash = hash(self.url + self.dest)

        #  Remove from list of currently running downloads
        if arg_hash in _running_downloads:
            _running_downloads.pop(arg_hash)

    def finalize(self, tmp):
        """
            Move the .part file to the final destination and perform any
            actions that should be performed at completion.
        """
        os.rename(tmp, self.dest)
        if self.complete_action:
            self.complete_action(self.dest)

    def cleanup(self, tmp):
        """
            Cleanup after download attempt
        """
        if exists(tmp):
            os.remove(self.dest + '.part')
        if self.status == 200:
            self.status = -1

    def abort(self):
        """
            Abort download process
        """
        self._abort = True

def download(url, dest, complete_action=None, header=None):
    global _running_downloads
    arg_hash = hash(url + dest)
    if arg_hash not in _running_downloads:
        _running_downloads[arg_hash] = Downloader(url, dest, complete_action,
                                                  header)
    return _running_downloads[arg_hash]

def untar(file_path, dest, remove=False):
    if not exists(file_path):
        raise AssertionError("file does not exist")
    if not file_path.endswith(".tar.gz") or not file_path.endswith(".tar.bz2"):
        raise AssertionError("invalid file format")
    with tarfile.open(file_path) as tar:
        tar.extractall(dest)
    if remove:
        os.remove(file_path)


import textwrap
import itertools

def sidebyside(left, right, width=79):
    w = (width -1)//2
    llist = textwrap.wrap(left,width=w)
    rlist = textwrap.wrap(right,width=w)
    out = ""
    for l,r in itertools.zip_longest(llist,rlist,fillvalue=" "*w):
        out += l+(w-len(l))*" "+'|'+ r +(w-len(r))*" "+ "\n"
    return out

left = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
"Sed non risus. "
"Suspendisse lectus tortor, dignissim sit amet, "
"adipiscing nec, utilisez sed sin dolor."
)
right = (
"Morbi venenatis, felis nec pretium euismod, "
"est mauris finibus risus, consectetur laoreet "
"sem enim sed arcu. Maecenas sit amet eleifend sem. "
"Nullam ac libero metus. Praesent ac finibus nulla, vitae molestie dolor."
" Aliquam vestibulum viverra nisl, id porta mi viverra hendrerit."
" Ut et porta augue, et convallis ante."
)

print(sidebyside(left, right))

"""
Lorem ipsum dolor sit amet, consectetur|Morbi venenatis, felis nec pretium
adipiscing elit. Sed non risus.        |euismod, est mauris finibus risus,
Suspendisse lectus tortor, dignissim   |consectetur laoreet sem enim sed arcu.
sit amet, adipiscing nec, utilisez sed |Maecenas sit amet eleifend sem. Nullam
sin dolor.                             |ac libero metus. Praesent ac finibus
                                       |nulla, vitae molestie dolor. Aliquam
                                       |vestibulum viverra nisl, id porta mi
                                       |viverra hendrerit. Ut et porta augue,
                                       |et convallis ante.
"""
print(sidebyside(left, right, 50))
"""
Lorem ipsum dolor sit   |Morbi venenatis, felis
amet, consectetur       |nec pretium euismod, est
adipiscing elit. Sed non|mauris finibus risus,
risus. Suspendisse      |consectetur laoreet sem
lectus tortor, dignissim|enim sed arcu. Maecenas
sit amet, adipiscing    |sit amet eleifend sem.
nec, utilisez sed sin   |Nullam ac libero metus.
dolor.                  |Praesent ac finibus
                        |nulla, vitae molestie
                        |dolor. Aliquam
                        |vestibulum viverra nisl,
                        |id porta mi viverra
                        |hendrerit. Ut et porta
                        |augue, et convallis
                        |ante.
"""
print(sidebyside(left, right, 100))
"""
Lorem ipsum dolor sit amet, consectetur          |Morbi venenatis, felis nec pretium euismod, est
adipiscing elit. Sed non risus. Suspendisse      |mauris finibus risus, consectetur laoreet sem
lectus tortor, dignissim sit amet, adipiscing    |enim sed arcu. Maecenas sit amet eleifend sem.
nec, utilisez sed sin dolor.                     |Nullam ac libero metus. Praesent ac finibus
                                                 |nulla, vitae molestie dolor. Aliquam vestibulum
                                                 |viverra nisl, id porta mi viverra hendrerit. Ut
                                                 |et porta augue, et convallis ante.
"""
print(sidebyside(left, sidebyside(left, left, 50), 100))
"""
Lorem ipsum dolor sit amet, consectetur          |Lorem ipsum dolor sit   |Lorem ipsum dolor sit
adipiscing elit. Sed non risus. Suspendisse      |amet, consectetur       |amet, consectetur
lectus tortor, dignissim sit amet, adipiscing    |adipiscing elit. Sed non|adipiscing elit. Sed non
nec, utilisez sed sin dolor.                     |risus. Suspendisse      |risus. Suspendisse
                                                 |lectus tortor, dignissim|lectus tortor, dignissim
                                                 |sit amet, adipiscing    |sit amet, adipiscing
                                                 |nec, utilisez sed sin   |nec, utilisez sed sin
                                                 |dolor.                  |dolor.
"""
print(sidebyside(left, right, width = 20))
"""
Lorem    |Morbi ven
ipsum    |enatis,
dolor sit|felis nec
amet, con|pretium
sectetur |euismod,
adipiscin|est
g elit.  |mauris
Sed non  |finibus
risus. Su|risus, co
spendisse|nsectetur
lectus   |laoreet
tortor,  |sem enim
dignissim|sed arcu.
sit amet,|Maecenas
adipiscin|sit amet
g nec,   |eleifend
utilisez |sem.
sed sin  |Nullam ac
dolor.   |libero
         |metus.
         |Praesent
         |ac
         |finibus
         |nulla,
         |vitae
"""


#!/usr/bin/python2.7
import sys
import fileinput
import ast

length = 0

pos = 0
neg = 0
neu = 0

for line in fileinput.input():
    length = length + 1
    (_1,stat) = ast.literal_eval(line)
    if stat == "positive":
        pos = pos + 1
    if stat == "negative":
        neg = neg + 1
    if stat == "neutral":
        neu = neu +1

print("Positive: " + str(100*pos/length) + "%\n")
print("Negative: " + str(100*neg/length) + "%\n")
print("Neutral: " + str(100*neu/length) + "%\n")


class Pessoa:
      def __init__(self, nome, idade):
        self.nome = nome
        self.idade = idade

empresa=[]
x=0
y=0
while(x==0):
    nome=str(input("Digite seu nome: "))
    idade=int(input("Digite sua idade: "))
    empresa.append(Pessoa(nome,idade))
    y += 1
    x = int(input("Deseja novo cadastro: \n(0) - Sim\n(1)-Não\nSua resposta: "))

def tabela():
    for x in range(0,y,1):
        print("{:<3}{:<12}{:<0}".format(x,empresa[x].nome,empresa[x].idade))
tabela()
res = int(input("Deseja alterar algo: \n(0) - Sim\n(1)-Não\nSua resposta: "))
if(res==0):
    res = int(input("Escolha o numero da pessoa: "))
    nome=str(input("Digite seu nome: "))
    idade=int(input("Digite sua idade: "))
    empresa[res] = Pessoa(nome,idade)
tabela()


for _ in range(int(input())):
    n, c, q = [int(x) for x in input().split()]
    s = input()

    indices = []
    queries = []

    for i in range(c):
        index = [int(x) for x in input().split()]
        indices += index
    for i in range(q):
        queries.append(int(input()))

    for i in range(q):
        if n >= queries[i]:
            print(s[queries[i]-1])
        else:
            tempN = n
            for j in range(0, len(indices), 2):
                print("tempN", queries[i])
                l = tempN - indices[j]
                r = tempN - indices[j + 1]
                tempN += indices[j + 1] - indices[j] + 1
                if tempN > queries[i]:
                    print("ashse", s, l, r, indices[j], indices[j+1])
                    break
                elif tempN == queries[i]:
                    print(s[indices[j] - 1:indices[j + 1]][-1])
                    break