from picamera.array import PiRGBArray
from picamera import PiCamera
import numpy as np
import time
import datetime
import cv2
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(0.1)
# capture frames from the camera
def nothing(x):
pass
cv2.namedWindow("Parameters")
cv2.resizeWindow("Parameters",640,150)
cv2.createTrackbar("Threshold1","Parameters",102,255,nothing)
cv2.createTrackbar("Threshold2","Parameters",135,255,nothing)
cv2.createTrackbar("Area","Parameters",3913,30000,nothing)
用于物体检测
def stackImages(scale,imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0], list)
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
if rowsAvailable:
for x in range ( 0, rows):
for y in range(0, cols):
if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
else:
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((height, width, 3), np.uint8)
hor = [imageBlank]*rows
hor_con = [imageBlank]*rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
ver = np.vstack(hor)
else:
for x in range(0, rows):
if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
else:
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
hor= np.hstack(imgArray)
ver = hor
return ver
//用于获取轮廓 对于camera.capture_continuous(rawCapture,format =“ bgr”,use_video_port = True)中的帧: def getContours(img,imgContour): _,轮廓,层次结构= cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) 对于轮廓的cnt: 面积= cv2.contourArea(cnt) areaMin = cv2.getTrackbarPos(“ Area”,“ Parameters”) 如果area> areaMin: cv2.drawContours(imgContour,cnt,-1,(0,255,0),7) 围= cv2.arcLength(cnt,True) 大约= cv2.approxPolyDP(cnt,0.02 * peri,True) x,y,w,h = cv2.boundingRect(大约) 文字=“已占用” cv2.putText(imgContour,“状态:{}”。format(文本),(10,20), cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255),2)
success, img = camera.read()
cv2.putText(img, "Status : ", (10, 20),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.putText(img, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S %p"),(10, img.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
imgContour = img.copy()
imgBlur = cv2.GaussianBlur(img, (7, 7), 1)
imgGray = cv2.cvtColor(imgBlur, cv2.COLOR_BGR2GRAY)
threshold1 = cv2.getTrackbarPos("Threshold1", "Parameters")
threshold2 = cv2.getTrackbarPos("Threshold2", "Parameters")
imgCanny = cv2.Canny(imgGray,threshold1,threshold2)
kernel = np.ones((5, 5))
imgDil = cv2.dilate(imgCanny, kernel, iterations=1)
getContours(imgDil,imgContour)
imgStack = stackImages(0.8,([imgContour]))
cv2.imshow("Result", imgStack)
if cv2.waitKey(1) & 0xFF == ord('q'):
break