├── 01b6e60239fc0d5f8e6ea557be35726e.png ├── 19b29958d9866212e41bb31ad96f26e2.png ├── 97d33f3c236ae7f4531e56d2dad58015.png ├── dc834c19a7bd0efb6bec39c3e62e517a.png ├── ff93aac71249d792d5ff851de0e663af.png ├── TLState.py └── README.md /01b6e60239fc0d5f8e6ea557be35726e.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qunshansj/python-yolo-deepsort-traffic-light-violation/HEAD/01b6e60239fc0d5f8e6ea557be35726e.png -------------------------------------------------------------------------------- /19b29958d9866212e41bb31ad96f26e2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qunshansj/python-yolo-deepsort-traffic-light-violation/HEAD/19b29958d9866212e41bb31ad96f26e2.png -------------------------------------------------------------------------------- /97d33f3c236ae7f4531e56d2dad58015.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qunshansj/python-yolo-deepsort-traffic-light-violation/HEAD/97d33f3c236ae7f4531e56d2dad58015.png -------------------------------------------------------------------------------- /dc834c19a7bd0efb6bec39c3e62e517a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qunshansj/python-yolo-deepsort-traffic-light-violation/HEAD/dc834c19a7bd0efb6bec39c3e62e517a.png -------------------------------------------------------------------------------- /ff93aac71249d792d5ff851de0e663af.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qunshansj/python-yolo-deepsort-traffic-light-violation/HEAD/ff93aac71249d792d5ff851de0e663af.png -------------------------------------------------------------------------------- /TLState.py: -------------------------------------------------------------------------------- 1 | 2 | import cv2 3 | import random 4 | import numpy as np 5 | from enum import Enum 6 | from detectColor import detectColor 7 | import matplotlib.pyplot as plt 8 | import matplotlib.image as mpimg 9 | class TLState(Enum): 10 | red = 1 11 | yellow = 2 12 | green = 3 13 | red_yellowArrow = 4 14 | red_greenArrow = 5 15 | green_yellowArrow = 6 16 | green_greenArrow = 7 17 | redArrow = 8 18 | yellowArrow = 9 19 | greenArrow = 10 20 | flashingYellowArrow = 11 21 | class TLType(Enum): 22 | regular = 0 23 | five_lights = 1 24 | four_lights = 2 25 | def imgResize(image, height, inter = cv2.INTER_AREA): 26 | dim = None 27 | (h, w) = image.shape[:2] 28 | r = height / float(h) 29 | dim = (int(w * r), height) 30 | # resize the image 31 | resized = cv2.resize(image, dim, interpolation = inter) 32 | # return the resized image 33 | return resized 34 | def detectState(image, TLType): 35 | image = imgResize(image, 200) 36 | (height, width) = image.shape[:2] 37 | output = image.copy() 38 | gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 39 | # 霍夫圆环检测 40 | circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,1,20, 41 | param1=50,param2=30,minRadius=15,maxRadius=30) 42 | overallState = 0 43 | stateArrow = 0 44 | stateSolid = 0 45 | if circles is not None: 46 | circles = np.uint16(np.around(circles)) 47 | for i in circles[0,:]: 48 | if i[1] < i[2]: 49 | i[1] = i[2] 50 | roi = image[(i[1]-i[2]):(i[1]+i[2]),(i[0]-i[2]):(i[0]+i[2])] 51 | color = detectColor(roi) 52 | if color > 0: 53 | if TLType == 1 and i[0] < width/2 and i[1] > height/3: 54 | stateArrow = color 55 | elif TLType == 2: 56 | stateArrow = color 57 | if i[1] > height/2 and i[1] < height/4*3: 58 | stateArrow = color + 2 59 | else: 60 | stateSolid = color 61 | if TLType == 1: 62 | overallState = stateArrow + stateSolid + 1 63 | elif TLType == 2: 64 | overallState = stateArrow + 7 65 | else: 66 | overallState = stateSolid 67 | return overallState 68 | def plot_light_result(images): 69 | for i, image in enumerate(images): 70 | plt.subplot(1, len(images), i+1) 71 | lena = mpimg.imread(image) 72 | label = TLState(detectState(cv2.imread(image),TLType.regular.value)).name 73 | plt.title(label) 74 | plt.imshow(lena) 75 | plt.show() 76 | light_path = ["images/red.jpg","images/green.png", "images/yellow.png"] 77 | random.shuffle(light_path) 78 | plot_light_result(light_path) 79 | def plot_arrow_result(images): 80 | for i, image in enumerate(images): 81 | plt.subplot(1, len(images), i+1) 82 | lena = mpimg.imread(image) 83 | label = TLState(detectState(cv2.imread(image),TLType.five_lights.value)).name 84 | plt.title(label) 85 | plt.imshow(imgResize(lena, 200)) 86 | plt.show() 87 | arrow_path = ["images/red_greenarrow.png", "images/red_yellowarrow.png"] 88 | random.shuffle(arrow_path) 89 | plot_arrow_result(arrow_path) 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # 1.项目展示 3 | ![1.png](97d33f3c236ae7f4531e56d2dad58015.png) 4 | 5 | ![2.png](01b6e60239fc0d5f8e6ea557be35726e.png) 6 | 7 | ![3.png](ff93aac71249d792d5ff851de0e663af.png) 8 | # 2.视频演示 9 | [Python基于YOLO&Deepsort的闯红灯检测系统(完整源码&自定义UI操作界面&视频教程)_哔哩哔哩_bilibili](https://www.bilibili.com/video/BV1va411R7KL/?vd_source=bc9aec86d164b67a7004b996143742dc) 10 | 11 | # 3.红绿灯检测 12 | 交通信号灯的检测与识别是无人驾驶与辅助驾驶必不可少的一部分,其识别精度直接关乎智能驾驶的安全。一般而言,在实际的道路场景中采集的交通信号灯图像具有复杂的背景,且感兴趣的信号灯区域只占很少的一部分。针对这些难点,国内外的众多研究者提出了相应的解决方案。总的来说,大多基于传统的图像处理方法;但目前也有用强学习能力的卷积神经网络去进行识别,但这类方法往往需要大量的训练样本避免过拟合的风险。截至目前的大多数方法都是在各种颜色空间中利用信号灯颜色的先验进行分割得到兴趣区域,然后再通过信号灯所特有的形状特征和角点特征等进行进一步的判定。比如,Masako Omachi等人提出在RGB色彩空间分割交通信号灯,使用HOUGH变换检测分割出的信号灯所在的圆形区域;徐成等提出在Lab色彩空间分割交通信号灯,使用模板匹配的方法识别交通信号灯的状态;谷明琴等则在HSV色彩空间中使用颜色直方图统计图像的H分量,确定交通信号灯的类型。[参考该博客](https://mbd.pub/o/bread/Yp6clZdt),本项目将基于传统的图像处理算法来进行交通信号灯的识别,重点介绍核心技术。 13 | ![5.png](dc834c19a7bd0efb6bec39c3e62e517a.png) 14 | 15 | # 4.红绿灯检测代码实现 16 | [参考博客](https://afdian.net/item?plan_id=68cfc6185e4111ed9f5052540025c377) 17 | ``` 18 | import cv2 19 | import random 20 | import numpy as np 21 | from enum import Enum 22 | from detectColor import detectColor 23 | import matplotlib.pyplot as plt 24 | import matplotlib.image as mpimg 25 | class TLState(Enum): 26 | red = 1 27 | yellow = 2 28 | green = 3 29 | red_yellowArrow = 4 30 | red_greenArrow = 5 31 | green_yellowArrow = 6 32 | green_greenArrow = 7 33 | redArrow = 8 34 | yellowArrow = 9 35 | greenArrow = 10 36 | flashingYellowArrow = 11 37 | class TLType(Enum): 38 | regular = 0 39 | five_lights = 1 40 | four_lights = 2 41 | def imgResize(image, height, inter = cv2.INTER_AREA): 42 | dim = None 43 | (h, w) = image.shape[:2] 44 | r = height / float(h) 45 | dim = (int(w * r), height) 46 | # resize the image 47 | resized = cv2.resize(image, dim, interpolation = inter) 48 | # return the resized image 49 | return resized 50 | def detectState(image, TLType): 51 | image = imgResize(image, 200) 52 | (height, width) = image.shape[:2] 53 | output = image.copy() 54 | gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 55 | # 霍夫圆环检测 56 | circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,1,20, 57 | param1=50,param2=30,minRadius=15,maxRadius=30) 58 | overallState = 0 59 | stateArrow = 0 60 | stateSolid = 0 61 | if circles is not None: 62 | circles = np.uint16(np.around(circles)) 63 | for i in circles[0,:]: 64 | if i[1] < i[2]: 65 | i[1] = i[2] 66 | roi = image[(i[1]-i[2]):(i[1]+i[2]),(i[0]-i[2]):(i[0]+i[2])] 67 | color = detectColor(roi) 68 | if color > 0: 69 | if TLType == 1 and i[0] < width/2 and i[1] > height/3: 70 | stateArrow = color 71 | elif TLType == 2: 72 | stateArrow = color 73 | if i[1] > height/2 and i[1] < height/4*3: 74 | stateArrow = color + 2 75 | else: 76 | stateSolid = color 77 | if TLType == 1: 78 | overallState = stateArrow + stateSolid + 1 79 | elif TLType == 2: 80 | overallState = stateArrow + 7 81 | else: 82 | overallState = stateSolid 83 | return overallState 84 | def plot_light_result(images): 85 | for i, image in enumerate(images): 86 | plt.subplot(1, len(images), i+1) 87 | lena = mpimg.imread(image) 88 | label = TLState(detectState(cv2.imread(image),TLType.regular.value)).name 89 | plt.title(label) 90 | plt.imshow(lena) 91 | plt.show() 92 | light_path = ["images/red.jpg","images/green.png", "images/yellow.png"] 93 | random.shuffle(light_path) 94 | plot_light_result(light_path) 95 | def plot_arrow_result(images): 96 | for i, image in enumerate(images): 97 | plt.subplot(1, len(images), i+1) 98 | lena = mpimg.imread(image) 99 | label = TLState(detectState(cv2.imread(image),TLType.five_lights.value)).name 100 | plt.title(label) 101 | plt.imshow(imgResize(lena, 200)) 102 | plt.show() 103 | arrow_path = ["images/red_greenarrow.png", "images/red_yellowarrow.png"] 104 | random.shuffle(arrow_path) 105 | plot_arrow_result(arrow_path) 106 | ``` 107 | # 6.项目整合 108 | 下图[完整源码&环境部署视频教程&自定义UI界面](https://s.xiaocichang.com/s/6da068) 109 | ![4.png](19b29958d9866212e41bb31ad96f26e2.png) 110 | 参考文献[《Python基于YOLO&Deepsort的闯红灯检测系统(完整源码&自定义UI操作界面&视频教程)》](https://mbd.pub/o/qunma/work) 111 | 112 | # 7.参考文献 113 | 114 | [1]张光旭.非理想条件下视频序列人脸识别算法研究与实现[D].河北科技大学,2019,. 115 | 116 | [[2]白雪.人工智能技术在智慧交通领域的应用研究[J].产业与科技论坛,2019,(10):49-50.](https://doc.taixueshu.com/journal/20190027cyykjlt.html#origin=3) 117 | 118 | [[3]华佳峰.吴昌成.袁晓君.行人闯红灯治理措施及建议[J].中国公共安全(学术版),2019,(4):86-88.](https://doc.taixueshu.com/journal/20190086zgggaq-xsb.html#origin=3) 119 | 120 | [[4]韩玉鹏.阎春利.杨淇.李昕.徐琳琳.罗晓亮.道路交叉口3D光屏信号装置研究[J].山西建筑,2019,(9):131-132.](https://doc.taixueshu.com/journal/20192247shanxjz.html#origin=3) 121 | 122 | [[5]李随伟.李刚柱.行人动态实时监测系统的设计与实现[J].信息技术,2020,(5):15-20.](https://doc.taixueshu.com/journal/20200253xxjs.html#origin=3) 123 | 124 | 125 | 126 | 127 | 128 | --- 129 | #### 如果您需要更详细的【源码和环境部署教程】,除了通过【系统整合】小节的链接获取之外,还可以通过邮箱以下途径获取: 130 | #### 1.请先在GitHub上为该项目点赞(Star),编辑一封邮件,附上点赞的截图、项目的中文描述概述(About)以及您的用途需求,发送到我们的邮箱 131 | #### sharecode@yeah.net 132 | #### 2.我们收到邮件后会定期根据邮件的接收顺序将【完整源码和环境部署教程】发送到您的邮箱。 133 | #### 【免责声明】本文来源于用户投稿,如果侵犯任何第三方的合法权益,可通过邮箱联系删除。 --------------------------------------------------------------------------------