Skip to content

Commit cb17309

Browse files
authored
Create main.py
1 parent fc5b462 commit cb17309

File tree

1 file changed

+57
-0
lines changed
  • OpenCV Projects/ParkingSpaceDetector

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import cv2
2+
import pickle
3+
import cvzone
4+
import numpy as np
5+
6+
# Video feed
7+
cap = cv2.VideoCapture('carPark.mp4')
8+
9+
with open('CarParkPos', 'rb') as f:
10+
posList = pickle.load(f)
11+
12+
width, height = 107, 48
13+
14+
15+
def checkParkingSpace(imgPro):
16+
spaceCounter = 0
17+
18+
for pos in posList:
19+
x, y = pos
20+
21+
imgCrop = imgPro[y:y + height, x:x + width]
22+
# cv2.imshow(str(x * y), imgCrop)
23+
count = cv2.countNonZero(imgCrop)
24+
25+
26+
if count < 900:
27+
color = (0, 255, 0)
28+
thickness = 5
29+
spaceCounter += 1
30+
else:
31+
color = (0, 0, 255)
32+
thickness = 2
33+
34+
cv2.rectangle(img, pos, (pos[0] + width, pos[1] + height), color, thickness)
35+
cvzone.putTextRect(img, str(count), (x, y + height - 3), scale=1,
36+
thickness=2, offset=0, colorR=color)
37+
38+
cvzone.putTextRect(img, f'Free: {spaceCounter}/{len(posList)}', (100, 50), scale=3,
39+
thickness=5, offset=20, colorR=(0,200,0))
40+
while True:
41+
42+
if cap.get(cv2.CAP_PROP_POS_FRAMES) == cap.get(cv2.CAP_PROP_FRAME_COUNT):
43+
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
44+
success, img = cap.read()
45+
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
46+
imgBlur = cv2.GaussianBlur(imgGray, (3, 3), 1)
47+
imgThreshold = cv2.adaptiveThreshold(imgBlur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
48+
cv2.THRESH_BINARY_INV, 25, 16)
49+
imgMedian = cv2.medianBlur(imgThreshold, 5)
50+
kernel = np.ones((3, 3), np.uint8)
51+
imgDilate = cv2.dilate(imgMedian, kernel, iterations=1)
52+
53+
checkParkingSpace(imgDilate)
54+
cv2.imshow("Image", img)
55+
# cv2.imshow("ImageBlur", imgBlur)
56+
# cv2.imshow("ImageThres", imgMedian)
57+
cv2.waitKey(10)

0 commit comments

Comments
 (0)