mirror of
https://github.com/kristoferssolo/Traffic-Light-Detector.git
synced 2025-10-21 20:00:36 +00:00
Added detection, when signal is green
This commit is contained in:
parent
322c40669b
commit
b359ec7ed1
26
main.py
26
main.py
@ -6,6 +6,12 @@ from paths import create_dirs, IMAGES_IN_PATH
|
|||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="Traffic light detection script.")
|
parser = argparse.ArgumentParser(description="Traffic light detection script.")
|
||||||
|
parser.add_argument(
|
||||||
|
"-i",
|
||||||
|
"--image",
|
||||||
|
action="store_true",
|
||||||
|
help="detects traffic lights in images located in ./assets/images_in/",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-w",
|
"-w",
|
||||||
"--webcam",
|
"--webcam",
|
||||||
@ -13,20 +19,16 @@ 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(
|
||||||
"-i",
|
"-c",
|
||||||
"--image",
|
"--change",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="detects traffic lights in images located in ./assets/images_in/",
|
help="detects traffic lights change",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@logger.catch
|
@logger.catch
|
||||||
def main(args) -> None:
|
def main(args) -> None:
|
||||||
create_dirs()
|
create_dirs()
|
||||||
if args.webcam:
|
|
||||||
from TrafficLightDetector.traffic_light_webcam import TrafficLightDetectorWebcam
|
|
||||||
camera = TrafficLightDetectorWebcam(0) # Change number if webcam didn't detect
|
|
||||||
camera.enable()
|
|
||||||
|
|
||||||
if args.image:
|
if args.image:
|
||||||
from TrafficLightDetector.traffic_light_images import TrafficLightDetectorImages
|
from TrafficLightDetector.traffic_light_images import TrafficLightDetectorImages
|
||||||
@ -34,6 +36,16 @@ def main(args) -> None:
|
|||||||
image = TrafficLightDetectorImages(path)
|
image = TrafficLightDetectorImages(path)
|
||||||
image.draw()
|
image.draw()
|
||||||
|
|
||||||
|
if args.webcam:
|
||||||
|
from TrafficLightDetector.traffic_light_webcam import TrafficLightDetectorWebcam
|
||||||
|
camera = TrafficLightDetectorWebcam(0) # Change number if webcam didn't detect
|
||||||
|
camera.enable()
|
||||||
|
|
||||||
|
if args.change:
|
||||||
|
from TrafficLightDetector.traffic_light_webcam import TrafficLightDetectorWebcam
|
||||||
|
camera = TrafficLightDetectorWebcam(0, sound=True) # Change number if webcam didn't detect
|
||||||
|
camera.enable()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|||||||
@ -22,6 +22,8 @@ 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
|
||||||
@ -62,7 +64,4 @@ class TrafficLightDetector:
|
|||||||
if self.TEXT:
|
if self.TEXT:
|
||||||
cv2.putText(self.roi if self.detect_traffic_lights else self.image, color.name,
|
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
|
(value[0], value[1]), self.FONT, 1, color.color, 2, cv2.LINE_AA) # draws text
|
||||||
self.signal = color.name
|
self.signal_color = color.name
|
||||||
|
|
||||||
def get_signal(self) -> str:
|
|
||||||
return self.signal
|
|
||||||
|
|||||||
@ -1,19 +1,32 @@
|
|||||||
import cv2
|
import cv2
|
||||||
from TrafficLightDetector.traffic_light_detector import TrafficLightDetector
|
from TrafficLightDetector.traffic_light_detector import TrafficLightDetector
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
|
||||||
class TrafficLightDetectorWebcam(TrafficLightDetector):
|
class TrafficLightDetectorWebcam(TrafficLightDetector):
|
||||||
|
|
||||||
def __init__(self, source: int) -> None:
|
def __init__(self, source: int, sound: bool = False) -> None:
|
||||||
self.video_capture = cv2.VideoCapture(source)
|
self.video_capture = cv2.VideoCapture(source)
|
||||||
|
self.sound = sound
|
||||||
|
|
||||||
def enable(self) -> None:
|
def enable(self) -> None:
|
||||||
while True:
|
while True:
|
||||||
_, frame = self.video_capture.read()
|
self._get_video()
|
||||||
self._set_image(frame)
|
if self.sound:
|
||||||
self._outline_traffic_lights()
|
self._make_sound()
|
||||||
cv2.imshow("Video", self.image)
|
|
||||||
if cv2.waitKey(1) & 0xFF == ord("q"):
|
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||||
break
|
break
|
||||||
self.video_capture.release()
|
self.video_capture.release()
|
||||||
cv2.destroyAllWindows()
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
|
def _get_video(self) -> None:
|
||||||
|
_, frame = self.video_capture.read()
|
||||||
|
self._set_image(frame)
|
||||||
|
self._outline_traffic_lights()
|
||||||
|
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")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user