Skip to content

Commit

Permalink
File commit
Browse files Browse the repository at this point in the history
  • Loading branch information
VijayrajS committed Jul 1, 2020
0 parents commit eaf9b47
Show file tree
Hide file tree
Showing 137 changed files with 26,781 additions and 0 deletions.
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Smart Traffic Analysis

![Build Status](https://sumaid.semaphoreci.com/badges/smart_traffic_analysis/branches/master.svg?style=shields) ![Docs Status](https://readthedocs.org/projects/pip/badge/?version=latest&style=shields)

[Project Documentation](https://sumaid.github.io/smart_traffic_analysis/)

## Overview

Everyday a lot of accidents in the world happen due to the recklessness of drivers. Getting details immediately about the parties involved in the accident manually takes time. Hence, we propose using CCTV cameras and applying computer vision techniques in order to monitor traffic situations. Previous attempts at this have been restricted to one or two aspects of rule-breaking. We want to build a unified platform which will detect cars which don’t adhere to a set of rule-breaking aspects.

## Goals

Building a highly scalable web application with features to upload video or embed live stream directly to the application. Once the user uploads a video/opens the live stream to the platform, he can see the output video stream on the application.
The application will also capture photos of vehicles breaking following rules and store them in a database for future analysis. The app will be built such that it will be flexible to the addition of various other rules if required as well.
The following scenarios will be taken care of by the app implemented in the project::
1. Vehicles should stop for pedestrians.
2. Vehicles should stop if the signal is red.
3. Vehicles should be under the speed limit.
4. Crash detection.

# Building the application

## Clone/Fork repository

First fork or clone this repo:

e.g. `git clone https://github.com/Sumaid/smart_traffic_analysis.git`


## Build images and run containers with docker-compose

Install [Docker Desktop](https://docs.docker.com/get-docker/).

After cloning the repository go inside the project folder:

`cd smart_traffic_analysis`

Run `docker-compose up` which will start a Flask web application for the backend API (default port `8081`) and an Angular frontend served through a webpack development web server (default port `4200`).
Few things to keep in mind:
1. If you want to run docker-compose in background use -b flag.
2. If your one part crashes like if backend crashes, then you can simply fix the issue and run `docker-compose restart backend` in a different terminal to get that part running.
Similar if frontend crashes, just run `docker-compose restart frontend`.

## Access your app

In your browser navigate to: `http://localhost:4200` (or whatever port you defined for the frontend in `docker-compose.yml`).

For testing your backend API I recommend using [Postman](https://www.getpostman.com/) or Curl, but you can also go to `http://localhost:8081/api` ( if you are just testing simple GET requests ).


## Working __without__ docker

I highly recommend the use of docker and docker-compose as it is far simpler to get started. The great thing about docker is you can run one command `docker-compose up` on any operating system & environment and your application will be up and running!
But still if you want to work without docker, you can go through following steps:

### Backend development

Navigate inside the backend directory: `cd backend`

Install pip dependencies: `pip3 install -r requirements.txt`

Run `python3 app.py` in backend root (will watch files and restart server on port `8081` on change).

### Frontend development

Navigate inside the frontend directory: `cd frontend`

Assure you have [Nodejs](https://nodejs.org/en/) installed.

Run following commands:
1. `rm -rf node_modules dist tmp`
2. `npm install --save-dev angular-cli@latest`
3. `npm install`
4. `npm init`

Run `ng serve --host 0.0.0.0 --disableHostCheck --proxy-config proxy.conf.json` in frontend root (will watch files and restart dev-server on port `4200` on change).
All calls made to `/api` will be proxied to backend server (default port for backend `8081`), this can be changed in `proxy.conf.json`.

## Things to keep in mind

1. You can change port numbers in `docker-compose.yml`, but if you change backend port number,
keep in mind to change it in `frontend/proxy.conf.dev.json` also.
2. If you add new requirements, rerun the command `docker-compose up`.
3. Each api route in backend should have base `/api/`, so create apis in the format, `/api/yourroute/`.
4. For any new feature, create a new branch & PR before merging it into master.
83 changes: 83 additions & 0 deletions backend/CompositeRuleObject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os
from ObjectDetectionModule import ObjectDetectionModule

from PedestrianModule import PedestrianModule
from OverspeedingModule import OverspeedingModule
from CrashDetectionModule import CrashDetectionModule
from TrafficSignalModule import TrafficSignalModule

from VideoToFrame import VideoToFrame
from FrameToVideo import FrameToVideo

import numpy as np

class CompositeRuleObject:
def __init__(self, metadata, frame_list, fps):
self.video_frames = frame_list
self.video_fps = fps

self.rule_dict = {
'default': ObjectDetectionModule(np.array(frame_list)),
'crash': CrashDetectionModule(np.array(frame_list)),
'overspeed': OverspeedingModule(np.array(frame_list)),
'pedestrian': PedestrianModule(np.array(frame_list)),
# 'trafficsignal': TrafficSignalModule(frame_list)
}

self.metadata = metadata

def build_rule(self, rule_object):
print(self.metadata)
rule_object.set_frame_rate(self.video_fps)
rule_object.set_metadata(self.metadata)
return rule_object

def rule_list_constructor(self):
rule_list = []
for rule_processor in sorted(self.rule_dict.keys()):
built_rule = self.build_rule(self.rule_dict[rule_processor])
rule_list.append(built_rule)

return rule_list

def process_rules(self):
self.rule_list = self.rule_list_constructor()
rule_list_strings = sorted(self.rule_dict.keys())

for rule in self.rule_list:
rule.process_rule()

def save_videos(self):
rule_names = sorted(self.rule_dict.keys())

for rule, rule_name in zip(self.rule_list, rule_names):
output_frames = rule.output_processed_frames()
frame_to_video_converter = FrameToVideo(output_frames, self.video_fps)

frame_to_video_converter.save_video(rule_name)

def save_snapshots(self):
rule_names = sorted(self.rule_dict.keys())

for rule, rule_name in zip(self.rule_list, rule_names):
rule.output_snapshots()

# if __name__ == '__main__':
# inputfilename = "cars2.mp4"

# metadata = {
# 'speed_limit': 20,
# 'lane_data': [[195.84,165.6],[577.28,163.44],[572.16,158.4],[200.96,161.28]],
# 'zebra_crossing': [[668.16,200.16],[716.8000000000001,221.76],[652.8000000000001,257.76],[555.52,230.39999999999998]],
# 'pedestrian_area': [[552.96,249.12],[564.48,194.4],[628.48,223.92],[628.48,244.07999999999998]],
# 'road_length': 200
# }

# video_frame_convertor = VideoToFrame(inputfilename)
# video_frame_convertor.generate_frames_from_video()
# video_frames, fps = video_frame_convertor.return_frame_data()

# composite_rule_object = CompositeRuleObject(metadata, video_frames, fps)
# composite_rule_object.process_rules()
# composite_rule_object.save_videos()
# composite_rule_object.save_snapshots()
Loading

0 comments on commit eaf9b47

Please sign in to comment.