请编写一个Python程序,使用yolo对图片识别昼夜、交通信号灯和车辆
时间: 2024-06-10 07:09:20 浏览: 13
好的,以下是Python程序的示例代码,使用yolo对图片进行昼夜识别、交通信号灯识别和车辆识别:
```python
import cv2
import numpy as np
import time
# 加载yolo模型
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
# 获取物体类别
classes = []
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# 获取昼夜检测模型
day_night_net = cv2.dnn.readNet("day_night.weights", "day_night.cfg")
# 获取交通信号灯检测模型
traffic_light_net = cv2.dnn.readNet("traffic_light.weights", "traffic_light.cfg")
# 设置参数
conf_threshold = 0.5
nms_threshold = 0.4
# 加载图片
img = cv2.imread("test.jpg")
# 获取图片尺寸
height, width, channels = img.shape
# 将图片转换为blob格式
blob = cv2.dnn.blobFromImage(img, 1/255, (416, 416), swapRB=True, crop=False)
# 将blob输入到yolo模型中进行识别
net.setInput(blob)
outs = net.forward(net.getUnconnectedOutLayersNames())
# 处理yolo模型的输出结果
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > conf_threshold:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(center_x - w/2)
y = int(center_y - h/2)
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([x, y, w, h])
# 非最大抑制
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
# 处理昼夜检测模型的输出结果
day_night_blob = cv2.dnn.blobFromImage(img, 1/255, (32, 32), swapRB=True, crop=False)
day_night_net.setInput(day_night_blob)
day_night_out = day_night_net.forward()
day_night_pred = np.argmax(day_night_out)
# 处理交通信号灯检测模型的输出结果
traffic_light_blob = cv2.dnn.blobFromImage(img, 1/255, (32, 32), swapRB=True, crop=False)
traffic_light_net.setInput(traffic_light_blob)
traffic_light_out = traffic_light_net.forward()
traffic_light_pred = np.argmax(traffic_light_out)
# 显示识别结果
colors = np.random.uniform(0, 255, size=(len(classes), 3))
for i in indices:
i = i[0]
box = boxes[i]
x, y, w, h = box
label = str(classes[class_ids[i]])
confidence = confidences[i]
color = colors[class_ids[i]]
cv2.rectangle(img, (x, y), (x+w, y+h), color, 2)
cv2.putText(img, label + " " + str(round(confidence,2)), (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
if day_night_pred == 0:
cv2.putText(img, "day", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
else:
cv2.putText(img, "night", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
if traffic_light_pred == 0:
cv2.putText(img, "traffic light: red", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
elif traffic_light_pred == 1:
cv2.putText(img, "traffic light: green", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
else:
cv2.putText(img, "traffic light: unknown", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2)
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
该程序通过yolo模型对图片进行物体检测,并通过昼夜识别模型和交通信号灯识别模型对图片进行昼夜和交通信号灯的识别。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![mp4](https://img-home.csdnimg.cn/images/20210720083504.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)