Raspberry Pi OpenCV:src不是一个numpy数组,也不是标量

时间:2016-01-19 08:10:19

标签: python opencv numpy raspberry-pi

我正在尝试让我的Raspberry Pi B +使用USB网络摄像头来测量它与固定宽度(11.0英寸)物体之间的距离。

我现在关注这个guide。但是,我使用来自网络摄像头的视频,而不是使用静态图像。

这是我试图运行的代码:

import argparse
import datetime
import imutils
import time
import cv2
import numpy as np

def find_marker(frame):
    # convert the image to grayscale, blur it, and detect edges
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(gray, 35, 125)

    # find the contours in the edged image and keep the largest one;
    # we'll assume that this is our piece of paper in the image
    (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    c = max(cnts, key = cv2.contourArea)

    # compute the bounding box of the of the paper region and return it
    return cv2.minAreaRect(c)

def distance_to_camera(knownWidth, focalLength, perWidth):
    # compute and return the distance from the maker to the camera
    return (knownWidth * focalLength) / perWidth


#======================================================================
#main is here

# initialize the known distance from the camera to the object, which
# in this case is 24 inches
KNOWN_DISTANCE = 24.0

# initialize the known object width, which in this case, the piece of
# paper is 11 inches wide
KNOWN_WIDTH = 11.0

frame = cv2.VideoCapture(0)
marker = find_marker(frame)
focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file")
ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size")
args = vars(ap.parse_args())

# if the video argument is None, then we are reading from webcam
if args.get("video", None) is None:
    camera = cv2.VideoCapture(0)
    time.sleep(0.25)

# otherwise, we are reading from a video file
else:
    camera = cv2.VideoCapture(args["video"])

# loop over the frames of the video
while True:
    # grab the current frame and initialize the occupied/unoccupied
    # text
    (grabbed, frame) = camera.read()

    # if the frame could not be grabbed, then we have reached the end
    # of the video
    if not grabbed:
        break

    # resize the frame, convert it to grayscale, and blur it
    frame = imutils.resize(frame, width=500)
    marker = find_marker(frame)
    inches = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0])

    # draw a bounding box around the image and display it
    box = np.int0(cv2.cv.BoxPoints(marker))
    cv2.drawContours(frame, [box], -1, (0, 255, 0), 2)
    cv2.putText(frame, "%.2fft" % (inches / 12),
        (frame.shape[1] - 200, frame.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX,
        2.0, (0, 255, 0), 3)
    cv2.imshow("Frame", frame)
    cv2.waitKey(0)

但是,这是我尝试运行时获得的输出:

Traceback (most recent call last):
  File "testcam.py", line 39, in <module>
    marker = find_marker(frame)
  File "testcam.py", line 10, in find_marker
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
TypeError: src is not a numpy array, neither a scalar

我是opencv的新手,所以我不确定这个错误是什么意思..

1 个答案:

答案 0 :(得分:2)

你正在做的事情是

frame = cv2.VideoCapture(0)

cv2.VideoCapture(0) 初始化捕获设备或相机设备以从您需要调用cap.read()获取帧,但是您传递了提供错误的捕获对象

哪个应该是

capForFocal = cv2.VideoCapture(0)
_,frame=capForFocal.read()
capForFocal.release()