[Python] Stable torch, ultralytics, tensorflow packages for requirements.txt

Discussão em 'Python' iniciado por Stack, Fevereiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm writing a requirements.txt for a real-time sports video analysis project and testing various libraries such as YOLOv8, Torch, TensorFlow, etc.

    I've tried multiple Python versions but encountered compatibility issues.

    Which versions should I install?

    Requirements

    • torch==2.1.2
    • torchvision==0.16.2
    • ultralytics==8.3.80
    • opencv-python==4.9.0.80
    Sample code


    from ultralytics import YOLO
    import cv2
    import os
    import glob


    FRAME_DIR = "data/videos/res/720p"
    OUTPUT_DIR = "data/videos/detections"
    os.makedirs(OUTPUT_DIR, exist_ok=True)


    model = YOLO("yolov8n.pt")

    TARGET_CLASSES = {
    "sports ball": 32,
    "person": 0,
    }


    for frame_path in sorted(glob.glob(os.path.join(FRAME_DIR, "*.jpg"))):
    frame = cv2.imread(frame_path)
    results = model(frame)

    for result in results:
    for box in result.boxes:
    cls_id = int(box.cls[0])
    if cls_id in TARGET_CLASSES.values():
    x1, y1, x2, y2 = map(int, box.xyxy[0])
    label = [k for k, v in TARGET_CLASSES.items() if v == cls_id][0]
    cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
    cv2.putText(frame, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

    output_path = os.path.join(OUTPUT_DIR, os.path.basename(frame_path))
    cv2.imwrite(output_path, frame)

    print(OUTPUT_DIR)


    Trace


    python -c "import ultralytics; print(ultralytics.__version__)"

    A module that was compiled using NumPy 1.x cannot be run in
    NumPy 2.1.1 as it may crash. To support both 1.x and 2.x
    versions of NumPy, modules must be compiled with NumPy 2.0.
    Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

    If you are a user of the module, the easiest solution will be to
    downgrade to 'numpy<2' or try to upgrade the affected module.
    We expect that some modules will need time to support NumPy 2.

    Traceback (most recent call last): File "<string>", line 1, in <module>
    File "/Library/WebServer/Documents/my_project/venv/lib/python3.11/site-packages/ultralytics/__init__.py", line 11, in <module>
    from ultralytics.models import NAS, RTDETR, SAM, YOLO, FastSAM, YOLOWorld
    File "/Library/WebServer/Documents/my_project/venv/lib/python3.11/site-packages/ultralytics/models/__init__.py", line 3, in <module>
    from .fastsam import FastSAM
    File "/Library/WebServer/Documents/my_project/venv/lib/python3.11/site-packages/ultralytics/models/fastsam/__init__.py", line 3, in <module>
    from .model import FastSAM
    File "/Library/WebServer/Documents/my_project/venv/lib/python3.11/site-packages/ultralytics/models/fastsam/model.py", line 5, in <module>
    from ultralytics.engine.model import Model
    File "/Library/WebServer/Documents/my_project/venv/lib/python3.11/site-packages/ultralytics/engine/model.py", line 8, in <module>
    import torch
    File "/Library/WebServer/Documents/my_project/venv/lib/python3.11/site-packages/torch/__init__.py", line 1477, in <module>
    from .functional import * # noqa: F403
    File "/Library/WebServer/Documents/my_project/venv/lib/python3.11/site-packages/torch/functional.py", line 9, in <module>
    import torch.nn.functional as F
    File "/Library/WebServer/Documents/my_project/venv/lib/python3.11/site-packages/torch/nn/__init__.py", line 1, in <module>
    from .modules import * # noqa: F403
    File "/Library/WebServer/Documents/my_project/venv/lib/python3.11/site-packages/torch/nn/modules/__init__.py", line 35, in <module>
    from .transformer import TransformerEncoder, TransformerDecoder, \
    File "/Library/WebServer/Documents/my_project/venv/lib/python3.11/site-packages/torch/nn/modules/transformer.py", line 20, in <module>
    device: torch.device = torch.device(torch._C._get_default_device()), # torch.device('cpu'),
    /Library/WebServer/Documents/my_project/venv/lib/python3.11/site-packages/torch/nn/modules/transformer.py:20: UserWarning: Failed to initialize NumPy: _ARRAY_API not found (Triggered internally at /Users/runner/work/pytorch/pytorch/pytorch/torch/csrc/utils/tensor_numpy.cpp:84.)
    device: torch.device = torch.device(torch._C._get_default_device()), # torch.device('cpu'),
    8.3.80

    Continue reading...

Compartilhe esta Página