Exemple #1
0
def checker():
    if request.method == 'POST':
        print("Create log folder if not exists...")
        if not os.path.exists(LOG_IMAGES):
            os.makedirs(LOG_IMAGES)

        if 'image' not in request.values:
            message = "no image in req!"
            result = "error"
            print(result, message, sep="-> ")
            # return jsonify({"result" : result, "message" : message, "data" : {}})
        elif 'cid' not in request.values:
            message = "no cid in req!"
            result = "error"
            print(result, message, sep="-> ")
            # return jsonify({"result" : result, "message" : message, "data" : {}})
        else:
            cid = request.values['cid']
            print("cid from req ->", cid)
            today = datetime.datetime.now().strftime("%Y-%m-%d")
            saving_folder = os.path.join(LOG_IMAGES, cid, today)
            if not os.path.exists(saving_folder):
                os.makedirs(saving_folder)
            dest_file = os.path.join(saving_folder, uuid.uuid4().hex + ".jpg")
            print("New log image path ->", dest_file)
            print("Getting the image...")
            b64image = request.values['image']
            with open(dest_file, "wb") as f:
                f.write(base64.decodebytes(b64image.encode()))
            print("Saved at", dest_file)

            print("Recognizing people in new image...")
            print("=== Recognizer Begin ===")
            recognized_students = recognizer.recognize(dest_file)
            print("=== Recognizer End ===")

            # Create a log entry
            print("Creating a Log entry in DB...")
            log_date = datetime.datetime.now().date()
            log_create_time = datetime.datetime.now()
            # find log of cid and created today
            old = Log.query.filter_by(course_id=cid).filter_by(
                date=log_date).first()
            # if there is none then create a new one, if there is a log then update it
            if old is None:
                print("Creating new log...")
                new_log = Log(date=log_date, created_at=log_create_time)
                new_log.course_id = cid
                print("Adding attendees...")
                for std in recognized_students:
                    s = Student.query.filter_by(code=std).first()
                    if s is not None:
                        new_log.attendees.append(s)
                rate = new_log.calculate_present_rate()
                new_log.present_rate = rate
                db.session.add(new_log)
                db.session.commit()
            else:
                print("Update old log...")
                for std in recognized_students:
                    s = Student.query.filter_by(code=std).first()
                    if s is not None:
                        old.attendees.append(s)
                old.created_at = log_create_time
                rate = old.calculate_present_rate()
                old.present_rate = rate
                db.session.commit()
            print("Done! Sending results...")
            result = "ok"
            message = "Found " + str(len(recognized_students)) + " student(s)"
        return jsonify({
            "result": result,
            "message": message,
            "data": recognized_students
        })
    else:
        result = "error"
        message = "Please make a POST request!"
        print(result, message, sep="-> ")
        return jsonify({"result": result, "message": message, "data": {}})