5分钟让你的电脑运行yolo图像识别

首先我们需要安装python3.8以上,具体安装方法就不多说了

我这里已经安装好了

第二步,安装依赖包

打开CMD,然后CD到yolo文件夹的位置上,然后pip install -r requirements.txt。等待他自己下载安装依赖包。

这里我已经全部下载好了。

第三步,打开IDLE(第一步已经安装), CTRL+O,然后打开yolo文件夹中的Untitled-1.py文件。

 

import cv2
import numpy as np
 
# Load Yolo
print("LOADING YOLO")
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
#save all the names in file o the list classes
classes = []
with open("coco.names", "r") as f:
    classes = [line.strip() for line in f.readlines()]
#get layers of the network
layer_names = net.getLayerNames()
#Determine the output layer names from the YOLO model 
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))

print("YOLO LOADED")


img = cv2.imread("room.png")
img = cv2.resize(img,None,fx = 0.4,fy = 0.4)
height, width, channel = img.shape
print(width,height,channel)

# Detect Objects
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
# print(outs)

print(width,height,channel)



####################################

# Showing Information on the screen
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 > 0.5:
            # Object detection
            center_x = int(detection[0] * width)
            center_y = int(detection[1] * height)
            w = int(detection[2] * width)
            h = int(detection[3] * height)
            # cv.circle(img, (center_x, center_y), 10, (0, 255, 0), 2 )
            # Reactangle Cordinate
            x = int(center_x - w/2)
            y = int(center_y - h/2)
            boxes.append([x, y, w, h])
            confidences.append(float(confidence))
            class_ids.append(class_id)

# print(len(boxes))
# number_object_detection = len(boxes)

indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
print(indexes)

font = cv2.FONT_HERSHEY_PLAIN
for i in range(len(boxes)):
    if i in indexes:
        x, y, w, h = boxes[i]
        label = str(classes[class_ids[i]])
        # print(label)
        color = colors[i]
        cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
        cv2.putText(img, label, (x, y + 30), font, 3, color, 3)

cv2.imshow("IMG", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

第四步,按下F5运行程序,就能看到room.png这张照片中的物体被识别出来。

 当然,如果你会开启USB摄像头,就可以获得实时显示识别的效果。

文件地址:

链接:https://pan.baidu.com/s/1JT1K7q2-PR9pAYNBV-3DtQ?pwd=qnlo 
提取码:qnlo

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!针对您的问题,我可以为您提供一些关于在Windows上使用YOLO进行图像识别的指导。 首先,YOLO(You Only Look Once)是一种实时目标检测算法,它通过单次前向计算即可同时预测图像中多个目标的类别和边界框。在Windows上使用YOLO进行图像识别,您可以按照以下步骤进行操作: 1. 安装Python环境:请确保您的系统中已安装Python。您可以从Python官方网站(https://www.python.org)下载并安装最新版本的Python。 2. 安装相关库:使用YOLO需要一些额外的Python库,如OpenCV、NumPy等。您可以使用pip命令在命令行中安装这些库。例如,运行以下命令来安装OpenCV和NumPy: ``` pip install opencv-python pip install numpy ``` 3. 下载YOLO权重文件和配置文件:YOLO需要预训练的权重文件和配置文件来进行目标检测。您可以从YOLO官方网站(https://pjreddie.com/darknet/yolo/)下载这些文件。确保将它们保存在您的工作目录中。 4. 编写代码:您可以使用Python编写代码来加载YOLO模型并进行图像识别。以下是一个简单的示例代码: ```python import cv2 import numpy as np # 加载模型配置和权重 net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg") # 加载类别标签 classes = [] with open("coco.names", "r") as f: classes = [line.strip() for line in f.readlines()] # 加载图像 image = cv2.imread("image.jpg") height, width, _ = image.shape # 构建输入图像的blob blob = cv2.dnn.blobFromImage(image, 1/255.0, (416, 416), swapRB=True, crop=False) # 设置模型的输入 net.setInput(blob) # 运行前向传递以获取输出层 outputs = net.forward(net.getUnconnectedOutLayersNames()) # 解析输出并绘制边界框 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] * 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) cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(image, classes[class_id], (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # 显示结果图像 cv2.imshow("Image", image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 以上代码将加载YOLO模型和相关文件,然后使用模型进行目标检测,并在图像上绘制边界框显示结果。 请注意,这只是一个简单的示例,您可能需要根据您的需求进行进一步的定制和优化。希望这可以对您有所帮助!如果您有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值