Skip to content

Commit ca1ff17

Browse files
authored
Create carParkPos.py
1 parent 26d8ae0 commit ca1ff17

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import cv2
2+
import pickle
3+
4+
# Initialize list to hold parking space coordinates
5+
posList = []
6+
7+
# Load video
8+
cap = cv2.VideoCapture('carPark.mp4')
9+
10+
# Load first frame to select parking spaces
11+
success, img = cap.read()
12+
13+
# Mouse callback function to get parking space positions
14+
def mouseClick(events, x, y, flags, params):
15+
if events == cv2.EVENT_LBUTTONDOWN: # If left mouse button is clicked
16+
posList.append((x, y))
17+
if events == cv2.EVENT_RBUTTONDOWN: # If right mouse button is clicked, remove the last point
18+
for i, pos in enumerate(posList):
19+
if pos[0] < x < pos[0] + 107 and pos[1] < y < pos[1] + 48: # Within parking space size
20+
posList.pop(i)
21+
22+
# Save the parking space positions into a file using pickle
23+
with open('CarParkPos', 'wb') as f:
24+
pickle.dump(posList, f)
25+
26+
# Show video frame and enable mouse clicks to mark parking positions
27+
while True:
28+
for pos in posList:
29+
cv2.rectangle(img, pos, (pos[0] + 107, pos[1] + 48), (0, 255, 0), 2) # Draw a rectangle for each parking space
30+
31+
cv2.imshow("Image", img)
32+
cv2.setMouseCallback("Image", mouseClick)
33+
34+
if cv2.waitKey(1) & 0xFF == ord('q'): # Press 'q' to quit
35+
break
36+
37+
# Close video feed
38+
cap.release()
39+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)