Bắt đầu

Suy luận trực tiếp bằng ứng dụng Streamlit sử dụng Ultralytics YOLO26

Giới thiệu#

Streamlit giúp dễ dàng xây dựng và triển khai các ứng dụng web tương tác. Kết hợp Streamlit với Ultralytics YOLO26 cho phép phát hiện đối tượng và phân tích theo thời gian thực ngay trong trình duyệt. Tốc độ và độ chính xác của YOLO26 khiến model này phù hợp với các luồng video trực tiếp trong những ứng dụng như an ninh và bán lẻ.



Xem: Cách xây dựng ứng dụng inference trực tiếp với Ultralytics YOLO26 và Streamlit | Phát hiện và phân đoạn 🚀
Nuôi trồng thủy sảnChăn nuôi
Phát hiện cá bằng Ultralytics YOLO26Phát hiện động vật bằng Ultralytics YOLO26
Phát hiện cá bằng Ultralytics YOLO26Phát hiện động vật bằng Ultralytics YOLO26

Ưu điểm của suy luận trực tiếp#

  • Phát hiện đối tượng theo thời gian thực: Kết hợp Streamlit với YOLO26 cho phép phát hiện đối tượng theo thời gian thực trực tiếp từ luồng webcam. Nhờ đó, bạn có thể phân tích và thu thập thông tin ngay lập tức, rất phù hợp với các ứng dụng cần phản hồi tức thì.
  • Triển khai thân thiện với người dùng: Giao diện tương tác của Streamlit giúp triển khai và sử dụng ứng dụng dễ dàng mà không cần kiến thức kỹ thuật chuyên sâu. Người dùng có thể bắt đầu suy luận trực tiếp chỉ bằng một cú nhấp, tăng khả năng tiếp cận và tính tiện dụng.
  • Sử dụng tài nguyên hiệu quả: Các thuật toán được tối ưu hóa của YOLO26 đảm bảo xử lý tốc độ cao với mức tài nguyên tính toán tối thiểu. Hiệu quả này cho phép suy luận webcam mượt mà và đáng tin cậy ngay cả trên phần cứng thông thường, giúp nhiều người tiếp cận thị giác máy tính tiên tiến hơn.

Mã ứng dụng Streamlit#

Cài đặt Ultralytics

Trước khi bắt đầu xây dựng ứng dụng, hãy đảm bảo bạn đã cài đặt gói Python Ultralytics.

pip install ultralytics
Suy luận bằng Streamlit với Ultralytics YOLO
yolo solutions inference

yolo solutions inference model="path/to/model.pt"

yolo solutions help # list the available solution commands and arguments

Các lệnh này khởi chạy giao diện Streamlit mặc định được cung cấp cùng Ultralytics.

Thao tác này sẽ khởi chạy ứng dụng Streamlit trong trình duyệt web mặc định của bạn. Bạn sẽ thấy tiêu đề chính, tiêu đề phụ và thanh bên có các tùy chọn cấu hình. Chọn model YOLO26 mong muốn, đặt các ngưỡng NMS, rồi nhấp vào nút "Start" để bắt đầu phát hiện đối tượng theo thời gian thực.

Cách hoạt động#

Bên dưới giao diện, ứng dụng Streamlit sử dụng module solutions của Ultralytics để tạo giao diện tương tác. Khi bạn bắt đầu suy luận, ứng dụng sẽ:

  1. Thu thập các khung hình từ webcam, tệp video đã tải lên hoặc hình ảnh đã tải lên
  2. Xử lý từng khung hình bằng model YOLO26
  3. Thực hiện phát hiện đối tượng với các ngưỡng confidence và IoU do bạn chỉ định
  4. Hiển thị đồng thời khung hình gốc và khung hình đã chú thích theo thời gian thực
  5. Cho phép theo dõi đối tượng nếu được chọn

Ứng dụng cung cấp giao diện rõ ràng, thân thiện với người dùng cùng các nút điều khiển để điều chỉnh tham số model và bắt đầu/dừng suy luận bất cứ lúc nào.

Kết luận#

Sau khi làm theo hướng dẫn này, bạn đã tạo một ứng dụng phát hiện đối tượng theo thời gian thực bằng Streamlit và Ultralytics YOLO26. Ứng dụng chạy YOLO26 trên đầu vào từ webcam, video hoặc ảnh thông qua một giao diện đơn giản; bạn có thể dừng luồng bất cứ lúc nào.

Để mở rộng thêm, bạn có thể tìm hiểu việc bổ sung các tính năng như ghi lại luồng video, lưu các khung hình đã chú thích hoặc tích hợp với các thư viện thị giác máy tính khác.

Câu hỏi thường gặp#

  • Việc thiết lập ứng dụng phát hiện đối tượng theo thời gian thực với Streamlit và Ultralytics YOLO26 khá đơn giản. Trước tiên, hãy đảm bảo bạn đã cài đặt gói Python Ultralytics bằng lệnh:

    pip install ultralytics

    Sau đó, bạn có thể tạo một ứng dụng Streamlit cơ bản để chạy suy luận trực tiếp:

    Ứng dụng Streamlit
    from ultralytics import solutions
    
    inf = solutions.Inference(
        model="yolo26n.pt",  # bạn có thể sử dụng bất kỳ model nào được Ultralytics hỗ trợ, ví dụ: YOLO26 hoặc model được huấn luyện tùy chỉnh
    )
    
    inf.inference()
    
    # Hãy đảm bảo chạy tệp bằng lệnh `streamlit run path/to/file.py`

    Để biết thêm chi tiết về cách thiết lập thực tế, hãy tham khảo phần Mã ứng dụng Streamlit trong tài liệu.

  • Việc sử dụng Ultralytics YOLO26 với Streamlit để phát hiện đối tượng theo thời gian thực mang lại một số ưu điểm:

    • Phát hiện theo thời gian thực: Phát hiện đối tượng với độ chính xác cao theo thời gian thực, trực tiếp từ luồng webcam.
    • Giao diện thân thiện với người dùng: Giao diện trực quan của Streamlit giúp dễ dàng sử dụng và triển khai mà không cần kiến thức kỹ thuật chuyên sâu.
    • Hiệu quả sử dụng tài nguyên: Các thuật toán được tối ưu hóa của YOLO26 đảm bảo xử lý tốc độ cao với tài nguyên tính toán tối thiểu.

    Tìm hiểu thêm về những lợi ích này trong phần Ưu điểm của suy luận trực tiếp.

  • Sau khi viết mã ứng dụng Streamlit tích hợp Ultralytics YOLO26, bạn có thể triển khai ứng dụng bằng cách chạy:

    streamlit run path/to/file.py

    Lệnh này sẽ khởi chạy ứng dụng trong trình duyệt web mặc định, cho phép bạn chọn model YOLO26, thiết lập ngưỡng confidence và IoU, rồi bắt đầu phát hiện đối tượng theo thời gian thực chỉ bằng một cú nhấp chuột.

  • Phát hiện đối tượng theo thời gian thực bằng Streamlit và Ultralytics YOLO26 có thể được áp dụng trong nhiều lĩnh vực:

    Để tìm hiểu các trường hợp sử dụng và ví dụ chuyên sâu hơn, hãy khám phá Ultralytics Solutions.

  • Ultralytics YOLO26 có một số cải tiến so với các model trước đây như YOLOv5 và các bộ phát hiện thuộc họ R-CNN:

    • Tốc độ và độ chính xác cao hơn: Hiệu năng được cải thiện cho các ứng dụng theo thời gian thực.
    • Dễ sử dụng: Giao diện và quy trình triển khai được đơn giản hóa.
    • Hiệu quả sử dụng tài nguyên: Được tối ưu hóa để tăng tốc độ trong khi yêu cầu tối thiểu tài nguyên tính toán.

Bình luận26

Đặt câu hỏi, chia sẻ kết quả hoặc giúp đỡ người khác.

  1. j3dd4hGitHub

    the manual execution using rknn model (yolo predict model='./best_rknn_model' source='ppe.jpg') is working but got an error when running under Streamlit.

    TypeError: model='best_rknn_model/best-rk3588.rknn' is not a supported model format. Ultralytics supports: ('PyTorch', 'TorchScript', 'ONNX', 'OpenVINO', 'TensorRT', 'CoreML', 'TensorFlow SavedModel', 'TensorFlow GraphDef', 'TensorFlow Lite', 'TensorFlow Edge TPU', 'TensorFlow.js', 'PaddlePaddle', 'MNN', 'NCNN', 'IMX', 'RKNN') See https://docs.ultralytics.com/modes/predict for help.

    1
    1. RizwanMunawarGitHub

      @j3dd4h Hi, thanks for sharing the details. We currently don't support the rknn model, but you can adjust this check in code to make the application work for rknn models. We will add support for this soon.

  2. NNQHuongGitHub

    AttributeError: module 'torch' has no attribute '__version__' Traceback: File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\streamlit\runtime\scriptrunner\exec_code.py", line 88, in exec_func_with_error_handling result = func() ^^^^^^ File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 579, in code_to_exec exec(code, module.__dict__) File "C:\WasteSegregation\app.py", line 1, in <module> from ultralytics import solutions File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\ultralytics\__init__.py", line 11, in <module> from ultralytics.models import NAS, RTDETR, SAM, YOLO, FastSAM, YOLOWorld File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\ultralytics\models\__init__.py", line 3, in <module> from .fastsam import FastSAM File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\ultralytics\models\fastsam\__init__.py", line 3, in <module> from .model import FastSAM File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\ultralytics\models\fastsam\model.py", line 5, in <module> from ultralytics.engine.model import Model File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\ultralytics\engine\model.py", line 11, in <module> from ultralytics.cfg import TASK2DATA, get_cfg, get_save_dir File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\ultralytics\cfg\__init__.py", line 12, in <module> from ultralytics.utils import ( File "C:\Users\myname\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\ultralytics\utils\__init__.py", line 51, in <module> TORCH_VERSION = torch.__version__ ^^^^^^^^^^^^^^^^^ This is the error I encountered when using from ultralytics import solutions solutions.inference(model =r'C:path\to\my\model.pt') method (VSCode), can you please explain it to me? For further information, I've tried using the method for CLI and it works, but not with VSCode. I wonder if there's any method for CLI that allows me to use my custom trained model? It's YOLO11n-cls version. Thank you for reading this!

    1
    1. Glenn JocherUltralytics

      @NNQHuong the error you're encountering suggests a possible issue with your PyTorch installation. Please verify that PyTorch is properly installed and compatible with your Python version by running:

      import torch
      print(torch.__version__)

      If the error persists, ensure you're using the latest version of PyTorch and Ultralytics with:

      pip install --upgrade torch ultralytics

      Regarding your CLI query, you can use your custom-trained YOLO11 model by specifying the model path directly, like so:

      yolo predict model=path/to/your/model.pt source=your_input

      For detailed examples, check the documentation here. Let me know if additional assistance is required!

      2
    2. RizwanMunawarGitHub

      Hi @NNQHuong, thank you for sharing the error log! It appears that the issue might be related to your PyTorch version. You may need to upgrade or downgrade it to ensure everything works correctly.

      However, if this still doesn't resolve the issue, please share your code here for further testing.

      Thanks, Ultralytics Team!

      1
    3. NNQHuongGitHub

      Thanks for your infomative reply, it worked smoothly! I'm kind of new to this field anyway, about 6 months , I bet you can see it in my words haha, so thanks for your patience too!

      1
  3. Tech-WattGitHub

    Is it posible to use different camera index ?. It picks up the default computer webcam and there's no means of changing it.

    1. Glenn JocherUltralytics(đã chỉnh sửa)

      The Inference class has no source argument: selecting "webcam" in the sidebar always opens camera index 0 (self.vid_file_name = 0 in ultralytics/solutions/streamlit_inference.py). To use a different camera, make it your system's default camera or change that index in the file:

      inf = solutions.Inference(model="yolo26n.pt")
      inf.inference()

      Run it with streamlit run <file-name.py>.

  4. VectorvinGitHub

    streamlit 这个解决方案可以使用tensorRT格式或者onnx格式吗?似乎程序被写死了

    1. Glenn JocherUltralytics(đã chỉnh sửa)

      @Vectorvin 您好!Streamlit 方案现在也支持导出的模型格式:除了 .pt,还可以把 .onnx、.torchscript、.mlpackage、.engine(TensorRT)、OpenVINO 或 RKNN 模型路径传给 solutions.Inference(model="yolo26n.engine")。导出步骤请参考导出文档。需要企业级部署支持请访问ultralytics.com/license。

  5. sathyanarayanaRPGitHub

    how does the streamlit gets is input , is it possible to push frames or is it possible to integrate machine vision cameras that does not support RTSP protocals. i need comprehensive knowledge how does it work as a streaming device

    1. Glenn JocherUltralytics

      @sathyanarayanaRP streamlit can receive input directly from a webcam or video file. For cameras that don't support RTSP, you can use USB or other compatible connections. The Streamlit app processes frames in real-time, allowing for seamless integration with various camera types. For more details, please refer to the Streamlit documentation.

  6. Cheikh-AhmedandGitHub

    hey!

    Once I run the app my Laptop camera turns on, instead of My webcam. How to use my external webcam instead of my LAPTOP camera?

    Best Regards!

    1. Glenn JocherUltralytics(đã chỉnh sửa)

      @Cheikh-Ahmedand the Streamlit app has no source argument: choosing "webcam" in the sidebar always opens camera index 0 (self.vid_file_name = 0 in ultralytics/solutions/streamlit_inference.py). To use your external webcam, make it the system's default camera, or change that index to 1 (or the index of your webcam) in the file.

      Run the app with yolo solutions inference, or with streamlit run <file-name.py> for your own script.

    2. Cheikh-AhmedandGitHub

      Hey ! Thanks lot for your response, but the code couldn't work with streamlit code. I pasted the following code on my Laptop anconda PowerShell Prompt:

      yolo streamlit-predict solutions.inference(source=1)

      Still, my Webcam can't run, but my Laptop 's Camera.

      Also, I tried the below code in jupyter notebook, but the problem still persists.

      from ultralytics import solutions

      solutions.inference() solutions.inference(source=1)

      Make sure to run the file using command streamlit run <file-name.py>

      Any help either from the Respectful Staff or others would be appreciated!!!

    3. Glenn JocherUltralytics(đã chỉnh sửa)

      @Cheikh-Ahmedand the Streamlit app has no source argument: choosing "webcam" in the sidebar always opens camera index 0 (self.vid_file_name = 0 in ultralytics/solutions/streamlit_inference.py). To use an external webcam, set it as your system's default camera or change that index in the file. Run the app with yolo solutions inference, or streamlit run <file-name.py> for your own script. If issues persist, please check your system's camera settings.

    1. Glenn JocherUltralytics(đã chỉnh sửa)

      @AdadAlShabab you can use the following code to run the Streamlit application for live inference:

      from ultralytics import solutions
      
      inf = solutions.Inference(model="yolo26n.pt")
      inf.inference()

      Run the file with the command streamlit run <file-name.py>. For more details, please refer to the Streamlit Live Inference Guide.

  7. ghostGitHub

    Thank you for your quick and kind response and the problem is sorted. But there is another issue while switching model to different task like pose,seg or cls and getting error of open cv not having func()

    1. Glenn JocherUltralytics

      @Charan2128 i'm glad to hear the initial issue is resolved. Regarding the error with OpenCV when switching tasks (pose, seg, cls), please ensure you have the latest versions of both the Ultralytics package and OpenCV. You can update them using:

      pip install --upgrade ultralytics opencv-python

      If the issue persists, please provide more details about the error message and the specific function that is causing the problem. This will help us diagnose and address the issue more effectively. For further guidance, you can also refer to our Streamlit Live Inference Guide.

    2. ghostGitHub

      That was a Quick Replay and Quick fix ,Thanks for your kind support on my issues. The problem is fixed now.

    3. Glenn JocherUltralytics

      @Charan2128 i'm glad to hear the initial issue is resolved. For the new issue with switching tasks and encountering OpenCV errors, please ensure you have the latest versions of both the Ultralytics package and OpenCV installed. You can update them using pip install --upgrade ultralytics opencv-python. If the problem persists, please provide the specific error message and function causing the issue so we can assist you further. For more detailed guidance, you can refer to our Streamlit Live Inference Guide.

  8. ghostGitHub

    AttributeError: module 'ultralytics.solutions' has no attribute 'inference' Traceback: File "C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\streamlit\runtime\scriptrunner\exec_code.py", line 75, in exec_func_with_error_handling result = func() ^^^^^^ File "C:\Users\DELL\AppData\Local\Programs\Python\Python312\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 574, in code_to_exec exec(code, module.dict) File "D:\YOLO\infer.py", line 3, in solutions.inference() ^^^^^^^^^^^^^^^^^^^ This is the error im getting while running this and i have installed necessary packages.

    1. Paula DerrengerUltralytics(đã chỉnh sửa)

      @Charan2128 it looks like you're encountering an AttributeError because solutions.inference is not an attribute of the ultralytics.solutions module. Please ensure you are using the latest version of the Ultralytics package by running pip install --upgrade ultralytics. Then launch the Streamlit app directly from the CLI with yolo solutions inference, or from Python with solutions.Inference(model="yolo26n.pt").inference() run via streamlit run <file-name.py>. For more details, you can refer to our Streamlit Live Inference Guide.