Skip to content

Commit 77ad48e

Browse files
committed
更新到教程5
1 parent 3d19cf7 commit 77ad48e

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

5 Realtime Dectect/Main.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import cv2
2+
import mss
3+
import numpy as np
4+
5+
from YoloObjectDetection import YoloObjectDetection
6+
7+
#实例化Yolo目标检测类
8+
yod = YoloObjectDetection()
9+
10+
#实时截屏的区域
11+
rect = {"left" :0, "top": 0, "width": 1000, "height": 500}
12+
13+
with mss.mss() as m:
14+
while True:
15+
#截屏
16+
img = m.grab(rect)
17+
#转成numpy数组
18+
img = np.array(img)
19+
#这里要转出Yolo能输入的格式
20+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
21+
22+
#进行目标检测
23+
yod.detectObjects(img)
24+
25+
#在图片上用方框标记出来
26+
yod.labelImg(img)
27+
28+
#实时显示图片
29+
cv2.imshow("DNF Yolo Auto", img)
30+
31+
#按Q键退出
32+
if cv2.waitKey(5) & 0xFF == ord('q'):
33+
cv2.destroyAllWindows()
34+
break

5 Realtime Dectect/TorchSupport.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import torch
2+
3+
def checkTorch():
4+
print("torch版本",torch.__version__)
5+
print("GPU是否可用",torch.cuda.is_available())
6+
print("GPU个数",torch.cuda.device_count())
7+
print("对应cudnn版本号",torch.backends.cudnn.version())
8+
print("对应cuda版本号",torch.version.cuda)
9+
print("--------------------------------------------------")
10+
#查看torchvision和torch版本是否匹配
11+
import torchvision
12+
print(torch.__version__)
13+
print(torchvision.__version__)
14+
15+
if __name__ == "__main__":
16+
checkTorch()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from ultralytics import YOLO
2+
3+
import cv2
4+
5+
class YoloObjectDetection:
6+
mModel = None
7+
mDetectResult = []
8+
9+
def __init__(self):
10+
#加载Yolo模型
11+
self.mModel = YOLO('yolov8n.pt', verbose=False)
12+
13+
def getDetectResult(self):
14+
return self.mDetectResult
15+
16+
# 调用Yolo模型进行目标检测
17+
def detectObjects(self, img):
18+
#调用Yolo模型进行预测并返回结果
19+
results = self.mModel(img, stream=True,verbose=False)
20+
21+
self.mDetectResult = []
22+
23+
# 读取检测的多个结果并保存
24+
for result in results:
25+
if len(result.boxes.cls) > 0:
26+
for i in range(len(result.boxes.cls)):
27+
# 类别ID
28+
leibie_id = int(result.boxes.cls[i].item())
29+
30+
# 类别
31+
leibie = result.names[leibie_id]
32+
33+
# 相似度
34+
xiangsidu = str(result.boxes.conf[i].item())[0:3]
35+
36+
# 坐标
37+
zuobiao = result.boxes.xyxy[i].tolist()
38+
39+
self.mDetectResult.append({'类别': leibie, '相似度': xiangsidu, '坐标': zuobiao})
40+
41+
return self.mDetectResult
42+
43+
#把检测的结果在传入的图片上用方框标记出来
44+
def labelImg(self, img, detectResult = None):
45+
if detectResult is None:
46+
detectResult = self.mDetectResult
47+
48+
if len(detectResult) > 0:
49+
for i in detectResult:
50+
# 画框
51+
cv2.rectangle(img, (int(i['坐标'][0]), int(i['坐标'][1])), (int(i['坐标'][2]), int(i['坐标'][3])),
52+
(0, 255, 0), 2)
53+
54+
# 标记类型
55+
cv2.putText(img, f" {i['类别']}", (int(i['坐标'][0]), int(i['坐标'][1]) + 15),
56+
cv2.FONT_HERSHEY_SIMPLEX, 0.6,
57+
(255, 255, 255), 1, cv2.LINE_AA)
58+
59+
# 相似度
60+
cv2.putText(img, f" {i['相似度']}", (int(i['坐标'][0]) + 80, int(i['坐标'][1]) + 15),
61+
cv2.FONT_HERSHEY_SIMPLEX, 0.6,
62+
(255, 255, 255), 1, cv2.LINE_AA)
63+
64+
65+

0 commit comments

Comments
 (0)