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

在VS Code中使用摄像头运行Roboflow训练模型遇异常求助

问题修复方案

一、摄像头窗口仅显示角落小矩形的解决

问题根源

代码中硬编码将摄像头帧强制缩放到(2000, 1500),破坏了原始图像的宽高比,导致Roboflow返回的可视化图像尺寸异常,窗口无法正常适配展示。

修复逻辑

替换infer函数中的resize逻辑,按照模型训练时的ROBOFLOW_SIZE参数等比例缩放图像,保证图像比例不变:

async def infer(requests):
    # 获取摄像头帧
    ret, img = video.read()
    # 添加摄像头读取失败判断
    if not ret:
        print("无法读取摄像头帧")
        return None
   
    # 按ROBOFLOW_SIZE等比例缩放图像
    target_size = int(ROBOFLOW_SIZE)
    height, width = img.shape[:2]
    # 计算缩放比例,保证图像不超出目标尺寸且比例不变
    scale = min(target_size / width, target_size / height)
    new_width = int(width * scale)
    new_height = int(height * scale)
    img = cv2.resize(img, (new_width, new_height))

    # 后续编码、请求API逻辑保持不变...

二、修改cv2.imshow参数后报错的解决

问题根源

报错!_src.empty()说明传入cv2.imshow的图像为空,原因包括:

  • 摄像头未成功读取到帧,缺少错误判断;
  • 异常尺寸的图像导致Roboflow API返回无效响应;
  • 存在无意义的冗余代码cv2.imread('img.jpg')。

修复逻辑

  1. 在main函数中添加空图像判断,避免传入空数据给cv2.imshow;
  2. 移除冗余的cv2.imread代码;
  3. 可选:添加窗口尺寸适配逻辑,让窗口匹配图像大小。

修复后的main函数关键代码:

async def main():
    # 初始化逻辑保持不变...

    async with httpx.AsyncClient() as requests:
        while True:
            # 按键退出逻辑保持不变...

            image = await futures.pop(0)
            # 跳过空图像,避免报错
            if image is None or image.size == 0:
                continue
            # 设置窗口适配图像尺寸
            cv2.resizeWindow('image', image.shape[1], image.shape[0])
            # 展示推理结果
            cv2.imshow('image', image)

完整修复后的代码

# load config
import json
with open('roboflow_config.json') as f:
    config = json.load(f)

    ROBOFLOW_API_KEY = "********"
    ROBOFLOW_MODEL = "penguins-ojf2k"
    ROBOFLOW_SIZE = "416"

    FRAMERATE = config["FRAMERATE"]
    BUFFER = config["BUFFER"]

import asyncio
import cv2
import base64
import numpy as np
import httpx
import time

# Construct the Roboflow Infer URL
# (if running locally replace https://detect.roboflow.com/ with eg http://127.0.0.1:9001/)
upload_url = "".join([
    "https://detect.roboflow.com/",
    ROBOFLOW_MODEL,
    "?api_key=",
    ROBOFLOW_API_KEY,
    "&format=image", # Change to json if you want the prediction boxes, not the visualization
    "&stroke=5"
])

# Get webcam interface via opencv-python
video = cv2.VideoCapture(0,cv2.CAP_DSHOW)

# Infer via the Roboflow Infer API and return the result
# Takes an httpx.AsyncClient as a parameter
async def infer(requests):
    # Get the current image from the webcam
    ret, img = video.read()
    # 添加摄像头读取失败判断
    if not ret:
        print("无法读取摄像头帧")
        return None
   
    # 按ROBOFLOW_SIZE等比例缩放图像
    target_size = int(ROBOFLOW_SIZE)
    height, width = img.shape[:2]
    scale = min(target_size / width, target_size / height)
    new_width = int(width * scale)
    new_height = int(height * scale)
    img = cv2.resize(img, (new_width, new_height))

    # Encode image to base64 string
    retval, buffer = cv2.imencode('.jpg', img)
    img_str = base64.b64encode(buffer)

    # Get prediction from Roboflow Infer API
    resp = await requests.post(upload_url, data=img_str, headers={
        "Content-Type": "application/x-www-form-urlencoded"
    })

    # Parse result image
    image = np.asarray(bytearray(resp.content), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)

    return image


# Main loop; infers at FRAMERATE frames per second until you press "q"
async def main():
    # Initialize
    last_frame = time.time()

    # Initialize a buffer of images
    futures = []

    async with httpx.AsyncClient() as requests:
        while True:
            
            # On "q" keypress, exit
            if(cv2.waitKey(1) == ord('q')):
                break

            # Throttle to FRAMERATE fps and print actual frames per second achieved
            elapsed = time.time() - last_frame
            await asyncio.sleep(max(0, 1/FRAMERATE - elapsed))
            print((1/(time.time()-last_frame)), " fps")
            last_frame = time.time()

            # Enqueue the inference request and safe it to our buffer
            task = asyncio.create_task(infer(requests))
            futures.append(task)

            # Wait until our buffer is big enough before we start displaying results
            if len(futures) < BUFFER * FRAMERATE:
                continue

            # Remove the first image from our buffer
            # wait for it to finish loading (if necessary)
            image = await futures.pop(0)
            # 跳过空图像,避免报错
            if image is None or image.size == 0:
                continue
            # 设置窗口适配图像尺寸
            cv2.resizeWindow('image', image.shape[1], image.shape[0])
            # And display the inference results
            cv2.imshow('image', image)
            
# Run our main loop
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())


# Release resources when finished
video.release()
cv2.destroyAllWindows()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 04:55:45