我正在使用一个树莓皮B+运行Raspbian喘息和运动一个USB网络摄像头。我的目标是实时测量物体和相机之间的距离。在
跟随a guide on how to do so with still images
这是我当前运行的代码:
# import the necessary packages
import numpy as np
import cv2
import datetime
import time
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
# initialize the known distance from the camera to the object, which
# in this case is 24 inches
KNOWN_DISTANCE = 11.811
# initialize the known object width, which in this case, the piece of
# paper is 11 inches wide
KNOWN_WIDTH = 2.3622
# initialize the list of images that we'll be using
#IMAGE_PATHS = ["images/2ft.png", "images/3ft.png", "images/4ft.png"]
# load the furst image that contains an object that is KNOWN TO BE 2 feet
# from our camera, then find the paper marker in the image, and initialize
# the focal length
#image = cv2.imread(IMAGE_PATHS[0])
#marker = find_marker(image)
#focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH
cap = cv2.VideoCapture(0)
timestamp = datetime.datetime.now()
while(1):
(grabbed, frame) = cap.read()
marker = find_marker(frame)
# for () LOOP THIS TO GET DISTANCE CALCULATION FULLY WORKING!
focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH
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)
ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p")
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.putText(frame, ts, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.35, (0, 0, 255), 1)
#Write to textfile here and send
# for () LOOP End
cv2.imshow("Frame",frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
# loop over the images
#for imagePath in IMAGE_PATHS:
# load the image, find the marker in the image, then compute the
# distance to the marker from the camera
# image = cv2.imread(imagePath)
# marker = find_marker(image)
# 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(image, [box], -1, (0, 255, 0), 2)
# cv2.putText(image, "%.2fft" % (inches / 12),
# (image.shape[1] - 200, image.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX,
# 2.0, (0, 255, 0), 3)
# cv2.imshow("image", image)
# cv2.waitKey(0)
以下是我的输出:
然而,时间(左下角的红色文本)和检测到的距离不会随着程序的运行而改变。有没有办法让这两个值更新到程序结束?在
这就是为什么两个值都没有更新:
时间戳
timestamp
超出了while
循环应该是:
^{pr2}$距离
使用此参数,
distance_to_camera
将生成一个常量输出:等于
KNOWN_DISTANCE
。如果你计算一下:KNOWN_DISTANCE / 12 = 0.98425
是你得到的距离编辑:
我刚刚阅读了教程,看起来您应该在
while
之外只做一次focalLenght
计算。在相关问题 更多 >
编程相关推荐