def new_user(): """ Add the user to the db. :return: """ userid = request.json.get('userid') password = request.json.get('password') company_id = request.json.get('companyid') # verify the existence of parameters utils.verify_param(abort, error_code=400, userid=userid, password=password, company_id=company_id) if User.query.filter_by(userid=userid).first() is not None: abort(403) # existing user user = User(userid=userid, company_id=company_id) user.hash_password(password) db.session.add(user) db.session.commit() return (jsonify({ 'userid': user.userid, 'company_id': user.company_id }), 201, { 'Location': url_for('get_user', userid=user.userid, _external=True) })
def verify_cow_exists(): """ verify the existence of cow :return: """ user_id = g.user.userid company_id = request.json.get('companyid') rfid_code = request.json.get('rfidcode') utils.verify_param(abort, logger, error_code=400, user_id=user_id, company_id=company_id, rfid_code=rfid_code, method_name="verify_cow_exists") if Archives.query.filter_by(rfid_code=rfid_code, company_id=company_id).first(): logger.info( "cow rfid_code={} from company_id={} already exist in the Archives" .format(rfid_code, company_id)) result = True else: logger.info( "cow rfid_code={} from company_id={} not exist in the Archives". format(rfid_code, company_id)) result = False return jsonify({ 'userid': user_id, 'companyid': company_id, 'rfidcode': rfid_code, 'result': result })
def new_user(): """ Add the user to the db. :return: """ userid = request.json.get('userid') password = request.json.get('password') company_id = request.json.get('companyid') # verify the existence of parameters utils.verify_param(abort, logger, error_code=400, userid=userid, password=password, company_id=company_id, method_name="new_user") if User.query.filter_by(userid=userid).first() is not None: logger.error("userid = {} already exists and cannot be added repeatedly".format(userid)) abort(403) # existing user user = User(userid=userid, company_id=company_id) user.hash_password(password) try: db.session.add(user) db.session.commit() logger.info("Database insert user(userid={},company_id={}) succeeded".format(userid, company_id)) except: logger.error("failure to store user(userid={},company_id={}) to database".format(userid, company_id)) abort(502) return (jsonify({'userid': user.userid, 'company_id': user.company_id}), 201, {'Location': url_for('get_user', userid=user.userid, _external=True)})
def list_detail(): """ Preview the picture of cow :return: image base64 """ company_id = request.json.get('companyid') rfid_code = request.json.get('rfidcode') # verify the existence of parameters utils.verify_param(abort, logger, error_code=400, company_id=company_id, rfid_code=rfid_code, method_name="list_detail") pic_path = os.path.join(config.base_images_path, company_id, rfid_code) + os.sep pre_files = glob.glob(pic_path + '*.jpg') pic_num = len(pre_files) if pic_num > 0: step = pic_num // 10 if step == 0: step = 1 # Get images and encode them to base64 pre_items = utils.read_image_to_base64(pre_files[0:pic_num:step]) return jsonify({ "items": pre_items }) else: logger.error("Folder {} not exist or No picture".format(pic_path)) abort(404)
def login_app(): """ User login :return: """ userid = request.json.get('un') password = request.json.get('ps') accessToken = request.json.get('accessToken') # verify the existence of parameters utils.verify_param(abort, logger, error_code=400, userid=userid, password=password, accessToken=accessToken, method_name="login_app") # AES Decrypt key = cache.get(accessToken).encode("utf-8") userid = utils.decrypt_aes(key, userid) password = utils.decrypt_aes(key, password) user = User.query.filter_by(userid=userid).first() if user and user.verify_password(password): newAccessToken = uuid.uuid1() cache.set(newAccessToken, "{}_{}".format(userid, password), ttl=600) return jsonify({ 'url': "http://101.201.121.61:6788?userName={}&userId={}&accessToken={}". format(userid, userid, newAccessToken), 'companyid': user.company_id }) else: abort(400)
def verify_cow_exists(): """ verify the existence of cow :return: """ user_id = g.user.userid company_id = request.json.get('companyid') rfid_code = request.json.get('rfidcode') utils.verify_param(abort, error_code=400, user_id=user_id, company_id=company_id, rfid_code=rfid_code) if Archives.query.filter_by(rfid_code=rfid_code, company_id=company_id).first(): result = True else: result = False return jsonify({ 'userid': user_id, 'companyid': company_id, 'rfid_code': rfid_code, 'result': result })
def login(): """ User login :return: companyid """ userid = request.json.get('userid') password = request.json.get('password') # verify the existence of parameters utils.verify_param(abort, logger, error_code=400, userid=userid, password=password, method_name="login") user = User.query.filter_by(userid=userid).first() if user: if user.verify_password(password): # If the userid and password is correct, return company_id company_id = User.query.filter_by(userid=userid).first().company_id else: # returned to empty incorrectly company_id = "" else: company_id = "" return jsonify({'companyid': company_id})
def cow_list_by_company_id(): """ Query the cows of the corresponding company :return: cow_list """ def json_serilize(instance): """ change the returned data to dict :param instance: data :return: message dict """ # Get registered images and encode them to base64 pre_files = utils.get_files( os.path.join(config.base_images_path, instance.company_id, instance.rfid_code) + os.sep, 1) pre_items = utils.read_image_to_base64(pre_files) return { "userid": g.user.userid, "aid": instance.aid, "rfidcode": instance.rfid_code, "age": instance.age, "companyid": instance.company_id, "gather_time": str(instance.gather_time), "health_status": instance.health_status, "extra_info": instance.extra_info, "items": pre_items } current_page = request.json.get("currentpage") cow_number = request.json.get("cownumber") company_id = request.json.get("companyid") utils.verify_param(abort, logger, error_code=400, company_id=company_id, method_name="cow_list_by_company_id") try: # get a list of cowrest based on the current page number and display number if current_page and cow_number: cow_list = Archives.query.filter_by( company_id=company_id).paginate(page=current_page, per_page=cow_number).items else: # return all cows list without current page or display number cow_list = Archives.query.filter_by(company_id=company_id).all() return jsonify({ "status": "200", "data": json.loads(json.dumps(cow_list, default=json_serilize)), "message": "Cow archives" }) except: logger.error("Database query cow list failed") abort(502)
def get_user(userid): """ Get the user by id. :param userid: user ID :return: userid """ user = User.query.get(userid) utils.verify_param(abort, error_code=502, user=user) return jsonify({'userid': user.userid})
def delete_pic(): """ Delete folders with poor image quality :return: """ company_id = request.json.get('companyid') rfid_code = request.json.get('rfidcode') # verify the existence of parameters utils.verify_param(abort, logger, error_code=400, company_id=company_id, rfid_code=rfid_code, method_name="delete_pic") user_id = g.user.userid # Verify that userid and companyid are correct if User.query.filter_by(userid=user_id).first().company_id != company_id: result = False else: cow = Archives.query.filter_by(rfid_code=rfid_code, company_id=company_id).first() cow2 = LogInfo.query.filter_by(rfid_code=rfid_code, company_id=company_id).first() folder_path = os.path.join(config.base_images_path, company_id, rfid_code) + os.sep if cow and cow2: try: db.session.delete(cow) db.session.delete(cow2) shutil.rmtree(folder_path) logger.info( "cow rfid_code={} from company_id={} Delete successful". format(rfid_code, company_id)) result = True db.session.commit() except: # If error rollback db.session.rollback() logger.error( "cow rfid_code={} from company_id={} Delete failed".format( rfid_code, company_id)) result = False else: logger.info( "cow rfid_code={} from company_id={} not exist in the Database" .format(rfid_code, company_id)) result = False return jsonify({ 'companyid': company_id, 'rfidcode': rfid_code, 'result': result })
def get_token(did): """ get token :return: token message and duration time """ utils.verify_param(abort, logger, error_code=400, did=did, method_name="get_token") token = uuid.uuid4().hex[0:16] cache.set(did, token, ttl=600) return jsonify({did: token})
def loginH5(): """ User login :return: companyid """ accessToken = request.json.get('accessToken') # verify the existence of parameters utils.verify_param(abort, logger, error_code=400, accessToken=accessToken, method_name="loginH5") user = cache.get(accessToken) userid = user.split("_")[0] password = user.split("_")[1] user = User.query.filter_by(userid=userid).first() if user and user.verify_password(password): return redirect(url_for("http://101.201.121.61:6788")) else: abort(400)
def cow_list_by_company_id(): """ Query the cows of the corresponding company :return: cow_list """ def json_serilize(instance): """ change the returned data to dict :param instance: data :return: message dict """ return { "userid": g.user.userid, "aid": instance.aid, "rfid_code": instance.rfid_code, "age": instance.age, "company_id": instance.company_id, "gather_time": instance.gather_time, "health_status": instance.health_status, "extra_info": instance.extra_info, "folder_path": instance.folder_path } current_page = request.json.get("currentpage") cow_number = request.json.get("cownumber") company_id = request.json.get("companyid") utils.verify_param(abort, error_code=400, company_id=company_id) try: # get a list of cowrest based on the current page number and display number if current_page and cow_number: cow_list = Archives.query.filter_by( company_id=company_id).paginate(page=current_page, per_page=cow_number).items else: # return all cows list without current page or display number cow_list = Archives.query.filter_by(company_id=company_id).all() return json.dumps(cow_list, default=json_serilize) except: abort(502)
def dead(): """ Video of dead cow :return: """ # get the params first user_id = g.user.userid entity = request.form.get('entity') utils.verify_param(abort, logger, error_code=400, entity=entity, method_name="dead") json_obj = json.loads(entity) company_id = json_obj.get('companyid') gather_time1 = json_obj.get('gathertime') gather_time = utils.verify_time_param(abort, logger, gather_time1) rfid_code = json_obj.get('rfidcode') ip = json_obj.get('ip') imei = json_obj.get('imei') try: video = request.files['video'] except: video = None # verify the existence of parameters utils.verify_param(abort, logger, error_code=400, user_id=user_id, json_obj=json_obj, company_id=company_id, gather_time=gather_time, rfid_code=rfid_code, ip=ip, imei=imei, video=video, method_name="dead") # Judging video size video_size = len(video.read()) / float(1000.0) if video_size > config.max_video_size: logger.error( 'From ' + ip + ' -> Upload dead cow video file : ' + video.filename + ' with size of {} kb , But video_size over 20MB failed to upload'.format( video_size)) abort(413) # make the save folder path and save the dead cow video folder_path = os.path.join(config.base_images_path, "dead_{}_{}".format(company_id, rfid_code)) + os.sep if not os.path.exists(folder_path): os.makedirs(folder_path) video.seek(0) video.save(folder_path + utils.secure_filename(video.filename)) logger.info('From ' + ip + ' -> Upload dead cow video file : ' + video.filename + ' with size of {} kb'.format( video_size)) # Change the health of cow try: cow = Archives.query.filter_by(rfid_code=rfid_code, company_id=company_id).first() cow.health_status = "0" db.session.commit() logger.info( "cow rfid_code = {} from company_id = {} successful to modify health_status".format(rfid_code, company_id)) except: db.session.rollback() shutil.rmtree(folder_path) logger.error( "cow rfid_code = {} from company_id = {} failed to modify health_status , so delete the folder".format( rfid_code, company_id)) abort(502) return jsonify({ 'userid': user_id, 'companyid': company_id, 'rfidcode': rfid_code, 'gathertime': str(gather_time), 'ip': ip, 'imei': imei, })
def verify(): """ verify params and save the cow information to the db :return: cow information """ # get the params first user_id = g.user.userid entity = request.form.get('entity') utils.verify_param(abort, logger, error_code=400, entity=entity, method_name="verify") json_obj = json.loads(entity) company_id = json_obj.get('companyid') gather_time1 = json_obj.get('gathertime') gather_time = utils.verify_time_param(abort, logger, gather_time1) rfid_code = json_obj.get('rfidcode') ip = json_obj.get('ip') imei = json_obj.get('imei') xvalue = json_obj.get('xvalue') yvalue = json_obj.get('yvalue') width = json_obj.get('width') height = json_obj.get('height') try: video = request.files['video'] except: video = None # give the age value, 0 for default now. age = 0 # json_obj.get("age") # give the health_status value, 1 for default now. health_status = '1' # json_obj.get("health_status") # verify the existence of parameters utils.verify_param(abort, logger, error_code=400, user_id=user_id, json_obj=json_obj, company_id=company_id, gather_time=gather_time, rfid_code=rfid_code, ip=ip, imei=imei, xvalue=xvalue, yvalue=yvalue, width=width, height=height, video=video, age=age, health_status=health_status, method_name="verify") # judge the existence of cow if Archives.query.filter_by(rfid_code=rfid_code, company_id=company_id).first(): logger.error("cow rfid_code = {} already exists and cannot be save repeatedly".format(rfid_code)) abort(403) else: # Judging video size video_size = len(video.read()) / float(1000.0) if video_size > config.max_video_size: logger.error( 'From ' + ip + ' -> Upload video file : ' + video.filename + ' with size of {} kb , But video_size over 20MB failed to upload'.format( video_size)) abort(413) # make the save folder path and save the video folder_path = os.path.join(config.base_images_path, company_id, rfid_code) + os.sep if not os.path.exists(folder_path): os.makedirs(folder_path) video.seek(0) video.save(folder_path + utils.secure_filename(video.filename)) logger.info('From ' + ip + ' -> Upload video file : ' + video.filename + ' with size of {} kb'.format( video_size)) # make async execution thread for the video save and frame grabber executor.submit(utils.process_video_to_image, abort, logger, video, folder_path, rfid_code, xvalue, yvalue, width, height) # assign values to database fields archives = Archives(rfid_code=rfid_code, age=age, company_id=company_id, gather_time=gather_time, health_status=health_status, folder_path=os.path.join(config.base_images_path, company_id, rfid_code), extra_info='file name is : ' + video.filename) li = LogInfo(company_id=company_id, rfid_code=rfid_code, remote_ip=ip, imei=imei, extra_info='file name is : ' + video.filename) db_list = [archives, li] # logs the submit info to the db utils.insert_record(logger, db_list, db, abort, company_id, rfid_code, folder_path) logger.info( "cow rfid_code = {} from company_id = {} was successfully inserted into the database".format(rfid_code, company_id)) return jsonify({ 'userid': user_id, 'companyid': company_id, 'resoult': True, 'gathertime': str(gather_time), 'verinfo': 'jobs was launched in background', 'ip': ip, 'imei': imei, })
def prospect(): """ get the predicted results based on message :return: predicted results and cow information """ user_id = g.user.userid company_id = request.json.get('companyid') gather_time1 = request.json.get('gathertime') gather_time = utils.verify_time_param(abort, logger, gather_time1) rfid_code = request.json.get('rfidcode') ip = request.json.get('ip') imei = request.json.get('imei') image_array = request.json.get('items') predict_array = [] cid_array = [] # verify the existence of parameters utils.verify_param(abort, logger, error_code=400, user_id=user_id, company_id=company_id, gather_time=gather_time, rfid_code=rfid_code, ip=ip, imei=imei, image_array=image_array, method_name="prospect") for i, item in enumerate(image_array): # get the base64 str and decode them to image img_base64 = item.get('cvalue') img_oriented = item.get('cid') starter = img_base64.find(',') img_base64 = img_base64[starter + 1:] bytes_buffer = BytesIO(base64.b64decode(img_base64)) # get one for pillow and another for img_pillow = Image.open(bytes_buffer) img_pillow1 = img_pillow.resize(config.img_size, Image.NEAREST) img_pillow2 = numpy.array(img_pillow1).astype(numpy.float32) # img_cv2 = cv2.imdecode(numpy.frombuffer(bytes_buffer.getvalue(), numpy.uint8), cv2.IMREAD_COLOR) # img_cv2 = cv2.resize(img_cv2, config.img_size) predict_array.append(img_pillow2) cid_array.append(img_oriented) # get the predicted results and returned result, predict_code = utils.get_predicted_result(predict_array, cid_array) if rfid_code == predict_code and result >= config.min_predict: resoult = 1 logger.info( "From ip {} -> cow rfid_code = {},company_id = {} prediction success,result = {}%".format(ip, rfid_code, company_id, result)) else: resoult = 0 logger.info( "From ip {} -> cow rfid_code = {},company_id = {} prediction failure,result = {}%".format(ip, rfid_code, company_id, result)) # get the previous registered images top3 and encode them to base64 pre_files = utils.get_files(os.path.join(config.base_images_path, company_id, predict_code) + os.sep, 3) pre_items = utils.read_image_to_base64(pre_files) return jsonify({ 'userid': user_id, 'companyid': company_id, 'resoult': resoult, 'gathertime': str(gather_time), 'percent': "predict_code:{},percent:{}%".format(predict_code, result), 'verinfo': 'test', 'ip': ip, 'imei': imei, 'items': pre_items })
def verify(): """ verify params and save the cow information to the db :return: cow information """ # get the params first user_id = g.user.userid json_obj = json.loads(request.form.get('entity')) company_id = json_obj.get('companyid') gather_time1 = json_obj.get('gathertime') try: gather_time = datetime.datetime.strptime(gather_time1, "%Y/%m/%d %H:%M:%S") except: gather_time = gather_time1 rfid_code = json_obj.get('rfidcode') ip = json_obj.get('ip') imei = json_obj.get('imei') try: video = request.files['video'] except: video = None # give the age value, 0 for default now. age = 0 # json_obj.get("age") # give the health_status value, 1 for default now. health_status = '1' # json_obj.get("health_status") # verify the existence of parameters utils.verify_param(abort, error_code=400, user_id=user_id, json_obj=json_obj, company_id=company_id, gather_time=gather_time, rfid_code=rfid_code, ip=ip, imei=imei, video=video, age=age, health_status=health_status) # judge the existence of cow if Archives.query.filter_by(rfid_code=rfid_code).first(): abort(403) else: # make the save folder path and save the video folder_path = os.path.join(config.base_images_path, company_id, rfid_code) + os.sep if not os.path.exists(folder_path): os.makedirs(folder_path) video.save(folder_path + utils.secure_filename(video.filename)) # make async execution thread for the video save and frame grabber executor.submit( utils.process_video_to_image, video, os.path.join(config.base_images_path, company_id, rfid_code) + os.sep, rfid_code) # assign values to database fields archives = Archives(rfid_code=rfid_code, age=age, company_id=company_id, gather_time=gather_time, health_status=health_status, folder_path=os.path.join(config.base_images_path, company_id, rfid_code), extra_info='file name is : ' + video.filename) li = LogInfo(company_id=company_id, rfid_code=rfid_code, remote_ip=ip, imei=imei, extra_info='file name is : ' + video.filename) db_list = [archives, li] # log the submit info to the db utils.insert_record(db_list, db, abort) return jsonify({ 'userid': user_id, 'companyid': company_id, 'resoult': True, 'gathertime': str(gather_time), 'verinfo': 'jobs was launched in background', 'ip': ip, 'imei': imei, })