opencv可视化caffemodel

 
 
#coding=utf-8
import numpy as np
import sys, os
import cv2
import csv

import time

net_file=''
caffe_model=''
net = cv2.dnn.readNetFromCaffe(net_file,caffe_model)inScaleFactor = 0.007843#CLASSES = ('background', 'bicycle','motorbike', 'car','bus', 'truck')CLASSES = ('background','car')def postprocess(img, out): h = img.shape[0] w = img.shape[1] box = out[0, 0, :, 3:7] * np.array([w, h, w, h]) cls = out[0, 0, :, 1] conf = out[0, 0, :, 2] return (box.astype(np.int32), conf, cls)def detect(tese_img_dir): #img_name_list=[img for img in os.listdir(test_img_dir) if img.endswith('.jpg')] cap = cv2.VideoCapture("/home/sam/test_forward.mp4") while(1): ret, frame = cap.read() img = cv2.resize(frame, (321, 321)) blob = cv2.dnn.blobFromImage(img, 1, (321,321), [104, 117, 123])#donot need scalar-transforming setting 1 net.setInput(blob) time_start = time.time() out = net.forward() time_end = time.time() print "time consuming:{}".format(time_end - time_start) box, conf, cls = postprocess(frame, out) for i in range(len(box)): p1 = (box[i][0], box[i][1]) p2 = (box[i][2], box[i][3]) cv2.rectangle(frame, p1, p2, (0, 255, 0)) p3 = (max(p1[0], 15), max(p1[1], 15))#left conner handle class_index=int(cls[i]) if class_index==-1: class_index=0 #print class_index title = "%s:%.2f" % (CLASSES[class_index], conf[i]) print title #if conf[i]<0.2: #cv2.putText(frame, title, p3, cv2.FONT_ITALIC, 0.6, (0, 255, 0), 1) cv2.imshow("detect_car", frame) #cv2.imwrite(test_img_dir+'/gout_result/'+cur_img,origimg) cv2.waitKey(1)if __name__=='__main__': detect(test_img_dir)

猜你喜欢

转载自blog.csdn.net/Touch_Dream/article/details/80772804