mirror of
https://github.com/kristoferssolo/Traffic-Light-Detector.git
synced 2025-10-21 20:00:36 +00:00
Added playing sound option
This commit is contained in:
parent
b359ec7ed1
commit
9733586563
1
.gitignore
vendored
1
.gitignore
vendored
@ -130,6 +130,5 @@ dmypy.json
|
||||
|
||||
assets/*
|
||||
!/assets/haar
|
||||
model.h5
|
||||
debug
|
||||
.logs/*
|
||||
|
||||
8
main.py
8
main.py
@ -19,10 +19,10 @@ parser.add_argument(
|
||||
help="reads webcam inputs to determine traffic light color",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-c",
|
||||
"--change",
|
||||
"-a",
|
||||
"--audio",
|
||||
action="store_true",
|
||||
help="detects traffic lights change",
|
||||
help="plays audio when green light is detected",
|
||||
)
|
||||
|
||||
|
||||
@ -41,7 +41,7 @@ def main(args) -> None:
|
||||
camera = TrafficLightDetectorWebcam(0) # Change number if webcam didn't detect
|
||||
camera.enable()
|
||||
|
||||
if args.change:
|
||||
if args.audio:
|
||||
from TrafficLightDetector.traffic_light_webcam import TrafficLightDetectorWebcam
|
||||
camera = TrafficLightDetectorWebcam(0, sound=True) # Change number if webcam didn't detect
|
||||
camera.enable()
|
||||
|
||||
@ -1,2 +1,4 @@
|
||||
PyGObject==3.42.2
|
||||
loguru==0.6.0
|
||||
opencv-python==4.6.0.66
|
||||
playsound==1.3.0
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[metadata]
|
||||
name = TrafficLightDetector
|
||||
desctiption = Reduce traffic congestion effects due to human reaction times at intersections with traffic lights by utilizing a computer system based on direct communication method of changes in the circumscribed route intersection.
|
||||
description = Reduce traffic congestion effects due to human reaction times at intersections with traffic lights by utilizing a computer system based on direct communication method of changes in the circumscribed route intersection.
|
||||
author = Kristofers Solo
|
||||
license = MIT
|
||||
license_file = LICENSE
|
||||
@ -11,8 +11,10 @@ classifiers =
|
||||
[options]
|
||||
packages = TrafficLightDetector
|
||||
install_requires =
|
||||
PyGObject>=3.42.2
|
||||
loguru>=0.6.0
|
||||
opencv-python>=4.6.0.66
|
||||
playsound>=1.3.0
|
||||
|
||||
python_requires = >=3.10
|
||||
package_dir = =src
|
||||
|
||||
@ -22,8 +22,6 @@ class TrafficLightDetector:
|
||||
YELLOW = (0, 175, 225)
|
||||
GREEN = (0, 150, 0)
|
||||
|
||||
signal_color = ""
|
||||
|
||||
def _set_image(self, image=None, roi=None, detectTrafficLights=True) -> None:
|
||||
self.image = image
|
||||
self.roi = self.image if roi is None else roi
|
||||
@ -34,6 +32,7 @@ class TrafficLightDetector:
|
||||
self.green = Color("GREEN", self.GREEN, self.GREEN_LOWER, self.GREEN_UPPER, hsv, minDist=30, param2=5)
|
||||
self.colors = [self.red, self.yellow, self.green]
|
||||
self.detect_traffic_lights = detectTrafficLights
|
||||
self.signal_color = ""
|
||||
|
||||
def _outline_traffic_lights(self) -> None:
|
||||
gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
|
||||
@ -46,22 +45,23 @@ class TrafficLightDetector:
|
||||
|
||||
def _draw_circle(self) -> None:
|
||||
for color in self.colors:
|
||||
if color.circle is not None:
|
||||
for value in color.circle[0, :]:
|
||||
if value[0] > self.size[1] or value[1] > self.size[0] or value[1] > self.size[0] * self.BOUNDARY:
|
||||
continue
|
||||
if color.circle is None:
|
||||
continue
|
||||
for value in color.circle[0, :]:
|
||||
if value[0] > self.size[1] or value[1] > self.size[0] or value[1] > self.size[0] * self.BOUNDARY:
|
||||
continue
|
||||
|
||||
h, s = 0, 0
|
||||
for i in range(-self.RADIUS, self.RADIUS):
|
||||
for j in range(-self.RADIUS, self.RADIUS):
|
||||
if (value[1] + i) >= self.size[0] or (value[0] + j) >= self.size[1]:
|
||||
continue
|
||||
h += color.mask[value[1] + i, value[0] + j]
|
||||
s += 1
|
||||
if h / s > 100:
|
||||
cv2.circle(self.roi if self.detect_traffic_lights else self.image, (value[0], value[1]), value[2] + 10, color.color, 2) # draws circle
|
||||
cv2.circle(color.mask, (value[0], value[1]), value[2] + 30, (255, 255, 255), 2)
|
||||
if self.TEXT:
|
||||
cv2.putText(self.roi if self.detect_traffic_lights else self.image, color.name,
|
||||
(value[0], value[1]), self.FONT, 1, color.color, 2, cv2.LINE_AA) # draws text
|
||||
self.signal_color = color.name
|
||||
h, s = 0, 0
|
||||
for i in range(-self.RADIUS, self.RADIUS):
|
||||
for j in range(-self.RADIUS, self.RADIUS):
|
||||
if (value[1] + i) >= self.size[0] or (value[0] + j) >= self.size[1]:
|
||||
continue
|
||||
h += color.mask[value[1] + i, value[0] + j]
|
||||
s += 1
|
||||
if h / s > 100:
|
||||
cv2.circle(self.roi if self.detect_traffic_lights else self.image, (value[0], value[1]), value[2] + 10, color.color, 2) # draws circle
|
||||
cv2.circle(color.mask, (value[0], value[1]), value[2] + 30, (255, 255, 255), 2)
|
||||
if self.TEXT:
|
||||
cv2.putText(self.roi if self.detect_traffic_lights else self.image, color.name,
|
||||
(value[0], value[1]), self.FONT, 1, color.color, 2, cv2.LINE_AA) # draws text
|
||||
self.signal_color = color.name
|
||||
|
||||
@ -9,12 +9,11 @@ class TrafficLightDetectorImages(TrafficLightDetector):
|
||||
|
||||
def __init__(self, path: Path) -> None:
|
||||
self.path = path
|
||||
image = cv2.imread(str(path))
|
||||
self._set_image(image)
|
||||
self._set_image(cv2.imread(str(self.path)))
|
||||
|
||||
def _save_image(self) -> None:
|
||||
cv2.imwrite(str(IMAGES_OUT_PATH.joinpath(self.path.name)), self.image)
|
||||
|
||||
def draw(self) -> None:
|
||||
self._draw_circle()
|
||||
self._outline_traffic_lights()
|
||||
self._save_image()
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import cv2
|
||||
from paths import SOUND_PATH
|
||||
from playsound import playsound
|
||||
from TrafficLightDetector.traffic_light_detector import TrafficLightDetector
|
||||
from loguru import logger
|
||||
|
||||
|
||||
class TrafficLightDetectorWebcam(TrafficLightDetector):
|
||||
@ -26,7 +27,5 @@ class TrafficLightDetectorWebcam(TrafficLightDetector):
|
||||
cv2.imshow("Video", self.image)
|
||||
|
||||
def _make_sound(self) -> None:
|
||||
"""Do some sound if green light"""
|
||||
if self.signal_color == "GREEN":
|
||||
# DO SOME SOUND
|
||||
logger.debug("DRIVE")
|
||||
playsound(str(SOUND_PATH.joinpath("move.mp3")))
|
||||
|
||||
@ -10,8 +10,9 @@ ASSETS_PATH = BASE_PATH.joinpath("assets")
|
||||
IMAGES_IN_PATH = ASSETS_PATH.joinpath("images_in")
|
||||
IMAGES_OUT_PATH = ASSETS_PATH.joinpath("images_out")
|
||||
HAAR_PATH = ASSETS_PATH.joinpath("haar").joinpath("TrafficLights.xml")
|
||||
SOUND_PATH = ASSETS_PATH.joinpath("sound")
|
||||
|
||||
PATHS = (LOGS_PATH, IMAGES_IN_PATH, IMAGES_OUT_PATH)
|
||||
PATHS = (LOGS_PATH, IMAGES_IN_PATH, IMAGES_OUT_PATH, SOUND_PATH)
|
||||
|
||||
|
||||
log_level = "DEBUG" if BASE_PATH.joinpath("debug").exists() else "INFO"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user