在命令行dnn文件夹路径下,python 那个文件,然后–input pb文件的路径,–output 输出路径/graph.pbtxt --config pipelin.config文件路径
python tf_text_graph_ssd.py --input C:/Machine_learning/models-master/research/object_detection/zjl_detection/frozen_inference_graph.pb --output C:/Machine_learning/models-master/research/object_detection/zjl_detection/graph.pbtxt --config C:/Machine_learning/models-master/research/object_detection/zjl_detection/pipeline.config
opencv文件地址
import cv2 as cv
import time
inference_pb = "C:/Machine_learning/models-master/research/object_detection/zjl_detection/frozen_inference_graph.pb"
graph_txt = "C:/Machine_learning/models-master/research/object_detection/zjl_detection/z.pbtxt"
net = cv.dnn.readNetFromTensorflow(inference_pb, graph_txt)#用cv的dnn模块调取tensorflow训练模型
layNames = net.getLayerNames()#调取所有标签文件的名字
id = net.getLayerId(layNames[-1])#通过调取.getLayerId函数调取倒数第一个标签的名字的id
last_layer = net.getLayer(id)
#print(id)
#print(layNames)
#print(last_layer.type)
cap = cv.VideoCapture('C:/Machine_learning/models-master/research/object_detection/video1.mp4')
#捕获视频文件
while True:
ret,frame = cap.read()
start=time.time()
cv.line(frame, (0, 960), (1900, 960), (0, 0, 255), 2)
h, w = frame.shape[:2]
im_tensor = cv.dnn.blobFromImage(frame, size=(300, 300), swapRB=True, crop=False)
net.setInput(im_tensor)
cvOut = net.forward()
#print(cvOut.shape)
point_list = []
for detect in cvOut[0, 0, :, :]:
score = detect[2]
if score > 0.3:
left = detect[3] * w
top = detect[4] * h
right = detect[5] * w
bottom = detect[6] * h
cv.rectangle(frame, (int(left), int(top)), (int(right), int(bottom)), (0, 255, 255), 2)
point = (int((detect[3] * w + detect[5] * w) / 2), int((detect[4] * h + detect[6] * h) / 2))
cv.circle(frame, point, 1, (0, 255, 0), 4)
if point[1] <= 960:
point_list.append(point[1])
#print(point_list)
#print(len(point_list))
font = cv.FONT_HERSHEY_SIMPLEX
cv.putText(frame, 'All_people: %d' % len(point_list), (10, 100), font, 1, (0, 0, 255),2)
end = time.time()
seconds = end-start
fps=1/seconds
cv.putText(frame, 'fps: %s' % fps, (10, 200), font, 1, (0, 0, 255), 2, cv.LINE_AA)
cv.imshow("image", frame)
cv.waitKey(1)
cv.destroyAllWindows()