def get_all_visitor_photos(): ''' Get photos of all visitors whose status is 0 ''' logger.info("Trying to fetch all visitor images") cnx = get_connection() try: cursor = cnx.cursor() query = ''' select v.id, i.image_data from visitor v, images i where v.status = 0 and v.uploaded_photo = i.image_id; ''' cursor.execute(query) res = cursor.fetchall() resp = [] for row in res: resp.append(row) cursor.close() cnx.close() except Exception as ex: logger.exception(str(ex)) raise Exception("Some thing went wrong") return resp
def approve_request(): ''' Show welcome message ! ''' # message = {"images" : sql_utils.get_photos(id)} # resp = make_response(json.dumps(message), config.HTTP_STATUS_OK) # resp.headers['Content-Type'] = 'application/json' # return resp logger.info("Processing request for approve request") status = config.HTTP_STATUS_OK result = {"success": "request approved"} try: code = request.args.get('code') visitor_id = int(EM.decrypt(code)) sql_utils.approve_request(visitor_id) except Exception as ex: logger.exception("Something went wrong") result = {"error": str(ex)} status = config.HTTP_STATUS_ERROR resp = make_response(json.dumps(result), status) resp.headers['Content-Type'] = 'application/json' return resp
def add_photo(img): cnx = get_connection() image_id = 0 try: cursor = cnx.cursor() cursor.execute("SELECT max(image_id) FROM images ") res = cursor.fetchone() image_id = res[0] + 1 cursor.close() # print("Image id :", image_id) # print(text) cursor = cnx.cursor() cursor.execute( "INSERT INTO images (image_id,image_data) VALUES (%s ,%s)", (image_id, img)) cnx.commit() cursor.close() cnx.close() except Exception as ex: logger.exception(str(ex)) raise Exception("Some thing went wrong") return image_id
def upload_photo(): logger.info("Processing request for upload-photo") status = config.HTTP_STATUS_OK try: file_list = list(request.files.keys()) num_files = len(file_list) logger.info(file_list) if num_files == 0: raise Exception( "Needs 1 image file but received {} files".format(num_files)) elif len(file_list) > 1: err_msg = "More than 2 files uploaded. Got {} files".format(num_files) raise Exception(err_msg) # Get POST parameters img = cv2.imdecode(np.fromstring(request.files[file_list[0]].read(), np.uint8), cv2.IMREAD_COLOR) # img = image_utils.read_b64(image_text) cropped_face = FM.get_face_from_image(img) base64_image = image_utils.image_to_b64(cropped_face) image_id = sql_utils.add_photo(base64_image) result={"image_id": image_id, "image_text":base64_image} except Exception as ex: logger.exception("Something went wrong") result = {"error" : str(ex)} status = config.HTTP_STATUS_ERROR resp = make_response(json.dumps(result), status ) resp.headers['Content-Type'] = 'application/json' return resp
def upload_photo_b64(): logger.info("Processing request for upload-photo-b64") status = config.HTTP_STATUS_OK try: if(request.headers['Content-Type'] != 'application/json'): return make_response('{"error":"unsupported content type"}', config.HTTP_STATUS_ERROR) # Get POST parameters input_json = request.json image_text = input_json["image"] img = image_utils.read_b64(image_text) cropped_face = FM.get_face_from_image(img) base64_image = image_utils.image_to_b64(cropped_face) image_id = sql_utils.add_photo(base64_image) result={"image_id": image_id, "image_text":base64_image} except Exception as ex: logger.exception("Something went wrong") result = {"error" : str(ex)} status = config.HTTP_STATUS_ERROR resp = make_response(json.dumps(result), status ) resp.headers['Content-Type'] = 'application/json' return resp
def face_similarity_endpoint(): ''' Check similaity of faces in two images ''' logger.info("Processing request for face similarity endpoint") status = config.HTTP_STATUS_OK try: message = do_post(request) except Exception as ex: logger.exception("Something went wrong !") message = {"error": str(ex)} status = config.HTTP_STATUS_ERROR resp = make_response(json.dumps(message), status) resp.headers['Content-Type'] = 'application/json' return resp
def find_visitor_by_face(): ''' Find a visitor by face ''' logger.info("Processing request for find-visitor-by-face endpoint") status = config.HTTP_STATUS_OK try: message = do_post(request) except Exception as ex: logger.exception("Something went wrong !") message = {"error": str(ex)} status = config.HTTP_STATUS_ERROR resp = make_response(json.dumps(message), status) resp.headers['Content-Type'] = 'application/json' return resp
def get_connection(): logger.info("Trying to connect to MySQL database") cnx = None try: uid = os.environ.get(config.SQL_SERVER_USER_ID) pwd = os.environ.get(config.SQL_SERVER_USER_PWD) # print(uid, pwd) cnx = mysql.connector.connect(host=config.SQL_SERVER_IP, port=config.SQL_SERVER_PORT, user=uid, password=pwd, database=config.DB_NAME) except Exception: logger.exception("SQL Connection failed") exit(1) return cnx
def approve_request(visitor_id): ''' Set visitor status to approved ''' logger.info("Trying to approve visitor request") cnx = get_connection() try: cursor = cnx.cursor() query = '''update vms.visitor set status = 0 where id = %s ''' cursor.execute(query, (visitor_id, )) cursor.close() cnx.close() except Exception as ex: logger.exception(str(ex)) raise Exception("Could not approve this request") return "sucess"
def get_photos(id=0): logger.info("fetching image with id %s", id) cnx = get_connection() try: cursor = cnx.cursor() cursor.execute( "SELECT image_id,image_data FROM images where image_id=%s", (int(id), )) res = cursor.fetchall() resp = [] for row in res: resp.append(row) cursor.close() cnx.close() except Exception as ex: logger.exception(str(ex)) raise Exception("Some thing went wrong") return resp
def generate_code(): logger.info("Processing request for gerete code") status = config.HTTP_STATUS_OK try: if (request.headers['Content-Type'] != 'application/json'): return make_response('{"error":"unsupported content type"}', config.HTTP_STATUS_ERROR) # Get POST parameters input_json = request.json plain_text = input_json["plain_text"] cipher_text = EM.encrypt(plain_text) result = {"cipher_text": cipher_text} except Exception as ex: logger.exception("Something went wrong") result = {"error": str(ex)} status = config.HTTP_STATUS_ERROR resp = make_response(json.dumps(result), status) resp.headers['Content-Type'] = 'application/json' return resp