[Python] ModuleNotFoundError: No module named 'transformers.utils'

Discussão em 'Python' iniciado por Stack, Junho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I wanted to run my simple object detection program but I ran into this error

    I'm trying to run a python program that uses OWL-ViT and installed transformers for that but it gives this "ModuleNotFoundError: No module named 'transformers.utils" error

    I installed it to virtual env if that's the problem.

    import requests
    from PIL import Image
    import torch

    from transformers import OwlViTProcessor, OwlViTForObjectDetection
    import os
    fcontent = os.listdir("..\imgs")

    processor = OwlViTProcessor.from_pretrained("google/owlvit-base-patch32")
    model = OwlViTForObjectDetection.from_pretrained("google/owlvit-base-patch32")


    def result(a):
    for imag in fcontent:
    image = Image.open(imag)
    texts = [[a]]
    inputs = processor(text=texts, images=image, return_tensors="pt")
    outputs = model(**inputs)

    # Target image sizes (height, width) to rescale box predictions [batch_size, 2]
    target_sizes = torch.Tensor([image.size[::-1]])
    # Convert outputs (bounding boxes and class logits) to Pascal VOC format (xmin, ymin, xmax, ymax)
    results = processor.post_process_object_detection(outputs=outputs, target_sizes=target_sizes, threshold=0.1)
    i = 0 # Retrieve predictions for the first image for the corresponding text queries
    text = texts
    boxes, scores, labels = results["boxes"], results["scores"], results["labels"]
    for box, score, label in zip(boxes, scores, labels):
    box = [round(i, 2) for i in box.tolist()]
    print(f"Detected {text[label]} with confidence {round(score.item(), 3)} at location {box}")

    Continue reading...

Compartilhe esta Página