【flask】YOLO挖掘机目标检测模型Python flask部署(附项目链接)
文章目录
说明
利用YOLO训练好的挖掘机目标检测模型,通过Python flask进行部署,是一个比较直观快捷的模型展示方式。
flask项目结构及效果
通过前端上传所需识别图片,调用训练好的模型进行目标检测,将结果传回前端进行展示。
主程序代码
主程序代码如下:
from flask import Flask, request, jsonifyimport numpy as npfrom PaddleDetection.image import base64_to_PILfrom config import Args_configFLAGS = Args_config(device='GPU')#是否需要更改为GPUfrom PaddleDetection.deploy.python.infer import PredictConfig, Detector, infer_imagepred_config = PredictConfig(FLAGS.model_dir)detector = Detector( pred_config, FLAGS.model_dir, device=FLAGS.device, run_mode=FLAGS.run_mode, batch_size=FLAGS.batch_size, trt_min_shape=FLAGS.trt_min_shape, trt_max_shape=FLAGS.trt_max_shape, trt_opt_shape=FLAGS.trt_opt_shape, trt_calib_mode=FLAGS.trt_calib_mode, cpu_threads=FLAGS.cpu_threads, enable_mkldnn=FLAGS.enable_mkldnn)print("*MODEL LOADED!*")app = Flask(__name__)@app.route('/api/', methods=["POST"])def main_interface(): response = request.get_json() data_str = response['image'] point = data_str.find(',') base64_str = data_str[point:] # remove unused part like this: "data:image/jpeg;base64," # convert base64 string to PIL image, to numpy array try: img_arr = np.array(base64_to_PIL(base64_str)) except: # todo img_arr = None # do object detection in inference function. try: results = infer_image(detector, img_arr, FLAGS) except: # todo results = {"results": []} print(results) return jsonify(results)@app.after_requestdef add_headers(response): response.headers.add('Access-Control-Allow-Origin', '*') response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization') return responseif __name__ == '__main__': app.run(debug=False, host='127.0.0.1', port=5000)
config.py
代码
```# -*- coding: utf-8 -*-class Args_config: def __init__(self, device): self.batch_size = 1 self.cpu_threads = 1 self.device = device self.use_gpu = False if device.upper() == 'CPU' else True self.enable_mkldnn = False self.image_dir = None self.image_file = 'PaddleDetection/test_imgs/test1.jpg' self.model_dir = 'PaddleDetection/inference_model/ppyolo_r50vd_dcn_voc' self.output_dir = 'PaddleDetection/output' self.reid_batch_size = 50 self.reid_model_dir = None self.run_benchmark = False self.run_mode = 'fluid' self.save_images = False self.save_mot_txts = False self.threshold = 0.5 self.trt_calib_mode = False self.trt_max_shape = 1280 self.trt_min_shape = 1 self.trt_opt_shape = 640 self.use_dark = True # predict from video file or camera video stream self.camera_id = -1 self.video_file = None
项目链接
文件比较大,下载本项目请点击:
基于YOLO的挖掘机目标检测模型+flask模型前端展示
环境配置有疑问可以留言或私信交流。
公众号:一个甜甜的大橙子
知识星球:知识的朋友
欢迎交流~~