資源簡介
yolo3,目標檢測、識別、跟蹤, 人和車 都已經實現
程序入口是app.py 已在python 3.7 tensorflow 1.12.0上測試通過 詳細說明請參照代碼 里面有詳細注釋
代碼片段和文件信息
import?os
from?timeit?import?time
import?sys
import?cv2
import?numpy?as?np
from?PIL?import?Image
from?yolo?import?YOLO
from?application_util?import?preprocessing
from?deep_sort?import?nn_matching
from?deep_sort.detection?import?Detection
from?deep_sort.tracker?import?Tracker
from?tools?import?generate_detections?as?gdet
from?deep_sort.detection?import?Detection?as?ddet
‘‘‘
This?project?does?both?detection?and?tracking?of?objects.
It?utilizes?the?following?project.
Tracker?is?based?on:?https://github.com/nwojke/deep_sort
Detector?is?based?on:?https://github.com/qqwweee/keras-yolo3
Just?call?detect_video?to?start?the?program.
Or?just?run?py?py?app.py?to?see?a?demo.
Also?you?can?refer?to?the?usage?example?below.
@Params
yolo:?Yolo?detector?object.
filename:?filename?of?video?to?be?detected
path:?path?of?video?to?be?detected
resize_flag:?whether?the?video?to?be?processed?should?be?rescaled?by?half.
‘‘‘
def?detect_video(yolo?filename=‘‘?path=‘‘?resize_flag=False):
????#?define?video?path
????fullpath?=?path?+?filename
????model_path?=?yolo.model_path
????model_name?=?model_path.split(‘/‘)[1][:-3]
????output_path?=?‘‘
????if?resize_flag?==?True:
????????output_path?=?f‘output/resized-{model_name}‘
????else:
????????output_path?=?f‘output/original-{model_name}‘
????#?create?output?dir?if?not?exist
????if?not?os.path.exists(output_path):
????????os.makedirs(output_path)
????#?Gating?threshold?for?cosine?distance
????max_cosine_distance?=?0.2
????#?Maximum?size?of?the?appearance?descriptors
????nn_budget?=?None
????#?Non-maxima?suppression?threshold
????nms_max_overlap?=?1.0
????#?deep_sort
????model_filename?=?‘model_data/mars-small128.pb‘
????encoder?=?gdet.create_box_encoder(model_filename?batch_size=1)
????metric?=?nn_matching.NearestNeighborDistanceMetric(
????????“cosine“?max_cosine_distance?nn_budget)
????tracker?=?Tracker(metric)
????writeVideo_flag?=?True
????video_capture?=?cv2.VideoCapture(fullpath)
????if?writeVideo_flag:
????????#?Define?the?codec?and?create?VideoWriter?object
????????w?=?int(video_capture.get(3))
????????h?=?int(video_capture.get(4))
????????if?resize_flag?==?True:
????????????#?default?to?resize?by?half
????????????w?=?int(w/2)
????????????h?=?int(h/2)
????????fourcc?=?cv2.VideoWriter_fourcc(*?‘MJPG‘)
????????#?trim?the?filename?to?get?rid?of?file?extension
????????filename?=?filename[:-4]
????????#?result?output
????????out?=?cv2.VideoWriter(
????????????f‘{output_path}/output-{filename}.avi‘?fourcc?15?(w?h))
????????#?location?of?detected?objects
????????list_file?=?open(f‘{output_path}/detection-{filename}.txt‘?‘w‘)
????????#?framerate?of?detection.
????????fps_file?=?open(f‘{output_path}/framerate-{filename}.txt‘?‘w‘)
????????frame_index?=?-1
????fps?=?0.0
????while?True:
????????ret?frame?=?video_capture.read()??#?frame?shape?640*480*3
????????if?ret?!=?True:
????????????break
????????h?w?_?=?frame.shape
????????if?resize_flag?==?True:
????????????frame?=?cv2.resize(frame?(int(w/2)?int(h/2))
評論
共有 條評論