Beispiel #1
0
def register():
    if flask_request.method == 'POST':
        username = flask_request.form['username_field']
        password = flask_request.form['password_field']
        netID = flask_request.form['network_id_field']
        hubID = flask_request.form['hub_id_field']
        acu_id = flask_request.form['acu_id_field']
        access_key = flask_request.form['access_key_field']

        #send username and password information to CoMPES

        db = DBHelper()
        dbSuccess = db.createUser(
            [username, password, hubID, netID, acu_id, access_key])

        if (dbSuccess):
            #sign in to CoMPES
            #process items from CoMPES
            #register face

            global comms
            comms.registerUser(username, password, netID, hubID, acu_id,
                               access_key)
            comms.cam_factory.rec.is_registering = True
            resp = make_response(render_template('profile.html',
                                                 user=username))
            return resp
        else:
            #display message on webpage and have the user try again.
            return "failed register"
    else:
        return render_template("register.html")
Beispiel #2
0
def connect():
    #process users credentials here.

    db = DBHelper(True)  #close the connection in this function.

    username = flask_request.form['username_field']
    password = flask_request.form['password_field']

    authSuccess = db.authenticate([username, password])

    if (authSuccess):
        #make the connection to compess here.

        hub_id = db.getHubIdByUsername(username)
        net_id = db.getNetIdByUsername(username)
        acu_id = db.getACUByUsername(username)
        access_key = db.getAccessKeyByUsername(username)

        comms.registerUser(username, password, net_id, hub_id, acu_id,
                           access_key)
        #comms.web_factory.rec.is_registering = True

        resp = make_response(render_template('active.html', user=username))
        return resp
    else:
        return render_template(
            'index.html', message="Failed to authenticate. Please try again. ")

    db.disconnect()
Beispiel #3
0
    def _register(self, frame, gray, username):
        """Detects faces in frames and uses them to train a face recognition algorithm.
            Face data is associated with the given username.
            :param frame: an 8 bit, 3-channel UMat object
            :param gray: an 8-bit, 1-channel ndarray
            :param username: name of registering user
        """
        if self.samples < self.sample_size:

            # Find all faces and store only the location of the largest (closest to the camera)
            faces = self._findFaces(frame)
            max_area = 0
            x = 0
            y = 0
            w = 0
            h = 0
            for (startX, startY, endX, endY) in faces:
                c_w = endX - startX
                c_h = endY - startY
                if c_w * c_h > max_area:
                    x = startX
                    y = startY
                    w = c_w
                    h = c_h
                    maxArea = w * h

            # Resize and add face image to list for training
            if faces:
                self.samples += 1
                gray = cv2.UMat(gray, [y, y + h], [x, x + w])
                gray = cv2.resize(gray, (100, 100))
                self.sample_images.append(gray)

        else:
            # Finished collecting face data
            # Associate registering user id with training data
            db = DBHelper()
            user_id = db.getIDByUsername(username)
            id_array = [user_id] * self.sample_size

            for i in range(self.sample_size):
                self.sample_images[i] = cv2.UMat.get(self.sample_images[i])

# Update or create new face recognizer
            if Path('./training_data/recognizer.yml').is_file():
                self.recognizer.update(self.sample_images, np.array(id_array))
            else:
                self.recognizer.train(self.sample_images, np.array(id_array))
            self.recognizer.write('./training_data/recognizer.yml')

            # registration complete
            self.reg_complete = True
            self.rec_trained = True

            # reset variables before detection begins
            self._reset()

        return frame
Beispiel #4
0
def associations():
    if flask_request.method == 'POST':
        try:
            #register the associations here.
            gestureDict = {}
            gestureDict['gest_1'] = flask_request.form['gest_1']
            gestureDict['gest_2'] = flask_request.form['gest_2']
            gestureDict['gest_3'] = flask_request.form['gest_3']
            gestureDict['gest_4'] = flask_request.form['gest_4']
            gestureDict['gest_5'] = flask_request.form['gest_5']

            print(str(gestureDict))
            print(comms.user)

            db = DBHelper(True)  #open in passive mode
            for key in gestureDict:
                db.addGesture(comms.user, key, gestureDict[key])
            db.disconnect()
            return render_template('active.html')
        except:
            return "Error processing form. "

    else:
        SRO = comms.getSRO()
        states = ujson.loads(SRO)['lab-cam']['States']
        states_str = ','.join(states)
        db = DBHelper(True)
        current_states = []
        for x in range(1, 6):
            gest = db.getGestureFunction(comms.user, 'gest_' + str(x))
            current_states.append(
                gest) if gest != None else current_states.append('')
        current_states = ','.join(current_states)

        resp = make_response(
            render_template('associations.html',
                            default=states_str,
                            data=current_states))
        return resp
Beispiel #5
0
def check_deadline(update, context):
    db = DBHelper(CONFIG["db_name"])
    db.clear_old_deadlines()
    dls = db.fetch_deadlines(update.effective_chat.id)
    header = None
    message_string = "<b>Deadlines</b>\n"
    for _, dl in dls:
        if header != dl.friendly_date():
            header = dl.friendly_date()
            message_string += "\n<b>{}</b>\n".format(header)
        message_string += html.escape(str(dl))
        message_string += "\n"
    delete_old_listings(update, context)
    m = update.message.reply_html(message_string)
    add_old_listing(update, context, m)
Beispiel #6
0
def new_deadline(update, context):
    """Insert new deadline into deadline list"""
    db = DBHelper(CONFIG["db_name"])
    args = " ".join(context.args)
    try:
        dl = deadline.Deadline.parse(args)
        if dl.in_how_many_days() < -1:
            update.message.reply_text(
                "You're a bit too late to submit something on {}".format(
                    dl.time.strftime("%d %b %Y, %H:%M:%S")))
        else:
            # actually handle the deadline
            db.insert_deadline(update.effective_chat.id, dl)
            update.message.reply_text("Succesfully added {}".format(str(dl)))
    except ValueError as e:
        update.message.reply_text(str(e))
Beispiel #7
0
def connect():
    #process users credentials here.
    db = DBHelper(True)  #close the connection in this function.

    username = flask_request.form['username_field']
    password = flask_request.form['password_field']

    authSuccess = True  #db.authenticate([username, password])

    if (authSuccess):
        return redirect(url_for('active'))
    else:
        return render_template(
            'index.html', message="Failed to authenticate. Please try again. ")

    db.disconnect()
Beispiel #8
0
def remove_deadline_by_index(update, context):
    db = DBHelper(CONFIG["db_name"])
    delete_old_listings(update, context)
    text = re.sub(r"/(\d+)(@{})?$".format(re.escape(context.bot.username)),
                  r"\g<1>", update.message.text)
    try:
        index = int(text)
        dl_id, dl = context.user_data["remove_list"][index]
        db.delete_deadline(dl_id)
        update.message.reply_text(str(dl) + " removed!")
        check_deadline(update, context)
    except KeyError as ke:
        update.message.reply_text(str(index) + " not in list!")
    except ValueError as ve:
        update.message.reply_text(text + " is not a valid integer!")
    context.user_data["remove_list"] = None
    return -1
Beispiel #9
0
def start_remove_deadline(update, context):
    db = DBHelper(CONFIG["db_name"])
    db.clear_old_deadlines()
    dls = db.fetch_deadlines(update.effective_chat.id)
    header = None
    message_string = "<b>Deadlines</b>\n"
    context.user_data["remove_list"] = {}
    for i, (dl_id, dl) in enumerate(dls):
        if header != dl.friendly_date():
            header = dl.friendly_date()
            message_string += "\n<b>{}</b>\n".format(header)
        message_string += "/{} {}".format(i + 1, html.escape(str(dl)))
        context.user_data["remove_list"][i + 1] = (dl_id, dl)
        message_string += "\n"
    m = update.message.reply_html(message_string)
    delete_old_listings(update, context)
    add_old_listing(update, context, m)
    return CHOOSING
Beispiel #10
0
def register():
    if flask_request.method == 'POST':
        username = flask_request.form['username_field']
        password = flask_request.form['password_field']

        db = DBHelper()
        dbSuccess = db.createUser([username, password])

        if (dbSuccess):
            global comms
            comms.registerUser(username, password)
            #comms.cam_factory.rec.is_registering = True
            resp = make_response(render_template('profile.html',
                                                 user=username))
            return resp
        else:
            #display message on webpage and have the user try again.
            return "failed register"
    else:
        return render_template("register.html")
Beispiel #11
0
def main():
    """Start the bot."""
    # Create the Updater and pass it your bot's token.
    # Make sure to set use_context=True to use the new context based callbacks
    # Post version 12 this will no longer be necessary

    db = DBHelper(CONFIG["db_name"])
    db.setup()

    updater = Updater(CONFIG["bot_token"], use_context=True)

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("newdl", new_deadline))
    dp.add_handler(CommandHandler("chkdl", check_deadline))
    dp.add_handler(
        ConversationHandler(
            entry_points=[CommandHandler("remdl", start_remove_deadline)],
            states={
                CHOOSING: [
                    MessageHandler(Filters.regex(r"^/\d+(@shimekiribot)?$"),
                                   remove_deadline_by_index)
                ]
            },
            fallbacks=[]))

    # log all errors
    dp.add_error_handler(error)

    # Start the Bot
    updater.start_polling()

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()
Beispiel #12
0
    def onMessage(self, data, isBinary):
        """
                Description: Decodes the image sent from the camera 
                """
        #STEP 1: Load in, convert, and decompress frame for use
        frame = ujson.loads(data.decode("utf8"))
        frame = np.asarray(frame, np.uint8)
        frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
        #post users client name here.

        #frame = message

        if (self.factory.bridge.user is not None):
            print(self.factory.bridge.user)
            frame, username, gesture = self.factory.rec.processFrame(
                frame, self.factory.bridge.user)
            if (gesture != '0'):  #gesture is '0' by default
                db = DBHelper(True)
                gest_func = db.getGestureFunction(username,
                                                  "gest_" + str(gesture))

                acu = db.getACUByUsername(username)
                tag = acu + ",," + str(gest_func)
                if gest_func != None:
                    self.factory.bridge.sendTag(tag)

        if (self.factory.rec.is_registering == False
                and self.factory.rec.reg_complete == True):
            self.factory.bridge.web_factory.connections["client1"].sendMessage(
                "registration".encode("UTF8"))
        frame = cv2.UMat(frame)
        frame = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 20])[1]
        frame = base64.b64encode(frame)

        #send to web factory
        self.factory.bridge.web_factory.post(self.factory.bridge.user, frame)
Beispiel #13
0
import config
from flask import Flask, request, Response, json
from database import DBHelper, DBHelperError
from datetime import date, datetime

app = Flask(__name__)
db_helper = DBHelper(**config.get_db_requisites())
logs_dir_path = config.get_logs_dir_path()


@app.route('/imports', methods=['POST'])
def import_data():
    try:
        import_id = db_helper.import_citizens(request.json)
    except DBHelperError as e:
        return Response(response=str(e), status=400)
    else:
        return Response(response=json.dumps({"data": {"import_id": import_id}}),
                        status=201,
                        mimetype='application/json')


@app.route('/imports/<int:import_id>/citizens/<int:citizen_id>', methods=['PATCH'])
def change_citizen_data(import_id, citizen_id):
    try:
        citizen_data = db_helper.change_citizen(import_id, citizen_id, request.json)
    except DBHelperError as e:
        return Response(response=str(e), status=400)
    else:
        return Response(response=json.dumps({"data": citizen_data}),
                        status=200,
Beispiel #14
0
    def _detect(self, frame, gray):
        """Detects faces, compares them registered faces, and detects hands for gesture
            recognition if a match is found.
            :param frame: a BGR color image for display
            :param gray: a grayscale copy of the passed BGR frame
            :returns: (out_frame, username, gesture) the processed frame
                for display on webpage, the detected user, the detected gesture
        """
        username = ""
        gesture = "0"
        num_fingers = 0

        if self.gesture_tracker is None:  # not currently tracking hands
            faces = self._findFaces(frame)

            for (startX, startY, endX, endY) in faces:
                # text is displayed at y coordinate
                y = startY - 10 if startY - 10 > 10 else startY + 10

                # gray_face sometimes gets converted back to ndarray, throwing an error
                # I do not know why
                try:
                    gray_face = cv2.UMat(gray, [startY, endY], [startX, endX])
                except:
                    gray_face = gray[startY:endY, startX:endX]

                # optional resize for slightly improved performance
                gray_face = cv2.resize(gray_face, (100, 100))
                user_id, confidence = self.recognizer.predict(gray_face)
                gray = cv2.UMat.get(gray)

                # mask detected face region with solid black to avoid false positives in hand detection
                gray[startY:endY, startX:endX] = self.black_mask[startY:endY,
                                                                 startX:endX]

                # for LBPH recognizer, lower confidence scores indicate better results
                if confidence <= 80:  # user is recognized
                    db = DBHelper()
                    username = db.getUsernameById(user_id)
                    cv2.putText(frame, username, (startX, y), self.font, .6,
                                (225, 105, 65), 2)
                else:
                    # face belongs to unknown user
                    cv2.putText(frame, "unknown", (startX, y), self.font, .6,
                                (0, 0, 255), 2)

            # a user is recognized and hand detection begins
            if username is not "" and faces:
                hands = self.hand_classifier.detectMultiScale(gray, 1.3, 5)

                # detected hand region is resized to allow for tracking an open hand
                for (x, y, w, h) in hands:
                    x_mid = (w // 2)
                    y = int(y - h * 1.3)
                    x = int(x - x_mid * 1.5)
                    w = int(w + 3 * x_mid)
                    h = int(h * 2 + h * 0.7)
                    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255),
                                  2)

                    # only attempt to recognize hand gesture if background model is finished calibrating
                    if self.bg_model.calibrated:
                        self.gesture_tracker = GestureTracker(
                            frame, (x, y, w, h))

            # if no faces are in the frame, assume the frame is background
            if not self.bg_model.calibrated and not faces:
                self.bg_model.runAverage(frame)

        else:  # hand has been detected and is being tracked by gesture_tracker
            timed_out, (x, y, w, h) = self.gesture_tracker.update(frame)
            if timed_out:
                self.gesture_tracker = None
            try:
                gray = cv2.UMat.get(gray)
                difference = cv2.absdiff(
                    self.bg_model.background.astype("uint8")[y:y + h, x:x + w],
                    gray[y:y + h, x:x + w])
                foreground = cv2.threshold(difference, 25, 255,
                                           cv2.THRESH_BINARY)[1]
                gest, frame[y:y + h, x:x +
                            w] = self.gesture_recognizer.recognize(foreground)
                self.last_gest = str(gest)
            except:
                pass

        return (frame, username, gesture)
Beispiel #15
0
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        gray = cv2.equalizeHist(gray)
        gray = cv2.GaussianBlur(gray, (9, 9), 0)
        if self.num_frames < 30:
            if self.background is None:
                self.background = gray.copy().astype("float")
            cv2.accumulateWeighted(gray, self.background, 0.5)
            self.num_frames += 1
        else:
            self.calibrated = True


if __name__ == "__main__":

    rec = Recognition()
    db = DBHelper()
    cam = WebcamVideoStream(src=0).start()
    user = ""

    response = input("Register new user? y or n \n")
    if response == 'y':
        rec.is_registering = True
        user = input("Enter a username: "******"", "", "", "", ""])
    else:
        rec.is_registering = False
    while (True):
        frame = cam.read()
        frame = cv2.resize(frame, (640, 480))
        out, user, gest = rec.processFrame(frame, user)
        cv2.imshow("out", out)
Beispiel #16
0
import requests
import time
import datetime as DT
from database import DBHelper
from matplotlib import pyplot as plt
import pandas as pd
import random
from config import CONFIG
import re
import os

CURRENCY_FROM = 0
AMOUNT_FROM = 1
CURRENCY_TO = -1
CURRENCY_PERIOD = 2
db = DBHelper(CONFIG["db_name"])


def get_list_url():
    r = requests.get('https://api.exchangeratesapi.io/latest?base=USD')
    r.raise_for_status()
    content = r.json()
    return content['rates']


def get_history_url(period, symbols):
    today = DT.date.today()
    end_at = today.strftime("%Y-%m-%d")
    start_at = (today - DT.timedelta(days=period)).strftime("%Y-%m-%d")
    r = requests.get(
        'https://api.exchangeratesapi.io/history?start_at={0}&end_at={1}&base=USD&symbols={2}'