Muhammad Fatheen

Project · Jan – Apr 2025 · Team of 4

AI Traffic Management System

Fixed timers give an empty road the same green as a full one. This system watches each road with a camera, counts the vehicles with OpenCV, and shares the green time by demand. When a microphone picks up an ambulance siren, it clears the way.

A working rebuild of our competition project: a Python package, a test suite, and the web API this page calls.

See it run

Output of python -m traffic junction on four approach videos and one microphone recording. Each tile is a camera: green boxes are vehicles OpenCV found, the yellow line is where they are counted, and the inset is the foreground mask. The bar along the bottom is the signal controller and the siren detector.

Watch around 1:15, when the siren on the east road is detected and the junction switches to give it green. The sample footage is synthetic, so the true counts are known; recorded clips go in with one command.

Signal plan, cycle by cycle

Each cycle's counts set the next cycle's greens. This run used a 40-second demo cycle (5 to 20 seconds per road) so several cycles fit in the clip.

CycleStartsGreen N · E · S · W (s)Counted N · E · S · W

    Counting accuracy on the sample footage

    Vehicles heading to the junction that crossed the line, against what the cameras counted.

    RoadActualCountedAccuracy

    Misses are mostly dark vehicles on dark asphalt and vehicles too close to separate. That is the main limit of background subtraction.

    Try the API

    Checking the API…

    These call the same Python code running on the server. Use the sample files or your own.

    1 · Count vehicles in a video

    A fixed camera looking along a road, under 4 MB. The first 30 seconds are processed.

    2 · Turn counts into green times

    Vehicles counted on each road last cycle. 120-second cycle, 10 to 60 seconds each.

    3 · Listen for a siren

    A WAV recording from the roadside, under 4 MB. An FFT compares the siren band (600 to 1,600 Hz) with the rest of the sound.

    How the code works

    Four small modules, each tested on its own, joined by junction.py.

    1. Find what moves

      traffic/detector.py · MOG2 learns the empty road and marks anything new. Shadows come back grey, so a threshold drops them. Opening removes specks, dilation joins broken shapes, and contours outline each vehicle.

      gray = cv2.GaussianBlur(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), (5, 5), 0)
      fg = self.mog.apply(gray)
      _, fg = cv2.threshold(fg, 200, 255, cv2.THRESH_BINARY)   # shadows are 127
      fg = cv2.morphologyEx(fg, cv2.MORPH_OPEN, self.k_open)
      fg = cv2.dilate(fg, self.k_dilate)
      contours, _ = cv2.findContours(fg, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
      found = [cv2.boundingRect(c) for c in contours
               if cv2.contourArea(c) >= self.cfg.min_area]
    2. Count each vehicle once

      traffic/tracker.py · Each centre is matched to the nearest track from the last frame. A vehicle counts when its centre crosses the line in the direction of the junction, and only once.

      pairs.sort()                              # closest matches first
      for d, ti, ci in pairs:
          ...
          prev_y = tr.y
          tr.x, tr.y = centres[ci]
          if not tr.counted and prev_y < self.line_y <= tr.y:
              self.down += 1                    # crossed, heading to the junction
              tr.counted = True
    3. Share the green

      traffic/signal.py · Next cycle, each road's green is its share of the count, kept between a minimum and a maximum. The controller then runs green, yellow and all-red for each road in turn.

      def green_times(counts, cycle=120, min_green=10, max_green=60):
          total = sum(counts)
          if total == 0:
              return [round(min(max_green, max(min_green, cycle / len(counts))))] * len(counts)
          return [round(min(max_green, max(min_green, n / total * cycle)))
                  for n in counts]
      
      green_times([24, 14, 10, 2])  # [58, 34, 24, 10]
    4. Hear the siren

      traffic/siren.py · Every quarter second, an FFT measures power inside the siren band against power outside it. Above 4× for 0.8 seconds counts as a siren, which ends the current green safely and holds green on that road.

      power = np.abs(np.fft.rfft(window * np.hanning(len(window)))) ** 2
      freqs = np.fft.rfftfreq(len(window), 1 / rate)
      inside = (freqs >= 600) & (freqs <= 1600)
      ratio = power[inside].mean() / power[~inside].mean()
      # ratio >= 4.0 for 0.8 s in a row -> controller.emergency(road)

    Project layout

    traffic/
      config.py      every tunable number
      detector.py    OpenCV background subtraction
      tracker.py     centroid tracking, line counting
      signal.py      green times, phases, emergencies
      siren.py       FFT siren detection
      junction.py    runs it all on four videos
      cli.py         python -m traffic …
    api/index.py     FastAPI, served on Vercel
    tests/           21 pytest tests
    scripts/make_samples.py

    Run it yourself

    pip install -r requirements.txt
    python scripts/make_samples.py
    python -m traffic plan 24 14 10 2
    python -m traffic count samples/north.mp4 --show
    python -m traffic junction \
      --north samples/north.mp4 --east samples/east.mp4 \
      --south samples/south.mp4 --west samples/west.mp4 \
      --siren samples/siren_east.wav --siren-road east \
      --out output/junction.mp4 --json output/results.json
    pytest

    Limits and next steps

    Next: a trained detector such as YOLO for jams and night, queue length as the input, and linked junctions. A pilot would compare average wait and ambulance clearance time against the fixed timer at one junction.