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/*
|
||||||
!/assets/haar
|
!/assets/haar
|
||||||
model.h5
|
|
||||||
debug
|
debug
|
||||||
.logs/*
|
.logs/*
|
||||||
|
|||||||
8
main.py
8
main.py
@ -19,10 +19,10 @@ parser.add_argument(
|
|||||||
help="reads webcam inputs to determine traffic light color",
|
help="reads webcam inputs to determine traffic light color",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-c",
|
"-a",
|
||||||
"--change",
|
"--audio",
|
||||||
action="store_true",
|
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 = TrafficLightDetectorWebcam(0) # Change number if webcam didn't detect
|
||||||
camera.enable()
|
camera.enable()
|
||||||
|
|
||||||
if args.change:
|
if args.audio:
|
||||||
from TrafficLightDetector.traffic_light_webcam import TrafficLightDetectorWebcam
|
from TrafficLightDetector.traffic_light_webcam import TrafficLightDetectorWebcam
|
||||||
camera = TrafficLightDetectorWebcam(0, sound=True) # Change number if webcam didn't detect
|
camera = TrafficLightDetectorWebcam(0, sound=True) # Change number if webcam didn't detect
|
||||||
camera.enable()
|
camera.enable()
|
||||||
|
|||||||
@ -1,2 +1,4 @@
|
|||||||
|
PyGObject==3.42.2
|
||||||
loguru==0.6.0
|
loguru==0.6.0
|
||||||
opencv-python==4.6.0.66
|
opencv-python==4.6.0.66
|
||||||
|
playsound==1.3.0
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[metadata]
|
[metadata]
|
||||||
name = TrafficLightDetector
|
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
|
author = Kristofers Solo
|
||||||
license = MIT
|
license = MIT
|
||||||
license_file = LICENSE
|
license_file = LICENSE
|
||||||
@ -11,8 +11,10 @@ classifiers =
|
|||||||
[options]
|
[options]
|
||||||
packages = TrafficLightDetector
|
packages = TrafficLightDetector
|
||||||
install_requires =
|
install_requires =
|
||||||
|
PyGObject>=3.42.2
|
||||||
loguru>=0.6.0
|
loguru>=0.6.0
|
||||||
opencv-python>=4.6.0.66
|
opencv-python>=4.6.0.66
|
||||||
|
playsound>=1.3.0
|
||||||
|
|
||||||
python_requires = >=3.10
|
python_requires = >=3.10
|
||||||
package_dir = =src
|
package_dir = =src
|
||||||
|
|||||||
@ -22,8 +22,6 @@ class TrafficLightDetector:
|
|||||||
YELLOW = (0, 175, 225)
|
YELLOW = (0, 175, 225)
|
||||||
GREEN = (0, 150, 0)
|
GREEN = (0, 150, 0)
|
||||||
|
|
||||||
signal_color = ""
|
|
||||||
|
|
||||||
def _set_image(self, image=None, roi=None, detectTrafficLights=True) -> None:
|
def _set_image(self, image=None, roi=None, detectTrafficLights=True) -> None:
|
||||||
self.image = image
|
self.image = image
|
||||||
self.roi = self.image if roi is None else roi
|
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.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.colors = [self.red, self.yellow, self.green]
|
||||||
self.detect_traffic_lights = detectTrafficLights
|
self.detect_traffic_lights = detectTrafficLights
|
||||||
|
self.signal_color = ""
|
||||||
|
|
||||||
def _outline_traffic_lights(self) -> None:
|
def _outline_traffic_lights(self) -> None:
|
||||||
gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
|
gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
|
||||||
@ -46,7 +45,8 @@ class TrafficLightDetector:
|
|||||||
|
|
||||||
def _draw_circle(self) -> None:
|
def _draw_circle(self) -> None:
|
||||||
for color in self.colors:
|
for color in self.colors:
|
||||||
if color.circle is not None:
|
if color.circle is None:
|
||||||
|
continue
|
||||||
for value in color.circle[0, :]:
|
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:
|
if value[0] > self.size[1] or value[1] > self.size[0] or value[1] > self.size[0] * self.BOUNDARY:
|
||||||
continue
|
continue
|
||||||
|
|||||||
@ -9,12 +9,11 @@ class TrafficLightDetectorImages(TrafficLightDetector):
|
|||||||
|
|
||||||
def __init__(self, path: Path) -> None:
|
def __init__(self, path: Path) -> None:
|
||||||
self.path = path
|
self.path = path
|
||||||
image = cv2.imread(str(path))
|
self._set_image(cv2.imread(str(self.path)))
|
||||||
self._set_image(image)
|
|
||||||
|
|
||||||
def _save_image(self) -> None:
|
def _save_image(self) -> None:
|
||||||
cv2.imwrite(str(IMAGES_OUT_PATH.joinpath(self.path.name)), self.image)
|
cv2.imwrite(str(IMAGES_OUT_PATH.joinpath(self.path.name)), self.image)
|
||||||
|
|
||||||
def draw(self) -> None:
|
def draw(self) -> None:
|
||||||
self._draw_circle()
|
self._outline_traffic_lights()
|
||||||
self._save_image()
|
self._save_image()
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import cv2
|
import cv2
|
||||||
|
from paths import SOUND_PATH
|
||||||
|
from playsound import playsound
|
||||||
from TrafficLightDetector.traffic_light_detector import TrafficLightDetector
|
from TrafficLightDetector.traffic_light_detector import TrafficLightDetector
|
||||||
from loguru import logger
|
|
||||||
|
|
||||||
|
|
||||||
class TrafficLightDetectorWebcam(TrafficLightDetector):
|
class TrafficLightDetectorWebcam(TrafficLightDetector):
|
||||||
@ -26,7 +27,5 @@ class TrafficLightDetectorWebcam(TrafficLightDetector):
|
|||||||
cv2.imshow("Video", self.image)
|
cv2.imshow("Video", self.image)
|
||||||
|
|
||||||
def _make_sound(self) -> None:
|
def _make_sound(self) -> None:
|
||||||
"""Do some sound if green light"""
|
|
||||||
if self.signal_color == "GREEN":
|
if self.signal_color == "GREEN":
|
||||||
# DO SOME SOUND
|
playsound(str(SOUND_PATH.joinpath("move.mp3")))
|
||||||
logger.debug("DRIVE")
|
|
||||||
|
|||||||
@ -10,8 +10,9 @@ ASSETS_PATH = BASE_PATH.joinpath("assets")
|
|||||||
IMAGES_IN_PATH = ASSETS_PATH.joinpath("images_in")
|
IMAGES_IN_PATH = ASSETS_PATH.joinpath("images_in")
|
||||||
IMAGES_OUT_PATH = ASSETS_PATH.joinpath("images_out")
|
IMAGES_OUT_PATH = ASSETS_PATH.joinpath("images_out")
|
||||||
HAAR_PATH = ASSETS_PATH.joinpath("haar").joinpath("TrafficLights.xml")
|
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"
|
log_level = "DEBUG" if BASE_PATH.joinpath("debug").exists() else "INFO"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user