2018年4月27日星期五

Python Resize 图片批量操作

直接上代码:

import os, re
from PIL import Image

_RAW_DIR = "/Users/sisyphus/darkflow/sample_img_test_resize/test/"
_RE_INDEX = re.compile(u"000(\d{3})\..+")

def parse_start():
    """
    parse the starter index in `./raw` dir
    """
    starter = os.listdir(_RAW_DIR)
    starter.sort()
    starter = starter[0]
    res = _RE_INDEX.match(starter)
    if not res:
        raise ValueError("No Files Found!")
    else:
        return int(res.group(1))

def resize_small(img):
    """
    The photo in the gallery would only display the top 174px at max
    The photo is in 5:3 size
    The crop para is (left, upper, right, lower)
    """
    # make sure the cut in the near middle of the img
    if img.width/5 > img.height/3:
        # the image is too wide
        cut_width = img.height/3*5
        padding = (img.width - cut_width)/2
        cut = img.crop((padding,0 ,img.width-padding,img.height))
    else:
        # the image is too high
        cut_height = img.width/5*3
        padding_top = (img.height - cut_height)*0.3
        padding_bot = (img.height - cut_height)*0.7
        cut = img.crop((0, padding_top, img.width, img.height-padding_bot))
#    resized = cut.resize((700,575), resample=Image.LANCZOS)
    resized = cut.resize((1280,720), resample=Image.LANCZOS)   
    return resized

def resize_large(img):
    """
    The photo in the large size should in full size
    Limit the width and height both below 900px
    """
#    img.thumbnail((900,900), resample=Image.LANCZOS)
    img.resize((900,900), resample=Image.LANCZOS)   
    return img

def run():
    starter = parse_start()
    file_cound = len(os.listdir(_RAW_DIR))
    for index in range(starter, starter + file_cound):
        this_name = _RAW_DIR+'/000%03d.jpg' %index
        with Image.open(this_name) as img:
            _small = resize_small(img)
#            _small.save(_RAW_DIR+"700/"+"000%03d_small.jpg"%index, format="jpeg")
            _small.save(_RAW_DIR+"1280/"+"000%03d_small.jpg"%index, format="jpeg")         
#            _large = resize_large(img)
#            _large.save(_RAW_DIR+"000%03d_large.jpg"%index, format="jpeg")

if __name__ == "__main__":
    run()

darkflow网络摄像头卡顿的解决方法

只需加一个if判断语句,在一定时间间隔后读取屏幕帧送入模型进行预测。

1)对于脚本(只打印box信息):

from darkflow.net.build import TFNet
import cv2
#cap = cv2.VideoCapture("rtsp://admin:cisco123@192.168.1.66//Streaming/Channels/2")
options = {"model": "cfg/yolo-voc-6c.cfg", "load": -1, "threshold": 0.1,"gpu": 1.0}

tfnet = TFNet(options)
cap = cv2.VideoCapture("rtsp://admin:cisco123@192.168.1.66//Streaming/Channels/2")
timeF = 10  #视频帧计数间隔频率
c=1
#cap.isOpened()
while(True):
    ret, frame = cap.read()
    if(c%timeF == 0): #每隔timeF帧进行存储操作
        frame=cv2.resize(frame,(400,400),interpolation=cv2.INTER_CUBIC)
        cv2.imshow('framettt',frame)
        result = tfnet.return_predict_cam(frame)
        print(result)
    c=c+1
    print(c)

    choice = cv2.waitKey(1)
    if choice == 27: break

cap.release()
cv2.destroyAllWindows()

2)对于全函数,在子模块help.py的camera函数中加一个循环:

def camera(self):
    file = self.FLAGS.demo
    SaveVideo = self.FLAGS.saveVideo
   
    if file == 'camera':
        file = 1####################0
    else:
        assert os.path.isfile(file), \
        'file {} does not exist'.format(file)
#    camera = cv2.VideoCapture(file)   
#    camera = cv2.VideoCapture('/Users/sisyphus/darkflow/sample_img_test/output.mp4')########file
    camera = cv2.VideoCapture("rtsp://admin:cisco123@192.168.1.66//Streaming/Channels/2")##
#    camera.set(3,800) #设置分辨率 
#    camera.set(4,800)
   
    if file == 1:#############0
        self.say('Press [ESC] to quit demo')
       
    assert camera.isOpened(), \
    'Cannot capture source'
   
    if file == 1:#camera window###############0
        #cv2.namedWindow('camera',0)###################0
        _, frame = camera.read()#################none
#        frame=cv2.resize(frame,(400,400),interpolation=cv2.INTER_CUBIC)
        #cv2.imshow('frame',frame)###############none
        #height, width, _ = frame.shape
        #cv2.resizeWindow('', width, height)
       
#        while True:
#            _,frame = camera.read(0)
#            height, width, _ = frame.shape
#            cv2.imshow('frame',frame)
#            cv2.resizeWindow('', width, height)
#            if cv2.waitKey(1) & 0xFF == ord('q'):
#                break
#        cap.release()
#        cv2.destroyAllWindows()
       
    else:
        _, frame = camera.read()
        height, width, _ = frame.shape

    if SaveVideo:
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        if file == 1:#camera window#############0
          fps = 1 / self._get_fps(frame)
          if fps < 1:
            fps = 1
        else:
            fps = round(camera.get(cv2.CAP_PROP_FPS))
        videoWriter = cv2.VideoWriter(
            'video.avi', fourcc, fps, (width, height))

    # buffers for demo in batch
    buffer_inp = list()
    buffer_pre = list()
   
    elapsed = int()
    start = timer()
    self.say('Press [ESC] to quit demo')
    # Loop through frames
    timeF=25
    while camera.isOpened():
        elapsed += 1
#        print(elapsed)
        _, frame = camera.read()
#        frame=cv2.resize(frame,(400,400),interpolation=cv2.INTER_CUBIC)

#        if(elapsed%timeF==0):
#            cv2.imwrite('/Users/sisyphus/py/image/'+str(elapsed)+'.jpg',frame)
        if frame is None:
            print ('\nEnd of Video')
            break
       
        if(elapsed%timeF == 0): #每隔timeF帧进行存储操作
            preprocessed = self.framework.preprocess(frame)
            buffer_inp.append(frame)
            buffer_pre.append(preprocessed)
       
            # Only process and imshow when queue is full
            if elapsed % self.FLAGS.queue == 0:
                feed_dict = {self.inp: buffer_pre}
                net_out = self.sess.run(self.out, feed_dict)
#               print(elapsed)
                for img, single_out in zip(buffer_inp, net_out):
                    postprocessed = self.framework.postprocess_video(
                            single_out, img, elapsed, False)#######
                   
                    if SaveVideo:
                        videoWriter.write(postprocessed)
                    if file == 1: #camera window###############0
                        cv2.imshow('', postprocessed)
                 # Clear Buffers
                buffer_inp = list()
                buffer_pre = list()
               
                if elapsed % 5 == 0:
                    sys.stdout.write('\r')
                    sys.stdout.write('{0:3.3f} FPS'.format(
                            elapsed / (timer() - start)))
                    sys.stdout.flush()


        if file == 1: #camera window####################0
            choice = cv2.waitKey(1)
            if choice == 27: break


    sys.stdout.write('\n')
    if SaveVideo:
        videoWriter.release()
    camera.release()
    if file == 1: #camera window#################0
        cv2.destroyAllWindows()


2018年4月23日星期一

darkflow摄像头设置

help.py


"""
tfnet secondary (helper) methods
"""
from ..utils.loader import create_loader
from time import time as timer
import tensorflow as tf
import numpy as np
import sys
import cv2
import os

import math
import json
from ..utils.box import BoundBox
from ..cython_utils.cy_yolo2_findboxes import box_constructor

old_graph_msg = 'Resolving old graph def {} (no guarantee)'

def build_train_op(self):
    self.framework.loss(self.out)
    self.say('Building {} train op'.format(self.meta['model']))
    optimizer = self._TRAINER[self.FLAGS.trainer](self.FLAGS.lr)
    gradients = optimizer.compute_gradients(self.framework.loss)
    self.train_op = optimizer.apply_gradients(gradients)

def load_from_ckpt(self):
    if self.FLAGS.load < 0: # load lastest ckpt
        with open(os.path.join(self.FLAGS.backup, 'checkpoint'), 'r') as f:
            last = f.readlines()[-1].strip()
            load_point = last.split(' ')[1]
            load_point = load_point.split('"')[1]
            load_point = load_point.split('-')[-1]
            self.FLAGS.load = int(load_point)
   
    load_point = os.path.join(self.FLAGS.backup, self.meta['name'])
    load_point = '{}-{}'.format(load_point, self.FLAGS.load)
    self.say('Loading from {}'.format(load_point))
    try: self.saver.restore(self.sess, load_point)
    except: load_old_graph(self, load_point)

def say(self, *msgs):
    if not self.FLAGS.verbalise:
        return
    msgs = list(msgs)
    for msg in msgs:
        if msg is None: continue
        print(msg)

def load_old_graph(self, ckpt):
    ckpt_loader = create_loader(ckpt)
    self.say(old_graph_msg.format(ckpt))
   
    for var in tf.global_variables():
        name = var.name.split(':')[0]
        args = [name, var.get_shape()]
        val = ckpt_loader(args)
        assert val is not None, \
        'Cannot find and load {}'.format(var.name)
        shp = val.shape
        plh = tf.placeholder(tf.float32, shp)
        op = tf.assign(var, plh)
        self.sess.run(op, {plh: val})

def _get_fps(self, frame):
    elapsed = int()
    start = timer()
    preprocessed = self.framework.preprocess(frame)
    feed_dict = {self.inp: [preprocessed]}
    net_out = self.sess.run(self.out, feed_dict)[0]
    processed = self.framework.postprocess(net_out, frame, False)
    return timer() - start

def findboxes(self, net_out):
# meta
meta = self.meta
boxes = list()
boxes=box_constructor(meta,net_out)
return boxes


def camera(self):
    file = self.FLAGS.demo
    SaveVideo = self.FLAGS.saveVideo
   
    if file == 'camera':
        file = 1####################0
    else:
        assert os.path.isfile(file), \
        'file {} does not exist'.format(file)
#    camera = cv2.VideoCapture(file)   
#    camera = cv2.VideoCapture('/Users/sisyphus/darkflow/sample_img_test/output.mp4')########file
    camera = cv2.VideoCapture("rtsp://admin:cisco123@192.168.1.66//Streaming/Channels/2")##
#    camera.set(3,640) #设置分辨率 
#    camera.set(4,480)
   
    if file == 1:#############0
        self.say('Press [ESC] to quit demo')
       
    assert camera.isOpened(), \
    'Cannot capture source'
   
    if file == 1:#camera window###############0
        #cv2.namedWindow('camera',0)###################0
        _, frame = camera.read()#################none
        #cv2.imshow('frame',frame)###############none
        #height, width, _ = frame.shape
        #cv2.resizeWindow('', width, height)
       
#        while True:
#            _,frame = camera.read(0)
#            height, width, _ = frame.shape
#            cv2.imshow('frame',frame)
#            cv2.resizeWindow('', width, height)
#            if cv2.waitKey(1) & 0xFF == ord('q'):
#                break
#        cap.release()
#        cv2.destroyAllWindows()
       
    else:
        _, frame = camera.read()
        height, width, _ = frame.shape

    if SaveVideo:
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        if file == 1:#camera window#############0
          fps = 1 / self._get_fps(frame)
          if fps < 1:
            fps = 1
        else:
            fps = round(camera.get(cv2.CAP_PROP_FPS))
        videoWriter = cv2.VideoWriter(
            'video.avi', fourcc, fps, (width, height))

    # buffers for demo in batch
    buffer_inp = list()
    buffer_pre = list()
   
    elapsed = int()
    start = timer()
    self.say('Press [ESC] to quit demo')
    # Loop through frames
    while camera.isOpened():
        elapsed += 1
        _, frame = camera.read()
        if frame is None:
            print ('\nEnd of Video')
            break
       
        preprocessed = self.framework.preprocess(frame)
        buffer_inp.append(frame)
        buffer_pre.append(preprocessed)
       
        # Only process and imshow when queue is full
        if elapsed % self.FLAGS.queue == 0:
            feed_dict = {self.inp: buffer_pre}
            net_out = self.sess.run(self.out, feed_dict)
#            print(elapsed)
            for img, single_out in zip(buffer_inp, net_out):
                postprocessed = self.framework.postprocess_video(
                    single_out, img, elapsed, False)#######
               
                if SaveVideo:
                    videoWriter.write(postprocessed)
                if file == 1: #camera window###############0
                    cv2.imshow('', postprocessed)
            # Clear Buffers
            buffer_inp = list()
            buffer_pre = list()

        if elapsed % 5 == 0:
            sys.stdout.write('\r')
            sys.stdout.write('{0:3.3f} FPS'.format(
                elapsed / (timer() - start)))
            sys.stdout.flush()
        if file == 1: #camera window####################0
            choice = cv2.waitKey(1)
            if choice == 27: break

    sys.stdout.write('\n')
    if SaveVideo:
        videoWriter.release()
    camera.release()
    if file == 1: #camera window#################0
        cv2.destroyAllWindows()


def to_darknet(self):
    darknet_ckpt = self.darknet

    with self.graph.as_default() as g:
        for var in tf.global_variables():
            name = var.name.split(':')[0]
            var_name = name.split('-')
            l_idx = int(var_name[0])
            w_sig = var_name[1].split('/')[-1]
            l = darknet_ckpt.layers[l_idx]
            l.w[w_sig] = var.eval(self.sess)

    for layer in darknet_ckpt.layers:
        for ph in layer.h:
            layer.h[ph] = None

    return darknet_ckpt

数据集

http://faculty.neu.edu.cn/yunhyan/NEU_surface_defect_database.html

2018年4月22日星期日

加载训练好的模型

加载训练好的模型
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 22 13:52:11 2018

@author: sisyphus
"""


from skimage import io,transform
import tensorflow as tf
import numpy as np



path1 = "/Users/sisyphus/darkflow/sample_img/sample_computer.jpg"
path2 = "/Users/sisyphus/darkflow/sample_img/sample_dog.jpg"
path3 = "/Users/sisyphus/darkflow/sample_img/sample_eagle.jpg"
path4 = "/Users/sisyphus/darkflow/sample_img/sample_horses.jpg"
path5 = "/Users/sisyphus/darkflow/sample_img/sample_person.jpg"

flower_dict = {0:'computer',1:'dog',2:'eagle',3:'horses',4:'person'}

w=416
h=416
c=3

def read_one_image(path):
    img = io.imread(path)
    img = transform.resize(img,(w,h))
    return np.asarray(img)

with tf.Session() as sess:
    data = []
    data1 = read_one_image(path1)
    data2 = read_one_image(path2)
    data3 = read_one_image(path3)
    data4 = read_one_image(path4)
    data5 = read_one_image(path5)
    data.append(data1)
    data.append(data2)
    data.append(data3)
    data.append(data4)
    data.append(data5)

    saver = tf.train.import_meta_graph('/Users/sisyphus/darkflow/ckpt/yolo-voc-9.meta')
    saver.restore(sess,tf.train.latest_checkpoint('/Users/sisyphus/darkflow/ckpt/'))

    graph = tf.get_default_graph()
    input = graph.get_tensor_by_name("input:0")
    feed_dict = {input:data}

    logits = graph.get_tensor_by_name("output:0")

    result = sess.run(logits,feed_dict)
   
    boxes = framework.findboxes(result)

    #打印出预测矩阵
    print(result)
    #打印出预测矩阵每一行最大值的索引
    print(tf.argmax(result,1).eval())
    #根据索引通过字典对应花的分类
#    outputt = []
#    outputt = tf.argmax(classification_result,1).eval()
#    for i in range(len(outputt)):
#        print("第",i+1,"张图片预测:"+flower_dict[outputt[i]])
       
       
       
       
       
       
#运行结果:     
#[[  5.76620245   3.18228579  -3.89464641  -2.81310582   1.40294015]
# [ -1.01490593   3.55570269  -2.76053429   2.93104005  -3.47138596]
# [ -8.05292606  -7.26499033  11.70479774   0.59627819   2.15948296]
# [ -5.12940931   2.18423128  -3.33257103   9.0591135    5.03963232]
# [ -4.25288343  -0.95963973  -2.33347392   1.54485476   5.76069307]]
#[0 1 2 3 4]
#第 1 朵花预测:dasiy
#第 2 朵花预测:dandelion
#第 3 朵花预测:roses
#第 4 朵花预测:sunflowers
#第 5 朵花预测:tulips

2018年4月20日星期五

命令行

import sys
from darkflow.cli import cliHandler
import darkflow.net.yolov2.predict

def Predict_Image(imgdir_):
    sys.argv = ['flow','--imgdir',imgdir_,'--model','cfg/yolo-voc-6c.cfg','--load','-1','--json']
    cliHandler(sys.argv)
#    result=darkflow.net.yolov2.predict.postprocess()
#    print(result)   
   
def Predict_Camera():
    sys.argv = ['flow','--model','cfg/yolo-voc-6c.cfg','--load','-1','--demo','camera','--json']
    cliHandler(sys.argv)
   
   
if __name__=='__main__':
    imgdir='sample_img_test'
    Predict_Image(imgdir)
   
#    Predict_Camera()

2018年4月13日星期五

darkflow命令行改函数运行

视频检测命令行:

sisyphus$ python flow --model cfg/yolo-voc-6c.cfg --load -1 --demo camera --json

import sys
from darkflow.cli import cliHandler
import darkflow.net.yolov2.predict

def Predict_Image(imgdir_):
    sys.argv = ['flow','--imgdir',imgdir_,'--model','cfg/yolo-voc-6c.cfg','--load','-1','--json']
    cliHandler(sys.argv)
    result=darkflow.net.yolov2.predict.postprocess()
    print(result)   
   
def Predict_Camera():
    sys.argv = ['flow','--model','cfg/yolo-voc-6c.cfg','--load','-1','--demo','camera','--json']
    cliHandler(sys.argv)
   
   
if __name__=='__main__':
    imgdir='sample_img_test1'
    Predict_Image(imgdir)

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############+










2018年4月9日星期一

darkflow同时输出图片和json文件

命令行:

python flow --imgdir sample_img_test/ --model cfg/yolo-voc-6c.cfg --load -1 --json
如果要在sample_img_test/out文件夹同时输出图片和json文件,只需将darkflow/darkflow/net/yolov2/predict.py中红色语句删除即可。

def postprocess(self, net_out, im, 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

2018年4月8日星期日

opencv读取摄像头

读取笔记本自带摄像头


import cv2
import numpy as np

cap=cv2.VideoCapture(0)
#camera.set(3,640) #设置分辨率 
#camera.set(4,480)

while True:
    ret, frame = cap.read()
    
    cv2.imshow('frame',frame)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    
    
cap.release()
cv2.destroyAllWindows()

如果要读取usb摄像头,序号0改为1即可。


BTW,在darkflow中,命令

python flow --model cfg/yolo-voc-6c.cf-1 --demo camera
是用默认的自带摄像头进行检测,如需改用usb摄像头,则需改写help.py中相关代码
主要是0改为1

def camera(self):
    file = self.FLAGS.demo
    SaveVideo = self.FLAGS.saveVideo
   
    if file == 'camera':
        file = 1####################0
    else:
        assert os.path.isfile(file), \
        'file {} does not exist'.format(file)
       
    camera = cv2.VideoCapture(file)########file
    camera.set(3,640) #设置分辨率 
    camera.set(4,480)
   
    if file == 1:#############0
        self.say('Press [ESC] to quit demo')
       
    assert camera.isOpened(), \
    'Cannot capture source'
   
    if file == 1:#camera window###############0
        #cv2.namedWindow('camera',0)###################0
        _, frame = camera.read()#################none
        cv2.imshow('frame',frame)###############none
        #height, width, _ = frame.shape
        #cv2.resizeWindow('', width, height)
       
#        while True:
#            _,frame = camera.read(0)
#            height, width, _ = frame.shape
#            cv2.imshow('frame',frame)
#            cv2.resizeWindow('', width, height)
#            if cv2.waitKey(1) & 0xFF == ord('q'):
#                break
#        cap.release()
#        cv2.destroyAllWindows()
       
    else:
        _, frame = camera.read()
        height, width, _ = frame.shape

    if SaveVideo:
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        if file == 1:#camera window#############0
          fps = 1 / self._get_fps(frame)
          if fps < 1:
            fps = 1
        else:
            fps = round(camera.get(cv2.CAP_PROP_FPS))
        videoWriter = cv2.VideoWriter(
            'video.avi', fourcc, fps, (width, height))

    # buffers for demo in batch
    buffer_inp = list()
    buffer_pre = list()
   
    elapsed = int()
    start = timer()
    self.say('Press [ESC] to quit demo')
    # Loop through frames
    while camera.isOpened():
        elapsed += 1
        _, frame = camera.read()
        if frame is None:
            print ('\nEnd of Video')
            break
        preprocessed = self.framework.preprocess(frame)
        buffer_inp.append(frame)
        buffer_pre.append(preprocessed)
       
        # Only process and imshow when queue is full
        if elapsed % self.FLAGS.queue == 0:
            feed_dict = {self.inp: buffer_pre}
            net_out = self.sess.run(self.out, feed_dict)
            for img, single_out in zip(buffer_inp, net_out):
                postprocessed = self.framework.postprocess(
                    single_out, img, False)
                if SaveVideo:
                    videoWriter.write(postprocessed)
                if file == 1: #camera window###############0
                    cv2.imshow('', postprocessed)
            # Clear Buffers
            buffer_inp = list()
            buffer_pre = list()

        if elapsed % 5 == 0:
            sys.stdout.write('\r')
            sys.stdout.write('{0:3.3f} FPS'.format(
                elapsed / (timer() - start)))
            sys.stdout.flush()
        if file == 1: #camera window####################0
            choice = cv2.waitKey(1)
            if choice == 27: break

    sys.stdout.write('\n')
    if SaveVideo:
        videoWriter.release()
    camera.release()
    if file == 1: #camera window#################0
        cv2.destroyAllWindows()












2018年4月3日星期二

ffmpeg多张图片转视频及视频拼接

mac安装:
brew install ffmpeg
查看安装信息
brew info ffmpeg

多张图片转视频命令:

ffmpeg -f image2 -r 1 -i /Users/sisyphus/darkflow/VOCtest2018/jpgtest/0003%02d.jpg -vcodec mpeg4 /Users/sisyphus/darkflow/VOCtest2018/video/test3.mp4
1、-r 1必须放在-i 前面不然生成的视频时长会有问题。-r 1表示每秒1帧,这样10张图片生成的视频时间长度就是10s。
2、图片按照000301.jpg,000302.jpg。。。顺序放置。
3、如果要输出mp4格式的文件,-vcodec mpeg4 表示使用mpeg4编码标准。




假如你的图片在c:\temp\下面。那么通过下面的命令就可以将这个目录下面的图片转换成视频。这里面有个要求就是你的图片全部是自然数为文件名, 001, 002, 003这样的。前面要几个0取决于你的图片的个数,如109张,那么就是3-1=2个0,从001 到109,如果是1009张就是0001到1009.

ffmpeg -f image2 -i c:\temp\d.jpg test.mp4

你可以指定编码格式:
ffmpeg -f image2 -i c:\temp\d.jpg -vcodec libx264 test.mp4

也许你还想指定输出帧率:

ffmpeg -f image2 -i c:\temp\d.jpg -vcodec libx264 -r 10  test.mp4
这样输出的test.mp4就是每秒播放10帧了

如果你要指定码率:

ffmpeg -f image2 -i c:\temp\d.jpg -vcodec libx264 -r 10 -b 200k  test.mp4


注意这里的200k的单位bit/s.


多个视频拼接 :
拼接的情况稍微复杂些,我们需要将需要拼接的视频文件按以下格式保存在一个列表 list.txt 中:
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'
相应的命令为:
ffmpeg -f concat -i **list.txt** -c copy output.mp4

darkflow开启tensorboard

summary文件夹是存储TensorBoard日志的地方。但是,#518开始,TensorBoard日志默认处于禁用状态,因此summary除非手动启用TensorBoard日志,否则不应再看到该文件夹已创建。

需要修改一行代码,改为红色。



@@ -12,7 +12,7 @@ def setDefaults(self):
self.define('dataset', '../pascal/VOCdevkit/IMG/', 'path to dataset directory')
self.define('labels', 'labels.txt', 'path to labels file')
self.define('backup', './ckpt/', 'path to backup folder')
- self.define('summary', './summary/', 'path to TensorBoard summaries directory')
+ self.define('summary', '', 'path to TensorBoard summaries directory')
self.define('annotation', '../pascal/VOCdevkit/ANN/', 'path to annotation directory')
self.define('threshold', -0.1, 'detection threshold')
self.define('model', '', 'configuration of choice')


训练完毕后在terminal输入 tensorboard  --logdir=path/summary/train

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|.. 解决方法: ...