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 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, "rfid_code": instance.rfid_code, "age": instance.age, "company_id": instance.company_id, "gather_time": str(instance.gather_time), "health_status": instance.health_status, "extra_info": instance.extra_info, "items": pre_items }
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 })