import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
# import matplotlib.image as plt_img
from collections import Counter
# from flask import request
import cv2

def invert():
    image = cv2.imread("static/img/img_now.jpg")
    new_image = cv2.bitwise_not(image)
    cv2.imwrite("static/img/img_now.jpg", new_image)

def grayscale():
    image = cv2.imread("static/img/img_now.jpg")
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    cv2.imwrite("static/img/img_now.jpg", gray)

def brightness(value):
    img = cv2.imread("static/img/img_now.jpg")
    # Hệ màu HSV/HSB
    # H: Vùng màu
    # S: Độ bão hoà màu
    # V(B): Độ sáng
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(hsv)

    lim = 255 - value
    v[v > lim] = 255
    v[v <= lim] += value

    print("processing")
    print(value)

    final_hsv = cv2.merge((h, s, v))
    img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
    cv2.imwrite("static/img/img_now.jpg", img)

def kmeans(k):
    # k : number of area
    image = cv2.imread("static/img/img_now.jpg")
    # convert to RGB
    new_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    # reshape the image to a 2D array of pixels and 3 color values (RGB)
    new_image = new_image.reshape((-1, 3))
    # convert to float
    new_image = np.float32(image)
    # xác định tiêu chí, số lượng cụm (K) và áp dụng công thức hàm kmeans ()
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)
    ret, label, center = cv2.kmeans(new_image, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
    # sau đó chuyển đổi trở lại thành uint8 và tạo hình ảnh gốc
    center = np.uint8(center)
    res = center[label.flatten()]
    new_image = res.reshape(image.shape)
    cv2.imwrite("static/img/img_now.jpg", new_image)

def edge_detection():
    image = cv2.imread("static/img/img_now.jpg")
    # Canny edge detection
    img = cv2.Canny(image, 30, 70)
    cv2.imwrite("static/img/img_now.jpg", img)

def threshold(low, high):
    image = cv2.imread("static/img/img_now.jpg")
    # cat nguong
    ret, th = cv2.threshold(image, low, high, cv2.THRESH_BINARY)
    # ret, th2 = cv2.threshold(image, low, high, cv2.THRESH_BINARY_INV)
    # ret, th3 = cv2.threshold(image, low, high, cv2.THRESH_TRUNC)
    # ret, th4 = cv2.threshold(image, low, high, cv2.THRESH_TOZERO)
    # ret, th5 = cv2.threshold(image, low, high, cv2.THRESH_TOZERO_INV)
    cv2.imwrite("static/img/img_now.jpg", th)

def grabcut(bit):
    # Đọc hình ảnh trong thang độ xám
    image = cv2.imread("static/img/img_now.jpg")
    # Lặp lại từng pixel và thay đổi giá trị pixel thành nhị phân bằng cách sử dụng np.binary_repr () và lưu trữ
    # trong danh sách
    lst = []
    for i in range(image.shape[0]):
        for j in range(image.shape[1]):
            lst.append(np.binary_repr(image[i][j], width=8))  # width = no. of bits

    if bit == 1:
        new_image = (np.array([int(i[7]) for i in lst], dtype=np.uint8) * 1).reshape(image.shape[0], image.shape[1])
    elif bit == 2:
        new_image = (np.array([int(i[6]) for i in lst], dtype=np.uint8) * 2).reshape(image.shape[0], image.shape[1])
    elif bit == 3:
        new_image = (np.array([int(i[5]) for i in lst], dtype=np.uint8) * 4).reshape(image.shape[0], image.shape[1])
    elif bit == 4:
        new_image = (np.array([int(i[4]) for i in lst], dtype=np.uint8) * 8).reshape(image.shape[0], image.shape[1])
    elif bit == 5:
        new_image = (np.array([int(i[3]) for i in lst], dtype=np.uint8) * 16).reshape(image.shape[0], image.shape[1])
    elif bit == 6:
        new_image = (np.array([int(i[2]) for i in lst], dtype=np.uint8) * 32).reshape(image.shape[0], image.shape[1])
    elif bit == 7:
        new_image = (np.array([int(i[1]) for i in lst], dtype=np.uint8) * 64).reshape(image.shape[0], image.shape[1])
    else:
        new_image = (np.array([int(i[0]) for i in lst], dtype=np.uint8) * 128).reshape(image.shape[0], image.shape[1])
    cv2.imwrite("static/img/img_now.jpg", new_image)

def sharpening():
    image = cv2.imread("static/img/img_now.jpg", flags=cv2.IMREAD_COLOR)
    kernel = np.array([[0, -1, 0],
                       [-1, 5, -1],
                       [0, -1, 0]])
    new_image = cv2.filter2D(src=image, ddepth=-1, kernel=kernel)
    cv2.imwrite("static/img/img_now.jpg", new_image)

def smoothing(matrix):
    image = cv2.imread("static/img/img_now.jpg")
    # lọc trung vị nxn
    new_image = cv2.blur(image, (matrix, matrix))
    cv2.imwrite("static/img/img_now.jpg", new_image)

def zoomin():
    img = Image.open("static/img/img_now.jpg")
    img = img.convert("RGB")
    img_arr = np.asarray(img)
    new_size = ((img_arr.shape[0] * 2),
                (img_arr.shape[1] * 2), img_arr.shape[2])
    new_arr = np.full(new_size, 255)
    new_arr.setflags(write=1)

    r = img_arr[:, :, 0]
    g = img_arr[:, :, 1]
    b = img_arr[:, :, 2]

    new_r = []
    new_g = []
    new_b = []

    for row in range(len(r)):
        temp_r = []
        temp_g = []
        temp_b = []
        for i in r[row]:
            temp_r.extend([i, i])
        for j in g[row]:
            temp_g.extend([j, j])
        for k in b[row]:
            temp_b.extend([k, k])
        for _ in (0, 1):
            new_r.append(temp_r)
            new_g.append(temp_g)
            new_b.append(temp_b)

    for i in range(len(new_arr)):
        for j in range(len(new_arr[i])):
            new_arr[i, j, 0] = new_r[i][j]
            new_arr[i, j, 1] = new_g[i][j]
            new_arr[i, j, 2] = new_b[i][j]

    new_arr = new_arr.astype('uint8')
    new_img = Image.fromarray(new_arr)
    new_img.save("static/img/img_now.jpg")

def zoomout():
    img = Image.open("static/img/img_now.jpg")
    img = img.convert("RGB")
    x, y = img.size
    new_arr = Image.new("RGB", (int(x / 2), int(y / 2)))
    r = [0, 0, 0, 0]
    g = [0, 0, 0, 0]
    b = [0, 0, 0, 0]

    for i in range(0, int(x / 2)):
        for j in range(0, int(y / 2)):
            r[0], g[0], b[0] = img.getpixel((2 * i, 2 * j))
            r[1], g[1], b[1] = img.getpixel((2 * i + 1, 2 * j))
            r[2], g[2], b[2] = img.getpixel((2 * i, 2 * j + 1))
            r[3], g[3], b[3] = img.getpixel((2 * i + 1, 2 * j + 1))
            new_arr.putpixel((int(i), int(j)), (int((r[0] + r[1] + r[2] + r[3]) / 4), int(
                (g[0] + g[1] + g[2] + g[3]) / 4), int((b[0] + b[1] + b[2] + b[3]) / 4)))
    new_arr = np.uint8(new_arr)
    new_img = Image.fromarray(new_arr)
    new_img.save("static/img/img_now.jpg")

def move_left():
    img = Image.open("static/img/img_now.jpg")
    img_arr = np.asarray(img)
    r, g, b = img_arr[:, :, 0], img_arr[:, :, 1], img_arr[:, :, 2]
    r = np.pad(r, ((0, 0), (0, 50)), 'constant')[:, 50:]
    g = np.pad(g, ((0, 0), (0, 50)), 'constant')[:, 50:]
    b = np.pad(b, ((0, 0), (0, 50)), 'constant')[:, 50:]
    new_arr = np.dstack((r, g, b))
    new_img = Image.fromarray(new_arr)
    new_img.save("static/img/img_now.jpg")

def move_right():
    img = Image.open("static/img/img_now.jpg")
    img_arr = np.asarray(img)
    r, g, b = img_arr[:, :, 0], img_arr[:, :, 1], img_arr[:, :, 2]
    r = np.pad(r, ((0, 0), (50, 0)), 'constant')[:, :-50]
    g = np.pad(g, ((0, 0), (50, 0)), 'constant')[:, :-50]
    b = np.pad(b, ((0, 0), (50, 0)), 'constant')[:, :-50]
    new_arr = np.dstack((r, g, b))
    new_img = Image.fromarray(new_arr)
    new_img.save("static/img/img_now.jpg")

def move_up():
    img = Image.open("static/img/img_now.jpg")
    img_arr = np.asarray(img)
    r, g, b = img_arr[:, :, 0], img_arr[:, :, 1], img_arr[:, :, 2]
    r = np.pad(r, ((0, 50), (0, 0)), 'constant')[50:, :]
    g = np.pad(g, ((0, 50), (0, 0)), 'constant')[50:, :]
    b = np.pad(b, ((0, 50), (0, 0)), 'constant')[50:, :]
    new_arr = np.dstack((r, g, b))
    new_img = Image.fromarray(new_arr)
    new_img.save("static/img/img_now.jpg")

def move_down():
    img = Image.open("static/img/img_now.jpg")
    img_arr = np.asarray(img)
    r, g, b = img_arr[:, :, 0], img_arr[:, :, 1], img_arr[:, :, 2]
    r = np.pad(r, ((50, 0), (0, 0)), 'constant')[0:-50, :]
    g = np.pad(g, ((50, 0), (0, 0)), 'constant')[0:-50, :]
    b = np.pad(b, ((50, 0), (0, 0)), 'constant')[0:-50, :]
    new_arr = np.dstack((r, g, b))
    new_img = Image.fromarray(new_arr)
    new_img.save("static/img/img_now.jpg")

def brightness_addition():
    img = Image.open("static/img/img_now.jpg")
    img_arr = np.asarray(img).astype('uint16')
    img_arr = img_arr + 100
    img_arr = np.clip(img_arr, 0, 255)
    new_arr = img_arr.astype('uint8')
    new_img = Image.fromarray(new_arr)
    new_img.save("static/img/img_now.jpg")

def brightness_substraction():
    img = Image.open("static/img/img_now.jpg")
    img_arr = np.asarray(img).astype('int16')
    img_arr = img_arr - 100
    img_arr = np.clip(img_arr, 0, 255)
    new_arr = img_arr.astype('uint8')
    new_img = Image.fromarray(new_arr)
    new_img.save("static/img/img_now.jpg")

def convolution(img, kernel):
    h_img, w_img, _ = img.shape
    out = np.zeros((h_img - 2, w_img - 2), dtype=np.float)
    new_img = np.zeros((h_img - 2, w_img - 2, 3))
    if np.array_equal((img[:, :, 1], img[:, :, 0]), img[:, :, 2]):
        array = img[:, :, 0]
        for h in range(h_img - 2):
            for w in range(w_img - 2):
                S = np.multiply(array[h:h + 3, w:w + 3], kernel)
                out[h, w] = np.sum(S)
        out_ = np.clip(out, 0, 255)
        for channel in range(3):
            new_img[:, :, channel] = out_
    else:
        for channel in range(3):
            array = img[:, :, channel]
            for h in range(h_img - 2):
                for w in range(w_img - 2):
                    S = np.multiply(array[h:h + 3, w:w + 3], kernel)
                    out[h, w] = np.sum(S)
            out_ = np.clip(out, 0, 255)
            new_img[:, :, channel] = out_
    new_img = np.uint8(new_img)
    return new_img

def blur():
    img = Image.open("static/img/img_now.jpg")
    img_arr = np.asarray(img, dtype=np.int)
    kernel = np.array(
        [[0.0625, 0.125, 0.0625], [0.125, 0.25, 0.125], [0.0625, 0.125, 0.0625]])
    new_arr = convolution(img_arr, kernel)
    new_img = Image.fromarray(new_arr)
    new_img.save("static/img/img_now.jpg")

def is_grey_scale(img_path):
    im = Image.open(img_path).convert('RGB')
    w, h = im.size
    for i in range(w):
        for j in range(h):
            r, g, b = im.getpixel((i, j))
            if r != g != b:
                return False
    return True

def histogram_rgb():
    img_path = "static/img/img_now.jpg"
    img = Image.open(img_path)
    img_arr = np.asarray(img)
    if is_grey_scale(img_path):
        g = img_arr[:, :, 0].flatten()
        data_g = Counter(g)
        plt.bar(list(data_g.keys()), data_g.values(), color='black')
        plt.savefig(f'static/img/grey_histogram.jpg', dpi=300)
        plt.clf()
    else:
        r = img_arr[:, :, 0].flatten()
        g = img_arr[:, :, 1].flatten()
        b = img_arr[:, :, 2].flatten()
        data_r = Counter(r)
        data_g = Counter(g)
        data_b = Counter(b)
        data_rgb = [data_r, data_g, data_b]
        warna = ['red', 'green', 'blue']
        data_hist = list(zip(warna, data_rgb))
        for data in data_hist:
            plt.bar(list(data[1].keys()), data[1].values(), color=f'{data[0]}')
            plt.savefig(f'static/img/{data[0]}_histogram.jpg', dpi=300)
            plt.clf()

def df(img):  # to make a histogram (count distribution frequency)
    values = [0] * 256
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            values[img[i, j]] += 1
    return values

def cdf(hist):  # cumulative distribution frequency
    cdf = [0] * len(hist)  # len(hist) is 256
    cdf[0] = hist[0]
    for i in range(1, len(hist)):
        cdf[i] = cdf[i - 1] + hist[i]
    # Now we normalize the histogram
    # What your function h was doing before
    cdf = [ele * 255 / cdf[-1] for ele in cdf]
    return cdf

def histogram_equalizer():
    img = cv2.imread('static\img\img_now.jpg', 0)
    my_cdf = cdf(df(img))
    # use linear interpolation of cdf to find new pixel values. Scipy alternative exists
    image_equalized = np.interp(img, range(0, 256), my_cdf)
    cv2.imwrite('static/img/img_now.jpg', image_equalized)


from datetime import timedelta
from typing import List, Dict

from hypothesis import given, HealthCheck, settings, Phase
from hypothesis.strategies import integers

from tests.base_test_case import BaseTestCase

from electionguard.ballot import CiphertextBallot
from electionguard.decrypt_with_secrets import decrypt_ballot_with_secret
from electionguard.elgamal import ElGamalCiphertext, elgamal_encrypt, elgamal_add
from electionguard.encrypt import encrypt_ballot
from electionguard.group import ElementModQ
from electionguard.manifest import Manifest
from electionguard.nonces import Nonces
from electionguard_tools.strategies.election import (
    election_descriptions,
    elections_and_ballots,
    ElectionsAndBallotsTupleType,
)
from electionguard_tools.factories.election_factory import ElectionFactory
from electionguard_tools.strategies.group import elements_mod_q
from electionguard_tools.helpers.tally_accumulate import accumulate_plaintext_ballots

SEED = ElectionFactory.get_encryption_device().get_hash()

class TestElections(BaseTestCase):
    """Election hypothesis encryption tests"""

    @settings(
        deadline=timedelta(milliseconds=2000),
        suppress_health_check=[HealthCheck.too_slow],
        max_examples=10,
    )
    @given(election_descriptions())
    def test_generators_yield_valid_output(self, manifest: Manifest):
        """
        Tests that our Hypothesis election strategies generate "valid" output, also exercises the full stack
        of `is_valid` methods.
        """

        self.assertTrue(manifest.is_valid())

    @settings(
        deadline=timedelta(milliseconds=10000),
        suppress_health_check=[HealthCheck.too_slow],
        max_examples=5,
        # disabling the "shrink" phase, because it runs very slowly
        phases=[Phase.explicit, Phase.reuse, Phase.generate, Phase.target],
    )
    @given(
        integers(1, 3).flatmap(lambda n: elections_and_ballots(n)),
        elements_mod_q(),
    )
    def test_accumulation_encryption_decryption(
        self,
        everything: ElectionsAndBallotsTupleType,
        nonce: ElementModQ,
    ):
        """
        Tests that decryption is the inverse of encryption over arbitrarily generated elections and ballots.

        This test uses an abitrarily generated dataset with a single public-private keypair for the election
        encryption context.  It also manually verifies that homomorphic accumulation works as expected.
        """
        # Arrange
        (
            _election_description,
            internal_manifest,
            ballots,
            secret_key,
            context,
        ) = everything

        # Tally the plaintext ballots for comparison later
        plaintext_tallies = accumulate_plaintext_ballots(ballots)
        num_ballots = len(ballots)
        num_contests = len(internal_manifest.contests)
        zero_nonce, *nonces = Nonces(nonce)[: num_ballots + 1]
        self.assertEqual(len(nonces), num_ballots)
        self.assertTrue(len(internal_manifest.contests) > 0)

        # Generate a valid encryption of zero
        encrypted_zero = elgamal_encrypt(0, zero_nonce, context.elgamal_public_key)

        # Act
        encrypted_ballots = []

        # encrypt each ballot
        for i in range(num_ballots):
            encrypted_ballot = encrypt_ballot(
                ballots[i],
                internal_manifest,
                context,
                SEED,
                nonces[i],
                should_verify_proofs=True,
            )
            encrypted_ballots.append(encrypted_ballot)

            # sanity check the encryption
            self.assertIsNotNone(encrypted_ballot)
            self.assertEqual(num_contests, len(encrypted_ballot.contests))

            # decrypt the ballot with secret and verify it matches the plaintext
            decrypted_ballot = decrypt_ballot_with_secret(
                encrypted_ballot,
                internal_manifest,
                context.crypto_extended_base_hash,
                context.elgamal_public_key,
                secret_key,
            )
            self.assertEqual(ballots[i], decrypted_ballot)

        # homomorphically accumulate the encrypted ballot representations
        encrypted_tallies = _accumulate_encrypted_ballots(
            encrypted_zero, encrypted_ballots
        )

        decrypted_tallies = {}
        for object_id, encrypted_tally in encrypted_tallies.items():
            decrypted_tallies[object_id] = encrypted_tally.decrypt(secret_key)

        # loop through the contest descriptions and verify
        # the decrypted tallies match the plaintext tallies
        for contest in internal_manifest.contests:
            # Sanity check the generated data
            self.assertTrue(len(contest.ballot_selections) > 0)
            self.assertTrue(len(contest.placeholder_selections) > 0)

            decrypted_selection_tallies = [
                decrypted_tallies[selection.object_id]
                for selection in contest.ballot_selections
            ]
            decrypted_placeholder_tallies = [
                decrypted_tallies[placeholder.object_id]
                for placeholder in contest.placeholder_selections
            ]
            plaintext_tally_values = [
                plaintext_tallies[selection.object_id]
                for selection in contest.ballot_selections
            ]

            # verify the plaintext tallies match the decrypted tallies
            self.assertEqual(decrypted_selection_tallies, plaintext_tally_values)

            # validate the right number of selections including placeholders across all ballots
            self.assertEqual(
                contest.number_elected * num_ballots,
                sum(decrypted_selection_tallies) + sum(decrypted_placeholder_tallies),
            )

def _accumulate_encrypted_ballots(
    encrypted_zero: ElGamalCiphertext, ballots: List[CiphertextBallot]
) -> Dict[str, ElGamalCiphertext]:
    """
    Internal helper function for testing: takes a list of encrypted ballots as input,
    digs into all of the individual selections and then accumulates them, using
    their `object_id` fields as keys. This function only knows what to do with
    `n_of_m` elections. It's not a general-purpose tallying mechanism for other
    election types.

    Note that the output will include both "normal" and "placeholder" selections.

    :param encrypted_zero: an encrypted zero, used for the accumulation
    :param ballots: a list of encrypted ballots
    :return: a dict from selection object_id's to `ElGamalCiphertext` totals
    """
    tally: Dict[str, ElGamalCiphertext] = {}
    for ballot in ballots:
        for contest in ballot.contests:
            for selection in contest.ballot_selections:
                desc_id = (
                    selection.object_id
                )  # this should be the same as in the PlaintextBallot!
                if desc_id not in tally:
                    tally[desc_id] = encrypted_zero
                tally[desc_id] = elgamal_add(tally[desc_id], selection.ciphertext)
    return tally


import sys
import os
from PyQt6.QtGui import QKeySequence
from PyQt6.QtWidgets import QApplication
from toolbaricons import ToolbarIcons

class StatusShortcuts(ToolbarIcons):
    def __init__(self):
        super(StatusShortcuts, self).__init__()
        self.setWindowTitle("Python Status Bar & Shortcuts")
        self.statusBar = self.statusBar()
        self.openAction.setStatusTip("Open File")
        self.statusBar.showMessage("Ready")
        self.openAction.setShortcut(QKeySequence("Ctrl+O"))
        self.openAction.triggered.connect(self._openFile)
        self._addMenu()

    def _openFile(self):
        self.label.setText("Open File Clicked")
        self.statusBar.showMessage("Open File Clicked")

    def _addMenu(self):
        self.menuBar = self.menuBar()
        self.menuBar.setNativeMenuBar(False)
        self.fileMenu = self.menuBar.addMenu("File")
        self.fileMenu.addAction(self.newAction)
        self.fileMenu.addAction(self.openAction)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = StatusShortcuts()
    window.show()
    app.exec()


from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import ugettext_lazy as _
from users.models import UserProfile, MyGroup, Discount, DoctorMain

# Register your models here.

class UserAdmin(UserAdmin):
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'sur_name', 'adress', 'phone', 'email', 'dob', 'city')}),
        ('Для врачей', {'classes': ('collapse', ), 'fields': ('doctor_main', 'ref_doc', 'oklad', 'information', 'image' )}),
        ('Скидка', {'classes': ('collapse', ), 'fields': ('discount',)}),
        ('Агентство', {'classes': ('collapse', ), 'fields': ('agency',)}),
        (_('Permissions'), {'classes': ('collapse', ), 'fields': ('is_active', 'is_staff', 'is_superuser', 'groups',
                                       'user_permissions')}),
        (_('Important dates'), {'classes': ('collapse', ), 'fields': ('last_login', 'date_joined')}),
    )

admin.site.register(UserProfile, UserAdmin)
admin.site.unregister(Group)
admin.site.register(MyGroup)
admin.site.register(Discount)
admin.site.register(DoctorMain)


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 22 19:04:10 2017

@author: bernier2
"""
from __future__ import print_function

import os

import multiprocessing

import numpy as np

from scipy import ndimage

try:
    import dill as cpl
except(ImportError):
    import cPickle as cpl

import yaml

from hexrd import constants as cnst
from hexrd import config
from hexrd import imageseries
from hexrd.imageseries.omega import OmegaImageSeries
from hexrd import instrument
from hexrd.findorientations import \
    generate_orientation_fibers, \
    run_cluster
from hexrd.xrd import transforms_CAPI as xfcapi

from matplotlib import pyplot as plt

# plane data
def load_pdata(cpkl, key):
    with file(cpkl, "r") as matf:
        mat_list = cpl.load(matf)
    return dict(zip([i.name for i in mat_list], mat_list))[mat_key].planeData

# images
def load_images(yml):
    return imageseries.open(yml, "frame-cache")

# instrument
def load_instrument(yml):
    with file(yml, 'r') as f:
        icfg = yaml.load(f)
    return instrument.HEDMInstrument(instrument_config=icfg)

#%%
#==============================================================================
# START USER INPUT
#==============================================================================

# FIXME:  reconcile with config file!!! Goes for pretty much all the options

materials_fname = 'materials.hexrd'
mat_key = 'steel'

instr_config_filename = 'dexelas_f2_Apr17.yml'

cfg_filename = 'SS-304L2.yml'

image_stem = "%s_%05d"
fc_dir_stem = image_stem + "-fcache-dir"

h5_file_number = 108

# for indexing
map_threshold = 1000
fiber_gen_threshold = 1000
fiber_ndiv = 720
fiber_seeds = [0,1]
tth_tol=0.2
eta_tol=1.0
ome_tol=1.0
npdiv=1

min_samples=5
compl_thresh=0.75
cl_radius=1.0

omegas_filename = 'fastsweep_omegas_360.npy'

max_tth = np.radians(12.5)

make_max_frames = True
max_frames_output_name = 'SS-304L2_cal_scan_001.hdf5'
#==============================================================================
# END USER INPUT
#==============================================================================

#%%
cfg = config.open(cfg_filename)[0]

analysis_id = '%s_%s' %(
    cfg.analysis_name.strip().replace(' ', '-'),
    cfg.material.active.strip().replace(' ', '-'),
    )

# load plane data
pd = load_pdata(materials_fname, mat_key)
pd.tThMax = max_tth

# load instrument
instr = load_instrument(instr_config_filename)
det_keys = instr.detectors.keys()

# load omegas
# WARNING: using master array in root directory!
# FIXME: change ASAP to make saved caches contain omega info!!!
omegas_array = np.load(omegas_filename)

#%%

imsd = dict.fromkeys(det_keys)
for det_key in det_keys:
    str_tuple = (det_key.lower(), h5_file_number)
    yml_file = os.path.join(
            fc_dir_stem % str_tuple,
            image_stem % str_tuple + '-fcache.yml')
    ims = load_images(yml_file)
    # FIXME: imageseries needs to have omegas; kludged for now
    ims.metadata['omega'] = omegas_array
    imsd[det_key] = OmegaImageSeries(ims)

if make_max_frames:
    max_frames = dict.fromkeys(det_keys)
    for det_key in det_keys:
        max_frames[det_key] = np.max(
                np.array([i for i in imsd[det_key]]),
                axis=0
            )
    ims_out = imageseries.open(
            None, 'array',
            data=np.array([max_frames[i] for i in max_frames]),
            meta={'panels':max_frames.keys()}
        )
    imageseries.write(
            ims_out, max_frames_output_name,
            'hdf5', path='/imageseries'
        )
#%%

# note that here maps are only used for quaternion generation
active_hkls = fiber_seeds

# omega period...
# QUESTION: necessary???
ome_period = np.radians(cfg.find_orientations.omega.period)

# make eta_ome maps
eta_ome = instrument.GenerateEtaOmeMaps(
    imsd, instr, pd,
    active_hkls=active_hkls, threshold=map_threshold)

#%%
cmap = plt.cm.hot
cmap.set_under('b')

fig, ax = plt.subplots()
i_ring = 0
this_map_f = -ndimage.filters.gaussian_laplace(
    eta_ome.dataStore[i_ring], 1.0,
)
ax.imshow(this_map_f, interpolation='nearest',
          vmin=0.1, vmax=1.0, cmap=cmap)
labels, num_spots = ndimage.label(
    this_map_f > 0, ndimage.generate_binary_structure(2, 1)
)
coms = np.atleast_2d(
    ndimage.center_of_mass(
        eta_ome.dataStore[i_ring], labels=labels, index=range(1, num_spots+1)
    )
)
ax.hold(True)
ax.plot(coms[:, 1], coms[:, 0], 'm+', ms=12)
ax.axis('tight')
#%%
#==============================================================================
# SEARCH SPACE GENERATION
#==============================================================================
ncpus = multiprocessing.cpu_count()  # USE ALL BY DEFAULT!!!
qfib = generate_orientation_fibers(
    eta_ome, instr.chi, fiber_gen_threshold,
    fiber_seeds, fiber_ndiv,
    ncpus=ncpus)
print("INFO: will test %d quaternions" %qfib.shape[1])
#%%
#==============================================================================
# ORIENTATION SCORING
#==============================================================================

def test_orientation_FF_init(params):
    global paramMP
    paramMP = params

def test_orientation_FF_reduced(quat):
    """
    input parameters are [
    plane_data, instrument, imgser_dict,
    tth_tol, eta_tol, ome_tol, npdiv, threshold
    ]
    """
    plane_data = paramMP['plane_data']
    instrument = paramMP['instrument']
    imgser_dict = paramMP['imgser_dict']
    tth_tol = paramMP['tth_tol']
    eta_tol = paramMP['eta_tol']
    ome_tol = paramMP['ome_tol']
    npdiv = paramMP['npdiv']
    threshold = paramMP['threshold']

    phi = 2*np.arccos(quat[0])
    n = xfcapi.unitRowVector(quat[1:])
    grain_params = np.hstack([
        phi*n, cnst.zeros_3, cnst.identity_6x1,
    ])

    compl, scrap = instrument.pull_spots(
        plane_data, grain_params, imgser_dict,
        tth_tol=tth_tol, eta_tol=eta_tol, ome_tol=ome_tol,
        npdiv=npdiv, threshold=threshold,
        eta_ranges=None, ome_period=(-np.pi, np.pi),
        check_only=True)

    return sum(compl)/float(len(compl))

params = dict(
        plane_data=pd,
        instrument=instr,
        imgser_dict=imsd,
        tth_tol=tth_tol,
        eta_tol=eta_tol,
        ome_tol=ome_tol,
        npdiv=npdiv,
        threshold=fiber_gen_threshold)

pool = multiprocessing.Pool(ncpus, test_orientation_FF_init, (params, ))
completeness = pool.map(test_orientation_FF_reduced, qfib.T)
pool.close()

#%%
#==============================================================================
# CLUSTERING AND GRAINS OUTPUT
#==============================================================================
if not os.path.exists(cfg.analysis_dir):
    os.makedirs(cfg.analysis_dir)

qbar, cl = run_cluster(completeness, qfib, pd.getQSym(), cfg,
            min_samples=min_samples,
            compl_thresh=compl_thresh,
            radius=cl_radius)

np.savetxt('accepted_orientations_' + analysis_id + '.dat', qbar.T,
           fmt='%.18e', delimiter='\t')

gw = instrument.GrainDataWriter(os.path.join(cfg.analysis_dir, 'grains.out'))
grain_params_list = []
for i_g, q in enumerate(qbar.T):
    phi = 2*np.arccos(q[0])
    n = xfcapi.unitRowVector(q[1:])
    grain_params = np.hstack([phi*n, cnst.zeros_3, cnst.identity_6x1])
    gw.dump_grain(int(i_g), 1., 0., grain_params)
    grain_params_list.append(grain_params)
gw.close()


from datetime import datetime

from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from .models import Profile

class CustomUserCreationForm(UserCreationForm):
    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2")

    def clean(self):
        email = self.cleaned_data.get('email')
        if User.objects.filter(email=email).exists():
            raise ValidationError("User with that email already exists.")
        return self.cleaned_data

class ProfileForm(forms.ModelForm):
     class Meta:
        model = Profile
        fields = ['dob', 'image']
        forms.DateInput.input_type='date'
        widgets={
            'dob' : forms.DateInput(attrs={'class' : 'form-control', 'name' : 'dob', 'id' : 'dob', 'value' : '{{ user_id.dob }}'}),
            'image': forms.FileInput(attrs={'class' : 'form-control', 'name' : 'image', 'id' : 'image', 'label': 'Update your profile photo'}),
        }
        labels = {
        'dob': "Date of Birth",
        'image' : "Import Profile Image"
    }


from pathlib import Path
from typing import List, Optional, Union

from .commons import ModelOption
from .faceswap_cv2 import FaceswapCVOption
from .fomm import FOMMOption
from .simswap import SimswapOption

AVAILABLE_SWAP_TYPES = ["simswap", "fomm", "faceswap_cv2"]

class DOT:
    """Main DOT Interface.

    Supported Engines:
        - `simswap`
        - `fomm`
        - `faceswap_cv2`

    Attributes:
        use_cam (bool): Use camera descriptor and pipeline.
        use_video (bool): Use video-swap pipeline.
        use_image (bool): Use image-swap pipeline.
        save_folder (str): Output folder to store face-swaps and metadata file when `use_cam` is False.
    """

    def __init__(
        self,
        use_video: bool = False,
        use_image: bool = False,
        save_folder: str = None,
        *args,
        **kwargs,
    ):
        """Constructor method.

        Args:
            use_video (bool, optional): if True, use video-swap pipeline. Defaults to False.
            use_image (bool, optional): if True, use image-swap pipeline. Defaults to False.
            save_folder (str, optional): Output folder to store face-swaps and metadata file when `use_cam` is False.
                Defaults to None.
        """
        # init
        self.use_video = use_video
        self.save_folder = save_folder
        self.use_image = use_image

        # additional attributes
        self.use_cam = (not use_video) and (not use_image)

        # create output folder
        if self.save_folder and not Path(self.save_folder).exists():
            Path(self.save_folder).mkdir(parents=True, exist_ok=True)

    def build_option(
        self,
        swap_type: str,
        use_gpu: bool,
        gpen_type: str,
        gpen_path: str,
        crop_size: int,
        **kwargs,
    ) -> ModelOption:
        """Build DOT option based on swap type.

        Args:
            swap_type (str): Swap type engine.
            use_gpu (bool): If True, use GPU.
            gpen_type (str): GPEN type.
            gpen_path (str): path to GPEN model checkpoint.
            crop_size (int): crop size.

        Returns:
            ModelOption: DOT option.
        """
        if swap_type not in AVAILABLE_SWAP_TYPES:
            raise ValueError(f"Invalid swap type: {swap_type}")

        option: ModelOption = None
        if swap_type == "simswap":
            option = self.simswap(
                use_gpu=use_gpu,
                gpen_type=gpen_type,
                gpen_path=gpen_path,
                crop_size=crop_size,
            )
        elif swap_type == "fomm":
            option = self.fomm(
                use_gpu=use_gpu, gpen_type=gpen_type, gpen_path=gpen_path, **kwargs
            )
        elif swap_type == "faceswap_cv2":
            option = self.faceswap_cv2(
                use_gpu=use_gpu, gpen_type=gpen_type, gpen_path=gpen_path
            )

        return option

    def generate(
        self,
        option: ModelOption,
        source: str,
        target: Union[int, str],
        show_fps: bool = False,
        duration: int = None,
        **kwargs,
    ) -> Optional[List]:
        """Differentiates among different swap options.

        Available swap options:
            - `camera`
            - `image`
            - `video`

        Args:
            option (ModelOption): Swap engine class.
            source (str): File path of source image.
            target (Union[int, str]): Either `int` which indicates camera descriptor or target image file.
            show_fps (bool, optional): Displays FPS during camera pipeline. Defaults to False.
            duration (int, optional): Used to trim source video in seconds. Defaults to None.

        Returns:
            Optional[List]: None when using camera, otherwise metadata of successful and rejected face-swaps.
        """
        if self.use_cam:
            option.generate_from_camera(
                source, int(target), show_fps=show_fps, **kwargs
            )
            return None
        if isinstance(target, str):
            if self.use_video:
                option.generate_from_video(
                    source, target, self.save_folder, duration, **kwargs
                )
                return None
            elif self.use_image:
                [swappedDict, rejectedDict] = option.generate_from_image(
                    source, target, self.save_folder, **kwargs
                )
                return [swappedDict, rejectedDict]
            else:
                return None
        else:
            return None

    def simswap(
        self,
        use_gpu: bool,
        gpen_type: str,
        gpen_path: str,
        crop_size: int = 224,
        use_mask: bool = True,
    ) -> SimswapOption:
        """Build Simswap Option.

        Args:
            use_gpu (bool): If True, use GPU.
            gpen_type (str): GPEN type.
            gpen_path (str): path to GPEN model checkpoint.
            crop_size (int, optional): crop size. Defaults to 224.
            use_mask (bool, optional): If True, use mask. Defaults to True.

        Returns:
            SimswapOption: Simswap Option.
        """
        return SimswapOption(
            use_gpu=use_gpu,
            gpen_type=gpen_type,
            gpen_path=gpen_path,
            crop_size=crop_size,
            use_mask=use_mask,
        )

    def faceswap_cv2(
        self, use_gpu: bool, gpen_type: str, gpen_path: str, crop_size: int = 256
    ) -> FaceswapCVOption:
        """Build FaceswapCV Option.

        Args:
            use_gpu (bool): If True, use GPU.
            gpen_type (str): GPEN type.
            gpen_path (str): path to GPEN model checkpoint.
            crop_size (int, optional): crop size. Defaults to 256.

        Returns:
            FaceswapCVOption: FaceswapCV Option.
        """
        return FaceswapCVOption(
            use_gpu=use_gpu,
            gpen_type=gpen_type,
            gpen_path=gpen_path,
            crop_size=crop_size,
        )

    def fomm(
        self,
        use_gpu: bool,
        gpen_type: str,
        gpen_path: str,
        crop_size: int = 256,
        **kwargs,
    ) -> FOMMOption:
        """Build FOMM Option.

        Args:
            use_gpu (bool): If True, use GPU.
            gpen_type (str): GPEN type.
            gpen_path (str): path to GPEN model checkpoint.
            crop_size (int, optional): crop size. Defaults to 256.

        Returns:
            FOMMOption: FOMM Option.
        """
        return FOMMOption(
            use_gpu=use_gpu,
            gpen_type=gpen_type,
            gpen_path=gpen_path,
            crop_size=crop_size,
            offline=self.use_video,
        )


#
# python driver for Fischertechnik RoboLT
#

import os
import usb.core
import usb.backend.libusb1
import logging

logger = logging.getLogger('robolt')

ID_VENDOR          = 0x146a
ID_PRODUCT         = 0x000a

# limit the visibility to simplify the usage
__all__ = ["scan_for_devices", "RoboLT"]

def scan_for_devices():
    """ Find all available devices """
    devices = []
    try:
        backend = usb.backend.libusb1.get_backend(find_library=lambda x: "/usr/lib/libusb-1.0.so")
        for dev in usb.core.find(find_all=True, idVendor=ID_VENDOR, idProduct=ID_PRODUCT, backend=backend):
            devices.append(dev)
    except usb.core.USBError as e:
        logger.error("Could not find a connected RoboLT device: %s" % str(e))
    return devices

class RoboLT(object):
    # keep state of motor outputs
    enable = [ False, False, False, False ]
    pwm = [ 0,0,0,0 ]

    # constants for motor direction states
    Off = 0
    Left = 1
    Right = 2
    Brake = 3

    """
        Each instance of this class represents a physical RoboLT device.

        Usage :

        >>> from robolt import RoboLT
        >>> lt = RoboLT()
    """

    def __init__(self, device=None):
        """
        If a device is not given, it will attach this instance to the first one found.
        Otherwise you can pass a specific one from the list returned by scan_for_devices.
        """
        self.number = 0
        self.dev = device
        if self.dev is None:
            devices = scan_for_devices()
            if not(devices):
                raise OSError("Could not find a connected RoboLT device")
            self.dev = devices[0]
        self.init_device()

    def init_device(self):
        """
        Reinit device associated with the RoboLT instance
        """
        try:
            self.dev.set_configuration()
            # RoboLT uses two interrupt endpoints
            self.endpoint_in = self.dev[0][(0, 0)][0]
            self.endpoint_out = self.dev[0][(0, 0)][1]
            self.update_out()
        except usb.core.USBError as e:
            logger.error("Could not init device: %s" % str(e))

    def getRawData(self):
        """Read 6 bytes from the RoboLT's endpoint"""
        try:
            return self.endpoint_in.read(6)
        except usb.core.USBError as e:
            logger.exception("Could not read from RoboLT device")
            return None

    def getFw(self):
        """
        Return the RoboLT firmware version
        """
        try:
            ret = self.dev.ctrl_transfer(0xc0, 0xf0, 0x0001, 0, 5)
            return str(ret[1]) + "." + str(ret[2]) + "." + str(ret[3]) + "." + str(ret[4])
        except usb.core.USBError as e:
            logger.exception("Could not send control transfer")
            return None

    def getSerial(self):
        """
        Return the RoboLT serial number
        """
        try:
            ret = self.dev.ctrl_transfer(0xc0, 0xf0, 2, 0, 14)
            return ret[1] + ret[2]*100 + ret[3]*10000 + ret[4]*1000000
        except usb.core.USBError as e:
            logger.exception("Could not send control transfer")
            return None

    def setM(self, id, dir=Off, speed=0):
        """
        sets motor M1 or M2 direction and speed
        """
        if id < 1 or id > 2:
            raise ValueError('Motor id out of range')
        if dir < self.Off or dir > self.Brake:
            raise ValueError('Illegal motor direction value')
        if speed < 0 or speed > 100:
            raise ValueError('Motor speed out of range')

        # map motor values onto the shadow registers
        self.enable[2*id-2] = bool(dir & 1)
        self.enable[2*id-1] = bool(dir & 2)
        self.pwm[2*id-2] = speed
        self.pwm[2*id-1] = speed

        # and force transmission
        self.update_out()

    def setO(self, id, state, pwm=0):
        """
        sets output O1 - O4 state and pwm value
        """
        if id < 1 or id > 4:
            raise ValueError('Output id out of range')
        if state != True and state != False:
            raise ValueError('Illegal output state')
        if pwm < 0 or pwm > 100:
            raise ValueError('Output pwm out of range')

        # map output values onto the shadow registers
        self.enable[id-1] = state
        self.pwm[id-1] = pwm

        # and force transmission
        self.update_out()

    def update_out(self):
        # assemble command sequence from pwm/enable state
        data = [ 0xf2,0,0,0,0,0 ]
        for i in range(4):
            if self.enable[i]: data[1] |= (1<<i)
        data[2] |= int(self.pwm[0]*8/101)
        data[2] |= int(self.pwm[1]*8/101) << 3
        data[2] |= (int(self.pwm[2]*8/101) << 6) & 0xff
        data[3] |= (int(self.pwm[2]*8/101) >> 2)
        data[3] |= (int(self.pwm[3]*8/101) << 1)

        self.endpoint_out.write(data)

    def I(self):
        """
        Returns digital state of inputs I1, I2 and I3
        """
        data = self.getRawData()
        return (bool(data[0] & 1), bool(data[0] & 2), bool(data[0] & 4))

    def A(self):
        """
        Returns analog state of inputs I1 and I3
        """
        data = self.getRawData()
        return ((data[1] + 256 * (data[4]&3)),
                0.03*(data[2] + 256 * ((data[4]>>2)&3)))

    def getBattery(self):
        """
        Returns current supply voltage
        """
        data = self.getRawData()
        return 0.03*(data[3] + 256 * ((data[4]>>4)&3))


#!/usr/bin/python3

'''Test reading and writing binary.'''

import logging

logging.basicConfig(level=logging.DEBUG)

def write_binary_file(filename: str, data):
    logging.debug('data.__class__=%s', data.__class__)
    fd = open(filename, 'wb')
    fd.write(data)
    fd.close()

def write_binary_file_with_bytearray(filename: str, data):
    logging.debug('bytearray_data=%s', data)
    badata = bytearray(data)
    logging.debug('badata=%s', badata)
    fd = open(filename, 'wb')
    fd.write(badata)
    fd.close()

def _main():
    latin1_fd = open('binary-latin1.txt', 'rb')
    latin1 = latin1_fd.read()
    latin1_fd.close()
    logging.debug('latin1=%s', latin1)
    write_binary_file('binary_out.txt', latin1)
    write_binary_file_with_bytearray('babinary_out.txt', latin1)

if __name__ == '__main__':
    _main()


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

app_name = 'pupil'

urlpatterns = [

    url(r'^$',views.IndexView.as_view(),name='index'),
    url(r'^new/$',views.NewVisual.as_view(), name = 'newvisual'),
    url(r'^history/',views.history.as_view(),name='history'),
    url(r'^visual/(?P<pk>[0-9]+)/delete/$',views.DeleteVisual.as_view(), name = 'deletevisual'),
    url(r'^visual/(?P<pk>[0-9]+)/convert/$',views.ConvertVisual.as_view(),name = 'convertvisual'),
    url(r'^download/',views.download.as_view(),name='download'),

]


#!/usr/bin/env python3.6

import os
import sys
import time
from abc import ABCMeta, abstractproperty
from io import BytesIO
from mmap import mmap, PROT_READ
from pathlib import Path

from PIL import Image

from packed_struct import F, Struct

MAX_MMAP = 2 * 1024 * 1024
OUT_DIR = Path("out").resolve()

class Command(Struct, metaclass=ABCMeta):
    @abstractproperty
    def CODE(self):
        """The byte string code for this command
        """
        raise NotImplementedError

    @abstractproperty
    def code(self):
        """The (F.bytes) field containing the code
        """
        raise NotImplementedError

class MoveVertPos(Command):
    CODE = b"\x1b*rY"

    code = F.bytes(len(CODE), allowed={CODE})
    n_dots = F.ubyte()
    null = F.ubyte(allowed={0})

class StartRow(Command):
    CODE = b"b"

    code = F.bytes(len(CODE), allowed={CODE})
    n_bytes = F.uint16le()

def find_commands(buff, command_cls, start_offset=0, end_offset=-1):
    if end_offset == -1 or end_offset is None:
        end_offset = len(buff)

    if isinstance(start_offset, Command):
        start_offset = start_offset._offset + start_offset._size
    if isinstance(end_offset, Command):
        end_offset = end_offset._offset

    offset = buff.find(command_cls.CODE, start_offset)
    while start_offset <= offset < end_offset:
        yield command_cls(buff, offset)
        offset = buff.find(command_cls.CODE, offset + 1)

def with_next(iterable):
    it = iter(iterable)
    prev = next(it)
    try:
        while True:
            curr = next(it)
            yield (prev, curr)
            prev = curr

    except StopIteration:
        yield (prev, None)

def read_images(filename):
    filesize = os.stat(filename).st_size

    with open(filename) as _, mmap(_.fileno(), min(MAX_MMAP, filesize), prot=PROT_READ) as doc:
        for current, nxt in with_next(find_commands(doc, MoveVertPos)):
            image = []
            for start_row in find_commands(doc, StartRow, current, nxt):
                row_offset = start_row._offset + start_row._size
                image.append(doc[row_offset:row_offset + start_row.n_bytes])

            yield image

def dumpimage(data, outpath):
     size = (max(map(len, data)), len(data))
     image = bytearray(size[0] * size[1])
     for ii in range(size[1]):
         image[ii*size[0]:ii*size[0]+min(size[0], len(data[ii]))] = data[ii]
     Image.frombytes("1", (size[0]*8, size[1]), bytes(image), "raw", "1;I").save(outpath)

def main(filename):
    now = str(int(time.time()))
    outdir = OUT_DIR / now
    outdir.mkdir(exist_ok=True, parents=True)
    print("Dumping to '{}'".format(outdir))

    for idx, image in enumerate(read_images(filename), 1):
        if image and image[0]:
            dumpimage(image, outdir / "{:d}.bmp".format(idx))

if __name__ == "__main__":
    main(sys.argv[1])


import json
from typing import Dict, List, Set

import requests

WIKIPEDIA_REQUEST_URL = "http://en.wikipedia.org/w/api.php?action=query"

def _get_wikipedia_title(request_token: List[str]) -> Dict[str, str]:
    '''
    Returns Dict with <pageid>: title
    '''
    out_format = "json"
    srprop = "sectiontitle"
    srlimit = 1  # just the most relevant only
    srsearch = " ".join(request_token)

    request_url = f"{WIKIPEDIA_REQUEST_URL}&list=search&format={out_format}&srprop={srprop}&srlimit={srlimit}&srsearch={srsearch}"

    results: Dict[int, str] = {}
    try:
        for r in requests.get(request_url).json()["query"]["search"]:
            results[str(r["pageid"])] = r["title"]
    except Exception:
        pass

    return results

def _get_wikipedia_categories(pageids: Set[int]) -> List[str]:
    out_format = "json"
    prop = "categories"
    pageids = "|".join(list(pageids))

    request_url = f"{WIKIPEDIA_REQUEST_URL}&format={out_format}&prop={prop}&pageids={pageids}"

    categories: List[str] = []
    try:
        for page_id, r in requests.get(request_url).json()["query"]["pages"].items():
            for category in r["categories"]:
                categories.append(category["title"].replace("Category:", ""))
    except Exception:
        pass

    return categories

def request_wikipedia(top_words: List[str]) -> List[str]:
    top_words = [x.replace("_", " ") for x in top_words]  # get rid of _ in phrase

    page_ids_titles = _get_wikipedia_title(top_words)
    categories = _get_wikipedia_categories(page_ids_titles.keys())

    return categories

if __name__ == "__main__":
    token = [
        "artes",
        "august",
        "appointment",
        "beaufort",
        "be"
    ]
    page_ids_titles = _get_wikipedia_title(token)  # (["henry", "viii"])
    categories = _get_wikipedia_categories(page_ids_titles.keys())

    print(page_ids_titles)
    print()
    print(categories)


from django.shortcuts import render, redirect #Son los accesos directos y la redirección
from django.views import generic #importa listas genericas

from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse_lazy #Crea la URL cuando es necesario
from django.contrib.auth.decorators import login_required, permission_required #Recurre a la seguridadpara loguearse y los permisos que recibe elusuario
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin #Marca los errores de logueo
from django.http import HttpResponse #Para visualizar la pagina que es requerida en el momento
from datetime import datetime  #para la importación y manipulación de fecha y hora
from django.contrib import messages

from django.contrib.auth import authenticate #Importa para realizar la autenticación del usuario

from bases.views import SinPrivilegios #Los privilegios que recibe el usuario

from .models import Solicitud #de modelos se importa solicitud
from .forms import SolicitudForm #de formas se importa las formas de la solicitud

from reportlab.pdfgen import canvas #de reporte importa los metodos figuras, imagen, texto.
from reportlab.platypus import SimpleDocTemplate, Paragraph, TableStyle, Image #de reporte importa para la creación del documento, tabla de estilos y imagen
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle #de reporte se importa los estilos
from reportlab.lib import colors #de reportes se importan los colores
from reportlab.lib.pagesizes import letter, A4, inch, elevenSeventeen #de reporte se importa el tamaño de letra, su tipo
from reportlab.platypus import Table
from io import BytesIO #Los datos del archivo en bytes
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY #de reporte se importa la posición del cuerpo dentro del documento
from becas import settings

class SolicitudView(generic.ListView): #visualizaicón de la solicitud
    model = Solicitud #El modelo
    template_name = "solicitudes/becas_list.html"  #Lo que se va a mostrar a la persona de las listas de solicitudes
    context_object_name = "bec"
    permission_required = "solicitud.view_solicitud" #Los permisos de la solicitud

def entregados(request): #La función nos muestra las entregadas
    en = Solicitud.objects.filter(entrega='ENTREGADO') #Nos mostrara si nuestra fue entregada
    return render(request, 'solicitudes/entregasi_list.html', {'en': en})
    #Retornara a las listas y la entrega aparecera en la lista

def no_entregados(request): #La función nos muestra las no entregadas
    en = Solicitud.objects.filter(entrega='NO ENTREGADO')
    return render(request, 'solicitudes/entregano_list.html', {'en': en})
    #Retornara a las listas y en esta caso no aparecera en listas

class SolicitudNew(SinPrivilegios, SuccessMessageMixin, generic.CreateView): #Dentro de esta clase es para realizar nuevas solicitudes
    model = Solicitud #Nuestro modelo solicitud
    template_name = "solicitudes/becas_form.html" #lo que visualizara el usuario en la web solicitud y las formas de becas
    form_class = SolicitudForm #Nuestra clase formas, solicitud de formas
    success_url = reverse_lazy("solicitudes:beca_list")
    permission_required = "solicitud.add_solicitud" #Los permisos para agregar esa solicitud
    context_object_name = 'obj'
    success_message = "Registro Agregado Satisfactoriamente" #La impresión de pantalla

    def form_valid(self, form): #La función valida la forma/solicitud
        form.instance.uc = self.request.user #Dependiendo la instancia te pedira ingresar un dato faltante.
        return super().form_valid(form) #retorna a la validación de la forma

class SolicitudEdit(generic.UpdateView): #Nuestra clase llamada solicitudeditar
    model = Solicitud
    template_name = "solicitudes/becas_form.html" #Visualizar solicitudes y becas
    context_object_name = 'obj'
    form_class = SolicitudForm #Manda llamar a las formas requeridas en la solicitud
    success_url = reverse_lazy("solicitudes:beca_list")
    success_message = "Solicitud Editada" #Muestra que fueron editados los datos
    permission_required = "solicitud.change_solicitud" #Ve si existen los permisos para esas modificaciónes

def delete(request, solicitud_id): #función eliminar
    # Recuperamos la instancia de la persona y la borramos
    instancia = Solicitud.objects.get(id=solicitud_id)
    instancia.delete()

    # Después redireccionamos de nuevo a la lista
    return redirect('/solicitudes/solicitud')

def reporte(request, pk):#Función para generar reportes

    response = HttpResponse(content_type='application/pdf') #Para que la pagina permita visualizar un pdf
    report = generarPDF(pk) #Reporte que se generara en pdf
    buff = BytesIO() #El tipo de dato de información donde se almacena
    doc = SimpleDocTemplate(buff,
                            pagesize=A4,
                            rightMargin=40,
                            leftMargin=40,
                            topMargin=30,
                            bottomMargin=18,
                            )  #Es el tamaño de su estructura en que se generara el pdf
    doc.build(report) #Construye el reporte
    response.write(buff.getvalue())
    buff.close() #Para cerra el pdf
    return response #nos regresa a la opción response.

def texto(texto, tamanio): #Función de texto para reporte
    styles = getSampleStyleSheet() #Estilo de letra y formato
    style = 'Heading{}'.format(tamanio) #tamaño del archivo y texto
    return Paragraph(texto, styles[style]) #retorna a el texto y estilos de texto

def generarPDF(pk): #Función generar pdf
    title1 = ParagraphStyle('parrafos',
                            alignment=TA_CENTER,
                            fontSize=20,
                            fontName="Times-Bold",
                            spaceAfter=30,
                            spaceBefore=0
                            ) #Mostrara el titulo dentro del pdf, su tamaño, tipo de letra, donde se situa

    body = ParagraphStyle('parrafos',
                          alignment=TA_JUSTIFY,
                          fontSize=12,
                          fontName="Courier",
                          leftIndent=36,
                          spaceAfter=20,
                          spaceBefore=20
                          ) #El cuerpo dentro del pdf, tamaño, tipo de letra y donde se situa
    # header_bold = ParagraphStyle('parrafos',
    # alignment=TA_CENTER,
    # fontSize=15,
    # fontName="Times-Bold",
    # spaceAfter=5,
    # spaceBefore=15
    # )

    info = [] #La iformación del usuario
    I = Image(settings.PATH_MEDIA+'ljr.jpg')
    I.drawHeight = 1.0*inch
    I.drawWidth = 1.0*inch
    I.hAlign = 'LEFT'

    info.append(I)

    e = Solicitud.objects.get(pk=pk)

    title_ = Paragraph("SOLICITUD DE BECA 2020 ", title1) #Todo esto va dentro del cuerpo del pdf o visualización del admin
    info.append(title_)
    info.append(Paragraph("NOMBRE: {}".format(e.nombre), body))
    info.append(Paragraph("APELLIDO PATERNO: {}".format(e.ap), body))
    info.append(Paragraph("APELLIDO MATERNO: {}".format(e.am), body))
    info.append(Paragraph("DOMICILIO: {}".format(e.domicilio), body))
    info.append(Paragraph("COLONIA: {}".format(e.colonia), body))
    info.append(Paragraph("TELEFONO: {}".format(e.telefono), body))
    info.append(Paragraph("FECHA DE NACIMIENTO: {}".format(e.fn), body))
    info.append(Paragraph("GRADO ACADEMICO: {}".format(e.ga), body))
    info.append(Paragraph("MATRICULA: {}".format(e.matricula), body))
    info.append(Paragraph("FORMACIÓN ARTISTICA: {}".format(
        e.formacion_artistica), body))
    info.append(Paragraph("MODALIDAD: {}".format(e.modalidad), body))
    info.append(Paragraph("GENERO: {}".format(e.genero), body))
    info.append(Paragraph("CORREO: {}".format(e.correo), body))
    return info #Retorna a info


"""View module for handling requests about categories"""
from django.http import HttpResponseServerError
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework import serializers, status
from solehubapi.models import Order, User

class OrderView(ViewSet):
    """Level up Order view"""

    def create(self, request):
        """Handle GET requests for single order

        Returns:
            Response -- JSON serialized order
        """

        custId = User.objects.get(pk=request.data["customer_id"])

        order = Order.objects.create(
            status=request.data["status"],
            customer_id=custId,
            payment_type=request.data["payment_type"]

        )
        serializer = OrderSerializer(order)
        return Response(serializer.data)

    def retrieve(self, request, pk):
      try:
        order = Order.objects.get(pk=pk)
        serializer = OrderSerializer(order)
        return Response(serializer.data)
      except order.DoesNotExist as ex:
            return Response({'message': ex.args[0]}, status=status.HTTP_404_NOT_FOUND)

    def update(self, request, pk):
        """Handle PUT requests for a order

        Returns:
            Response -- Empty body with 204 status code
        """

        order = Order.objects.get(pk=pk)
        order.status = request.data["status"]
        order.customer_id = User.objects.get(pk=request.data["customer_id"])
        order.payment_type = request.data["payment_type"]
        order.save()

        return Response(None, status=status.HTTP_204_NO_CONTENT)

    def list(self, request):
        """Handle GET requests to get all category

        Returns:
            Response -- JSON serialized list of category
        """
        order = Order.objects.all()

        customer_id = request.query_params.get('customer_id', None)
        status = request.query_params.get('status')

        if customer_id is not None:
            try:
                # customer_id query param is converted to an integer
                customer_id = int(customer_id)
            except ValueError:
                return Response({'message': 'Invalid customer_id'}, status=status.HTTP_400_BAD_REQUEST)

            #filter orders by customer_id
            order = order.filter(customer_id=customer_id)

        if status is not None:
            if status.lower() in ['true', 'false']:
                status = status.lower() == 'true'
                order = order.filter(status=status)
            else:
                return Response({'message': 'Invalid status value'}, status=status.HTTP_400_BAD_REQUEST)

        serializer = OrderSerializer(order, many=True)
        return Response(serializer.data)

    def destroy(self, request, pk):
        order = Order.objects.get(pk=pk)
        order.delete()
        return Response(None, status=status.HTTP_204_NO_CONTENT)

class OrderSerializer(serializers.ModelSerializer):
    """JSON serializer for categories
    """
    class Meta:
        model = Order
        fields = ('id', 'customer_id', 'status', 'payment_type')
        depth = 1


import tkinter as tk
from PIL import Image, ImageTk

class Img_Window ():#tk.Toplevel):
    def __init__(self, master, data):
        #tk.Toplevel.__init__(self, master)
        self.img_index = 0
        self.img_list = data
        self.create_window()
        self.window.mainloop()

    def create_window(self):
        self.window = tk.Toplevel()
        self.window.title('Img Player')
        self.window.geometry('1280x720')
        self.top_frame = tk.Frame(self.window)
        self.top_frame.pack()
        self.bottom_frame = tk.Frame(self.window)
        self.bottom_frame.pack(side=tk.BOTTOM)
        img = Image.open(self.img_list[self.img_index]['src'])
        [imageSizeWidth, imageSizeHeight] = img.size
        img = img.resize((int(imageSizeWidth * 0.8), int(imageSizeHeight * 0.8)), Image.ANTIALIAS)
        self.photo = ImageTk.PhotoImage(img)
        self.label = tk.Label(self.top_frame, image=self.photo)
        self.label.pack()
        self.left_button = tk.Button(self.bottom_frame, text='pre', fg='red', command=self.img_pre)
        self.left_button.pack(side=tk.LEFT)

        self.right_button = tk.Button(self.bottom_frame, text='next', fg='blue', command=self.img_next)
        self.right_button.pack(side=tk.LEFT)

#    def set_layout(self):

    def img_next(self):
        self.img_index = (self.img_index + 1) % len(self.img_list)
        self.show_img()
        print(self.img_index)

    def img_pre(self):
        self.img_index = (self.img_index - 1) % len(self.img_list)
        self.show_img()
        print(self.img_index)

    def show_img(self):
        img = Image.open(self.img_list[self.img_index]['src'])
        [imageSizeWidth, imageSizeHeight] = img.size
        img = img.resize((int(imageSizeWidth * 0.75), int(imageSizeHeight * 0.75)), Image.ANTIALIAS)
        self.photo = ImageTk.PhotoImage(img)
        self.label.configure(image = self.photo)

#    def refresh_img(self):
#        img = Image.open(self.path_list[self.img_index])
#        label1.configure(image = displayImage)
#        self.photo = ImageTk.PhotoImage(img)


from util import database
from functools import wraps
import flask

CS542_TOKEN_COOKIE = 'CS542_TOKEN_COOKIE'
CS542_TOKEN_COOKIE_EXPIRY = 86400

def current_user():
    if flask.has_request_context() and CS542_TOKEN_COOKIE in flask.request.cookies:
        db = database.get_db()
        if 'current_user' not in flask.g:
            with db.cursor() as cursor:
                get_user_from_token_query = "SELECT U.*, (NOW()- L.start_time) as session_duration_seconds," + \
                    "L.token as session_token FROM UserDataWithRole U, LoginSession L WHERE L.token = %s AND U.userid = L.userid;"
                cursor.execute(get_user_from_token_query, flask.request.cookies[CS542_TOKEN_COOKIE])
                result = cursor.fetchone()
                flask.g.current_user = result
                return result
        else:
            return flask.g.current_user
    else:
        return None

def current_user_roles():
    if flask.has_request_context() and CS542_TOKEN_COOKIE in flask.request.cookies:
        if (current_user() is None or current_user()["roles"] == ""):
            return []
        return current_user()["roles"].split(", ")
    else:
        return []

def invalidate_token(token):
    db = database.get_db()
    with db.cursor() as cursor:
        delete_session_query = "DELETE FROM LoginSession where LoginSession.token=%s"
        cursor.execute(delete_session_query, token)
        if (cursor.rowcount == 1):
            db.commit()
        return (cursor.rowcount == 1)

def require_login(api_method):
    @wraps(api_method)

    def check_login(*args, **kwargs):
        if (current_user() is None):
            return flask.redirect(flask.url_for('accounts.signin', redirect=flask.request.path))
        else:
            return api_method(*args, **kwargs)

    return check_login

def require_oneof_roles(*argv):
    def real_decorator(api_method):
        @wraps(api_method)
        def check_login(*args, **kwargs):
            if (current_user() is None):
                return flask.redirect(flask.url_for('accounts.signin', redirect=flask.request.path))
            else:
                for arg in argv:
                    if (arg in current_user_roles()):
                        return api_method(*args, **kwargs)
                return flask.abort(403)

        return check_login
    return real_decorator


import torch
import torch.nn.functional as F

from mmdet.core.evaluation.panoptic_utils import INSTANCE_OFFSET
from mmdet.core.mask import mask2bbox
from mmdet.models.builder import HEADS
from .base_panoptic_fusion_head import BasePanopticFusionHead

@HEADS.register_module()
class MaskFormerFusionHead(BasePanopticFusionHead):

    def __init__(self,
                 num_things_classes=8,
                 num_stuff_classes=11,
                 cityscapes_thing_list=[11, 12, 13, 14, 15, 16, 17, 18],
                 cityscapes_label_divisor=1000,
                 semantic_inference_type='semantic_inference',
                 test_cfg=None,
                 loss_panoptic=None,
                 init_cfg=None,
                 **kwargs):
        super().__init__(num_things_classes, num_stuff_classes, test_cfg,
                         loss_panoptic, init_cfg, **kwargs)
        self.cityscapes_thing_list = cityscapes_thing_list
        self.semantic_inference_type = semantic_inference_type
        self.cityscapes_label_divisor = cityscapes_label_divisor

    def forward_train(self, **kwargs):
        """MaskFormerFusionHead has no training loss."""
        return dict()

    def panoptic_postprocess(self, mask_cls, mask_pred):
        """Panoptic segmengation inference.

        Args:
            mask_cls (Tensor): Classfication outputs of shape
                (num_queries, cls_out_channels) for a image.
                Note `cls_out_channels` should includes
                background.
            mask_pred (Tensor): Mask outputs of shape
                (num_queries, h, w) for a image.

        Returns:
            Tensor: Panoptic segment result of shape \
                (h, w), each element in Tensor means: \
                ``segment_id = _cls + instance_id * INSTANCE_OFFSET``.
        """
        object_mask_thr = self.test_cfg.get('object_mask_thr', 0.8)
        iou_thr = self.test_cfg.get('iou_thr', 0.8)
        filter_low_score = self.test_cfg.get('filter_low_score', False)

        scores, labels = F.softmax(mask_cls, dim=-1).max(-1)
        mask_pred = mask_pred.sigmoid()

        keep = labels.ne(self.num_classes) & (scores > object_mask_thr) # exculding the background class prediction, if there are 19 classes then labels are from 0 to 18,
                                                                        # and the 19th label-id is for no-calss or background class,
                                                                        # MaskFormer mask classification layer is trained on total_num_classes + 1 classes
                                                                        # i.e. for cityscapes, it predicts [N x K] mask classification scores,
                                                                        # N:num_queries=100, K=self.num_classes+1=20
        cur_scores = scores[keep]
        cur_classes = labels[keep]
        cur_masks = mask_pred[keep]

        cur_prob_masks = cur_scores.view(-1, 1, 1) * cur_masks

        h, w = cur_masks.shape[-2:]
        panoptic_seg = torch.full((h, w), 255 * self.cityscapes_label_divisor, dtype=torch.int32, device=cur_masks.device)
        # panoptic_seg = torch.full((h, w), self.num_classes, dtype=torch.int32, device=cur_masks.device) # original
        # semantic_seg = torch.full((self.num_classes, h, w), self.num_classes, dtype=torch.int32, device=cur_masks.device) # added by Suman
        if cur_masks.shape[0] == 0:
            # We didn't detect any mask :(
            pass
        else:
            cur_mask_ids = cur_prob_masks.argmax(0)
            instance_id = 1
            for k in range(cur_classes.shape[0]):
                pred_class = int(cur_classes[k].item())

                # isthing = pred_class < self.num_things_classes # original
                if pred_class in self.cityscapes_thing_list: # this if-else block is added by Suman
                    isthing = True
                else:
                    isthing = False

                mask = cur_mask_ids == k
                mask_area = mask.sum().item()
                original_area = (cur_masks[k] >= 0.5).sum().item()

                if filter_low_score:
                    mask = mask & (cur_masks[k] >= 0.5)

                if mask_area > 0 and original_area > 0:
                    if mask_area / original_area < iou_thr:
                        continue

                    if not isthing:
                        # different stuff regions of same class will be
                        # merged here, and stuff share the instance_id 0.
                        panoptic_seg[mask] = pred_class * self.cityscapes_label_divisor
                    else:
                        panoptic_seg[mask] = (pred_class * self.cityscapes_label_divisor + instance_id)
                        # panoptic_seg[mask] = (pred_class + instance_id * INSTANCE_OFFSET) # original used for COCO
                        #  class_id * label_divisor + new_ins_id
                        instance_id += 1
                    # semantic_seg[pred_class, mask] = pred_class  # added by Suman

        return panoptic_seg

    def semantic_postprocess(self, mask_cls, mask_pred):
        """Semantic segmengation postprocess.

        Args:
            mask_cls (Tensor): Classfication outputs of shape
                (num_queries, cls_out_channels) for a image.
                Note `cls_out_channels` should includes
                background.
            mask_pred (Tensor): Mask outputs of shape
                (num_queries, h, w) for a image.

        Returns:
            Tensor: Semantic segment result of shape \
                (cls_out_channels, h, w).
        """
        # K: num classes, N: num queries, H: hieght, W: width
        num_query, num_classes = mask_cls.shape  # [N, K]
        num_classes = num_classes - 1
        mask_cls = mask_cls[:, :num_classes]
        _, imgH, imgW = mask_pred.shape  # [N, H, W]
        mask_cls = F.softmax(mask_cls, dim=-1)  # convert logits to softmax prob., softamx over K classes
        mask_cls = mask_cls.permute(1, 0)  # [N, K] -> [K, N] : permute matrix for matrix multiplication
        mask_pred = mask_pred.sigmoid()  # converts logits to score between 0 and 1
        mask_pred = mask_pred.view(num_query, -1)  # [N, H, W] -> [N, H*W] : change view for matrix multiplication
        semantic_seg = torch.matmul(mask_cls, mask_pred)  # [K, N] x [N, H*W] --> [K, H*W] # during this matrix multiplication, we multiply sigmoid and softmax prob scores, but
                                                          # but the output matrix might have values > 1
        semantic_seg = semantic_seg.view(num_classes, imgH, imgW)  # [K, H*W] --> [K, H, W]
        # semseg = semantic_seg.argmax(0) # final prediction
        # --- visualization ---
        # rows, cols = 2, 2
        # gridspec_kw = {'hspace': 0.1, 'wspace': 0, 'top': 0.95, 'bottom': 0, 'right': 1, 'left': 0}
        # fig, axs = plt.subplots(rows, cols, figsize=(3 * cols, 3 * rows), gridspec_kw=gridspec_kw)
        # semseg = semantic_seg.argmax(0)
        # subplotimg(axs[0][0], semseg, 'SemGT', cmap='cityscapes')
        # for ax in axs.flat:
        #     ax.axis('off')
        # out_dir = '/home/suman/Downloads'
        # outfile = os.path.join(out_dir, meta['ori_filename'])
        # plt.savefig(outfile)
        # plt.close()
        return semantic_seg

    def instance_postprocess(self, mask_cls, mask_pred, pseudo_threshold_instance=0):
        """Instance segmengation postprocess.

        Args:
            mask_cls (Tensor): Classfication outputs of shape
                (num_queries, cls_out_channels) for a image.
                Note `cls_out_channels` should includes
                background.
            mask_pred (Tensor): Mask outputs of shape
                (num_queries, h, w) for a image.

        Returns:
            tuple[Tensor]: Instance segmentation results.

            - labels_per_image (Tensor): Predicted labels,\
                shape (n, ).
            - bboxes (Tensor): Bboxes and scores with shape (n, 5) of \
                positive region in binary mask, the last column is scores.
            - mask_pred_binary (Tensor): Instance masks of \
                shape (n, h, w).
        """
        max_per_image = self.test_cfg.get('max_per_image', 100) # max_per_image = 100
        num_queries = mask_cls.shape[0]                         # mask_cls.shape = torch.Size([100, 20])
        # shape (num_queries, num_class)
        scores = F.softmax(mask_cls, dim=-1)[:, :-1]            # scores.shape = torch.Size([100, 19]); :-1 to exclude the "no-object" class
        # shape (num_queries * num_class, )
        labels = torch.arange(self.num_classes, device=mask_cls.device).unsqueeze(0).repeat(num_queries, 1).flatten(0, 1) # labels.shape = 1900, labels has values [0,1,2,..,18, 0,1,2,...,18, 0,1,2,..18] i.e. repating for 100 queries
        scores_per_image, top_indices = scores.flatten(0, 1).topk(max_per_image, sorted=False) # scores.flatten(0, 1).shape = 1900; scores_per_image.shape = 100, top_indices.shape=100, selecting the 100 queries (there scores and indices) with top scores
        labels_per_image = labels[top_indices] # labels_per_image.shape=100, labels for topk 100 queries

        query_indices = top_indices // self.num_classes # top_indices value ranges between 0 and 1900, map them to query indices, i.t., 0 to 99, for selecting the best masks, top_indices.shape = 100, self.num_classes=19, query_indices.shape=100,
        mask_pred = mask_pred[query_indices] # query_indices value ranges between 0 and 99, query_indices.shape=100, mask_pred.shape=torch.Size([100, 512, 1024])

        # extract things
        is_thing = []
        for c in labels_per_image:
            if c.item() in self.cityscapes_thing_list:
                is_thing.append(True)
            else:
                is_thing.append(False)
        is_thing = torch.BoolTensor(is_thing) # is_thing.shape=100, bool tensor to pick the scores, labels and masks

        scores_per_image = scores_per_image[is_thing] # before: scores_per_image.shape=100, after: scores_per_image.shape=57
        labels_per_image = labels_per_image[is_thing] # before: labels_per_image.shape=100, after: labels_per_image.shape=57
        mask_pred = mask_pred[is_thing]               # before: mask_pred.shape=torch.Size([100, 512, 1024]), after: mask_pred.shape=torch.Size([57, 512, 1024])

        mask_pred_binary = (mask_pred > 0).float()  # mask_pred.min()=-477.0528, mask_pred.max()=22.03, raw logits, only cosider those pixels as valid predictions which have predicted logits > 0, convert the bool to float, values 0 or 1
        mask_scores_per_image = (mask_pred.sigmoid() *  mask_pred_binary).flatten(1).sum(1) / ( mask_pred_binary.flatten(1).sum(1) + 1e-6) # mask_pred.sigmoid().shape=torch.Size([57, 512, 1024]), (mask_pred.sigmoid() *  mask_pred_binary).flatten(1).shape=torch.Size([57, 524288]), (mask_pred.sigmoid() *  mask_pred_binary).flatten(1).sum(1).shape=57, mask_pred_binary.flatten(1).shape=torch.Size([57, 524288]), mask_pred_binary.flatten(1).sum(1).shape=57, mask_scores_per_image.shape=57,tensor([0.9687, 0.8592, 0.8592, 0.8305, 0.0000, 0..., 0.8631], device='cuda:0')
        det_scores = scores_per_image * mask_scores_per_image # scores_per_image.shape=mask_scores_per_image.shape=det_scores.shape=57, scores_per_image=mask_scores_per_image=det_scores=[value between 0 and 1,]
        mask_pred_binary = mask_pred_binary.bool() # mask_pred_binary.shape=torch.Size([57, 512, 1024]), convert float to bool

        if pseudo_threshold_instance > 0:
            # labels_per_image = labels_per_image[det_scores > pseudo_threshold_instance]
            # bboxes = bboxes[det_scores > pseudo_threshold_instance]
            # mask_pred_binary = mask_pred_binary[det_scores > pseudo_threshold_instance]
            mask_pseudo_weight = torch.sum(det_scores > pseudo_threshold_instance).item() / det_scores.shape[0]
            return labels_per_image, mask_pred_binary, mask_pseudo_weight
        else:
            bboxes = mask2bbox(mask_pred_binary)
            bboxes = torch.cat([bboxes, det_scores[:, None]], dim=-1)  # bboxes.shape=torch.Size([57, 5])
            return labels_per_image, bboxes, mask_pred_binary

    def simple_test(self,
                    mask_cls_results,
                    mask_pred_results,
                    img_metas,
                    rescale=False,
                    pseudo_threshold_instance=0,
                    **kwargs):

        panoptic_on = self.test_cfg.get('panoptic_on', True)
        semantic_on = self.test_cfg.get('semantic_on', False)
        instance_on = self.test_cfg.get('instance_on', False)
        # assert not semantic_on, 'segmantic segmentation  results are not supported yet.'

        results = []
        for mask_cls_result, mask_pred_result, meta in zip(mask_cls_results, mask_pred_results, img_metas):
            # remove padding
            img_height, img_width = meta['img_shape'][:2]
            mask_pred_result = mask_pred_result[:, :img_height, :img_width]

            if rescale:
                # return result in original resolution
                ori_height, ori_width = meta['ori_shape'][:2]
                mask_pred_result = F.interpolate(mask_pred_result[:, None], size=(ori_height, ori_width),  mode='bilinear', align_corners=False)[:, 0]
            else:
                # this else part is for debug mode and not for actual evaluation, for actual evaluation, we do on original shape
                mask_pred_result = F.interpolate(mask_pred_result[:, None], size=(512, 1024), mode='bilinear', align_corners=False)[:, 0]

            result = dict()
            if panoptic_on and pseudo_threshold_instance == 0: # because for pseduo labels we dont need panoptic predictions
                pan_results = self.panoptic_postprocess(mask_cls_result, mask_pred_result)
                result['pan_results'] = pan_results

            if instance_on:
                ins_results = self.instance_postprocess(mask_cls_result, mask_pred_result, pseudo_threshold_instance=pseudo_threshold_instance)
                result['ins_results'] = ins_results

            if semantic_on:
                # MaskFormer paper propose two ways of semantic inference: (1) general inference and (2) smenaitc inference under Sec. 3.4 Mask-classification inference
                # As per the authors of MaskFormer: for semantic segmentation evaluation, semantic inference gives better results
                assert self.semantic_inference_type == 'semantic_inference', 'semantic_inference_type must be set to "semantic_inference", and not to ' \
                                                        '"general_inference". The general inference is not implemented yet!'
                if self.semantic_inference_type == 'semantic_inference':
                    sem_results = self.semantic_postprocess(mask_cls_result, mask_pred_result)
                    result['sem_results'] = sem_results

            results.append(result)

        return results


import argparse, os
from PIL import Image, ImageDraw, ImageFont

parser = argparse.ArgumentParser(description='image watermarking')
parser.add_argument("path", help="path of input file or directory",
                    type=str)
parser.add_argument("-opacity", "--opacity", type=int, default = 40, help="opacity value of text")
parser.add_argument("-text", "--text", type=str, default = "@ARPAN", help="watermark text")
parser.add_argument("-size", "--size", type=int, default = 30, help="watermark text size")
parser.add_argument("-hspace", "--horizontalSpace", type=int, default = 70, help="Horizontal space")
parser.add_argument("-vspace", "--verticalSpace", type=int, default = 90, help="Vertical space")
parser.add_argument("-fpath", "--fontPath", type=str, default = r"C:\Windows\Fonts\Proxima Nova Bold.otf", help="font file path")

args = parser.parse_args()

# Use // for file names if you are facing issues
INPUT_IMAGE_FILE = args.path
OUTPUT_IMAGE_FILE = args.path+".watermarked.png"
FONT_LOCATION  = args.fontPath
FONT_SIZE = args.size
H_SPACING = args.horizontalSpace
V_SPACING = args.verticalSpace
FONT_OPACITY = args.opacity
WATERMARK_TEXT = args.text

# the following function reads an image stream from PIL and outputs the same image stream with watermarks

def put_watermark(im, WATERMARK_TEXT, FONT_LOCATION, FONT_SIZE, H_SPACING, V_SPACING, FONT_OPACITY):

    font = ImageFont.truetype(FONT_LOCATION, FONT_SIZE)
    watermark_text = WATERMARK_TEXT
    im_width, im_height = im.size

    drawing = ImageDraw.Draw(im)
    text_width, text_height = drawing.textsize(watermark_text, font)

    im_text = Image.new('RGBA', (text_width, (text_height)), (255, 255, 255, 0))
    drawing = ImageDraw.Draw(im_text)
    drawing.text((0,0), watermark_text, fill=(203,205,213, FONT_OPACITY), font=font)

    current_width = im_width
    current_height = im_height

    up_down = +1

    while current_width > text_width + H_SPACING:
        new_position = (current_width - text_width) - H_SPACING , current_height + (up_down * (V_SPACING//2))
        im.paste(im_text, new_position, im_text)
        current_width, current_height = new_position

        repeat_current_width, repeat_current_height = new_position

        while repeat_current_height > text_height + V_SPACING:
            repeat_new_position = repeat_current_width , (repeat_current_height - text_height - V_SPACING)
            im.paste(im_text, repeat_new_position, im_text)
            repeat_current_width, repeat_current_height = repeat_new_position

        up_down *= -1

    return im

# Usage

if os.path.isdir(INPUT_IMAGE_FILE):

    onlyfiles = [os.path.join(INPUT_IMAGE_FILE, f) for f in os.listdir(INPUT_IMAGE_FILE) if os.path.isfile(os.path.join(INPUT_IMAGE_FILE, f))]
    only_image_files = filter(lambda filename : filename.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp')), onlyfiles)
    for file_path in only_image_files:
        try:
            im = Image.open(file_path) # opening image
            output = put_watermark(im, WATERMARK_TEXT, FONT_LOCATION, FONT_SIZE, H_SPACING, V_SPACING, FONT_OPACITY) # Getting watermarked outfile stream
            output.save(file_path+".watermarked.png") # saving file
        except Exception as e:
            print(e)

else:
    im = Image.open(INPUT_IMAGE_FILE) # opening image

    output = put_watermark(im, WATERMARK_TEXT, FONT_LOCATION, FONT_SIZE, H_SPACING, V_SPACING, FONT_OPACITY) # Getting watermarked outfile stream

    output.save(OUTPUT_IMAGE_FILE) # saving file


from scipy.io import loadmat
from scipy.signal import butter, lfilter

import numpy as np

import math

from pyriemann.estimation import Covariances, Coherences
from pyriemann.tangentspace import TangentSpace, FGDA

sample_rate = 512 #in Hz
num_channels = 12
path = './db/'

# load train data
def load_train_data(path_to_file):
    annots = loadmat(path_to_file)
    raw_eeg_data = annots['RawEEGData']
    labels = annots['Labels']

    return raw_eeg_data, labels

# load evaluate data
def load_eval_data(path_to_file):
    annots = loadmat(path_to_file)
    raw_eeg_data = annots['RawEEGData']

    return raw_eeg_data

def get_coh(X):
    coh = Coherences().transform(X)
    return coh[:,:,:,0]

def get_covar(X):
    covar = Covariances(estimator='lwf').transform(X)
    return covar

def get_riemann_ts(X):
    covar = Covariances(estimator='lwf').transform(X)
    tang = TangentSpace().fit_transform(covar)
    return tang

# Butterworth Bandpass Filter
# Source: https://scipy-cookbook.readthedocs.io/items/ButterworthBandpass.html
def butter_bandpass(lowcut, highcut, fs, order=5):
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq
    b, a = butter(order, [low, high], btype='band')
    return b, a

def butter_bandpass_filter(signal, lowcut, highcut, fs, order=5):
    b, a = butter_bandpass(lowcut, highcut, fs, order=order)
    n_trials, n_chans, n_time = signal.shape
    filtered = np.zeros(signal.shape)
    for i in range(n_trials):
        for j in range(n_chans):
            filtered[i][j] = lfilter(b, a, signal[i][j])
    return filtered

def get_data_within(patient_number, trim=(-1536,0), low=8, high=24, order=5):
    if patient_number<1 or patient_number>10:
        print('Invalid Patient Number')
        return None

    print('Mode = within')
    if patient_number<10:
        patient_str = '0'+str(patient_number)
    else:
        patient_str = str(patient_number)

    tlow, thigh = trim
    raw_data, labels = load_train_data(path+'parsed_P'+patient_str+'T.mat')
    if thigh==0:
        raw_data = raw_data[:,:,tlow:]
    else:
        raw_data = raw_data[:,:,tlow:thigh]
    labels = np.squeeze(labels)
    labels -= 1
    y = labels

    raw_eval = load_eval_data(path+'parsed_P'+patient_str+'E.mat')
    if thigh==0:
        raw_eval = raw_eval[:,:,tlow:]
    else:
        raw_eval = raw_eval[:,:,tlow:thigh]

    print('Loaded data for patient %d'%(patient_number))

    #filter data
    X = butter_bandpass_filter(raw_data, low, high, sample_rate, order=order)

    #filter evaluation data
    X_eval = butter_bandpass_filter(raw_eval, low, high, sample_rate, order=order)

    return X, y, X_eval

def get_data_inter(patient_number, trim=(-1536,0), low=8, high=24, order=5, mode="test"):
    if patient_number<1 or patient_number>10:
        print('Invalid Patient Number')
        return None

    tlow, thigh = trim
    print('Mode = inter')
    raw_train = np.array([])
    y_train = np.array([])
    for i in range(1,11):
        if i!=patient_number and i!=9 and i!=10:
            if i<10:
                patient_str = '0'+str(i)
            else:
                patient_str = str(i)
            raw_data_train, labels = load_train_data(path+'parsed_P'+patient_str+'T.mat')
            if thigh==0:
                raw_data_train = raw_data_train[:,:,tlow:]
            else:
                raw_data_train = raw_data_train[:,:,tlow:thigh]
            labels = np.squeeze(labels)
            labels -= 1
            print('Loaded data for patient %d'%(i))

            if raw_train.size==0:
                raw_train = raw_data_train
                y_train = labels
            else:
                raw_train = np.concatenate((raw_train, raw_data_train))
                y_train = np.concatenate((y_train, labels))

    if patient_number<10:
        patient_str = '0'+str(patient_number)
    else:
        patient_str = str(patient_number)

    if mode=="test":
        raw_test, labels = load_train_data(path+'parsed_P'+patient_str+'T.mat')
        if thigh==0:
            raw_test = raw_test[:,:,tlow:]
        else:
            raw_test = raw_test[:,:,tlow:thigh]
        labels = np.squeeze(labels)
        labels -= 1
        y_test = labels

    raw_eval = load_eval_data(path+'parsed_P'+patient_str+'E.mat')
    if thigh==0:
        raw_eval = raw_eval[:,:,tlow:]
    else:
        raw_eval = raw_eval[:,:,tlow:thigh]

    print('Loaded data for patient %d'%(patient_number))

    #filter training data
    X_train = butter_bandpass_filter(raw_train, low, high, sample_rate, order=order)

    if mode=="test":
        #filter testing data
        X_test = butter_bandpass_filter(raw_test, low, high, sample_rate, order=order)

    #filter evaluation data
    X_eval = butter_bandpass_filter(raw_eval, low, high, sample_rate, order=order)

    if mode=="test":
        return X_train, y_train, X_test, y_test, X_eval

    return X_train, y_train, X_eval

def mat_to_vect(mat):
    mat_size = mat.shape[0]
    vect_size = mat_size*mat_size
    vect = np.zeros(vect_size)
    idx=0
    for i in range(mat_size):
        for j in range(mat_size):
            vect[idx] = mat[i][j]
            idx+=1
    return vect

def symm_to_vect(mat):
    mat_size = mat.shape[0]
    vect_size = int((mat_size*(mat_size+1))/2)
    vect = np.zeros(vect_size)
    idx=0
    for i in range(mat_size):
        for j in range(i, mat_size):
            vect[idx] = mat[i][j]
            idx+=1
    return vect

def extract_covar_windows(series, win_size, hop_size):
    channels, num_samples = series.shape
    num_wins = int(math.ceil((num_samples-win_size)/hop_size))+1
    time_wins = np.zeros((num_wins, channels, win_size))
    covar_size = int((channels*(channels+1))/2)
    covar_wins = np.zeros((covar_size, num_wins))
    for i in range(num_wins):
        if i<num_wins-1:
            time_wins[i,:,:] = series[:,i*hop_size:i*hop_size+win_size]
        else:
            time_wins[i,:,:num_samples-i*hop_size] = series[:,i*hop_size:num_samples]
    covar = get_covar(time_wins)
    for i in range(num_wins):
        covar_wins[:,i] = symm_to_vect(covar[i])
    return covar_wins

def extract_riemann_windows(series, win_size, hop_size):
    channels, num_samples = series.shape
    num_wins = int(math.ceil((num_samples-win_size)/hop_size))+1
    time_wins = np.zeros((num_wins, channels, win_size))
    covar_size = int((channels*(channels+1))/2)
    for i in range(num_wins):
        if i<num_wins-1:
            time_wins[i,:,:] = series[:,i*hop_size:i*hop_size+win_size]
        else:
            time_wins[i,:,:num_samples-i*hop_size] = series[:,i*hop_size:num_samples]
    covar = np.transpose(get_riemann_ts(time_wins))
    return covar


import os
import numpy as np
import cv2
import csv
from keras.preprocessing.image import ImageDataGenerator
from tqdm import tqdm
import random
import argparse

src_path = os.path.dirname(os.path.abspath(__file__))
input_path = os.path.dirname(src_path)
data_path = os.path.join(input_path, 'plant-pathology-2021-fgvc8')
img_path = os.path.join(data_path, 'train_images')
root_path = os.path.dirname(input_path)
output_path = os.path.join(root_path, 'working')

parser = argparse.ArgumentParser()
parser.add_argument('--kaggle', action='store_true', help='if the script is run on kaggle')
parser.add_argument('--local', action='store_true', help='if save output to local desktop')
opt = parser.parse_args()

if __name__ == '__main__':
    img_cls = ['complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab']
    img_aug_cls = {'healthy': 3}  # class: fach

    if opt.kaggle:
        img_aug_save_path = os.path.join(output_path, 'img_aug')
    elif opt.local:
        img_aug_save_path = os.path.join('C:\\Users\\Public\\Desktop\\', 'img_aug')
    else:
        img_aug_save_path = os.path.join(input_path, 'img_aug')
    if not os.path.exists(img_aug_save_path):
        os.mkdir(img_aug_save_path)

    datagen = ImageDataGenerator(
        rotation_range=20,
        fill_mode='constant',
        height_shift_range=0.1,
        width_shift_range=0.1,
        horizontal_flip=True,
        vertical_flip=True,
        shear_range=0.2
    )

    reader = csv.reader(open(os.path.join(data_path, 'train.csv')))
    next(reader)
    reader = list(reader)

    for row in tqdm(reader):
        label = row[-1]

        if len(label) > 0 and label.find(' ') == -1:
            img_file_path = os.path.join(img_path, row[0])
            img = cv2.imread(img_file_path)
            if img is not None:
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                img = cv2.resize(img, (256, 256))  # !!!sehr wichtig fuer ImageDataGenerator.flow()
                # img = cv2.addWeighted(img, 4, cv2.GaussianBlur(img, (0, 0), 15), -4, 128)

                if label not in img_aug_cls.keys():
                    save_pth = os.path.join(img_aug_save_path, str(0) + 'unhealthy')
                    if not os.path.exists(save_pth):
                        os.mkdir(save_pth)
                    cv2.imwrite(os.path.join(save_pth, row[0]), img)
                else:
                    save_pth = os.path.join(img_aug_save_path, str(1) + 'healthy')
                    if not os.path.exists(save_pth):
                        os.mkdir(save_pth)
                    cv2.imwrite(os.path.join(save_pth, row[0]), img)
                    img_gen = datagen.flow(
                        np.expand_dims(np.asarray(img), 0),
                        [label],
                        save_to_dir=save_pth,
                        save_format='jpg',
                        batch_size=1
                    )
                    n = 1
                    while True:
                        if n > img_aug_cls[label]:
                            break
                        next(img_gen)
                        n += 1
            else:
                print('Image {} not found.'.format(row[0]))

    data_list = []
    for root, dirs, files in os.walk(img_aug_save_path):
        for d in dirs:
            print(d + ': ' + str(len(os.listdir(os.path.join(root, d)))))

        for file in files:
            if file.endswith('.jpg'):
                label = list(os.path.basename(root))[0]
                data_list.append(file + ',' + label + '\n')

    train_csv_path = os.path.join(img_aug_save_path, 'train.csv')
    val_csv_path = os.path.join(img_aug_save_path, 'val.csv')
    for prefix in ['train', 'val']:
        if os.path.exists(eval('{}_csv_path'.format(prefix))):
            os.remove(eval('{}_csv_path'.format(prefix)))
        with open(eval('{}_csv_path'.format(prefix)), 'w') as f:
            f.write('image,label' + '\n')

    random.shuffle(data_list)
    data_split = int(len(data_list) * 0.9)

    for prefix in ['train', 'val']:
        with open(eval('{}_csv_path'.format(prefix)), 'a') as f:
            if prefix == 'train':
                f.writelines(data_list[:data_split])
            else:
                f.writelines(data_list[data_split:])


import  os
from  PIL import Image  as  im_open,  ImageDraw
from keras.preprocessing import image as Image
from keras.applications.resnet50 import preprocess_input, decode_predictions
from keras.models import load_model
import numpy as np

#model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'
#del model  # deletes the existing model

# returns a compiled model
# identical to the previous one
model = load_model('/scratch/ii1n17/model50_.h5')

files = os.listdir('/scratch/ii1n17/SD4060_patches/')
im = im_open.open('/lyceum/ii1n17/SD4060.TIF')
if not os.path.exists('/scratch/ii1n17/SD4060_patches_car/'):
    os.makedirs('/scratch/ii1n17/SD4060_patches_car/')
draw = ImageDraw.Draw(im)
k = 0
for el in files:
    img_path = '/scratch/ii1n17/SD4060_patches/'+el
    img = Image.load_img(img_path, target_size=(224, 224))
    x = Image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    preds = model.predict(x)
#    print(preds)
#    if (preds[0][0]>0.98):
    if (preds[0][0]>=0.9):
#        img.save('/scratch/ii1n17/SD4060_patches_car/'+el)
#        k += 1
# that is car = draw it
        str1 = el
        str2 = '_'
        str3 = '.TIF'
        k2 = str1.find(str2)
        k3 = str1.find(str3)
        i = int(str1[0:k2])
        j = int(str1[k2+1:k3])
#        draw.rectangle([i, j, i+32, j+32], fill=128, outline=128)
        draw.line((i, j, i+32, j), fill="white", width=3)
        draw.line((i+32, j, i+32, j+32), fill="white", width=3)
        draw.line((i+32, j+32, i, j+32), fill="white", width=3)
        draw.line((i, j+32,i, j), fill="white", width=3)
        im.save('SD4060_resnet50.TIF')


import pygame
import os

SCREEN_WIDTH=300
SCREEN_HEIGHT=240

pygame.init()
clock=pygame.time.Clock()

screen=pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption('NarutoTrial')

sprite_sheet_image=pygame.image.load(os.path.join('assets','kakashi.png')).convert_alpha()

isGameRunning=True

BG=(50,50,50)
SPRITE_BG_COLOR=(0,128,0)#sprite sheet background Color

#kakashi-kunaieAttack1
kakashi_kunai_attack_frame_info=[
    [534,3447,34,44],
    [582,3441,32,50],
    [633,3448,42,43],
    [687,3423,43,68],
    [753,3427,48,64],
    [821,3435,54,56],
    [891,3436,46,53],
    [955,3444,36,47]
]

def get_image(sheet,frame,scale,color):

    starting_X=kakashi_kunai_attack_frame_info[frame][0]
    starting_Y=kakashi_kunai_attack_frame_info[frame][1]
    width=kakashi_kunai_attack_frame_info[frame][2]
    height=kakashi_kunai_attack_frame_info[frame][3]

    image=pygame.Surface((width,height)).convert_alpha()
    image.blit(sheet,(0,0),(starting_X,starting_Y,width,height))
    image=pygame.transform.scale(image,(scale*width,scale*height))
    image.set_colorkey(color)
    return image

frame=0
last_update=pygame.time.get_ticks()
animation_cooldown=80

while isGameRunning:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            isGameRunning=False
    screen.fill(BG)
    #update animation
    current_time=pygame.time.get_ticks()
    if current_time-last_update>=animation_cooldown:
        frame+=1
        last_update=current_time
        if frame >=len(kakashi_kunai_attack_frame_info):
            frame=0

    kunaie_attack=get_image(sprite_sheet_image,frame,2,SPRITE_BG_COLOR)

    screen.blit(kunaie_attack,(20,20))
    pygame.display.update()
    clock.tick(60)