Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-30b-a3b· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

write a python script that uses yolo model for object detection that takes in image from…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Amazon Novaamazon.nova-pro-v1:0’s answer.
  1. User

    write a python script that uses yolo model for object detection that takes in image from my device

  2. Qwen Assistant

    Here's a complete and well-documented Python script that uses the YOLOv5 model (from the Ultralytics library) to perform object detection on an image from your device.


    ✅ Requirements

    Before running the script, make sure you have the following installed:

    pip install torch torchvision torchaudio
    pip install ultralytics
    

    ⚠️ If you have a compatible GPU, install the appropriate PyTorch version from PyTorch's official website.


    🧠 Script Overview

    This script:

    • Loads a pre-trained YOLOv5 model (yolov5s).
    • Accepts an image path as a command-line argument.
    • Performs object detection on the image.
    • Displays the image with detected objects highlighted using bounding boxes and labels.

    📄 Python Script: yolo_detection.py

    import sys
    import cv2
    from ultralytics import YOLO
    
    def main():
        # Check if the image path is provided
        if len(sys.argv) < 2:
            print("Usage: python yolo_detection.py <image_path>")
            print("Example: python yolo_detection.py images/test.jpg")
            return
    
        image_path = sys.argv[1]
    
        # Load a pre-trained YOLOv5 model
        # 'yolov5s' is a lightweight version; you can use 'yolov5m', 'yolov5l', etc.
        model = YOLO('yolov5s')
    
        # Perform object detection on the image
        results = model.predict(source=image_path, conf=0.5)  # 0.5 is the confidence threshold
    
        # Display the result
        for result in results:
            # Draw bounding boxes and labels on the image
            annotated_frame = result.plot()
    
            # Show the image with detections
            cv2.imshow('Object Detection', annotated_frame)
            cv2.waitKey(0)  # Wait until a key is pressed
            cv2.destroyAllWindows()
    
    if __name__ == "__main__":
        main()
    

    🖼️ How to Use

    1. Place an image file (e.g., test.jpg) in the same directory as the script or provide the full path.

    2. Run the script from the terminal:

    python yolo_detection.py images/test.jpg
    

    Replace images/test.jpg with the actual path to your image.


    📝 Optional Enhancements

    • Save the output image:

      result.save(filename="output.jpg")
      
    • Adjust confidence threshold:

      results = model.predict(source=image_path, conf=0.7)
      
    • Add custom class filtering:

      results = model.predict(source=image_path, classes=[0, 1, 2])  # Detect only specific classes
      

    🧪 Sample Output

    When you run the script, a window will open showing your image with:

    • Bounding boxes around detected objects.
    • Labels (e.g., "person", "car", "dog").
    • Confidence scores (if enabled).

    🧩 Notes

    • YOLOv5 automatically downloads the model weights (yolov5s.pt) if not found.
    • The script uses OpenCV (cv2) for displaying the image. If you're using a headless environment, consider saving the result instead of displaying it.

    Let me know if you'd like to extend this to video, webcam, or custom datasets!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.