先安装MMEdu库!
MMEdu安装:https://blog.youkuaiyun.com/zyl_coder/article/details/132483865
下面的代码请在Jupyter上运行,并自己准备数据集。若模型还未训练,请先在本地训练完模型后再进行模型推理。
import cv2
capture = cv2.VideoCapture(0)
from MMEdu import MMClassification as cls
model = cls(backbone='MobileNet')
checkpoint = ' ' #填写最佳权重文件
model.load_checkpoint( checkpoint = checkpoint)
while True:
ret, frame = capture.read()
frame = cv2.flip(frame,1)
result = model.fast_inference(image=frame, show=False)
x = model.print_result(result)
text = ""
if x[0]['标签'] == 0:
text = ' ' #图像类别1
else:
text = ' ' #图像类别2
cv2.putText(frame , text, (200, 100),