YOLOv8 Flask整合问题

YOLOv8 Flask整合问题

yolov8 + flask 后代码没有进行推理问题。

Bug model.predict()+pyinstaller+HTTPServer/flask: not executing

yolov8是异步线程调用了,flask打包exe后会应该异步问题,model.predict()不会进行返回,导致没有看着没有执行而已。

Behind the scenes, ultralytics (or one of its dependencies) is using multiprocessing, and if you want to use multiprocessing in a PyInstaller-frozen application, you need to call multiprocessing.freeze_support before making any use of multiprocessing functionality.

In the case of your non-flask example:

python 复制代码
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import cv2
import multiprocessing  # For multiprocessing.freeze_support()
import numpy as np
import base64
import time
from ultralytics import YOLO

class MyServer(BaseHTTPRequestHandler):
    def do_POST(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()

        content_length = int(self.headers['Content-Length'])
        post_data = json.loads(self.rfile.read(content_length))
        img = post_data['img']

        nparr = np.frombuffer(base64.b64decode(img), np.uint8)
        img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        
        print('starting..')
        model = YOLO('best.pt')
        print('model loaded')
        results = model.predict(source=img_np, show=False, save=False, save_conf=False, show_conf=False, save_txt=False)
        print('model predicted')

        self.wfile.write(bytes(f"ok", "utf-8"))

if __name__ == "__main__":
    multiprocessing.freeze_support()  # <--- Added
    print('starting server..')    
    webServer = HTTPServer(('localhost', 5000), MyServer)
    try:
        webServer.serve_forever()
    except KeyboardInterrupt:
        pass
    finally:
        print('aborted')
        time.sleep(2)
    webServer.server_close()

解决!!!

相关推荐
m0_64030930几秒前
Golang Gin怎么绑定JSON参数_Golang Gin JSON绑定教程【精通】
jvm·数据库·python
databook5 分钟前
用Manim实现动态交点计算--从一个动点问题说起
python·动效
2301_764150567 分钟前
CSS如何用Less实现多维度的样式复用_结合混合与继承技术实现
jvm·数据库·python
清平乐的技术专栏8 分钟前
Python依赖包实现window下载上传到Linux
linux·开发语言·python
m0_748839499 分钟前
MySQL触发器实现简单的分表逻辑_垂直分表与自动化路由
jvm·数据库·python
2301_8176722615 分钟前
mysql如何批量增加表的字段_脚本化DDL操作实践
jvm·数据库·python
荪荪24 分钟前
yolov8检测模型pt转rknn
人工智能·yolo·机器人·瑞芯微
DaqunChen29 分钟前
mysql存储引擎性能基准测试_InnoDB与MyISAM对比指南
jvm·数据库·python
2301_7826591829 分钟前
CSS Flex布局中如何实现导航栏与Logo的左右分布_利用justify-content- space-between
jvm·数据库·python
mailangduoduo29 分钟前
实战对比PyTorch VS PyTorch Lighting以MNIST为例
人工智能·pytorch·python·深度学习·图像分类·全连接网络