import cv2
import pickle
import cvzone
import numpy as np
#video feed
cap = cv2.VideoCapture(0)
with open("CarParkPos", "rb") as f:
posList = pickle.load(f)
width, height = 100, 50
def checkParkingSpace(frameProcess):
for pos in posList:
x,y = pos
frameCrop = frame[y:y+height, x:x+width]
cv2.imshow(str(x*y), frameCrop)
count = cv2.countNonZero(frameCrop)
cvzone.putTextRect(frame, "asd", (x,y+height-3), scale = 1, thickness=2, offset=0)cv2.countNonZero在实时摄像机中计数像素时似乎不起作用,但在使用视频文件时工作得很好。
#converted rgb camera to black and white
while True:
ret, frame = cap.read()
frameGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frameBlur = cv2.GaussianBlur(frameGray, (3,3), 1)
frameThreshold = cv2.adaptiveThreshold(frameBlur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 25, 16)
frameMedian = cv2.medianBlur(frameThreshold,5)
kernel = np.ones((1,1), np.uint8)
frameDilate = cv2.dilate(frameMedian,kernel, iterations = 1)
checkParkingSpace(frameDilate)
#writing the rectangle shapes in camera
for pos in posList:
cv2.rectangle(frame, pos, (pos[0] + width, pos[1] + height), (0, 200, 0), 2)
cv2.imshow("Frame", frame)
cv2.waitKey(1)我不完全理解所有的代码,因为我刚刚看了一个教程。我想要做的正是视频https://www.youtube.com/watch?v=caKnQlCMIYI中的那个,但是我不想使用视频文件,而是想使用一个实时摄像头。
发布于 2022-05-30 11:19:09
for pos in posList:
x,y = pos
frameCrop = frame[y:y+height, x:x+width]
cv2.imshow(str(x*y), frameCrop)
count = cv2.countNonZero(cv2.Canny(frameCrop, 100, 200))
cvzone.putTextRect(frame, str(count), (x,y+height-3), scale = 1, thickness=2, offset=0)我做的是精明的帧,然后计数非黑色像素。
https://stackoverflow.com/questions/72431249
复制相似问题