如何正确串流深度相机

问题描述

我目前正在使用emlids image处理树莓派4b。我正在尝试运行下面的代码。当我跑线

python track.py

我明白了

Unable to stop the stream: Invalid argument

但是,当我跑步时

python3 track.py

我明白了

[ WARN:0] global /tmp/pip-wheel-2l8ccy47/opencv-python/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video1): can't open camera by index

我的程序的代码来自pyimagesearch,如下所示:

from collections import deque
from imutils.video import VideoStream
import numpy as np
import math
import argparse
import cv2
import imutils
import time


ap = argparse.ArgumentParser()
ap.add_argument("-v","--video",help="path to the (optional) video file")
ap.add_argument("-b","--buffer",type=int,default=2,#Change
    help="max buffer size")
args = vars(ap.parse_args())

# define the lower and upper boundaries of the "green"
# ball in the HSV color space,then initialize the
# list of tracked points
greenLower = (29,86,6)
greenUpper = (64,255,255)
pts = deque(maxlen=args["buffer"])
# if a video path was not supplied,grab the reference
# to the webcam
if not args.get("video",False):
    vs = VideoStream(src=0).start()
# otherwise,grab a reference to the video file
else:
    vs = cv2.VideoCapture(args["video"])
# allow the camera or video file to warm up
time.sleep(2.0)
# keep looping
while True:
    # grab the current frame
    frame = vs.read()
    # handle the frame from VideoCapture or VideoStream
    frame = frame[1] if args.get("video",False) else frame
    # if we are viewing a video and we did not grab a frame,# then we have reached the end of the video
    if frame is None:
        break
    # resize the frame,blur it,and convert it to the HSV
    # color space
    frame = imutils.resize(frame,width=600)
    blurred = cv2.GaussianBlur(frame,(11,11),0)
    hsv = cv2.cvtColor(blurred,cv2.COLOR_BGR2HSV)
    # construct a mask for the color "green",then perform
    # a series of dilations and erosions to remove any small
    # blobs left in the mask
    mask = cv2.inRange(hsv,greenLower,greenUpper)
    mask = cv2.erode(mask,None,iterations=2)
    mask = cv2.dilate(mask,iterations=2)
        # find contours in the mask and initialize the current
    # (x,y) center of the ball
    cnts = cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    center = None
    # only proceed if at least one contour was found
    if len(cnts) > 0:
        # find the largest contour in the mask,then use
        # it to compute the minimum enclosing circle and
        # centroid
        c = max(cnts,key=cv2.contourArea)
        ((x,y),radius) = cv2.minenclosingCircle(c)
        M = cv2.moments(c)
        center = (int(M["m10"] / M["m00"]),int(M["m01"] / M["m00"]))
        # only proceed if the radius meets a minimum size
        if radius > 10:
            # draw the circle and centroid on the frame,# then update the list of tracked points
            cv2.circle(frame,(int(x),int(y)),int(radius),(0,255),2)
            cv2.circle(frame,center,5,-1)
    # update the points queue
    pts.appendleft(center)
    # print(pts)
        # loop over the set of tracked point
    
    for i in range(1,len(pts)):
        # if either of the tracked points are None,ignore
        # them
        if pts[i - 1] is None or pts[i] is None:
            continue
        # otherwise,compute the thickness of the line and
        # draw the connecting lines
        thickness = int(np.sqrt(args["buffer"] / float(i + 1)) * 2.5)
        cv2.line(frame,pts[i - 1],pts[i],thickness)
        nodeList = pts[0]
        helper = nodeList[1]
        nodeList2 = pts[-1]
        helper2 = nodeList2[1]
        if helper<helper2-20:
            vH = (nodeList[0]-nodeList2[0])/(1/30)
            vV = (nodeList[1]-nodeList2[1])/(1/30)
            zSpeed = np.sqrt((vH*vH)+(vV*vV))
            helping = zSpeed/1000
            zSpeed2 = round(helping,2)
            
            
            if(zSpeed2 < 3):
                print("Dodge ",zSpeed2,"m/s")
                print("Center will make contact: ",nodeList)
                if nodeList[0]>nodeList2[0]:
                    print("Avoid Left")
                elif nodeList[0]<nodeList2[0]:
                    print("Avoid Right")
                elif nodeList[0]==nodeList2[0]:
                    print("Avoid Up")
        
    # show the frame to our screen
    cv2.imshow("Frame",frame)
    key = cv2.waitKey(1) & 0xFF
    # if the 'q' key is pressed,stop the loop
    
    if key == ord("q"):
        break
# if we are not using a video file,stop the camera video stream
if not args.get("video",False):
    vs.stop()
# otherwise,release the camera
else:
    vs.release()
# close all windows
cv2.destroyAllWindows()

到目前为止,我已检查设备是否已连接。

Intel(R) RealSense(TM) Depth Ca (usb-0000:01:00.0-1):
    /dev/video0
    /dev/video1
    /dev/video2
    /dev/video3
    /dev/video4
    /dev/video5

我也尝试过更改其中的值

vs = cv2.VideoCapture()

对于0、1、2、3等。我查找了针对同一问题的许多解决方案,但没有一个我有用。有什么建议吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)