You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于PyTorch与YOLOv5:色彩通道及检测结果显示等技术问询

Hey there! Let's tackle your YOLOv5 + real-time screen capture issues one by one— I’ve worked through similar real-time detection workflows, so these are common hurdles to sort out.


1. Fixing Color Channel Mismatch

The root problem here is channel order differences:

  • mss and YOLOv5 expect/use RGB channel order
  • OpenCV (cv2) uses BGR by default

When you pass np.array(screen) (RGB from mss) to YOLOv5, the model processes it correctly. But results.show() uses PIL’s RGB display system, which clashes with cv2’s BGR setup— hence the weird color shift.

Fix:

  • Keep feeding RGB images to YOLOv5 (no conversion needed for input)
  • When you want to display the annotated result with cv2, convert the rendered RGB image to BGR:
    annotated_rgb = results.render()[0]  # Get RGB annotated image from YOLO
    annotated_bgr = cv2.cvtColor(annotated_rgb, cv2.COLOR_RGB2BGR)
    

2. Why Detection Performance Dropped & How to Fix It

Color channel mismatch absolutely can hurt performance— your model was trained on RGB data, so feeding BGR (by accident) would scramble feature extraction. But there are other key factors:

  1. Input size mismatch: You used size=400, but YOLOv5 is typically trained on 640x640. Using a smaller size reduces detail and detection accuracy.
  2. Model loading location: If you’re loading the model inside your loop (don’t do this!), it will cripple performance. Load it once before the loop starts.
  3. Inference device: By default, YOLOv5 runs on CPU. If you have an NVIDIA GPU, add device='cuda' when loading the model to get massive speedups:
    model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True, device='cuda')
    
  4. Frame rate bottlenecks: Your screen capture loop might be limiting speed— use cv2.waitKey(1) instead of 25 to reduce display latency.

3. Display Detection Results in a Single CV2 Window

Forget results.show()— it opens a new window for every frame. Instead, use YOLOv5’s render() method to get the annotated image, convert it to BGR, and reuse the same cv2 window:

Here’s the updated loop with detection integrated:

# Load model ONCE before the loop (critical for performance!)
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True, device='cuda')

sct = mss()
xx = 1
tstart = time.time()
while xx < 10000:
    hwnd = win32gui.FindWindow(None, 'Calculator')
    left_x, top_y, right_x, bottom_y = win32gui.GetWindowRect(hwnd)
    bbox = {'top': top_y, 'left': left_x, 'width': right_x-left_x, 'height': bottom_y-top_y }
    
    # Capture screen (RGB format)
    screen = sct.grab(bbox)
    scr = np.array(screen)
    
    # Run detection
    results = model(scr, size=640)  # Use training-sized input for better accuracy
    
    # Get annotated image and convert to BGR for cv2
    annotated_img = results.render()[0]
    annotated_img_bgr = cv2.cvtColor(annotated_img, cv2.COLOR_RGB2BGR)
    
    # Show in a single window
    cv2.imshow('YOLOv5 Real-Time Detection', annotated_img_bgr)
    
    # Exit on 'q' press
    if cv2.waitKey(1) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break
    xx += 1

4. Saving Results & Accessing YOLOv5 Documentation

Saving to Disk

  • YOLO’s built-in save: results.save() doesn’t take a filename argument (that’s why you got the TypeError). It saves all results to a runs/detect/exp folder by default. To specify a custom folder:
    results.save(save_dir='my_detection_results')
    
  • Save a single frame: Use cv2 to write the BGR annotated image directly:
    cv2.imwrite('single_detection_frame.png', annotated_img_bgr)
    

Accessing Model Documentation

  • Use Python’s built-in help() function to get details about the Results object and model methods:
    help(results)
    help(model)
    
  • When you load the model, YOLOv5 prints usage tips to the console. You can also explore the source code of the Results class (in the yolov5 repo’s models/common.py file) to see all available methods like xyxy() (get bounding box coordinates), pandas() (export results to a DataFrame), and more.

内容的提问来源于stack exchange,提问作者gaut

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:23:33