資源簡介
caffe ssd 深度學習 目標檢測 python代碼,包括單線程和多線程,使用攝像頭作為輸入視頻源。

代碼片段和文件信息
#?coding:?utf-8
#?#?Detection?with?SSD
#
#?In?this?example?we?will?load?a?SSD?model?and?use?it?to?detect?objects.
#?###?1.?Setup
#
#?*?First?Load?necessary?libs?and?set?up?caffe?and?caffe_root
#?In[1]:
import?cv2
import?numpy?as?np
import?matplotlib.pyplot?as?plt
import?skimage.io
import?time
plt.rcParams[‘figure.figsize‘]?=?(10?10)
plt.rcParams[‘image.interpolation‘]?=?‘nearest‘
plt.rcParams[‘image.cmap‘]?=?‘gray‘
#?Make?sure?that?caffe?is?on?the?python?path:
caffe_root?=?‘/ssda/software/caffe/‘??#?this?file?is?expected?to?be?in?{caffe_root}/examples
import?os
os.chdir(caffe_root)
import?sys
sys.path.insert(0?‘python‘)
import?getopt
import?caffe
caffe.set_device(0)
caffe.set_mode_gpu()
#?The?device?id?for?webcam
webcam_id?=?0
#?Number?of?frames?to?be?skipped.
skip_frames?=?0
#?*?Load?LabelMap.
#?In[2]:
from?google.protobuf?import?text_format
from?caffe.proto?import?caffe_pb2
#?load?PASCAL?VOC?labels
labelmap_file?=?caffe_root?+?‘/data/VOC2012_SMALL/labelmap_voc.prototxt‘
file?=?open(labelmap_file?‘r‘)
labelmap?=?caffe_pb2.LabelMap()
text_format.Merge(str(file.read())?labelmap)
def?get_labelname(labelmap?labels):
????num_labels?=?len(labelmap.item)
????labelnames?=?[]
????if?type(labels)?is?not?list:
????????labels?=?[labels]
????for?label?in?labels:
????????found?=?False
????????for?i?in?xrange(0?num_labels):
????????????if?label?==?labelmap.item[i].label:
????????????????found?=?True
????????????????labelnames.append(labelmap.item[i].display_name)
????????????????break
????????assert?found?==?True
????return?labelnames
#?*?Load?the?net?in?the?test?phase?for?inference?and?configure?input?preprocessing.
#?In[3]:
model_def?=?‘models/VGGNet/VOC2012_SMALL/SSD_300x300/deploy.prototxt‘
model_weights?=?‘models/VGGNet/VOC2012_SMALL/SSD_300x300/VOC2012_SMALL_SSD_300x300_iter_40000.caffemodel‘
net?=?caffe.Net(
????model_def??#?defines?the?structure?of?the?model
????model_weights??#?contains?the?trained?weights
????caffe.TEST)??#?use?test?mode?(e.g.?don‘t?perform?dropout)
#?input?preprocessing:?‘data‘?is?the?name?of?the?input?blob?==?net.inputs[0]
transformer?=?caffe.io.Transformer({‘data‘:?net.blobs[‘data‘].data.shape})
transformer.set_transpose(‘data‘?(2?0?1))
transformer.set_mean(‘data‘?np.array([104?117?123]))??#?mean?pixel
transformer.set_raw_scale(
????‘data‘?255
)??#?the?reference?model?operates?on?images?in?[0255]?range?instead?of?[01]
transformer.set_channel_swap(
????‘data‘
????(2?1?0))??#?the?reference?model?has?channels?in?BGR?order?instead?of?RGB
#
#
#?###?2.?SSD?detection
#?*?Load?an?image.
#?In[4]:
#?set?net?to?batch?size?of?1
image_resize?=?300
net.blobs[‘data‘].reshape(1?3?image_resize?image_resize)
#?by?ASUKA
global?num
num?=?0
def?detect(image1):
????#?傳進來的image1的dtype為uint8
????#?print?image1.shape
????#?print?image1.dtype
????#?print?image1.size
????#?image?=?np.array(image1?dtype=np.float32)
????#?image?=?caffe.io.resize_image(image1?(480?640))
????image?=?skimage.img_as_float(i
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????9221??2017-07-04?20:23??ssd_vid_voc2012_small_multthread.py
?????文件???????7255??2017-07-03?17:00??ssd_vid_voc2012_small.py
-----------?---------??----------?-----??----
????????????????16476????????????????????2
評論
共有 條評論