我已经被困了一段时间了。有没有人知道任何可以帮助我使用
的链接(硬件) - Raspberry Pi 3连接到Pi Cam 非网络摄像头
然后使用上面提到的两种硬件,我想使用任何可用的软件,我猜测openCV从顶部开始计算人数。
视频示例:https://www.youtube.com/watch?v=BszUJXLR2oA
使用树莓派的几乎所有可用示例都是从顶部开始计算的人不使用picam ..网络摄像头很大而且体积庞大。所以,如果有任何教程或可用的内容请帮助。谢谢
=============================================== ===========================
我尝试了什么:
所以我遇到的问题是我有一个使用openCV和网络摄像头的示例代码..说明可以在这里找到:https://www.hackster.io/deligence-technologies/person-counting-system-using-opencv-and-python-faf14f
在这段代码中,它使用了一个usb网络摄像头,因此我评论的那条线说“#HERE我需要使用pi cam而不是”那条线正在使用cv2.VideoCapture(0)..我需要知道如何改用picam。任何想法?
import argparse
import datetime
import imutils
import math
import cv2
import numpy as np
width = 800
textIn = 0
textOut = 0
def testIntersectionIn(x, y):
res = -450 * x + 400 * y + 157500
if((res >= -550) and (res < 550)):
print (str(res))
return True
return False
def testIntersectionOut(x, y):
res = -450 * x + 400 * y + 180000
if ((res >= -550) and (res <= 550)):
print (str(res))
return True
return False
camera = cv2.VideoCapture(0) #HERE i need to use the pi cam instead
firstFrame = None
while True:
(grabbed, frame) = camera.read()
text = "Unoccupied"
if not grabbed:
break
frame = imutils.resize(frame, width=width)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if firstFrame is None:
firstFrame = gray
continue
frameDelta = cv2.absdiff(firstFrame, gray)
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
_, cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in cnts:
if cv2.contourArea(c) < 12000:
continue
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.line(frame, (width / 2, 0), (width, 450), (250, 0, 1), 2) #blue line
cv2.line(frame, (width / 2 - 50, 0), (width - 50, 450), (0, 0, 255), 2)#red line
rectagleCenterPont = ((x + x + w) /2, (y + y + h) /2)
cv2.circle(frame, rectagleCenterPont, 1, (0, 0, 255), 5)
if(testIntersectionIn((x + x + w) / 2, (y + y + h) / 2)):
textIn += 1
if(testIntersectionOut((x + x + w) / 2, (y + y + h) / 2)):
textOut += 1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.putText(frame, "In: {}".format(str(textIn)), (10, 50),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.putText(frame, "Out: {}".format(str(textOut)), (10, 70),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
(10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
cv2.imshow("Security Feed", frame)
camera.release()
cv2.destroyAllWindows()
答案 0 :(得分:1)
你说你读过我在评论中发布的链接,但事实显然并非如此。
为清楚起见,this tutorial shows you how to do what you want to do您需要阅读文章中的代码并将其带入您的代码库。
您在代码中尝试执行的操作是打开连接到树莓派的第一个USB网络摄像头。你可以在这一行上这样做:
camera = cv2.VideoCapture(0) #HERE i need to use the pi cam instead
正如你的评论确实陈述的那样。
您需要做的是使用PiCamera库,如下所示:
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
rawCapture = PiRGBArray(camera)
# allow the camera to warmup
time.sleep(0.1)
# grab an image from the camera
camera.capture(rawCapture, format="bgr")
image = rawCapture.array
# display the image on screen and wait for a keypress
cv2.imshow("Image", image)
cv2.waitKey(0)
上面的示例和教程应该让您了解基础知识,然后您可以修改您正在使用的Hackster教程来使用PiCamera。