mirror of
https://github.com/kristoferssolo/Traffic-Light-Detector.git
synced 2026-03-22 00:36:22 +00:00
Added detection, when signal is green
This commit is contained in:
@@ -22,6 +22,8 @@ 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
|
||||
@@ -62,7 +64,4 @@ class TrafficLightDetector:
|
||||
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.name
|
||||
|
||||
def get_signal(self) -> str:
|
||||
return self.signal
|
||||
self.signal_color = color.name
|
||||
|
||||
@@ -1,19 +1,32 @@
|
||||
import cv2
|
||||
from TrafficLightDetector.traffic_light_detector import TrafficLightDetector
|
||||
from loguru import logger
|
||||
|
||||
|
||||
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.sound = sound
|
||||
|
||||
def enable(self) -> None:
|
||||
while True:
|
||||
_, frame = self.video_capture.read()
|
||||
self._set_image(frame)
|
||||
self._outline_traffic_lights()
|
||||
cv2.imshow("Video", self.image)
|
||||
self._get_video()
|
||||
if self.sound:
|
||||
self._make_sound()
|
||||
if cv2.waitKey(1) & 0xFF == ord("q"):
|
||||
break
|
||||
self.video_capture.release()
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user