import typing

from qtpy import QtCore, QtGui, QtWidgets

from .constants import RibbonCategoryStyle
from .panel import RibbonPanel
from .separator import RibbonSeparator
from .utils import DataFile

if typing.TYPE_CHECKING:
    from .ribbonbar import RibbonBar  # noqa: F401

class RibbonCategoryLayoutButton(QtWidgets.QToolButton):
    """Previous/Next buttons in the category when the
    size is not enough for the widgets.
    """

    pass

class RibbonCategoryScrollArea(QtWidgets.QScrollArea):
    """Scroll area for the gallery"""

    pass

class RibbonCategoryScrollAreaContents(QtWidgets.QFrame):
    """Scroll area contents for the gallery"""

    pass

class RibbonCategoryLayoutWidget(QtWidgets.QFrame):
    """The category layout widget's category scroll area to arrange the widgets in the category."""

    displayOptionsButtonClicked = QtCore.Signal()

    def __init__(self, parent=None):
        """Create a new category layout widget.

        :param parent: The parent widget.
        """
        super().__init__(parent)

        # Contents of the category scroll area
        self._categoryScrollAreaContents = RibbonCategoryScrollAreaContents()  # type: ignore
        self._categoryScrollAreaContents.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)  # type: ignore
        self._categoryLayout = QtWidgets.QHBoxLayout(self._categoryScrollAreaContents)
        self._categoryLayout.setContentsMargins(0, 0, 0, 0)
        self._categoryLayout.setSpacing(5)
        self._categoryLayout.setSizeConstraint(QtWidgets.QLayout.SetMinAndMaxSize)

        # Category scroll area
        self._categoryScrollArea = RibbonCategoryScrollArea()  # type: ignore
        self._categoryScrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self._categoryScrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self._categoryScrollArea.setWidget(self._categoryScrollAreaContents)

        # Previous/Next buttons
        self._previousButton = RibbonCategoryLayoutButton(self)
        self._previousButton.setIcon(QtGui.QIcon(DataFile("icons/backward.png")))
        self._previousButton.setIconSize(QtCore.QSize(12, 12))
        self._previousButton.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
        self._previousButton.setAutoRaise(True)
        self._previousButton.clicked.connect(self.scrollPrevious)  # type: ignore
        self._nextButton = RibbonCategoryLayoutButton(self)
        self._nextButton.setIcon(QtGui.QIcon(DataFile("icons/forward.png")))
        self._nextButton.setIconSize(QtCore.QSize(12, 12))
        self._nextButton.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
        self._nextButton.setAutoRaise(True)
        self._nextButton.clicked.connect(self.scrollNext)  # type: ignore

        # Add the widgets to the main layout
        self._mainLayout = QtWidgets.QHBoxLayout(self)
        self._mainLayout.setContentsMargins(5, 0, 5, 0)
        self._mainLayout.setSpacing(5)
        self._mainLayout.addWidget(self._previousButton, 0, QtCore.Qt.AlignVCenter)
        self._mainLayout.addWidget(self._categoryScrollArea, 1)
        self._mainLayout.addSpacerItem(QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Expanding,
                                                             QtWidgets.QSizePolicy.Minimum))  # fmt: skip
        self._mainLayout.addWidget(self._nextButton, 0, QtCore.Qt.AlignVCenter)

        # Auto set the visibility of the scroll buttons
        self.autoSetScrollButtonsVisible()

    def paintEvent(self, a0: QtGui.QPaintEvent) -> None:
        """Override the paint event to draw the background."""
        super().paintEvent(a0)
        self.autoSetScrollButtonsVisible()

    def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
        """Override the resize event to resize the scroll area."""
        super().resizeEvent(a0)
        self.autoSetScrollButtonsVisible()

    def autoSetScrollButtonsVisible(self):
        """Set the visibility of the scroll buttons."""
        horizontalScrollBar = self._categoryScrollArea.horizontalScrollBar()
        self._previousButton.setVisible(horizontalScrollBar.value() > horizontalScrollBar.minimum())
        self._nextButton.setVisible(horizontalScrollBar.value() < horizontalScrollBar.maximum())
        self._previousButton.setIconSize(QtCore.QSize(12, self.size().height() - 15))
        self._nextButton.setIconSize(QtCore.QSize(12, self.size().height() - 15))

    def scrollPrevious(self):
        """Scroll the category to the previous widget."""
        horizontalScrollBar = self._categoryScrollArea.horizontalScrollBar()
        horizontalScrollBar.setValue(horizontalScrollBar.value() - 50)
        self.autoSetScrollButtonsVisible()

    def scrollNext(self):
        """Scroll the category to the next widget."""
        self._categoryScrollArea.horizontalScrollBar().setValue(
            self._categoryScrollArea.horizontalScrollBar().value() + 50
        )
        self.autoSetScrollButtonsVisible()

    def addWidget(self, widget: QtWidgets.QWidget):
        """Add a widget to the category layout.

        :param widget: The widget to add.
        """
        self._categoryLayout.addWidget(widget)

    def removeWidget(self, widget: QtWidgets.QWidget):
        """Remove a widget from the category layout.

        :param widget: The widget to remove.
        """
        self._categoryLayout.removeWidget(widget)

    def takeWidget(self, widget: QtWidgets.QWidget) -> QtWidgets.QWidget:
        """Remove and return a widget from the category layout.

        :param widget: The widget to remove.
        :return: The widget that was removed.
        """
        self._categoryLayout.removeWidget(widget)
        return widget

class RibbonCategory(RibbonCategoryLayoutWidget):
    """The RibbonCategory is the logical grouping that represents the contents of a ribbon tab."""

    #: Title of the category
    _title: str
    #: The button style of the category.
    _style: RibbonCategoryStyle
    #: Panels
    _panels: typing.Dict[str, RibbonPanel]
    #: color of the context category
    _color: typing.Optional[QtGui.QColor]
    #: Maximum rows
    _maxRows: int = 6

    @typing.overload
    def __init__(
        self,
        title: str = "",
        style: RibbonCategoryStyle = RibbonCategoryStyle.Normal,
        color: QtGui.QColor = None,
        parent=None,
    ):
        pass

    @typing.overload
    def __init__(self, parent=None):
        pass

    def __init__(self, *args, **kwargs):
        """Create a new category.

        :param title: The title of the category.
        :param style: The button style of the category.
        :param color: The color of the context category.
        :param parent: The parent widget.
        """
        if (args and not isinstance(args[0], QtWidgets.QWidget)) or (
            "title" in kwargs or "style" in kwargs or "color" in kwargs
        ):
            title = args[0] if len(args) > 0 else kwargs.get("title", "")
            style = args[1] if len(args) > 1 else kwargs.get("style", RibbonCategoryStyle.Normal)
            color = args[2] if len(args) > 2 else kwargs.get("color", None)
            parent = args[3] if len(args) > 3 else kwargs.get("parent", None)
        else:
            title = ""
            style = RibbonCategoryStyle.Normal
            color = None
            parent = args[0] if len(args) > 0 else kwargs.get("parent", None)
        super().__init__(parent)
        self._title = title
        self._style = style
        self._panels = {}
        self._ribbon = parent  # type: RibbonBar
        self._color = color

    def setMaximumRows(self, rows: int):
        """Set the maximum number of rows.

        :param rows: The maximum number of rows.
        """
        self._maxRows = rows

    def title(self) -> str:
        """Return the title of the category."""
        return self._title

    def setCategoryStyle(self, style: RibbonCategoryStyle):
        """Set the button style of the category.

        :param style: The button style.
        """
        self._style = style
        self.repaint()

    def categoryStyle(self) -> RibbonCategoryStyle:
        """Return the button style of the category.

        :return: The button style.
        """
        return self._style

    def addPanelsBy(
        self,
        data: typing.Dict[
            str,  # title of the panel
            typing.Dict,  # data of the panel
        ],
    ) -> typing.Dict[str, RibbonPanel]:
        """Add panels from a dictionary.

        :param data: The dictionary. The keys are the titles of the panels. The value is a dictionary of
                     arguments. the argument showPanelOptionButton is a boolean to decide whether to show
                     the panel option button, the rest arguments are passed to the RibbonPanel.addWidgetsBy() method.
                     The dict is of the form:

                     .. code-block:: python

                        {
                            "panel-title": {
                                "showPanelOptionButton": True,
                                "widgets": {
                                    "widget-name": {
                                        "type": "Button",
                                        "arguments": {
                                            "key1": "value1",
                                            "key2": "value2"
                                        }
                                    },
                                }
                            },
                        }
        :return: A dictionary of the newly created panels.
        """
        panels = {}
        for title, panel_data in data.items():
            showPanelOptionButton = panel_data.get("showPanelOptionButton", True)
            panels[title] = self.addPanel(title, showPanelOptionButton)
            panels[title].addWidgetsBy(panel_data.get("widgets", {}))
        return panels

    def addPanel(self, title: str, showPanelOptionButton=True) -> RibbonPanel:
        """Add a new panel to the category.

        :param title: The title of the panel.
        :param showPanelOptionButton: Whether to show the panel option button.
        :return: The newly created panel.
        """
        panel = RibbonPanel(title, maxRows=self._maxRows, showPanelOptionButton=showPanelOptionButton, parent=self)
        panel.setFixedHeight(
            self.height()
            - self._mainLayout.spacing()
            - self._mainLayout.contentsMargins().top()
            - self._mainLayout.contentsMargins().bottom()
        )
        self._panels[title] = panel
        self.addWidget(panel)  # type: ignore
        self.addWidget(RibbonSeparator(width=10))  # type: ignore
        return panel

    def removePanel(self, title: str):
        """Remove a panel from the category.

        :param title: The title of the panel.
        """
        # self._panelLayout.removeWidget(self._panels[title])
        self.removeWidget(self._panels[title])
        self._panels.pop(title)

    def takePanel(self, title: str) -> RibbonPanel:
        """Remove and return a panel from the category.

        :param title: The title of the panel.
        :return: The removed panel.
        """
        panel = self._panels[title]
        self.removePanel(title)
        return panel

    def panel(self, title: str) -> RibbonPanel:
        """Return a panel from the category.

        :param title: The title of the panel.
        :return: The panel.
        """
        return self._panels[title]

    def panels(self) -> typing.Dict[str, RibbonPanel]:
        """Return all panels in the category.

        :return: The panels.
        """
        return self._panels

class RibbonNormalCategory(RibbonCategory):
    """A normal category."""

    def __init__(self, title: str, parent: QtWidgets.QWidget):
        """Create a new normal category.

        :param title: The title of the category.
        :param parent: The parent widget.
        """
        super().__init__(title, RibbonCategoryStyle.Normal, parent=parent)

    def setCategoryStyle(self, style: RibbonCategoryStyle):
        """Set the button style of the category.

        :param style: The button style.
        """
        raise ValueError("You can not set the category style of a normal category.")

class RibbonContextCategory(RibbonCategory):
    """A context category."""

    def __init__(self, title: str, color: QtGui.QColor, parent: QtWidgets.QWidget):
        """Create a new context category.

        :param title: The title of the category.
        :param color: The color of the context category.
        :param parent: The parent widget.
        """
        super().__init__(title, RibbonCategoryStyle.Context, color=color, parent=parent)

    def setCategoryStyle(self, style: RibbonCategoryStyle):
        """Set the button style of the category.

        :param style: The button style.
        """
        raise ValueError("You can not set the category style of a context category.")

    def color(self) -> QtGui.QColor:
        """Return the color of the context category.

        :return: The color of the context category.
        """
        return self._color

    def setColor(self, color: QtGui.QColor):
        """Set the color of the context category.

        :param color: The color of the context category.
        """
        self._color = color
        self._ribbon.repaint()

    def showContextCategory(self):
        """Show the given category, if it is not a context category, nothing happens."""
        self._ribbon.showContextCategory(self)

    def hideContextCategory(self):
        """Hide the given category, if it is not a context category, nothing happens."""
        self._ribbon.hideContextCategory(self)

    def categoryVisible(self) -> bool:
        """Return whether the category is shown.

        :return: Whether the category is shown.
        """
        return self._ribbon.categoryVisible(self)

    def setCategoryVisible(self, visible: bool):
        """Set the state of the category.

        :param visible: The state.
        """
        if visible:
            self.showContextCategory()
        else:
            self.hideContextCategory()

class RibbonContextCategories(typing.Dict[str, RibbonContextCategory]):
    """A list of context categories."""

    def __init__(
        self,
        name: str,
        color: QtGui.QColor,
        categories: typing.Dict[str, RibbonContextCategory],
        ribbon,
    ):
        self._name = name
        self._color = color
        self._ribbon = ribbon
        super().__init__(categories)

    def name(self) -> str:
        """Return the name of the context categories."""
        return self._name

    def setName(self, name: str):
        """Set the name of the context categories."""
        self._name = name

    def color(self) -> QtGui.QColor:
        """Return the color of the context categories."""
        return self._color

    def setColor(self, color: QtGui.QColor):
        """Set the color of the context categories."""
        self._color = color

    def showContextCategories(self):
        """Show the categories"""
        self._ribbon.showContextCategory(self)

    def hideContextCategories(self):
        """Hide the categories"""
        self._ribbon.hideContextCategory(self)

    def categoriesVisible(self) -> bool:
        """Return whether the categories are shown."""
        for category in self.values():
            if category.categoryVisible():
                return True
        return False

    def setCategoriesVisible(self, visible: bool):
        """Set the state of the categories."""
        self.showContextCategories() if visible else self.hideContextCategories()


from urllib.request import Request, urlopen, urlretrieve
from bs4 import BeautifulSoup
from urllib.parse import unquote
files = []
errors = []

def take_url_tv(file):
    le = len(file)
    z = 0
    x = 0
    for i in range(-1, -le - 1, -1):
        if file[i] == '/':
            z = i
            break

    for i in range(z - 1, -le - 1, -1):
        if file[i] == '/':
            x = i
            break

    sliceobj = slice(0, x, 1)
    return file[sliceobj]

def read_url_tv(url):
    url = url.replace(" ", "%20")
    req = Request(url)
    a = urlopen(req).read()
    soup = BeautifulSoup(a, 'html.parser')
    x = (soup.find_all('a'))
    for i in x:
        try:
            file_name = i.get("href")
            url_new = url + file_name
            url_new = unquote(url_new)

            if file_name[-1] == '/' and file_name[0] != '.':
                read_url_tv(url_new)

            if url_new.endswith(".mp4") or url_new.endswith(".mkv"):
                # print("GOT URL "+url_new)
                files.append(url_new)
                print("FOUND - "+url_new)

        except:
            file_name = i.get("href")
            url_new = url + file_name
            errors.append(url_new)

    series = [take_url_tv(s) for s in files]
    return series, errors

# print(read_url_tv("http://dhakaftp.com/Data/Disk9/English%20TV%20Series/"))


import requests
from bs4 import BeautifulSoup

def scrape():
  result = requests.get("https://www.campussafety.utoronto.ca/news/categories/community-safety-alerts")
  doc = BeautifulSoup(result.text, "html.parser")

  event = str(doc.find(class_ = "bD0vt9 KNiaIk").string)
  text = str(doc.find(class_ = "BOlnTh").string)

  time_text_start = text.find("Broadcast Time") + len("Broadcast Time: ")
  date_text_start = text.find("Date") + len("Date: ")

  time = text[time_text_start:text.find("Date")]
  date = text[date_text_start:text.find("Type")]

  hyphen = event.find("-", event.find("-")+1)
  event_text = event[:hyphen] + event[hyphen + 1:]
  event_text = event_text.lower().split()
  subject = "-".join(event_text)

  link = "https://www.campussafety.utoronto.ca/post/" + subject

  return """
Latest News
Date: %s
Time: %s
Event: %s
link: %s
""" % (date, time, event, link)


import cocotb
from cocotb.triggers import Event, Combine, with_timeout, Timer

from tp_tb.testbench.evt_sync.evt_sync_ports import EvtSyncPorts

from tp_tb.utils import events
from tp_tb.utils import block_wrapper

class EvtSyncWrapper(block_wrapper.BlockWrapper):
    def __init__(self, clock, name):
        super().__init__(
            clock, name, len(EvtSyncPorts.Inputs), len(EvtSyncPorts.Outputs),
        )

    def send_input_events(
        self, input_testvectors, n_to_send=-1, l0id_request=-1, event_delays=False
    ):

        n_input_files = len(input_testvectors)
        if n_input_files != self.n_input_ports:
            raise ValueError(
                f"Number of input event tables (={n_input_files}) is not equal to number of B2B input ports (={self.n_input_ports})"
            )

        hooks = []
        for port_num, testvector_file in enumerate(input_testvectors):

            driver, io, active = self.input_ports[port_num]

            input_events = events.load_events_from_file(
                filename=testvector_file, n_to_load=n_to_send, l0id_request=l0id_request
            )
            cocotb.log.info(
                f"Sending {len(input_events)} events to input (port_num, port_name) = ({io.value}, {io.name}) from testvector {testvector_file}"
            )

            hook = None
            for ievent, event in enumerate(input_events):
                words = list(event)
                for iword, word in enumerate(words):
                    flow_kwargs = {}

                    # delays are entered at event boundaries
                    # if word.is_start_of_event():
                    #    flow_kwargs.update(
                    #        b2b_flow.event_rate_delay(
                    #            io, event, pass_through=not event_delays
                    #        )
                    #    )
                    hook = (
                        Event()
                    )  # used to tell outside world that all events have been queued to be sent into the fifos
                    driver.append(word.get_binary(), event=hook, **flow_kwargs)
            if hook:
                hooks.append(hook.wait())

        return hooks

    # def send_input_events(self, input_testvectors, n_to_send=-1, l0id_request=-1):
    #    n_input_files = len(input_testvectors)
    #    if n_input_files != self.n_input_ports:
    #        raise ValueError(f"Number of input event tables (={n_input_files}) is not equal to number of EventSync input ports (={self.n_input_ports})")
    #    hooks = []
    #    n_sent_total = 0
    #    for port_num, testvector_file in enumerate(input_testvectors):
    #        driver, io, active = self.input_ports[port_num]
    #        input_events = events.load_events_from_file(filename = testvector_file, n_to_load = n_to_send, l0id_request = l0id_request)
    #        hook = None
    #        n_sent = 0
    #        for ievent, event in enumerate(input_events):
    #            words = list(event)
    #            for iword, word in enumerate(words):
    #                flow_kwargs = {}
    #                hook = Event()
    #                driver.append(word.get_binary(), event = hook, **flow_kwargs)
    #                n_sent += 1
    #        if hook:
    #            hooks.append(hook.wait())
    #        cocotb.log.info(60 * "-")
    #        cocotb.log.info(f"Sent {n_sent} words to input (port_num, port_name) = ({io.value}, {io.name}) from testvector {testvector_file}")
    #        n_sent_total += n_sent

    #    cocotb.log.info(f"Sent {n_sent_total} events in total")
    #    return hooks


from django.contrib import admin
from django.urls import path, include

import board
from board import views

app_name = 'board'

urlpatterns = [
    path('', views.index, name="index"),
    path('<int:board_id>/', views.detail, name="detail"),
    path('delete/<int:content_id>/', views.content_delete, name="delete"),
    path('update/<int:content_id>/', views.content_update, name="update"),
    path('content/create/<int:board_id>/', views.content_create, name="content_create"),
    path('create/', views.board_create, name = 'board_create'),
    path('content/<int:content_id>', views.content_detail, name = 'content_detail'),
    path('create/<int:content_id>', views.comment_create, name = 'comment_create'),
    path('update/<int:comment_id>', views.comment_update, name = 'comment_update'),
    path('delete/<int:comment_id>', views.comment_delete, name = 'comment_delete'),
    path('api/boards/', views.api_board_list)
]


import torch
from torchvision.models import vgg16
from torch import nn, sigmoid
#from torch.nn.functional import interpolate #Upsampling is supposedly deprecated, replace with interpolate, eventually, maybe
from torch.nn.modules.upsampling import Upsample
from torch.nn.functional import interpolate, dropout2d
from torch.autograd import Variable
from torch.nn.modules.conv import Conv2d
from torch.nn.modules.activation import Sigmoid, ReLU

class Upsample(nn.Module):
    # Upsample has been deprecated, this workaround allows us to still use the function within sequential.https://discuss.pytorch.org/t/using-nn-function-interpolate-inside-nn-sequential/23588
    def __init__(self, scale_factor, mode):
        super(Upsample, self).__init__()
        self.interp = interpolate
        self.scale_factor = scale_factor
        self.mode = mode

    def forward(self, x):
        x = self.interp(x, scale_factor=self.scale_factor, mode=self.mode)
        return x

class SalEMA(nn.Module):
    """
    In this model, we pick a Convolutional layer from the bottleneck and apply EMA as a simple temporal regularizer.
    The smaller the alpha, the less each newly added frame will impact the outcome. This way the temporal information becomes most relevant.
    """
    def  __init__(self, alpha, ema_loc, residual, dropout, use_gpu=True):
        super(SalEMA,self).__init__()

        self.dropout = dropout
        self.residual = residual
        self.use_gpu = use_gpu
        if alpha == None:
            self.alpha = nn.Parameter(torch.Tensor([0.25]))
            print("Initial alpha set to: {}".format(self.alpha))
        else:
            self.alpha = torch.Tensor([alpha])
            if use_gpu:
                self.alpha = self.alpha.cuda()

        assert(self.alpha<=1 and self.alpha>=0)
        self.ema_loc = ema_loc # 30 = bottleneck

        # Create encoder based on VGG16 architecture
        original_vgg16 = vgg16()

        # select only convolutional layers
        encoder = torch.nn.Sequential(*list(original_vgg16.features)[:30])

        # define decoder based on VGG16 (inverse order and Upsampling layers)
        decoder_list=[
            Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Upsample(scale_factor=2, mode='nearest'),

            Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Upsample(scale_factor=2, mode='nearest'),

            Conv2d(512, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Upsample(scale_factor=2, mode='nearest'),

            Conv2d(256, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Upsample(scale_factor=2, mode='nearest'),

            Conv2d(128, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(64, 1, kernel_size=(1, 1), stride=(1, 1), padding=0),
            Sigmoid(),
        ]

        decoder = torch.nn.Sequential(*decoder_list)

        # assamble the full architecture encoder-decoder
        self.salgan = torch.nn.Sequential(*(list(encoder.children())+list(decoder.children())))

        print("Model initialized, EMA located at {}".format(self.salgan[self.ema_loc]))
        #print(len(self.salgan))

    def forward(self, input_, prev_state=None):
        x = self.salgan[:self.ema_loc](input_)
        residual = x
        batch_size = x.data.size()[0]
        spatial_size = x.data.size()[2:]

        if self.dropout == True:
            x = dropout2d(x)
        # salgan[self.ema_loc] will act as the temporal state
        if prev_state is None:
            current_state = self.salgan[self.ema_loc](x) #Initially don't apply alpha as there is no prev state we will consistently have bad saliency maps at the start if we were to do so.
        else:
            current_state = sigmoid(self.alpha)*self.salgan[self.ema_loc](x)+(1-sigmoid(self.alpha))*prev_state

        if self.residual == True:
            x = current_state+residual
        else:
            x = current_state

        if self.ema_loc < len(self.salgan)-1:
            x = self.salgan[self.ema_loc+1:](x)

        return current_state, x #x is a saliency map at this point

class SalGAN_EMA2(nn.Module):
    """
    In this model, we pick two Convolutional layers from decoder and encoder  and apply EMA
    The smaller the alpha, the less each newly added frame will impact the outcome. This way the temporal information becomes most relevant.
    """
    def  __init__(self, alpha, ema_loc_1, ema_loc_2, use_gpu=True):
        super(SalGAN_EMA2,self).__init__()

        self.use_gpu = use_gpu
        self.alpha = alpha
        self.ema_loc_1 = ema_loc_1 # 30 = bottleneck
        self.ema_loc_2 = ema_loc_2 # 30 = bottleneck
        assert(self.alpha<=1 and self.alpha>=0)

        # Create encoder based on VGG16 architecture
        original_vgg16 = vgg16()

        # select only convolutional layers
        encoder = torch.nn.Sequential(*list(original_vgg16.features)[:30])

        # define decoder based on VGG16 (inverse order and Upsampling layers)
        decoder_list=[
            Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Upsample(scale_factor=2, mode='nearest'),

            Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Upsample(scale_factor=2, mode='nearest'),

            Conv2d(512, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Upsample(scale_factor=2, mode='nearest'),

            Conv2d(256, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Upsample(scale_factor=2, mode='nearest'),

            Conv2d(128, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            ReLU(),
            Conv2d(64, 1, kernel_size=(1, 1), stride=(1, 1), padding=0),
            Sigmoid(),
        ]

        decoder = torch.nn.Sequential(*decoder_list)

        # assamble the full architecture encoder-decoder
        self.salgan = torch.nn.Sequential(*(list(encoder.children())+list(decoder.children())))

        print("Model initialized, EMAs located at {} and {}".format(self.salgan[self.ema_loc_1], self.salgan[self.ema_loc_2]))
        #print(len(self.salgan))

    def forward(self, input_, prev_state_1=None, prev_state_2=None):
        x = self.salgan[:self.ema_loc_1](input_)

        batch_size = x.data.size()[0]
        spatial_size = x.data.size()[2:]

        if prev_state_1 is None:
            x = self.salgan[self.ema_loc_1](x)
        else:
            x = self.alpha*self.salgan[self.ema_loc_1](x)+(1-self.alpha)*prev_state_1

        prev_state_1 = x
        x = self.salgan[self.ema_loc_1+1:self.ema_loc_2](x)

        if prev_state_2 is None:
            x = self.salgan[self.ema_loc_2](x)
        else:
            x = self.alpha*self.salgan[self.ema_loc_2](x)+(1-self.alpha)*prev_state_2

        prev_state_2 = x
        x = self.salgan[self.ema_loc_2+1:](x)

        return (prev_state_1, prev_state_2), x #x is a saliency map at this point

if __name__ == '__main__':
    model = SalEMA(alpha=0.1, ema_loc=7)
    print(model)


from ast import Or
from email import header
from urllib import response
from flask import session
import requests

from . import ORDER_API_URL

class OrderClient:
    @staticmethod
    def get_orders():
        header = {'Authorization':session['user_api_key']}
        response = requests.get(ORDER_API_URL + '/api/order',headers=header)
        return response.json()

    @staticmethod
    def add_to_cart(book_id,quantity=1):
        payload = {
            'book_id' : book_id,
            'quantity' : quantity
        }
        header = {'Authorization': session['user_api_key']}
        response = requests.post(ORDER_API_URL+'/api/order/add-item',data=payload,headers=header)
        return response.json()

    @staticmethod
    def checkout():
        header = {'Authorization':session['user_api_key']}
        response = requests.post(ORDER_API_URL+'/api/order/checkout',headers=header)
        return response.json()

    @staticmethod
    def get_order_from_session():
        default_order = {
            'items':{}
        }
        return session.get('order',default_order)


from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from . import views

app_name = 'main'

urlpatterns = [
    path('', views.index, name='index'),
    path('search/', views.search, name='search'),
    path('best-tracks/', views.best_tracks, name='best_tracks'),
    path('novelties/', views.novelties, name='novelties'),
    path('hits-of-the-week/', views.hits_of_week, name='hits_of_week'),
    path('mix/', views.mix, name='mix'),
    path('best-performers/', views.best_performers, name='best_performers'),
    path('genres/', views.genres, name='genres'),
    path('genres/<str:genre>', views.genre, name='genre'),
    path('add-track/', views.add_track, name='add_track'),
    path('my-track/', views.my_track, name='my_track'),
    path('create-playlist/', views.create_playlist, name='create_playlist'),
    path('playlist/<int:playlist>', views.playlist, name='playlist'),
    path('add-favourite-track/<int:song_id>', views.like, name='like'),
    path('favourite-track', views.favourite_song, name='favourite_song'),
    path('user_songs/<int:user_id>', views.user_songs, name='user'),
    path('delete-song/<int:song_id>', views.delete_song, name='delete_song'),
    path('add-song-playlist/<int:song_id>/<int:playlist_id>', views.add_song_playlist, name='add_song_playlist'),
    path('delete-song-playlist/<int:song_id>/<int:playlist_id>', views.delete_song_playlist, name='delete_song_playlist'),
    path('delete-playlist/<int:playlist_id>', views.delete_playlist, name='delete_playlist'),
    path('edit-playlist/<int:playlist_id>', views.edit_playlist, name='edit_playlist')
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


import string
import math
import os
import json
import cv2
import skimage.data
import random as rd

import numpy as np
import matplotlib.pyplot as plt
import PIL.Image as PImage
import PIL.ImageFont as PFont
import PIL.ImageDraw as PDraw
import PIL.ImageColor as PColor

from skimage import transform

#Mở file từ điển và đưa vào dict_arr
dictfile = "ocr_tester/words.txt"
dict_arr = []
with open(dictfile, encoding="UTF-8") as df:
    for line in df:
        dict_arr.append(line[:-1])

def get_rand_word():
    """
    Lấy từ ngẫu nhiên trong từ điển
    """
    return dict_arr[rd.randint(0, len(dict_arr) - 1)]

def get_rand_str(max_size=50):
    """
    Lấy string ngẫu nhiên thỏa mãn độ dài
    """
    ret = ""
    while True:
        word = get_rand_word()
        if len(ret) + len(word) + 1 > max_size:
            return ret[:-1]
        else:
            ret = ret + get_rand_word() + " "

def generate_text_box(font, size=7, lines=8):
    """Tạo một textbox để test. Textbox ở vị trí ngẫu nhiên và xoay nhẹ trong không gian 3D
    alphabet: bảng chữ cái để tạo test, ở dạng string
    font: đường dẫn đến file font (.ttf)
    lines: số dòng của textbox
    """
    font_bound = (16, 24)
    rotationX_bound = (0, 0)
    rotationY_bound = (0, 0)
    rotationZ_bound = (-0.01, 0.01)

    background = PImage.open('ocr_tester/backgrounds/background.png')

    #Tạo chữ trên ảnh
    font_size = np.random.randint(font_bound[0], font_bound[1])
    texts = [get_rand_str() for i in range(lines)]
    text_boxes = [generate_text_mask(texts[j], font, font_size, font_align='left') for j in range(lines)]
    unpack = lambda boxes: [box.size for box in boxes]

    box_x, box_y = max(unpack(text_boxes))
    bg_x, bg_y = background.size
    box_bound_x = np.random.randint(low=0, high=max(bg_x - box_x, 1))
    box_bound_y = np.random.randint(low=0, high=max(bg_y - box_y * lines, 1))

    # print("max box: ", box_x, box_y)
    # print("background size: ", bg_x, bg_y)
    # print("box bound: ", box_bound_x, box_bound_y)
    # print(texts)

    for text_box in text_boxes:
            box_bound_y += box_y
            background.paste(im=text_box, box=(box_bound_x, box_bound_y))

    # Tạo textbox bị xoay, chuyển về dạng np array
    background = PImage.fromarray((255 * transform_image(
        background,
        thetaX = np.random.uniform(rotationX_bound[0], rotationX_bound[1]),
        thetaY = np.random.uniform(rotationY_bound[0], rotationY_bound[1]),
        thetaZ = np.random.uniform(rotationZ_bound[0], rotationZ_bound[1])
    )).astype(np.int8), mode='RGB')

    return np.asarray(background), texts

def generate_text_mask(
        text,
        font_filepath,
        font_size,
        color=(0,0,0),
        font_align="center"
    ):
    """Tạo từng dòng trong textbox
    """
    # Tạo textbox
    font = PFont.truetype(font_filepath, size=font_size)
    box = font.getbbox(text)
    img = PImage.new(mode='RGB', size=(box[2], box[3]), color=(255, 255, 225)) # box là tuple(left, top, right, bottom)
    draw = PDraw.Draw(img)

    draw_point = (0, 0)
    draw.text(draw_point, text=text, fill=color, anchor='lt', font=font, align=font_align)
    return img

def transform_image(image, thetaX=0, thetaY=0, thetaZ=0):
    #Ma trận xoay
    R_x = np.array([[1, 0, 0],
                    [0, math.cos(thetaX), -math.sin(thetaX)],
                    [0, math.sin(thetaX), math.cos(thetaX)]])

    R_y = np.array([[math.cos(thetaY), 0, math.sin(thetaY)],
                    [0, 1, 0],
                    [-math.sin(thetaY), 0, math.cos(thetaY)]])

    R_z = np.array([[math.cos(thetaZ), -math.sin(thetaZ), 0],
                    [math.sin(thetaZ), math.cos(thetaZ), 0],
                    [0, 0, 1]])

    rotation_matrix = np.matmul(R_x, np.matmul(R_y, R_z))

    # Xoay ảnh
    image = np.asarray(image, dtype=np.uint8) # phải chuyển về np array để tránh lỗi
    tform = transform.ProjectiveTransform(matrix=rotation_matrix)
    tf_img = transform.warp(image, tform.inverse)

    # Vẽ ảnh đã được xoay
    # fig, ax = plt.subplots()
    # ax.imshow(tf_img)
    # ax.set_title('Ảnh đã biến đổi')
    # plt.show()
    return tf_img

def test_autogen(count=5):
    font = 'ocr_tester/custom_font/arial.ttf'
    text_res = []
    img_res = []
    for i in range(0, count):
        img, text = generate_text_box(font, lines=20, size=1)
        img = PImage.fromarray(img)
        img.save("ocr_tester/generated_test/" + str(i) + ".png")
        img_res.append(img)
        text_res.append(text)
    return img_res, text_res

def main():
    font = 'ocr_tester/custom_font/arial.ttf'
    font_size = 16

    img, text = generate_text_box(font, lines=20, size=1)
    img = PImage.fromarray(img)
    img.save("ocr_tester/generated_test/odf.png")

if __name__ == "__main__":
    main()


from typing import Tuple

import numpy as np

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

from skorch import NeuralNetBinaryClassifier
from skorch.helper import SliceDict

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

class NetModule(nn.Module):
    def __init__(
        self,
        dim_input: int = 1280,
        dim_hidden: int = 2560,
        dropout: float = 0.5,
    ):
        super(NetModule, self).__init__()

        self.encoder = nn.Sequential(
            nn.Linear(dim_input, dim_hidden),
            nn.ReLU(),
        )

        self.decoder = nn.Sequential(
            nn.Linear(dim_hidden, dim_input),
            nn.ReLU(),
        )

        self.output = nn.Linear(dim_hidden, 1)

    def forward(self, X, **kwargs):
        hidden_noise = self.encoder(X + torch.randn_like(X) * 0.1)
        hidden = self.encoder(F.dropout(X, 0.5, self.training))
        rec = self.decoder(hidden_noise)
        logits = self.output(hidden)
        return logits.view(-1), rec

class NeuralNetClassifier(NeuralNetBinaryClassifier):
    def fit(self, X, y, **fit_params):
        self.set_params(module__dim_input=X.shape[1])
        super().fit(X, y.astype(np.float32))

    def get_loss(self, y_pred, y_true, X=None, training=False):
        logits, rec = y_pred
        return 0.5 * F.binary_cross_entropy_with_logits(logits, y_true) + 0.5 * F.mse_loss(rec, X) # type: ignore


from django.urls import path

from users.views import registration

from . import views

app_name = 'users'

urlpatterns = [
    path('users/registration/', views.registration, name='registration'),
    path('users/login/', views.login_request, name='login'),
    path('users/logout/', views.logout_request, name='logout'),
    path('users/account/', views.account, name='account'),
    path('users/manage-account/', views.manage_account, name='manage-account'),

]


import numpy as np
import cv2
from tkinter import *
from PIL import Image, ImageTk
import pytesseract
import webbrowser
from subprocess import Popen
import os
from tkinter import messagebox
import connectCOM as com
import database as dat
import firebasetest as frb
COM = com.connect_COM()
con = dat.sql_connection("database_dakt.db")
rows = dat.sql_fetch(con, "TinhVN")

def test(temp):
    s = temp
    i = 0
    j = len(s) - 1
    flag1 = False
    flag2 = False
    while True:
        if (s[i].isdigit()):
            flag1 = True
        else:
            i += 1
        if (s[j].isdigit()):
            flag2 = True
        else:
            j -= 1
        if (flag1 and flag2):
            break
    return s[i:j + 1]

#Set up GUI
window = Tk()  #Makes main window
window.wm_title("Nhan dien bien so xe")
window.config(background="#FFFFFF")

#Graphics window
imageFrame = Frame(window, width=600, height=500)
imageFrame.grid(row=0, column=0, padx=10, pady=2)

#Capture video frames
lmain = Label(imageFrame)
lmain.grid(row=0, column=0)
cap = cv2.VideoCapture(0)
data_key =  StringVar()
check_key = StringVar()
addr_key = StringVar()
def show_frame():
    ret, frame = cap.read()
    scale_percent = 90
    # calculate the 50 percent of original dimensions
    width = int(frame.shape[1] * scale_percent / 100)
    height = int(frame.shape[0] * scale_percent / 100)

    # dsize
    dsize = (width, height)

    # resize image
    frame = cv2.resize(frame ,dsize)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    noise_removal = cv2.bilateralFilter(gray, 9, 75, 75)
    equal_histogram = cv2.equalizeHist(noise_removal)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    morph_image = cv2.morphologyEx(equal_histogram, cv2.MORPH_OPEN, kernel, iterations=20)
    sub_morp_image = cv2.subtract(equal_histogram, morph_image)
    thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
    canny_image = cv2.Canny(thresh, 250, 255)
    kernel = np.ones((3, 3), np.uint8)
    # cv2.putText(frame, "KHUNG BIEN SO", (40, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
    # Tạo đường viền để theo dõi bien so
    contours, h = cv2.findContours(thresh, 1, 2)
    largest_rectangle = [0, 0]
    for cnt in contours:
        lenght = 0.05 * cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, lenght, True)
        if len(approx) == 4:
            area = cv2.contourArea(cnt)
            if area > largest_rectangle[0]:
                largest_rectangle = [cv2.contourArea(cnt), cnt, approx]
    x, y, w, h = cv2.boundingRect(largest_rectangle[1])
    image = frame[y:y + h, x:x + w]
    # cv2.drawContours(frame, [largest_rectangle[1]], 0, (0, 255, 0), 11)
    cropped = frame[y:y + h, x:x + w]
    cv2.putText(frame, "BIEN SO", (x, y-5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255))
    cv2.drawContours(frame, [largest_rectangle[1]], 0, (255, 0, 0), 8)
    # cv2.imshow('Dinh Vi Bien So Xe', frame)
    # DOC HINH ANH CHUYEN THANH FILE TEXT

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (3, 3), 1)
    thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    # cv2.imshow('Bien So La', thresh)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
    invert = 255 - opening
    data = pytesseract.image_to_string(invert, lang='eng', config='--psm 6')
    # print("Bien so xe la:", data)
    # dat = data[0:2]
    # print(dat)

    try:
        data = test(data)
        data = data.replace(".", "")
        data = data.replace("\n", " ")
        print(data)
        if (((int(data[0:2]) in i) for i in rows) and data[2] == "-" and data[4].isdigit()
    and data[3].isalpha() and (data[6:len(data)].isdigit()) ):
            print("Bien so xe la:", data)
            for i in rows:
                if int(data[0:2])in i:
                    #print(i[1])
                    addr_key.set(str(i[1]))

            lst = (int(data[0:2]), data[3:5], int(data[6:]))

            data_key.set(data)

        #if (int(data[0:2]) < 100 and int(data[0:2]) > 0 and data[2] == "-" and data[4].isdigit()):
            #if (data[6:10].isdigit() or data[6:11].isdigit() or data[6:9].isdigit()):
            #    print("Bien so xe la:", data)
             #   addr_key.set(str(array.get(int(data[0:2]))))
              #  for i in range(len(data)-1):
               #     if(data[i]=="\n"):
                #        data = data[0:i] + " " + data[i+1:]
                #data_key.set(data)

    except:
        pass
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    lmain.imgtk = imgtk
    lmain.configure(image=imgtk)
    lmain.after(10, show_frame)

def ghibienso():
    try:
        bienso = data_key.get()
        print(bienso)
        lst = (int(bienso[0:2]), bienso[3:5], bienso[6:])
        print(lst)
        if dat.find_e_luubienso(con,lst):
            frb.send_data_firebase(lst,False)
            dat.delete_e(con,lst)
            try:
                com.open_servo2(COM)
                com.close_servo2(COM)
            except:
                pass
            messagebox.showinfo(title=None, message="Biển "+bienso+" đã ra khỏi bãi xe")

        else:
            dat.sql_insert_luubienso(con,lst)
            frb.send_data_firebase(lst,True)
            try:
                com.open_servo(COM)
                com.close_servo(COM)
            except:
                pass
            messagebox.showinfo(title=None, message="Biển "+bienso+"  đã vào bãi xe")
    except:
        messagebox.showerror(title=None, message="Biển số không hợp lệ")
        print("Bien khong hop le")

def quit():
    if messagebox.askyesno(title="Thoát chương trình", message="Bạn có muốn thoát chương trình ?"):
        window.destroy()

def openCloud():
    webbrowser.open("https://daktbiensoxe-default-rtdb.asia-southeast1.firebasedatabase.app/")

def openSqlite():
    os.system("xdg-open /home/pi/Downloads/New/DAKR\ provip/database_dakt.db")
def saveCSV():
    try:
        bienso = data_key.get()
        print(bienso)
        lst = (int(bienso[0:2]), bienso[3:5], bienso[6:])
        frb.get_data_firebase(lst)
    except:
        messagebox.showerror(title=None, message="Biển số không hợp lệ")
#Slider window (slider controls stage position)
sliderFrame = Frame(window, width=600, height=200,bg="#FFFFFF")
sliderFrame.grid(row = 1, column=0, padx=10, pady=2)

button1 = Button(sliderFrame, text = "Ghi biển số" ,width=15,font=('Arial', 10, 'bold'),command = ghibienso).grid(row=0,column=0,padx=10, pady=10)

buttonCSV = Button(sliderFrame, text = "Xuat CSV" ,width=15,font=('Arial', 10, 'bold'), command = saveCSV).grid(row=0,column=2,padx=10, pady=10)
buttonData = Button(sliderFrame, text = "Cloud Database" ,width=15,font=('Arial', 10, 'bold'), command = openCloud).grid(row=4,column=0,padx=10, pady=10)
buttonSQLite = Button(sliderFrame, text = "SQLite Database" ,width=15,font=('Arial', 10, 'bold'), command = openSqlite).grid(row=4,column=1,padx=10, pady=10)
label1 = Label(sliderFrame, text = "Thông tin biển số",bg="#FFFFFF",font=('Arial', 10, 'bold')).grid(row=1,column=0,padx=5, pady=2)
entry1 = Entry(sliderFrame,width=25,textvariable=data_key,font=('Arial',12)).grid(row=1,column=1,padx=5, pady=2)
label1_2 = Label(sliderFrame, text = "Biển tỉnh",bg="#FFFFFF",font=('Arial', 10, 'bold')).grid(row=2,column=0,padx=5, pady=2)
entry1_2 = Entry(sliderFrame,width=25,textvariable=addr_key,font=('Arial',12)).grid(row=2,column=1,padx=5, pady=2)
label2 = Label(sliderFrame, text = "Kiểm tra thông tin",bg="#FFFFFF",font=('Arial', 10, 'bold')).grid(row=3,column=0,padx=5, pady=2)
#entry2 = Entry(sliderFrame,width=20,textvariable=check_key,font=('Arial',12)).grid(row=3,column=1,padx=5, pady=2)
text = Text(sliderFrame, width  = 28, height =2)
text.grid(row=3,column=1,padx=5, pady=2)
def kiemtra():
    text.delete('1.0', 'end')
    bienso = data_key.get()
    print(bienso)
    lst = (int(bienso[0:2]), bienso[3:5], bienso[6:])
    if dat.find_e_luubienso(con,lst):
        text.insert('1.0',"Biển "+data_key.get()+"\nđang có trong bãi")
    else:
        text.insert('1.0',"Biển "+data_key.get()+"\nkhong có trong bãi")

button2 = Button(sliderFrame, text = "Kiểm tra" ,width=15,font=('Arial', 10, 'bold'),command = kiemtra).grid(row=0,column=1,padx=10, pady=10)

button3 = Button(window, text = "Thoát" ,width=15,font=('Arial', 10, 'bold'),command=quit).grid(row=3,column=0,padx=40, pady=10,sticky=NE)
show_frame()  #Display 2

window.protocol("WM_DELETE_WINDOW", quit)
window.resizable(False, False)
window.mainloop()  #Starts GUI


import tensorflow as tf
from tensorflow import keras
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
import random
import math
import matplotlib.pyplot as plt

maxValue = 40000
daysToPredict = 7

def preprocessing(values, x_number, y_number):
    result = []
    for i in range(len(values) - x_number - y_number):
        tmp = []
        tmp2 = []
        for j in range(x_number):
            tmp.append(values[i+j]/maxValue)
        for k in range(y_number):
            tmp2.append(values[i+x_number+k]/maxValue)

        result.append([tmp, tmp2])
    return result

df = pd.read_excel("data.xlsx", header=None)

dataset = df.values
dataset = dataset[:, 1]

corr = np.correlate(dataset, dataset, mode='full')
corr = corr[int(len(corr)/2):]
print(corr)
most_correlated = np.count_nonzero(
    corr[0] - np.array(corr) < 10000000000)  # 17
most_correlated = 14

plt.plot(corr)
plt.title('Autocorrelation')
plt.xlabel('Next offsets')
plt.ylabel('Autocorrelation')
plt.show()

preprocessed_dataset = preprocessing(dataset, most_correlated, daysToPredict)

input_table = []
for i in range(len(preprocessed_dataset) - most_correlated, len(preprocessed_dataset)):
    input_table.append((preprocessed_dataset[i][1])[0])
random.shuffle(preprocessed_dataset)

x_learn = []
y_learn = []
x_test = []
y_test = []

for i in range(len(preprocessed_dataset)):
    if i <= 400:
        x_learn.append(preprocessed_dataset[i][0])
        y_learn.append(preprocessed_dataset[i][1])
    else:
        x_test.append(preprocessed_dataset[i][0])
        y_test.append(preprocessed_dataset[i][1])

x_plot = x_learn[:5]
y_plot = y_learn[:5]

x_learn = np.asarray(x_learn).astype(np.float32)
y_learn = np.asarray(y_learn).astype(np.float32)
x_test = np.asarray(x_test).astype(np.float32)
y_test = np.asarray(y_test).astype(np.float32)

model = Sequential([
    Dense(32, activation='relu', input_shape=(most_correlated,)),
    Dense(32, activation='relu'),
    Dense(daysToPredict)
])

model.compile(optimizer='adam', loss='mean_squared_error',
              metrics=['mean_squared_error'])

hist = model.fit(x_learn, y_learn,
                 batch_size=32, epochs=100,
                 validation_data=(x_test, y_test))

result = 1 - model.evaluate(x_test, y_test)[1]
print("accuracy: ", result)

# predictions for next 7 days using last 14 records
predictions = model.predict([input_table])

plot_predictions = []

for i in range(len(x_plot)):
    plot_predictions.append(
        [(p * maxValue).astype(np.int) for p in model.predict([x_plot[i]])])

new_y_plot = []
for i in range(len(y_plot)):
    new_y_plot.append([int(p * maxValue) for p in y_plot[i]])

y_plot = new_y_plot

print("plot_predictions: ", plot_predictions)
print("y_plot: ", y_plot)

print("most_correlated: ", most_correlated)
print("input table: ", input_table)
print("predictions: ", predictions)
predictions = [(p * maxValue).astype(np.int) for p in predictions]
print(predictions[0])

for i in range(len(plot_predictions)):
    plt.plot(plot_predictions[i][0], color='red', label="Prediction")

    plt.plot(y_plot[i], label="Reality")
    plt.title('Infections')
    plt.xlabel('Next days')
    plt.ylabel('Infections per day')
    plt.legend()
    plt.show()

    err = []
    for j in range(0, 7):
        err.append(math.fabs(y_plot[i][j] - plot_predictions[i][0][j]) / maxValue * 100)

    plt.plot(err, color='green', linestyle='dashed', linewidth=3,
         marker='o', markerfacecolor='blue', markersize=12)
    plt.title('Error')
    plt.xlabel('Next days')
    plt.ylim(0, 4)
    plt.ylabel('Error in percents [%]')
    plt.show()


#!/usr/bin/python
import boto3
import sys

def get_instance_ip(instance_id):
    client = boto3.client('ec2')
    response = client.describe_instances(InstanceIds = [instance_id])
    for reservation in response['Reservations']:
        for instance in reservation['Instances']:
            for interface in instance['NetworkInterfaces']:
                if 'PrivateIpAddress' in interface:
                   yield interface['PrivateIpAddress']
                if 'Association' in interface:
                   yield interface['Association']['PublicIp']

def as_get_instances(client, asgroup):

    irsp = client.describe_auto_scaling_instances()

    for i in irsp['AutoScalingInstances']:
        if i['AutoScalingGroupName'] == asgroup:
            yield i['InstanceId']

if __name__ == '__main__':
    client = boto3.client('autoscaling')
    id_list = list(as_get_instances(client, sys.argv[1]))
    print (id_list)
    for id in id_list:
        print(list(get_instance_ip(id)))


import sys
sys.path.append('./')
import cv2
import os
import numpy as np
from tqdm import tqdm
import json
import shutil
import pdb

if __name__ == "__main__":
    src_dir = '/home/data0/project-datasets/dehongcheng/exp_SYB/train_val_data/train/'
    pred_dir = '/home/data0/project-datasets/dehongcheng/exp_SYB/test/train_infer_ana_0001/'
    save_dir = '/home/data0/project-datasets/dehongcheng/exp_SYB/train_val_data/train_inpaint_kill/'

    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    imgs_list = list()
    for filepath, _, filenames in os.walk(src_dir):
        for filename in filenames:
            if '.jpg' in filename:
                imp = os.path.join(filepath, filename)
                imgs_list.append(imp)

    for imp in tqdm(imgs_list):
        jsp = imp.replace('.jpg', '.json')
        pred_jsp = jsp.replace(src_dir, pred_dir)

        img = cv2.imread(imp)
        with open(pred_jsp, "r", encoding="utf-8") as fj:
            info = json.load(fj)

        for shape in info['shapes']:
            if 'PRED_kill' == shape['group_id']:
                points = shape['points']
                x_list = [k[0] for k in points]
                y_list = [k[1] for k in points]

                x1 = int(min(x_list))
                y1 = int(min(y_list))
                x2 = int(max(x_list))
                y2 = int(max(y_list))

                mask = np.zeros_like(img)[:,:,0]
                # pdb.set_trace()
                mask[y1:y2, x1:x2] = 255

                # x1_crop = max(0, x1-20)
                # y1_crop = max(0, y1-20)
                # x2_crop = min(img.shape[1], x2+20)
                # y2_crop = min(img.shape[0], y2+20)
                # src_img_crop = img[y1_crop:y2_crop, x1_crop:x2_crop, :].copy()

                img = cv2.inpaint(img, mask, 5, cv2.INPAINT_TELEA)

                # dst_img_crop = img[y1_crop:y2_crop, x1_crop:x2_crop, :].copy()
                # res = np.concatenate([src_img_crop, dst_img_crop], 1)
                # cv2.imshow('res', res)
                # cv2.waitKey(0)
        save_sub_dir = '/'.join(imp.replace(src_dir, save_dir).split('/')[:-1])
        if not os.path.exists(save_sub_dir):
            os.makedirs(save_sub_dir)
        dst_imp = imp.replace(src_dir, save_dir)
        dst_jsp = dst_imp.replace('.jpg', '.json')
        cv2.imwrite(dst_imp, img)
        shutil.copy(jsp, dst_jsp)


import cv2 as opencv

def gen_frames(camera):
    """Generator function that continuously yields frames from a webcam connected to the client device"""
    while True:
        success, frame = camera.read()
        if not success:
            break
        else:
            ret, buffer = opencv.imencode('.jpg', frame)
            frame = buffer.tobytes()
            yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')


"""added timezone to student check in record

Revision ID: a51e95225d74
Revises: 8a1efca06e2a
Create Date: 2018-02-08 05:52:59.257192

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'a51e95225d74'
down_revision = '8a1efca06e2a'
branch_labels = None
depends_on = None

def upgrade():
    op.alter_column('student_check_in_records', 'checkin',
            type_=sa.DateTime(timezone=True), nullable=False)

def downgrade():
    op.alter_column('student_check_in_records', 'checkin',
            type_=sa.DateTime(), nullable=False)


"""
Test Spotlight external data cache and clear cache behaviour.
"""

import json
import shutil

import requests

from renumics import spotlight

def test_external_data_cache(non_existing_image_df_viewer: spotlight.Viewer) -> None:
    """
    Test loading non-existing external data, cache it and clear cache.
    """
    image_path = non_existing_image_df_viewer.df["image"][0]
    app_url = str(non_existing_image_df_viewer)

    response = requests.get(app_url + "api/table/", timeout=5)
    assert response.status_code == 200
    assert len(response.text) > 200
    generation_id = json.loads(response.text)["generation_id"]

    response = requests.get(
        f"{app_url}api/table/image/0?generation_id={generation_id}", timeout=5
    )
    assert response.status_code == 422
    assert json.loads(response.text)["type"] == "ConversionFailed"

    shutil.copyfile("data/images/nature-360p.jpg", image_path)
    response = requests.get(
        f"{app_url}api/table/image/0?generation_id={generation_id}", timeout=5
    )
    assert response.status_code == 200
    first_image_content = response.content
    assert len(first_image_content) == 19355

    shutil.copyfile("data/images/nature-720p.jpg", image_path)
    response = requests.get(
        f"{app_url}api/table/image/0?generation_id={generation_id}", timeout=5
    )
    assert response.status_code == 200
    assert response.content == first_image_content

    spotlight.clear_caches()
    response = requests.get(
        f"{app_url}api/table/image/0?generation_id={generation_id}", timeout=5
    )
    assert response.status_code == 200
    second_image_content = response.content
    assert len(second_image_content) == 70143
    assert second_image_content != first_image_content

    shutil.copyfile("data/audio/1.wav", image_path)
    response = requests.get(
        f"{app_url}api/table/image/0?generation_id={generation_id}", timeout=5
    )
    assert response.status_code == 200
    assert response.content == second_image_content

    spotlight.clear_caches()
    response = requests.get(
        f"{app_url}api/table/image/0?generation_id={generation_id}", timeout=5
    )
    assert response.status_code == 422
    assert json.loads(response.text)["type"] == "ConversionFailed"


from django.urls import path
from django.conf.urls import url

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('adduser', views.adduser.as_view(), name='adduser'),
    path('loginuser', views.loginuser.as_view(), name='loginuser'),

    path('markspam', views.markspam.as_view(), name='markspam'),

    path('namesearch', views.namesearch.as_view(), name='namesearch'),
    path('phonesearch', views.phonesearch.as_view(), name='phonesearch'),
    path('getcontactdetails', views.getcontactdetails.as_view(), name='getcontactdetails'),

    path('contactsearch', views.contactsearch.as_view(), name='contactsearch'),

    path('global', views.usershow.as_view(), name='users'),
]


from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^edit_map$', views.edit_map, name='edit_map'),
    url(r'^save_map$', views.save_map, name='save_map'),
    url(r'^get_active_users$', views.get_active_users, name='get_active_users'),
    url(r'^update_users$', views.update_users, name='update_users'),
    url(r'^hostnames$', views.show_hostnames, name='show_hostnames'),
]


import asyncio
import os
import codecs
from datetime import datetime
from random import shuffle
from random import randint
from PIL import Image, ImageDraw, ImageFilter, ImageFont

from pykeyboard import InlineKeyboard
from pyrogram import filters
from pyrogram.errors.exceptions.bad_request_400 import (ChatAdminRequired,
                                                        UserNotParticipant)
from pyrogram.types import (ChatPermissions, InlineKeyboardButton,
                            InlineKeyboardMarkup, Message, User)

from MashaRoBot import pbot as app
from MashaRoBot.pyrogramee.errors import capture_err
from wbb.utils.dbfunctions import (captcha_off, captcha_on, del_welcome,
                                   get_captcha_cache, get_welcome,
                                   is_captcha_on, is_gbanned_user, set_welcome,
                                   update_captcha_cache)

from functools import wraps
from pyrogram.errors.exceptions.forbidden_403 import ChatWriteForbidden

async def member_permissions(chat_id: int, user_id: int):
    perms = []
    member = await app.get_chat_member(chat_id, user_id)
    if member.can_post_messages:
        perms.append("can_post_messages")
    if member.can_edit_messages:
        perms.append("can_edit_messages")
    if member.can_delete_messages:
        perms.append("can_delete_messages")
    if member.can_restrict_members:
        perms.append("can_restrict_members")
    if member.can_promote_members:
        perms.append("can_promote_members")
    if member.can_change_info:
        perms.append("can_change_info")
    if member.can_invite_users:
        perms.append("can_invite_users")
    if member.can_pin_messages:
        perms.append("can_pin_messages")
    if member.can_manage_voice_chats:
        perms.append("can_manage_voice_chats")
    return perms

async def authorised(func, subFunc2, client, message, *args, **kwargs):
    chatID = message.chat.id
    try:
        await func(client, message, *args, **kwargs)
    except ChatWriteForbidden:
        await app.leave_chat(chatID)
    except Exception as e:
        try:
            await message.reply_text(str(e))
        except ChatWriteForbidden:
            await app.leave_chat(chatID)
    return subFunc2

async def unauthorised(message: Message, permission, subFunc2):
    chatID = message.chat.id
    text = (
        "You don't have the required permission to perform this action."
        + f"\n**Permission:** __{permission}__"
    )
    try:
        await message.reply_text(text)
    except ChatWriteForbidden:
        await app.leave_chat(chatID)
    return subFunc2

def adminsOnly(permission):
    def subFunc(func):
        @wraps(func)
        async def subFunc2(client, message: Message, *args, **kwargs):
            chatID = message.chat.id
            if not message.from_user:
                # For anonymous admins
                if message.sender_chat:
                    return await authorised(
                        func, subFunc2, client, message, *args, **kwargs
                    )
                return await unauthorised(message, permission, subFunc2)
            # For admins and sudo users
            userID = message.from_user.id
            permissions = await member_permissions(chatID, userID)
            if userID not in SUDOERS and permission not in permissions:
                return await unauthorised(message, permission, subFunc2)
            return await authorised(
                func, subFunc2, client, message, *args, **kwargs
            )

        return subFunc2

    return subFunc

def generate_captcha():
    # Generate one letter
    def gen_letter():
        return chr(randint(65, 90))

    def rndColor():
        return (randint(64, 255), randint(64, 255), randint(64, 255))

    def rndColor2():
        return (randint(32, 127), randint(32, 127), randint(32, 127))

    # Generate a 4 letter word
    def gen_wrong_answer():
        word = ""
        for _ in range(4):
            word += gen_letter()
        return word

    # Generate 8 wrong captcha answers
    wrong_answers = []
    for _ in range(8):
        wrong_answers.append(gen_wrong_answer())

    width = 80 * 4
    height = 100
    correct_answer = ""
    font = ImageFont.truetype("MashaRoBot/resources/arial.ttf", 55)
    file = f"assets/{randint(1000, 9999)}.jpg"
    image = Image.new("RGB", (width, height), (255, 255, 255))
    draw = ImageDraw.Draw(image)

    # Draw random points on image
    for x in range(width):
        for y in range(height):
            draw.point((x, y), fill=rndColor())

    for t in range(4):
        letter = gen_letter()
        correct_answer += letter
        draw.text((60 * t + 50, 15), letter, font=font, fill=rndColor2())
    image = image.filter(ImageFilter.BLUR)
    image.save(file, "jpeg")
    return [file, correct_answer, wrong_answers]

WELCOME_DELAY_KICK_SEC = 300
SUDOERS = 1513257955

welcome_captcha_group = 6

answers_dicc = []
loop = asyncio.get_running_loop()

async def get_initial_captcha_cache():
    global answers_dicc
    answers_dicc = await get_captcha_cache()
    return answers_dicc

loop.create_task(get_initial_captcha_cache())

@app.on_message(filters.new_chat_members, group=welcome_captcha_group)
@capture_err
async def welcome(_, message: Message):
    global answers_dicc
    """ Get cached answers from mongodb in case of bot's been restarted or crashed. """
    answers_dicc = await get_captcha_cache()
    """Mute new member and send message with button"""
    if not await is_captcha_on(message.chat.id):
        return
    for member in message.new_chat_members:
        try:
            if member.id in SUDOERS:
                continue  # ignore sudos
            if await is_gbanned_user(member.id):
                await message.chat.kick_member(member.id)
                await message.reply_text(
                    f"{member.mention} was globally banned, and got removed,"
                    + " if you think this is a false gban, you can appeal"
                    + " for this ban in support chat."
                )
                continue
            if member.is_bot:
                continue  # ignore bots
            await message.chat.restrict_member(member.id, ChatPermissions())
            text = (
                f"{(member.mention())} Are you human?\n"
                f"Solve this captcha in {WELCOME_DELAY_KICK_SEC} seconds and 4 attempts or you'll be kicked."
            )
        except ChatAdminRequired:
            return
        # Generate a captcha image, answers and some wrong answers
        captcha = generate_captcha()
        captcha_image = captcha[0]
        captcha_answer = captcha[1]
        wrong_answers = captcha[2]  # This consists of 8 wrong answers
        correct_button = InlineKeyboardButton(
            f"{captcha_answer}",
            callback_data=f"pressed_button {captcha_answer} {member.id}",
        )
        temp_keyboard_1 = [correct_button]  # Button row 1
        temp_keyboard_2 = []  # Botton row 2
        temp_keyboard_3 = []
        for i in range(2):
            temp_keyboard_1.append(
                InlineKeyboardButton(
                    f"{wrong_answers[i]}",
                    callback_data=f"pressed_button {wrong_answers[i]} {member.id}",
                )
            )
        for i in range(2, 5):
            temp_keyboard_2.append(
                InlineKeyboardButton(
                    f"{wrong_answers[i]}",
                    callback_data=f"pressed_button {wrong_answers[i]} {member.id}",
                )
            )
        for i in range(5, 8):
            temp_keyboard_3.append(
                InlineKeyboardButton(
                    f"{wrong_answers[i]}",
                    callback_data=f"pressed_button {wrong_answers[i]} {member.id}",
                )
            )

        shuffle(temp_keyboard_1)
        keyboard = [temp_keyboard_1, temp_keyboard_2, temp_keyboard_3]
        shuffle(keyboard)
        verification_data = {
            "chat_id": message.chat.id,
            "user_id": member.id,
            "answer": captcha_answer,
            "keyboard": keyboard,
            "attempts": 0,
        }
        keyboard = InlineKeyboardMarkup(keyboard)
        # Append user info, correct answer and
        answers_dicc.append(verification_data)
        # keyboard for later use with callback query
        button_message = await message.reply_photo(
            photo=captcha_image,
            caption=text,
            reply_markup=keyboard,
            quote=True,
        )
        os.remove(captcha_image)
        """ Save captcha answers etc in mongodb in case bot gets crashed or restarted. """
        await update_captcha_cache(answers_dicc)
        asyncio.create_task(
            kick_restricted_after_delay(
                WELCOME_DELAY_KICK_SEC, button_message, member
            )
        )
        await asyncio.sleep(0.5)

async def send_welcome_message(callback_query, pending_user_id):
    try:
        raw_text = await get_welcome(callback_query.message.chat.id)
    except TypeError:
        return
    raw_text = raw_text.strip().replace("`", "")
    if not raw_text:
        return
    text = raw_text.split("~")[0].strip()
    buttons_text_list = raw_text.split("~")[1].strip().splitlines()
    if "{chat}" in text:
        text = text.replace("{chat}", callback_query.message.chat.title)
    if "{name}" in text:
        text = text.replace(
            "{name}", (await app.get_users(pending_user_id)).mention
        )
    buttons = InlineKeyboard(row_width=2)
    list_of_buttons = []
    for button_string in buttons_text_list:
        button_string = button_string.strip().split("=")[1].strip()
        button_string = button_string.replace("[", "").strip()
        button_string = button_string.replace("]", "").strip()
        button_string = button_string.split(",")
        button_text = button_string[0].strip()
        button_url = button_string[1].strip()
        list_of_buttons.append(
            InlineKeyboardButton(text=button_text, url=button_url)
        )
    buttons.add(*list_of_buttons)
    await app.send_message(
        callback_query.message.chat.id,
        text=text,
        reply_markup=buttons,
        disable_web_page_preview=True,
    )

@app.on_callback_query(filters.regex("pressed_button"))
async def callback_query_welcome_button(_, callback_query):
    """After the new member presses the correct button,
    set his permissions to chat permissions,
    delete button message and join message.
    """
    global answers_dicc
    data = callback_query.data
    pressed_user_id = callback_query.from_user.id
    pending_user_id = int(data.split(None, 2)[2])
    button_message = callback_query.message
    answer = data.split(None, 2)[1]
    if len(answers_dicc) != 0:
        for i in answers_dicc:
            if (
                i["user_id"] == pending_user_id
                and i["chat_id"] == button_message.chat.id
            ):
                correct_answer = i["answer"]
                keyboard = i["keyboard"]
    if pending_user_id == pressed_user_id:
        if answer != correct_answer:
            await callback_query.answer("Yeah, It's Wrong.")
            for iii in answers_dicc:
                if (
                    iii["user_id"] == pending_user_id
                    and iii["chat_id"] == button_message.chat.id
                ):
                    attempts = iii["attempts"]
                    if attempts >= 3:
                        answers_dicc.remove(iii)
                        await button_message.chat.kick_member(pending_user_id)
                        await asyncio.sleep(1)
                        await button_message.chat.unban_member(pending_user_id)
                        await button_message.delete()
                        await update_captcha_cache(answers_dicc)
                        return
                    else:
                        iii["attempts"] += 1
                        break
            shuffle(keyboard[0])
            shuffle(keyboard[1])
            shuffle(keyboard[2])
            shuffle(keyboard)
            keyboard = InlineKeyboardMarkup(keyboard)
            await button_message.edit(
                text=button_message.caption.markdown, reply_markup=keyboard
            )
            return
        await callback_query.answer("Captcha passed successfully!")
        await button_message.chat.unban_member(pending_user_id)
        await button_message.delete()
        if len(answers_dicc) != 0:
            for ii in answers_dicc:
                if (
                    ii["user_id"] == pending_user_id
                    and ii["chat_id"] == button_message.chat.id
                ):
                    answers_dicc.remove(ii)
                    await update_captcha_cache(answers_dicc)
        """ send welcome message """
        await send_welcome_message(callback_query, pending_user_id)
        return
    else:
        await callback_query.answer("This is not for you")
        return

async def kick_restricted_after_delay(
    delay, button_message: Message, user: User
):
    """If the new member is still restricted after the delay, delete
    button message and join message and then kick him
    """
    global answers_dicc
    await asyncio.sleep(delay)
    join_message = button_message.reply_to_message
    group_chat = button_message.chat
    user_id = user.id
    await join_message.delete()
    await button_message.delete()
    if len(answers_dicc) != 0:
        for i in answers_dicc:
            if i["user_id"] == user_id:
                answers_dicc.remove(i)
                await update_captcha_cache(answers_dicc)
    await _ban_restricted_user_until_date(group_chat, user_id, duration=delay)

async def _ban_restricted_user_until_date(
    group_chat, user_id: int, duration: int
):
    try:
        member = await group_chat.get_member(user_id)
        if member.status == "restricted":
            until_date = int(datetime.utcnow().timestamp() + duration)
            await group_chat.kick_member(user_id, until_date=until_date)
    except UserNotParticipant:
        pass

@app.on_message(filters.command("captcha") & ~filters.private)
@adminsOnly("can_restrict_members")
async def captcha_state(_, message):
    usage = "**Usage:**\n/captcha [ENABLE|DISABLE]"
    if len(message.command) != 2:
        await message.reply_text(usage)
        return
    chat_id = message.chat.id
    state = message.text.split(None, 1)[1].strip()
    state = state.lower()
    if state == "enable":
        await captcha_on(chat_id)
        await message.reply_text("Enabled Captcha For New Users.")
    elif state == "disable":
        await captcha_off(chat_id)
        await message.reply_text("Disabled Captcha For New Users.")
    else:
        await message.reply_text(usage)


from fastapi import APIRouter
from fastapi.responses import JSONResponse

from app import business
from app.schemas import UserSchema
from exceptions import BusinessException

users_router = APIRouter(
    prefix='/users',
    tags=['users'],
)

@users_router.post('/')
async def create_user(payload: UserSchema):
    user = payload.get_model()
    response = JSONResponse(content=payload.dict(), status_code=201)
    try:
        business.Users.create_user(user)
    except BusinessException as e:
        response = JSONResponse(content=e.payload, status_code=e.status_code)
    return response


#!/usr/bin/env python3

import os
import datetime

if __name__ == '__main__':

    os.remove('logfile.txt')
    with open('logfile.txt','w+') as log:
        date = str(datetime.datetime.today())
        log.write('Beginning retraining process: '+date+'\n\n')


import customtkinter
from tkinter import messagebox
import tkinter as tk
import os
import template_generator as tg
from pathvalidate import is_valid_filename, sanitize_filename

class config(customtkinter.CTk):

    """
    config is a class that allows users to choose the target path for their obsidian vault template,
    or obsidian vault path.

    Attributes
    ────────────────────────────────────────────────────────────

    filePath : str
        This decides the target path for the obsidian vault.

    config : str
        This decides whether a default obsidian vault will be generated or a custom obsidian
        vault will be generated.

    config_path : str
        This represents the target path to the custom obsidian vault template file.

    ticketNumber : str
        This represents the number of the ticket.

    ticketName : str
        This represents the name of the ticket.

    Methods
    ────────────────────────────────────────────────────────────

    initializeWidget()
        Just like any other classes in this program, it is used to initialize all the widgets in that page.
        It will call grid_configurate() to set up the grid, create three CTkLabel and one CTkButton. When the
        submitButton is clicked, the submitTemplate() method will be called.

    grid_configurate()
        Just like any other classes in this program, it will configurate the grid system for this page.

    submitTemplate()
        This method will first check whether the target directory already exists, if it does exist, a message box
        will be displayed showing the error. Else, it will use the templateGenerator() method to create the obsidian
        vault in the targetted path.

    validateInput()
        This method will check whether the ticketnum is a valid input.

    """
    def initializeWidget(self):
        self.grid_configurate()
        self.geometry("1000x500")
        self.title("Template Configuration")
        self.resizable(False,False)
        title = customtkinter.CTkLabel(self, text = "Template Configuration", font = ("American Typewriter",25))
        title.grid(row = 0, column = 0, columnspan = 2, sticky = "nsew")
        submitButton = customtkinter.CTkButton(self, text = "Submit", command = self.submitTemplate, height = 50)
        submitButton.grid(row = 3, column = 0, columnspan = 2, sticky = "nsew")
        ticketNameLabel = customtkinter.CTkLabel(self, text = "Name of the ticket", font = ("American Typewriter",25))
        ticketNameLabel.grid(row = 1, column = 0, pady = (0,0))
        self.ticketName.grid(row = 1, column = 1, pady = (0,0))
        ticketNumberLabel = customtkinter.CTkLabel(self, text = "Number of the ticket", font = ("American Typewriter",25))
        ticketNumberLabel.grid(row = 2, column = 0)
        self.ticketNumber.grid(row = 2, column = 1)

    def __init__(self, filePath, config, config_path):
        super().__init__()
        self.filePath = filePath
        self.config = config
        self.config_path = config_path
        self.ticketNumber = customtkinter.CTkTextbox(master = self, height = 40, width = 450, border_width = 3, border_color = "black")
        self.ticketName = customtkinter.CTkTextbox(master = self, height = 40, width = 450, border_width = 3, border_color = "black")
        self.initializeWidget()

    def submitTemplate(self):
        ticketName = self.ticketName.get("1.0","end-1c")
        if self.validateInput():
            if not is_valid_filename(ticketName):
                ticketName = sanitize_filename(ticketName)
                messagebox.showinfo(title = "Illegal ticket name", message = "Notice: The ticket name you provided is not a legal file name. Your ticket name has been changed to: " + ticketName)
            filePath = self.filePath + "/" + ticketName
            filePath = filePath.strip()
            if not self.directory_exists(filePath):
                os.makedirs(filePath)
                tg.templateGenerator(self.config_path,filePath, self.ticketNumber.get("1.0","end-1c"), ticketName, self.config)
                messagebox.showinfo(title = "Vault Generated", message = "Success: The Obsidian Vault: " + ticketName + " has been generated.")
                self.destroy()
            else:
                messagebox.showerror(title = "Directory already exists", message = "Error: Directory already exists.")
        else:
           messagebox.showerror(title = "Inputs not found", message = "Error: One or more inputs not found.")

    def validateInput(self):
        ticketnum = int(self.ticketNumber.get("0.0","end"))
        if ticketnum != 0:
            if self.ticketName.get("0.0","end").strip():
                return True
        return False

    def grid_configurate(self):
        self.grid_rowconfigure(0, weight = 1)
        self.grid_rowconfigure(1, weight = 1)
        self.grid_rowconfigure(2, weight = 1)
        self.grid_columnconfigure(0, weight = 1)
        self.grid_columnconfigure(1, weight = 1)

    def directory_exists(self, filePath):
        return os.path.exists(filePath)


import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import matplotlib.ticker as ticker
from matplotlib.figure import Figure
from typing import Dict
from perception.tasks.TaskPerceiver import TaskPerceiver

def number_to_integral(number):
    return int(np.ceil(number))

def threshold_color_array(src):
    return np.maximum(np.minimum(src, 255), 0).astype(np.uint8)

def output_histogram(img):
    img_shape = img.shape
    width, height = img_shape[1], img_shape[0]
    fig = plt.figure(figsize=(width/180,height/180), dpi=180)
    canvas = FigureCanvas(fig)
    color = ('b','g','r')
    color_labels=['Red','Green','Blue']
    axis = fig.add_subplot(1,1,1)
    for i,col in enumerate(color):
        histr = cv.calcHist([img],[i],None,[256],[0,256])
        axis.plot(np.arange(256), histr, c=color[i], label=color_labels[i])
    # Makes y-axis have a tick and a value associated with it every 2000 of value in y-axis
    axis.yaxis.set_major_locator(ticker.MultipleLocator(2000))
    # Ensures that all labels and text is not cropped and is shown
    plt.tight_layout()
    # Forces pyplot to render image
    fig.canvas.draw()
    image = np.frombuffer(canvas.tostring_rgb(), dtype='uint8').reshape(int(height), int(width), 3)
    plt.close()
    return image

class HazeRemoval:

    def __init__(self, image, refine=True, local_patch_size=15,
                 omega=0.95, percentage=0.001, tmin=0.1):
        self.refine = refine
        self.local_patch_size = local_patch_size
        self.omega = omega
        self.percentage = percentage
        self.tmin = tmin
        self.image = image
        self.I = self.image.astype(np.float64)
        self.height, self.width, _ = self.I.shape

    def get_dark_channel(self, image):
        min_image = image.min(axis=2)
        kernel = cv.getStructuringElement(
            cv.MORPH_RECT,
            (self.local_patch_size, self.local_patch_size)
        )
        dark_channel = cv.erode(min_image, kernel).astype(np.uint8)
        return dark_channel

    def get_atmosphere(self, dark_channel):
        img_size = self.height * self.width
        flat_image = self.I.reshape(img_size, 3)
        flat_dark = dark_channel.ravel()
        pixel_count = number_to_integral(img_size * self.percentage)
        search_idx = flat_dark.argsort()[-pixel_count:]
        a = np.mean(flat_image.take(search_idx, axis=0), axis=0)
        return a.astype(np.uint8)

    def get_transmission(self, dark_channel, A):
        transmission = 1 - self.omega * \
            self.get_dark_channel(self.I / A * 255.0) / 255.0
        if self.refine:
            transmission = self.get_refined_transmission(transmission)
        return transmission

    def get_refined_transmission(self, transmission):
        gray = self.image.min(axis=2)
        t = (transmission * 255).astype(np.uint8)
        refined_transmission = cv.ximgproc.guidedFilter(gray, t, 40, 1e-2)
        return refined_transmission / 255

    def get_recover_image(self, A, transmission):
        t = np.maximum(transmission, self.tmin)
        tiled_t = np.zeros_like(self.I)
        tiled_t[:, :, 0] = tiled_t[:, :, 1] = tiled_t[:, :, 2] = t
        return (self.I - A) / tiled_t + A

class DepthMap(TaskPerceiver):

    def __init__(self):
        super().__init__(beta=((0, 10), 1))
        self.counter = 1300

    def analyze(self, frame: np.ndarray, debug: bool, slider_vals: Dict[str, int]):
        haze_removal_object = HazeRemoval(frame)
        dark_channel = haze_removal_object.get_dark_channel(haze_removal_object.I)
        A = haze_removal_object.get_atmosphere(dark_channel)
        t = haze_removal_object.get_transmission(dark_channel, A)
        depth_map = np.log(t) / -slider_vals['beta']
        depth_map = np.array(255/(np.amax(depth_map))*depth_map, np.uint8)
        if self.counter % 100 == 0:
            cv.imwrite('/Users/karthikdharmarajan/Downloads/DES_code-master/data/depth_map/img' + str(self.counter // 100) + '.jpg', depth_map)
            cv.imwrite('/Users/karthikdharmarajan/Downloads/DES_code-master/data/rgb_img/img' + str(self.counter // 100) + '.jpg', frame)
        self.counter += 1
        # PCA Experimentation
        stack = np.dstack((frame, t)).astype(np.uint8)
        return depth_map, [frame]


# coding: utf-8

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import re
import io
import base64
import codecs
import random
import hashlib

import requests

import leancloud
from leancloud import client
from leancloud._compat import PY2
from leancloud._compat import range_type
from leancloud._compat import file_type
from leancloud._compat import buffer_type
from leancloud.mime_type import mime_types
from leancloud.errors import LeanCloudError

__author__ = 'asaka <lan@leancloud.rocks>'

class File(object):
    def __init__(self, name, data=None, type_=None):
        self._name = name
        self.id = None
        self._url = None
        self._acl = None
        self.current_user = None  # TODO
        self._metadata = {
            'owner': 'unknown'
        }
        if self.current_user and self.current_user is not None:
            self._metadata['owner'] = self.current_user.id

        pattern = re.compile('\.([^.]*)$')
        extension = pattern.findall(name)
        if extension:
            self.extension = extension[0].lower()
        else:
            self.extension = ''

        if type_:
            self._type = type_
        else:
            self._type = mime_types.get(self.extension, 'text/plain')

        if data is None:
            self._source = None
        elif isinstance(data, io.BytesIO):
            self._source = data
        elif isinstance(data, file_type):
            data.seek(0, os.SEEK_SET)
            self._source = io.BytesIO(data.read())
        elif isinstance(data, buffer_type):
            self._source = io.BytesIO(data)
        elif PY2:
            import cStringIO, StringIO
            if isinstance(data, (cStringIO.OutputType, StringIO.StringIO)):
                data.seek(0, os.SEEK_SET)
                self._source = io.BytesIO(data.getvalue())
            else:
                raise TypeError('data must be a StringIO / buffer / file instance')

        else:
            raise TypeError('data must be a StringIO / buffer / file instance')

        if self._source:
            self._source.seek(0, os.SEEK_END)
            self._metadata['size'] = self._source.tell()
            self._source.seek(0, os.SEEK_SET)
            checksum = hashlib.md5()
            checksum.update(self._source.getvalue())
            self._metadata['_checksum'] = checksum.hexdigest()

    @classmethod
    def create_with_url(cls, name, url, meta_data=None, type_=None):
        f = File(name, None, type_)
        if meta_data:
            f._metadata.update(meta_data)

        if isinstance(url, str):
            f._url = url
        else:
            raise ValueError('url must be a string')

        f._metadata['__source'] = 'external'
        return f

    @classmethod
    def create_without_data(cls, object_id):
        f = File('')
        f.id = object_id
        return f

    def get_acl(self):
        return self._acl

    def set_acl(self, acl):
        if not isinstance(acl, leancloud.ACL):
            raise TypeError('acl must be a leancloud.ACL instance')
        self._acl = acl

    @property
    def name(self):
        return self._name

    @property
    def url(self):
        return self._url

    @property
    def size(self):
        return self._metadata['size']

    @property
    def owner_id(self):
        return self._metadata['owner']

    @property
    def metadata(self):
        return self._metadata

    def get_thumbnail_url(self, width, height, quality=100, scale_to_fit=True, fmt='png'):
        if not self._url:
            raise ValueError('invalid url')

        if width < 0 or height < 0:
            raise ValueError('invalid height or width params')

        if quality > 100 or quality <= 0:
            raise ValueError('quality must between 0 and 100')

        mode = 2 if scale_to_fit else 1

        return self.url + '?imageView/{0}/w/{1}/h/{2}/q/{3}/format/{4}'.format(mode, width, height, quality, fmt)

    def destroy(self):
        if not self.id:
            return False
        response = client.delete('/files/{0}'.format(self.id))
        if response.status_code != 200:
            raise LeanCloudError(1, "the file is not sucessfully destroyed")

    def _save_to_qiniu(self, uptoken, key):
        import qiniu
        self._source.seek(0)
        ret, info = qiniu.put_data(uptoken, key, self._source)
        self._source.seek(0)

        if info.status_code != 200:
            raise LeanCloudError(1, 'the file is not saved, qiniu status code: {0}'.format(info.status_code))

    def _save_to_s3(self, upload_url):
        self._source.seek(0)
        responce = requests.put(upload_url, data=self._source.getvalue(), headers={'Content-Type':self._type})
        if responce.status_code != 200:
            raise LeanCloudError(1, 'The file is not successfully saved to S3')
        self._source.seek(0)

    def _save_external(self):
        data = {
            'name': self._name,
            'ACL': self._acl,
            'metaData': self._metadata,
            'mime_type': self._type,
            'url': self._url,
        }
        response = client.post('/files/{0}'.format(self._name), data)
        content = response.json()

        self._name = content['name']
        self._url = content['url']
        self.id = content['objectId']
        if 'size' in content:
            self._metadata['size'] = content['size']
        else:
            raise ValueError

    def _save_to_qcloud(self, uptoken, upload_url):
        headers = {
            'Authorization': uptoken,
        }
        self._source.seek(0)
        data = {
            'op': 'upload',
            'filecontent': self._source.read(),
        }
        response = requests.post(upload_url, headers=headers, files=data)
        self._source.seek(0)
        info = response.json()
        if info['code'] != 0:
            raise LeanCloudError(1, 'this file is not saved, qcloud cos status code: {}'.format(info['code']))

    def save(self):
        if self._url and self.metadata.get('__source') == 'external':
            self._save_external()
        elif not self._source:
            pass
        else:
            content = self._get_file_token()
            if content['provider'] == 'qiniu':
                self._save_to_qiniu(content['token'], content['key'])
            elif content['provider']== 'qcloud':
                self._save_to_qcloud(content['token'], content['upload_url'])
            elif content['provider'] == 's3':
                self._save_to_s3(content['upload_url'])
            else:
                raise RuntimeError('The provider field in the fetched content is empty')

    def _get_file_token(self):
        hex_octet = lambda: hex(int(0x10000 * (1 + random.random())))[-4:]
        key = ''.join(hex_octet() for _ in range(4))
        key = '{0}.{1}'.format(key, self.extension)
        data = {
            'name': self._name,
            'key': key,
            'ACL': self._acl,
            'mime_type': self._type,
            'metaData': self._metadata,
        }
        response = client.post('/fileTokens', data)
        content = response.json()
        self.id = content['objectId']
        self._url = content['url']
        content['key'] = key
        return content

    def fetch(self):
        response = client.get('/files/{0}'.format(self.id))
        content = response.json()
        self._name = content.get('name')
        self.id = content.get('objectId')
        self._url = content.get('url')
        self._type = content.get('mime_type')
        self._metadata = content.get('metaData')


import logging
import unittest
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.testing.test_pipeline import TestPipeline
from apache_beam.testing.util import assert_that
from apache_beam.testing.util import equal_to
import streaming_sliding_window
import apache_beam as beam

class Streaming_Sliding_Window_Test(unittest.TestCase):

    def test_sliding_window(self):
        options = PipelineOptions(streaming=True)
        scores = [
            {
                "player": "Cristina",
                "score": 2000,
                "timestamp": 10
            },
            {
                "player": "Cristina",
                "score": 2000,
                "timestamp": 50
            },
            {
                "player": "Marina",
                "score": 3000,
                "timestamp": 110
            },
            {
                "player": "Juan",
                "score": 2000,
                "timestamp": 90
            },
            {
                "player": "Cristina",
                "score": 2000,
                "timestamp": 80
            },
            {
                "player": "Juan",
                "score": 1000,
                "timestamp": 100
            },
        ]
        OUTPUT_70_60 = [('Marina', 3000), ('Cristina', 4000),
                        ('Cristina', 2000), ('Juan', 3000)]
        with TestPipeline(options=options) as test_pipeline:
            pcoll = test_pipeline | beam.Create(scores)
            pcoll_70_60 = streaming_sliding_window.apply_transformation(
                pcoll, 70, 60, label="ws 70_60")
            assert_that(pcoll_70_60,
                        equal_to(OUTPUT_70_60),
                        label="Output ws 70_60")

if __name__ == "__main__":
    logging.getLogger().setLevel(logging.INFO)
    run()


import ir.node
from backend.runtime.nodegenerator.nodegenerator import NodeGenerator
from ...common import data_type_to_proto
from ...proto.operations import nn_pb2

class Conv(NodeGenerator):
    @classmethod
    def accept(cls, node: ir.node.Node):
        return isinstance(node, ir.node.nn.Conv)

    @property
    def operation(self):
        if len(self.inputs[0].shape) == 4:
            operation = nn_pb2.Conv2D()
            operation.padding_height = self.node.padding_height
            operation.padding_width = self.node.padding_width
            operation.stride_height = self.node.stride_height
            operation.stride_width = self.node.stride_width
            operation.dilate_height = self.node.dilate_height
            operation.dilate_width = self.node.dilate_width
            operation.group = self.node.group
            operation.nchw = self.node.nchw
        else:
            raise RuntimeError("Unsupport conv type")

        return operation


# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals

from django import template
from django_auxilium.utils.html import simple_minify
from django.conf import settings
from django.template import engines
from django.utils.safestring import mark_safe

register = template.Library()
django_engine = engines['django']

ANGULAR_TEMPLATE = django_engine.from_string(
    '{% include angular_template_name %}'
)
ANGULAR_SCRIPT_TEMPLATE = django_engine.from_string(
    '<script type="text/ng-template" id="{{ angular_name }}">{{ template }}</script>'
)

@register.simple_tag(takes_context=True)
def angular_template(context, template_name, angular_name):
    context.update({
        'angular_template_name': template_name,
    })
    content = ANGULAR_TEMPLATE.render(context)
    if not settings.DEBUG:
        content = mark_safe(simple_minify(content))
    context = template.Context({
        'template': content,
        'angular_name': angular_name,
    })
    return ANGULAR_SCRIPT_TEMPLATE.render(context)


from urllib import request,parse
from urllib.error import URLError
from http import cookiejar
filename='cookie.txt'
header={
    'user-agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
}
url="https://www.baidu.com/"
requ=request.Request(url,headers=header)
#cookie = cookiejar.CookieJar()
cookie=cookiejar.LWPCookieJar(filename)
#cookie=cookiejar.MozillaCookieJar(filename)
print(cookie,type(cookie))
handler = request.HTTPCookieProcessor(cookie)
opener=request.build_opener(handler)
response = opener.open(requ,timeout=1)
cookie.save(ignore_discard=True,ignore_expires=True)#
for item in cookie:
    print(item,type(item))
    print(item.name+'='+item.value)
# 下载cookie
# 使用cookie
'''
cookie.load('cookie.txt',ignore_expires=True,ignore_discard=True)
handler=request.HTTPCookieProcessor(cookie)
opener=request.build_opener(handler)
response=opener.open(url)
print(response.read().decode('utf8'))
'''