首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于机器人流的摄像头目标检测

基于机器人流的摄像头目标检测
EN

Stack Overflow用户
提问于 2022-09-23 11:25:25
回答 1查看 122关注 0票数 0

我试着用我的摄像头制作一个目标检测程序,但却无法工作

这是我的密码

代码语言:javascript
复制
import cv2 as cv
import base64
import numpy as np
import requests

ROBOFLOW_API_KEY = "*********"
ROBOFLOW_MODEL = "chess-full-ddsxf"  # eg xx-xxxx--#
ROBOFLOW_SIZE = 416

upload_url = "".join([
    "https://detect.roboflow.com/",
    ROBOFLOW_MODEL,
    "?access_token=",
    ROBOFLOW_API_KEY,
    "&format=image",
    "&stroke=5"
])

video = cv.VideoCapture(1)

# Check if camera opened successfully
if (video.isOpened()):
    print("successfully opening video stream or file")


# Infer via the Roboflow Infer API and return the result
def infer():
    # Get the current image from the webcam
    ret, frame = video.read()

    # Resize (while maintaining the aspect ratio)
    # to improve speed and save bandwidth
    height, width, channels = frame.shape
    scale = ROBOFLOW_SIZE / max(height, width)
    img = cv.resize(frame, (round(scale * width), round(scale * height)))

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

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

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

    return image


# Main loop; infers sequentially until you press "q"
while video.isOpened():
    # On "q" keypress, exit
    key = cv.waitKey(5)
    if key == ord("q"):
        break

    # Synchronously get a prediction from the Roboflow Infer API
    image = infer()
    # And display the inference results
    cv.imshow('image', image)

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

错误消息:成功打开视频流或文件

追溯(最近一次调用):文件"C:\Users\Administrator\Documents\Atom\testingCode\webcamRoboflow\infer.py",第64行,在cv.imshow(“图像”,图像)cv2中。错误: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:967:错误:(-215:断言失败) size.width>0 & size.height>0中的函数'cv::imshow‘

在22.357秒内完成

EN

回答 1

Stack Overflow用户

发布于 2022-09-24 00:02:26

看起来,您忘记了包含您想要推断的模型的版本,因此您到达了服务器上未定义的端点。

代码语言:javascript
复制
ROBOFLOW_MODEL = "chess-full-ddsxf"  # eg xx-xxxx--#

这应该是

代码语言:javascript
复制
ROBOFLOW_MODEL = "chess-full-ddsxf/1"  # eg xx-xxxx--#

如果您将模型训练为数据集的第一个生成版本。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73826984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档