python YOLO实现图像模型检测代码
时间: 2024-05-15 14:19:31 浏览: 14
以下是使用Python和YOLO实现图像模型检测的示例代码:
```python
import cv2
import numpy as np
# Load YOLOv3 weights and configuration files
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
# Load class names
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
# Set input image size and scale factor
input_size = (416, 416)
scale_factor = 1/255.0
# Read input image
img = cv2.imread("input.jpg")
# Create blob from input image
blob = cv2.dnn.blobFromImage(img, scale_factor, input_size, swapRB=True, crop=False)
# Set input blob for the network
net.setInput(blob)
# Get output layer names
output_layers = net.getUnconnectedOutLayersNames()
# Forward pass through the network
outputs = net.forward(output_layers)
# Get confidence scores, class IDs and bounding boxes
confidences = []
class_ids = []
boxes = []
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * img.shape[1])
center_y = int(detection[1] * img.shape[0])
width = int(detection[2] * img.shape[1])
height = int(detection[3] * img.shape[0])
left = int(center_x - width / 2)
top = int(center_y - height / 2)
confidences.append(float(confidence))
class_ids.append(class_id)
boxes.append([left, top, width, height])
# Apply non-max suppression to remove overlapping boxes
indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
# Draw bounding boxes and class labels on input image
for i in indices:
i = i[0]
box = boxes[i]
left = box[0]
top = box[1]
width = box[2]
height = box[3]
label = classes[class_ids[i]]
cv2.rectangle(img, (left, top), (left + width, top + height), (0, 255, 0), 2)
cv2.putText(img, label, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Display output image
cv2.imshow("Output", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个示例中,我们首先载入YOLOv3的权重和配置文件,然后读取类别名称。接下来,我们设置输入图像的大小和缩放因子,并从输入图像创建blob。我们将blob设置为网络的输入,并在网络上进行前向传递。通过输出层的名称,我们可以获得每个检测的置信度分数、类别ID和边界框。然后,我们应用非最大抑制来消除重叠的框,并在输入图像上绘制边界框和类标签。最后,我们显示输出图像。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)