Skip to content

Commit

Permalink
Merge pull request #1439 from ananas304/main
Browse files Browse the repository at this point in the history
Add initial project files for Parking Space Detection
  • Loading branch information
sanjay-kv authored Oct 14, 2024
2 parents 939eab1 + f1e8667 commit 0734ba3
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 0 deletions.
91 changes: 91 additions & 0 deletions OpenCV Projects/ParkingSpaceDetector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Parking Space Detection

This project allows users to detect available parking spaces in a video feed. The program utilizes OpenCV to process video frames and identifies parking spaces using a pre-defined set of coordinates, which can be manually selected using a mouse-click event. The positions of the parking spaces are saved in a file (`CarParkPos`), and the program then monitors whether each parking space is occupied or vacant.

## Table of Contents
- [Features](#features)
- [Demo](#demo)
- [Project Description](#project-description)
- [File Descriptions](#file-descriptions)
- [Requirements](#requirements)
- [Usage](#usage)
- [How to Load Your Own Video](#how-to-load-your-own-video)
- [Screenshots](#screenshots)

## Features
- Manually select parking spaces by clicking on the video frame.
- Save the selected parking space coordinates using pickle.
- Monitor the parking spaces in real-time and display the number of vacant spaces.
- Works with any video footage of parking lots.

## Demo
- 🖼️ Demo Video of Parking Detection

[empty space detection.webm](https://github.com/user-attachments/assets/ea920495-edf4-42a8-a212-dbee36e6c76f)


## Project Description

This project uses computer vision techniques with OpenCV to detect available parking spaces in a video feed. The system allows users to manually define the parking spaces in a given video by selecting their positions using mouse clicks. Once the spaces are defined, the program monitors each space in real-time to check if it is occupied or vacant based on pixel intensity changes. The workflow involves processing each frame of the video, comparing the state of the selected parking spaces, and updating their occupancy status accordingly.

The technology stack includes:
- **OpenCV** for video processing and real-time space monitoring.
- **Pickle** for saving and loading the parking space positions.
- **CvZone** for assisting with various computer vision operations.



## File Descriptions

- **`CarParkPos.py`**: This script allows users to manually select parking spaces in the video by clicking on the video frame. The positions of the parking spaces are saved to a file (`CarParkPos`) using the `pickle` module, which stores the coordinates for future use. This file acts as a setup step for defining the parking spaces.

- **`main.py`**: This script monitors the parking spaces in real-time. It reads the positions saved from `CarParkPos.py` and processes each video frame to check whether each defined parking space is occupied or vacant. It then displays the real-time status of each parking space on the video and tracks the number of vacant spaces.


## Requirements
`requirements.txt` contains all the required Python libraries.
```txt
opencv-python
cvzone
```

You can install the required packages using the following command:

```bash
pip install -r requirements.txt
```

## Usage

### Step 1: Run `CarParkPos.py`
This script allows you to manually select parking spaces by clicking on the video frame.
- **Left Click** on the video to mark the top-left corner of a parking space.
- **Right Click** to remove a previously selected parking space.

The selected positions are saved in a binary file `CarParkPos` using the `pickle` module.

### Step 2: Run `main.py`
This script will use the saved parking positions from the `CarParkPos` file and start monitoring the parking spaces in the video feed. It will display the number of vacant spaces in real-time.

```bash
python main.py
```

## How to Load Your Own Video
To use this project with your own parking lot video:
1. Rename your video file to `carPark.mp4` or modify the code in both `CarParkPos.py` and `main.py` to point to your video filename.
```python
cap = cv2.VideoCapture('your-video-file.mp4')
```
2. Ensure the video shows a clear view of the parking lot so you can manually mark parking spaces.

3. Run the `CarParkPos.py` script to mark parking spaces in your video, and then run `main.py` to start monitoring those spaces.

## Screenshots
- **Selecting Parking Spaces**:
Below are screenshots from `CarParkPos.py` to demonstrate how parking spaces can be selected:
![Parking Space Selection](https://github.com/ananas304/machine-learning-repos/blob/main/OpenCV%20Projects/ParkingSpaceDetector/carParkPos.png)

- **Detecting Available Spaces**:
Detecting available spaces in real-time by analyzing pixel changes in the predefined parking regions to determine occupancy status.
![Detecting available spaces](https://github.com/ananas304/machine-learning-repos/blob/main/OpenCV%20Projects/ParkingSpaceDetector/empty%20space%20detection.png)
Binary file added OpenCV Projects/ParkingSpaceDetector/carPark.mp4
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions OpenCV Projects/ParkingSpaceDetector/carParkPos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import cv2
import pickle

# Initialize list to hold parking space coordinates
posList = []

# Load video
cap = cv2.VideoCapture('carPark.mp4')

# Load first frame to select parking spaces
success, img = cap.read()

# Mouse callback function to get parking space positions
def mouseClick(events, x, y, flags, params):
if events == cv2.EVENT_LBUTTONDOWN: # If left mouse button is clicked
posList.append((x, y))
if events == cv2.EVENT_RBUTTONDOWN: # If right mouse button is clicked, remove the last point
for i, pos in enumerate(posList):
if pos[0] < x < pos[0] + 107 and pos[1] < y < pos[1] + 48: # Within parking space size
posList.pop(i)

# Save the parking space positions into a file using pickle
with open('CarParkPos', 'wb') as f:
pickle.dump(posList, f)

# Show video frame and enable mouse clicks to mark parking positions
while True:
for pos in posList:
cv2.rectangle(img, pos, (pos[0] + 107, pos[1] + 48), (0, 255, 0), 2) # Draw a rectangle for each parking space

cv2.imshow("Image", img)
cv2.setMouseCallback("Image", mouseClick)

if cv2.waitKey(1) & 0xFF == ord('q'): # Press 'q' to quit
break

# Close video feed
cap.release()
cv2.destroyAllWindows()
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
3 changes: 3 additions & 0 deletions OpenCV Projects/ParkingSpaceDetector/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
opencv-python
cvzone
numpy

0 comments on commit 0734ba3

Please sign in to comment.