准备好这三个文件
coco.names
中包含各种我们能够检测的类名
frozen_inference_graph.pb
ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt
为配置文件和权重文件
下载链接
其中有一个压缩文件进行解压
classname = []
classfile = 'coco.names'
with open(classfile,'rt') as f:
# rstrip()删除string字符串末尾的指定字符
classname = f.read().rstrip('n').split('n')
print(classname)
处理结果为classname[]包含所有的类
[‘person’, ‘bicycle’, ‘car’, ‘motorcycle’, ‘airplane’, ‘bus’, ‘train’, ‘truck’, ‘boat’, ‘traffic light’, ‘fire hydrant’, ‘street sign’, ‘stop sign’, ‘parking meter’, ‘bench’, ‘bird’, ‘cat’, ‘dog’, ‘horse’, ‘sheep’, ‘cow’, ‘elephant’, ‘bear’, ‘zebra’, ‘giraffe’, ‘hat’, ‘backpack’, ‘umbrella’, ‘shoe’, ‘eye glasses’, ‘handbag’, ‘tie’, ‘suitcase’, ‘frisbee’, ‘skis’, ‘snowboard’, ‘sports ball’, ‘kite’, ‘baseball bat’, ‘baseball glove’, ‘skateboard’, ‘surfboard’, ‘tennis racket’, ‘bottle’, ‘plate’, ‘wine glass’, ‘cup’, ‘fork’, ‘knife’, ‘spoon’, ‘bowl’, ‘banana’, ‘apple’, ‘sandwich’, ‘orange’, ‘broccoli’, ‘carrot’, ‘hot dog’, ‘pizza’, ‘donut’, ‘cake’, ‘chair’, ‘couch’, ‘potted plant’, ‘bed’, ‘mirror’, ‘dining table’, ‘window’, ‘desk’, ‘toilet’, ‘door’, ‘tv’, ‘laptop’, ‘mouse’, ‘remote’, ‘keyboard’, ‘cell phone’, ‘microwave’, ‘oven’, ‘toaster’, ‘sink’, ‘refrigerator’, ‘blender’, ‘book’, ‘clock’, ‘vase’, ‘scissors’, ‘teddy bear’, ‘hair drier’, ‘toothbrush’, ‘hair brush’]
configpath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt' weightpath = 'frozen_inference_graph.pb' net = cv2.dnn_DetectionModel(weightpath,configpath) net.setInputSize(320,320) net.setInputScale(1.0/127.5) net.setInputMean((127.5,127.5,127.5)) net.setInputSwapRB(True)导入图片进行目标检测并对每一个物体进行遍历标注
img =cv2.imread('C:/Users/Lenovo/PycharmProjects/pythonProject2/fullview/3.png')
img = cv2.resize(img,(0,0),None,0.8,0.8)
classId, confs, boxs = net.detect(img,confThreshold=0.5)
print(classId, confs, boxs)
# flatten返回一个折叠成一维的数组
# zip从两个列表中分别迭代元素
for classid,confidence,box in zip(classId.flatten(),confs.flatten(),boxs):
cv2.rectangle(img,box,color=(0,0,255),thickness=2)
cv2.putText(img,classname[classid-1],(box[0]+10,box[1]+30),
cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255),2)
cv2.imshow('img',img)
cv2.waitKey(0)
代码
import cv2
classname = []
classfile = 'coco.names'
with open(classfile,'rt') as f:
# rstrip()删除string字符串末尾的指定字符
classname = f.read().rstrip('n').split('n')
print(classname)
configpath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightpath = 'frozen_inference_graph.pb'
net = cv2.dnn_DetectionModel(weightpath,configpath)
net.setInputSize(320,320)
net.setInputScale(1.0/127.5)
net.setInputMean((127.5,127.5,127.5))
net.setInputSwapRB(True)
img =cv2.imread('C:/Users/Lenovo/PycharmProjects/pythonProject2/fullview/3.png')
img = cv2.resize(img,(0,0),None,0.8,0.8)
classId, confs, boxs = net.detect(img,confThreshold=0.5)
print(classId, confs, boxs)
# flatten返回一个折叠成一维的数组
# zip从两个列表中分别迭代元素
for classid,confidence,box in zip(classId.flatten(),confs.flatten(),boxs):
cv2.rectangle(img,box,color=(0,0,255),thickness=2)
cv2.putText(img,classname[classid-1],(box[0]+10,box[1]+30),
cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255),2)
cv2.imshow('img',img)
cv2.waitKey(0)



