2018年4月10日星期二

darkflow视频检测同时输出json文件

命令行

python flow --model cfg/yolo-voc-6c.cfg --load -1 --demo camera --json
其中camera已经由默认的摄像头输入设置为视频文件夹地址:
camera = cv2.VideoCapture('/Users/sisyphus/darkflow/sample_img_test/test3.mp4')

darkflow/net/yolov2/predict.py中新建一个postprocess_video函数。主要是传入elapsed参数,用于json储存文件名。

def postprocess_video(self, net_out, im, elapsed, save = True):
"""
Takes net output, draw net_out, save to disk
"""
boxes = self.findboxes(net_out)

# meta
meta = self.meta
threshold = meta['thresh']
colors = meta['colors']
labels = meta['labels']
if type(im) is not np.ndarray:
imgcv = cv2.imread(im)
else: imgcv = im
h, w, _ = imgcv.shape

resultsForJSON = []
for b in boxes:
boxResults = self.process_box(b, h, w, threshold)
if boxResults is None:
continue
left, right, top, bot, mess, max_indx, confidence = boxResults
thick = int((h + w) // 300)
if self.FLAGS.json:
resultsForJSON.append({"label": mess, "confidence": float('%.2f' % confidence), "topleft": {"x": left, "y": top}, "bottomright": {"x": right, "y": bot}})
#continue

cv2.rectangle(imgcv,
(left, top), (right, bot),
colors[max_indx], thick)
cv2.putText(imgcv, mess, (left, top - 12),
0, 1e-3 * h, colors[max_indx],thick//3)

# if not save: return imgcv###########
# outfolder = os.path.join(self.FLAGS.imgdir, 'out')
# img_name = os.path.join(outfolder, os.path.basename(im))
# cv2.imwrite(img_name, imgcv)##### 
# if self.FLAGS.json:
# textJSON = json.dumps(resultsForJSON)
# textFile = os.path.splitext(img_name)[0] + ".json"
# with open(textFile, 'w') as f:
# f.write(textJSON)
# return



outfolder = os.path.join(self.FLAGS.imgdir, 'out')
img_name = os.path.join(outfolder, str(elapsed))
# print(elapsed)
# cv2.imwrite(img_name, imgcv)##### 
 if self.FLAGS.json: # print(resultsForJSON) if resultsForJSON == []: print('Normal') else: textJSON = json.dumps(resultsForJSON) # print(textJSON) textFile = os.path.splitext(img_name)[0] + ".json" print(textFile) with open(textFile, 'w') as f: f.write(textJSON) if not save: return imgcv###########

darkflow/net/framework.py中增加相应语句。
postprocess_video = yolov2.predict.postprocess_video############+










没有评论:

发表评论

Failed to find TIFF library

ImportError: Failed to find TIFF library. Make sure that libtiff is installed and its location is listed in PATH|LD_LIBRARY_PATH|.. 解决方法: ...