File size: 1,408 Bytes
6806c9f
117b368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from utils.load_input import *


# 使用YOLO进行语义分割
def segmentation(input,
                 img_size=(1920, 1088),
                 model_path='./config/weight/yolov8n-seg.pt'):
    # 加载YOLOv8语义分割模型(最小参数量)
    model = load_model(model_path)

    # 加载模型到图形计算设备
    model = load_device(model)

    # 模型推理
    results = model(input, imgsz=img_size, classes=0)

    return results


# 循环读取摄像头推理
def reuse_camera_segmentation(camera_num=0,
                              box=True):
    # 循环读取摄像头推理
    while True:

        # 加载摄像头
        cap = load_camera(camera_num)

        # 读取摄像头内容
        _, input = cap.read()

        out = segmentation(input)

        # 显示结果(带框/不带框)
        if box:
            out = out[0].plot()
        else:
            out = out[0].plot(boxes=False)  # 不显示预测框

        # 显示结果
        cv2.imshow('frame', out)

        # 等待退出
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # 关闭摄像头
    cap.release()

    # 关闭窗口
    cv2.destroyAllWindows()


if __name__ == '__main__':
    # 单次展示语义分割
    # cap = load_camera(1)
    # _, input = cap.read()
    # out = segmentation(input)

    # 展示实时语义分割
    reuse_camera_segmentation(camera_num=1)