python - 创建队列系统,按照请求的资历顺序依次处理请求?
问题描述
我有一个非常简单的 API,可以预测是否存在不安全的驾驶行为。除了识别行为的不规则性之外,API 还允许描述所犯的违规行为。
如何设置排队系统(例如使用 RabbitMQ)以按照请求的资历顺序依次处理图像?
main.py
from model import ClassificationModel
import argparse
import os
def predict_img(img_path, with_heatmap=False):
model = ClassificationModel() <--- Aqui descargo el modelo cada vez que llamo a predict_img
row = model.predict(img_path=img_path, row=None, img=None, load=True, original_image_path=img_path,
print_heatmap=with_heatmap)
results = row.to_json()
print(results)
return results
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--path", "-p", help="path of the image", required=True)
parser.add_argument("--heatmap", "-heat", help="generate heatmap or not", required=True)
args = parser.parse_args()
if not(os.path.exists(args.path)):
raise FileNotFoundError("{} does not exist.".format(args.path))
elif args.path.split(".")[-1] not in ["png", "jpg", "jpeg"]:
raise TypeError("The file is not a valid image file.")
if args.heatmap.upper() == "TRUE":
args.heatmap = True
else:
args.heatmap = False
result = predict_img(img_path=args.path, with_heatmap=args.heatmap)
model.py
import numpy as np
np.random.seed(40)
from keras_preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import load_model
import settings as st
from predict import Predict
class ClassificationModel(Predict):
def __init__(self):
self.list_pos = [
"safe driving",
"texting - right",
"talking to the phone - right",
"texting - left",
"talking on the phone - left",
"operating the radio",
"drinking",
"reaching behind",
"hair and makeup",
"talking to passenger"
]
self.image_size = 384
self.generator = ImageDataGenerator(brightness_range=[0.7, 1.5],
rotation_range=35,
fill_mode='nearest',
zoom_range=0.125,
rescale=1. / 255)
self.model = load_model(st.model_path)
self.threshold = 0.5
self.n_step = 5
self.normal_index = 0
解决方案
推荐阅读
- python - python tkinter自我错误
- php - laravel Guzzle 发布数据数组
- angularjs - Chartjs - 数据集颜色被 angular-chartjs 超载
- java - 从 Java 调用球拍
- git - Bitnami Jenkins VM:为什么管道在第二次运行时尝试签出 git repo 时缺少权限?
- sql - SQL Server:在where条件下用逗号传递公司ID(int)
- elasticsearch - NEST (ElasticSearch) 的动态布尔查询
- python - python字典中的探测如何工作?
- javascript - Javascript - 在 iOS Safari 中打开新选项卡而不会看到弹出警告
- oracle - EF - 无法调用存储过程