先安裝MMEdu庫!
MMEdu安裝:https://blog.csdn.net/zyl_coder/article/details/132483865
下面的代碼請在Jupyter上運行,并自己準備數(shù)據(jù)集。若模型還未訓(xùn)練,請先在本地訓(xùn)練完模型后再進行模型推理。
import cv2
capture = cv2.VideoCapture(0)
from MMEdu import MMClassification as cls
model = cls(backbone='MobileNet')
checkpoint = ' ' #填寫最佳權(quán)重文件
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]['標(biāo)簽'] == 0:
text = ' ' #圖像類別1
else:
text = ' ' #圖像類別2
cv2.putText(frame , text, (200, 100), cv2.FONT_HERSHEY_COMPLEX, 1.0, (100, 200, 200), 5)
cv2.imshow("video", frame)
key = cv2.waitKey(50)
#print(key)
if key == ord('q'):
break
cv2.destroyAllWindows()
?注意:1.因為我們是面對攝像頭的,所以通過flip對攝像頭獲得的內(nèi)容做了左右鏡像;
? ? ? ? ? ? 2.通過waitKey這個函數(shù)進行獲取按下的鍵(以毫秒為單位,這里就是每50毫秒獲取一? ? ? ? ? ? ? ? ? ? 次),當(dāng)按下q鍵時,關(guān)閉視頻。
本文內(nèi)容為小編自己匯總,內(nèi)容可能會有錯誤或疏漏,感謝大家的提議!文章來源:http://www.zghlxwxcb.cn/news/detail-669760.html
記得點贊和關(guān)注哦~文章來源地址http://www.zghlxwxcb.cn/news/detail-669760.html
到了這里,關(guān)于MMEdu實現(xiàn)攝像頭圖像分類(Python版)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!