一环境配置:
1,python安装
2,matplotlib
3,torch
4,torchvision
python 3.10 或3.9 版本,pip直接安装
二 代码,
# Load libraries
from PIL import Image
import matplotlib.pyplot as plt
import torch
import torchvision.transforms as T
import torchvision
import numpy as np
import cv2
import threading
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()
COCO_INSTANCE_CATEGORY_NAMES = [
'__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A', 'stop sign',
'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack', 'umbrella', 'N/A', 'N/A',
'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
'bottle', 'N/A', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table',
'N/A', 'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A', 'book',
'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
]
def get_prediction(img_path, threshold,img):
#img = Image.open(img_path)
transform = T.Compose([T.ToTensor()])
img = transform(img)
pred = model([img])
pred_class = [COCO_INSTANCE_CATEGORY_NAMES[i] for i in list(pred[0]['labels'].numpy())]
pred_boxes = [[(i[0], i[1]), (i[2], i[3])] for i in list(pred[0]['boxes'].detach().numpy())]
pred_score = list(pred[0]['scores'].detach().numpy())
pred_t = [pred_score.index(x) for x in pred_score if x > threshold][-1]
pred_boxes = pred_boxes[:pred_t+1]
pred_class = pred_class[:pred_t+1]
#print("pred_class:",pred_class)
#print("pred_boxes:",pred_boxes)
return pred_boxes, pred_class,pred_score
def object_detection_api(img_path, threshold=0.5, rect_th=3, text_size=1, text_th=1):
#boxes, pred_cls = get_prediction(img_path, threshold)
addr = "rtsp://admin:[email protected]:554/stream2";
cap=cv2.VideoCapture(addr)
if cap.isOpened():
ret, frame = cap.read()
if ret:
img = frame
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
boxes, pred_cls,score = get_prediction(img_path, threshold,frame)
for i in range(len(boxes)):
if (score[i]>0.85):
cv2.rectangle(img,(int( boxes[i][0][0]),int( boxes[i][0][1])),(int( boxes[i][1][0]),int( boxes[i][1][1])),color=(0, 255, 0), thickness=rect_th)
cv2.putText(img,pred_cls[i], (int( boxes[i][0][0]),int( boxes[i][0][1])), cv2.FONT_HERSHEY_SIMPLEX, text_size, (0,255,0),thickness=text_th)
cap.release()
#cv2.destroyAllWindows()
return img
#plt.imshow(img)
# plt.show()
def cammera():
addr = "rtsp://admin:[email protected]:554/stream2";
cap=cv2.VideoCapture(addr)
while cap.isOpened():
ret, frame = cap.read()
if ret:
img = frame
cv2.imshow('fr', img)
cv2.waitKey(1)
def imgthread ():
while True:
image=object_detection_api(img_path="1234.jpg")
cv2.imshow('frame', image)
cv2.waitKey(1)
if __name__ == '__main__':
# imgth=threading.Thread(target=cammera ,name='cammerath')
# imgth.start()
th=threading.Thread(target=imgthread,name='aa')
th.start()
三 代码解析
model 函数原型
def fasterrcnn_resnet50_fpn(pretrained=False, progress=True,
num_classes=91, pretrained_backbone=True, trainable_backbone_layers=3, **kwargs):
参数:
pretrianed(bool):如果为真,返回一个在 COCO train2017 上的预训练模型。
progress(bool):如果为真,将下载进度条展示在屏幕。
pretrained_backbone(bool):如果为真,返回一个在 Imagenet 上的主干网络预训练模型。
num_classes(int):模型输出的种类数量(包括背景)。
trainable_backbone_layers(int):从最后一个块开始可训练 ResNet 层的数量(未被冻结)。合法的值在 0~5 之间,5 意味着所有主干网络的层都是可训练的。