yolov8使用opencv2实时检测,两种物品,左上角显示信息

效果为:
左上角显示
帧率:FPS
序列号:SN
两种物品的检测数量和总数

可以应用于工业检测

yolov8使用opencv2实时检测,两种物品,左上角显示信息_第1张图片

import cv2
from ultralytics import YOLO
from cv2 import getTickCount, getTickFrequency
# 加载 YOLOv8 模型
model = YOLO("weights/yolov8s.pt")
SN = '123'
male_dic = ['123','124','125']
female_dic = ['223','224','225']
# 获取摄像头内容,参数 0 表示使用默认的摄像头
cap = cv2.VideoCapture(0)


def showFPS(frame,FPS):
    # 在图像左上角添加FPS文本
    fps_text = f"FPS: {FPS:.2f}"
    font = cv2.FONT_HERSHEY_SIMPLEX
    font_scale = 1
    font_thickness = 2
    text_color = (0, 0, 255)  # 红色
    text_position = (10, 30)  # 左上角位置

    cv2.putText(frame, fps_text, text_position, font, font_scale, text_color, font_thickness)
    
    return frame
def showSN(frame,SN):

    # 在图像左上角添加FPS文本
    SN_text = 'SN: '+SN
    font = cv2.FONT_HERSHEY_SIMPLEX
    font_scale = 1
    font_thickness = 2
    text_color = (0, 0, 255)  # 红色
    text_position = (10, 60)  # 左上角位置

    cv2.putText(frame, SN_text, text_position, font, font_scale, text_color, font_thickness)
    
    return frame
def showOKNG(frame,OKNG):
    # 在图像左上角添加FPS文本
    OKNG_text = 'result: '+OKNG
    font = cv2.FONT_HERSHEY_SIMPLEX
    font_scale = 1
    font_thickness = 2
    text_color = (0, 0, 255)  # 红色
    text_position = (10, 120)  # 左上角位置

    cv2.putText(frame, OKNG_text, text_position, font, font_scale, text_color, font_thickness)
    return frame
def showClassNum(frame,results):
    #公的数量,母的数量,一共检测到多少个东西
    male_num = 0
    female_num = 0
    total_dets = len(results[0].boxes)
    for i in range(total_dets):
        det_cls = results[0].boxes[i].cls
        print("一轮for i")
        print("results[0].boxes[i].cls=",results[0].boxes[i].cls)
        if int(det_cls)==0:
            male_num +=1
        if int(det_cls) ==41:
            female_num +=1 
        

    # 在图像左上角添加FPS文本
    ClassNum_text = 'male:'+str(male_num)+'|female:"'+str(female_num)+'|total:'+str(total_dets)
    font = cv2.FONT_HERSHEY_SIMPLEX
    font_scale = 1
    font_thickness = 2
    text_color = (0, 0, 255)  # 红色
    text_position = (10, 90)  # 左上角位置

    cv2.putText(frame, ClassNum_text, text_position, font, font_scale, text_color, font_thickness)

    return frame,male_num,female_num
def isOKorNG(male_num,female_num,SN):
    OK_or_NG = False
    if SN in male_dic:
        if male_num>0 and female_num<1:
            OK_or_NG = True
    elif SN in female_dic:
        if female_num>0 and male_num<1:
            OK_or_NG = True

    return OK_or_NG

          


while cap.isOpened():
    loop_start = getTickCount()
    success, frame = cap.read()  # 读取摄像头的一帧图像

    if success:
        results = model.predict(source=frame) # 对当前帧进行目标检测并显示结果
    annotated_frame = results[0].plot()

    # 计算FPS
    loop_time = getTickCount() - loop_start
    total_time = loop_time / (getTickFrequency())
    FPS = int(1 / total_time)
    frame = showFPS(annotated_frame,FPS)
    #显示序列号
    frame = showSN(frame,SN)
    #显示总检测数量:
    frame,male_num,female_num = showClassNum(frame,results)
    #显示返回状态:
    state = isOKorNG(male_num,female_num,SN)
    frame = showOKNG(frame,str(state))
    cv2.imshow('img', frame)

    # 通过按下 'q' 键退出循环
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()  # 释放摄像头资源
cv2.destroyAllWindows()  # 关闭OpenCV窗口


你可能感兴趣的:(YOLO)