def listG(): query = 'SELECT groups.*, subgroups.subgroupID, subgroups.subgroupName FROM groups LEFT JOIN subgroups ON groups.groupID=subgroups.groupID' # try: cur.execute(query) temp = cur.fetchall() # except Error as E: # return jsonify({'error' : E}) return jsonify(temp)
def enter(): req = request.json query = 'INSERT INTO userinsubgroup VALUES (%s,%s)' try: cur.execute(query, (req['id'], req['subgroup'])) con.commit() except Error as E: return E return "Success"
def addG(): req = request.json name = req['groupname'] query = f"INSERT INTO groups(groupName) VALUES('{name}')" try: cur.execute(query) con.commit() except Error as E: return jsonify({'error': str(E)}) return jsonify({"user": get_jwt_identity(), "result": "Succeed"})
def addSG(): req = request.json name = req['subgroupname'] groupId = req['groupID'] query = 'INSERT INTO subgroups(groupID,subgroupName) VALUES (%s,%s)' try: cur.execute(query, (groupId, name)) con.commit() except Error as E: return jsonify({'error': E}) return "suceed"
def deleteface(): req = request.args try: query2 = f"DELETE FROM images WHERE id={req['id']}" query = f"DELETE FROM encoding WHERE faceID={req['id']}" cur.execute(query2) cur.execute(query) except Error as E: print(E) return 'Err' con.commit() return "Succeed "
def selectFaces(): req = request.args try: query = f"SELECT faceID, faceOwner FROM encoding WHERE subgroupID={req['subgrouid']}" cur.execute(query) except Error as E: print(E) return 'Err' data = cur.fetchall() mylist = [] for i in data: mylist.append(i) return jsonify({'Result': mylist})
def sign(): req = request.json uname = req['userid'] role = req['role'] if role.lower() not in ['superuser', 'user']: return jsonify({'error': "invalid role"}) query = 'INSERT INTO user (username,role) VALUES (%s,%s)' try: cur.execute(query, (uname, role)) con.commit() except Error as E: # print(E) return 500, jsonify({"error": str(E)}) return "succeed"
def recognize(): req = request.form imgfile = request.files['image'] try: query = f"SELECT faceOwner,encodingblob FROM encoding WHERE subgroupID={req['subgroupid']}" cur.execute(query) except Error as E: # print(E) return str(E) x = cur.fetchall() knownEncodings = [] knownNames = [] for i in x: res = compare(imgfile, [fromBlob(i[1])], [i[0]]) if res != 'unknown name': return res return jsonify({'Result': res})
def byteface(): idF = request.args['id'] if client.bucket_exists('facerecimages'): # try: query = f"SELECT path FROM images WHERE id={idF}" cur.execute(query) filetype = cur.fetchall()[0][0] if not filetype: return "id not found" resp = client.get_object('facerecimages', str(idF)) byt = resp.data # print(byt) # KALO MAU NGESAVE JADI FILE LOCAL # stream = BytesIO(byt) # img = Image.open(stream) # img.save('test.jpg') else: return jsonify({"error": "bucket not found"}) return jsonify({"filetype": filetype, "b64data": str(byt)})
def saveFile(idF, imgfile, upload_folder, client): # if allowed(file.name): # filetype = imgfile.name.split('.')[-1] # images tadinya ga kepake, skrg dibuat nyimpen filetype query = "INSERT INTO images(id, faceOwner, path) VALUES(%s,%s,%s)" cur.execute(query,(idF,"",imgfile.filename.split('.')[-1])) con.commit() bytestr = imgfile.read() # stream = BytesIO(bytestr) streamdata = b64encode(str(bytestr).encode('utf-8')) stream = BytesIO(streamdata) # stream = StringIO(streamdata) if client.bucket_exists("facerecimages"): pass else: client.make_bucket("facerecimages") client.put_object("facerecimages", str(idF), stream, len(bytestr)) # imgfile.save(path) # else: # raise jsonify({"Error" : })
def enroll(): # role = 'Superuser' req = request.form print(req) user = req['userid'] imgfile = request.files['image'] # print(imgfile.filename) # filetype = req['filename'].split('.')[-1] try: query = f"SELECT role FROM user WHERE username='******'" cur.execute(query) except Error as E: return str(E) role = cur.fetchall() if role[0][0] != 'superuser': return jsonify({"error": 401, "reason": "Invalid role"}) query = 'INSERT INTO encoding (subgroupID, faceOwner, encodingblob) VALUES (%s,%s,%s)' query2 = "UPDATE encoding SET encodingblob=%s WHERE faceID=%s" try: cur.execute(query, (req['subgroupid'], req['name'], "")) lastid = cur.lastrowid saveFile(lastid, imgfile, UPLOAD_FOLDER, client) imgfile.seek(0) x = makeBlob(imgfile) cur.execute(query2, (x, lastid)) con.commit() return jsonify({"last face id": lastid}) except Error as E: return jsonify({'error': str(E)})