Skip to content

Commit 59b295d

Browse files
Add files via upload
0 parents  commit 59b295d

File tree

8 files changed

+87
-0
lines changed

8 files changed

+87
-0
lines changed

CarParkPos

521 Bytes
Binary file not shown.

ParkingSpacePicker.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import cv2
2+
import pickle
3+
4+
width, height = 107, 48
5+
6+
try:
7+
with open('TniParkPos', 'rb') as f:
8+
posList = pickle.load(f)
9+
except:
10+
posList = []
11+
12+
def mouseClick(events, x, y, flags, params):
13+
if events == cv2.EVENT_LBUTTONDOWN:
14+
posList.append((x, y))
15+
if events == cv2.EVENT_RBUTTONDOWN:
16+
for i, pos in enumerate(posList):
17+
x1, y1 = pos
18+
if x1 < x < x1 + width and y1 < y < y1 + height:
19+
posList.pop(i)
20+
21+
with open('TniParkPos', 'wb') as f:
22+
pickle.dump(posList, f)
23+
24+
while True:
25+
img = cv2.imread('TniParkImg.png')
26+
for pos in posList:
27+
cv2.rectangle(img, pos, (pos[0] + width, pos[1] + height), (255, 0, 255), 2)
28+
29+
cv2.imshow("Image", img)
30+
cv2.setMouseCallback("Image", mouseClick)
31+
cv2.waitKey(1)

TniPark.mp4

19.6 MB
Binary file not shown.

TniParkImg.png

3.24 MB
Loading

TniParkPos

163 Bytes
Binary file not shown.

carPark.mp4

10.1 MB
Binary file not shown.

carParkImg.png

988 KB
Loading

main.py

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

0 commit comments

Comments
 (0)