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()

解决!!!

相关推荐
2201_756847332 分钟前
HTML函数在老旧浏览器运行慢是硬件问题吗_软硬协同分析【教程】
jvm·数据库·python
雨墨✘18 分钟前
CSS如何提高团队协作效率_推广BEM规范减少样式沟通成本
jvm·数据库·python
hef28823 分钟前
如何实现SQL字段值的计算输出:算术运算符与别名结合
jvm·数据库·python
2401_8274999924 分钟前
python项目实战11-正则表达式基础
python·mysql·正则表达式
QQ6765800831 分钟前
建筑热成像检测数据集 建筑物表面缺陷图像识别 建筑外墙保温缺陷检测、管道热损失识别 建筑物表面温度识别第10357期(代码+数据集+模型+界面)
yolo·建筑物表面缺陷图像·建筑外墙保温缺陷检测·管道热损失·建筑物表面温度
小糖学代码34 分钟前
LLM系列:1.python入门:6.元组型对象(tuple)
linux·运维·服务器·python
ZC跨境爬虫34 分钟前
Scrapy实战:5sing原创音乐网多页数据爬取(完整可运行,附避坑指南)
爬虫·python·scrapy·html
西西弗Sisyphus42 分钟前
Python dataclasses 中 field 的 default_factory 参数用法
python·field·dataclasses·default_factory
yuyuyuliang001 小时前
python笔记1
开发语言·笔记·python
摇滚侠1 小时前
Groovy 如何给集合中添加元素
java·开发语言·windows·python