常用小功能代码——python

将视频按照一定时间截取图片并且保存,或者说隔几帧保存一下一幅图像

import os
import subprocess

def convert_video_images(source_path, output_path):
    fileTypes = ['.avi', '.mp4', '.flv', '.mov', '.mkv']
    if not os.path.exists(source_path):
        print("not exit sourcepath :%s" % (source_path))
        return
    if not os.path.exists(output_path):
        os.mkdir(output_path)

    for video_file in os.listdir(source_path):
        print(video_file)
    for video_file in os.listdir(source_path):
        video_name = os.path.join(source_path, video_file)

        if os.path.splitext(video_name)[1] not in fileTypes:
            print("skip %s" % video_file)
            continue
        if video_name == ".DS_Store":
            print(video_name)
            continue

        print("video name:", video_name)

        strcmd = 'ffmpeg -i ' + video_name + ' -vf  "scale=1920:1080,fps=1/5" ' + output_path + '/' + \
                 os.path.splitext(video_file)[0] + '%5d.png'
        print(strcmd)

        subprocess.call(strcmd, shell=True)
        print('the video has generated image') 

此方法需要安装FFmpeg,安装步骤网上有很多,不在介绍。代码中的fps=1/5,意味着5秒取一帧。

对视频依次取出每一帧,然后对该帧处理后在合成新的视频

    i = 1
    cap = cv2.VideoCapture('输入文件路径.mp4')
    fps = cap.get(cv2.CAP_PROP_FPS)
    print(fps)
    size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    print(size)
    fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', '2')
    outVideo = cv2.VideoWriter('输出文件路径', fourcc, fps, size)
    if cap.isOpened():
        rval, frame = cap.read()
        print('ture')
    else:
        rval = False
        print('False')
    while rval:
        rval, frame = cap.read()
        #对每一帧进行处理后,产生新一帧:my_image
        my_image = my_demo(sess, net, frame)
        cv2.imshow('test', my_image)
        outVideo.write(my_image)
        cv2.waitKey(1)
    cap.release()
    outVideo.release()
    cv2.destroyAllWindows()

对xml结点内容进行修改:例子为将name结点内容guai修改为Guai

def _load_pascal_annotation(filename):
    tree = ET.parse(filename)
    objs = tree.findall('object')
    for ix, obj in enumerate(objs):
        if obj.find('name').text.lower().strip() == 'guai':
            print('the Guai is the key but now it is guai ')
            key_word = 'Guai'
            obj.find('name').text = key_word
            tree.write(filename)
            print(filename)
        if obj.find('name').text.lower().strip() == 'texiao':
            print('the Texiao is the key but now it is texiao ')
            key_word = 'Texiao'
            obj.find('name').text = key_word
            tree.write(filename)
            print(filename)

将xml结点内容输入到txt文件内,针对文件夹内包括文件夹的:

def nodeToTxt():
    source_path = '/media/ksyun/PROJECTDATA/810_xml/xml'
    text_file = '/home/ksyun/11/11.txt'
    writer_file = open(text_file,'a')
    for names in os.listdir(source_path):
        xml_flie = os.path.join(source_path, names)
        print xml_flie
        for name in os.listdir(xml_flie):

            image_name_png = os.path.join(xml_flie, name)
            name_image = os.path.splitext(name)[0]
            isNone = _load_pascal_annotation(image_name_png)
            if isNone is True:
                print ('is None')

            else:

                writer_file.write(name_image + '\n')

移动或者拷贝文件从一个文件夹到另一个文件夹:

def mymovefile(srcfile, dstfile):
    if not os.path.isfile(srcfile):
        print "%s not exist!" % (srcfile)
    else:
        fpath, fname = os.path.split(dstfile)  # 分离文件名和路径
        if not os.path.exists(fpath):
            os.makedirs(fpath)  # 创建路径
        shutil.move(srcfile, dstfile)  # 移动文件
        print "move %s -> %s" % (srcfile, dstfile)


def mycopyfile(srcfile, dstfile):
    if not os.path.isfile(srcfile):
        print "%s not exist!" % (srcfile)
    else:
        fpath, fname = os.path.split(dstfile)  # 分离文件名和路径
        if not os.path.exists(fpath):
            os.makedirs(fpath)  # 创建路径,
#dstfile可以是文件夹的名字,此时移动后的srcfile里的文件保持原来的名字
        shutil.copyfile(srcfile, dstfile)  # 复制文件
        print "copy %s -> %s" % (srcfile, dstfile)

TensorFlow中pb文件与pbtxt文件的相互转换(其实就是参数as_text是否为真):


import tensorflow as tf
from tensorflow.python.platform import gfile
from google.protobuf import text_format
 
def convert_pb_to_pbtxt(filename):
  with gfile.FastGFile(filename,'rb') as f:
    graph_def = tf.GraphDef()
 
    graph_def.ParseFromString(f.read())
 
    tf.import_graph_def(graph_def, name='')
 
    tf.train.write_graph(graph_def, './', 'protobuf.pbtxt', as_text=True)
  return
 
def convert_pbtxt_to_pb(filename):
  """Returns a `tf.GraphDef` proto representing the data in the given pbtxt file.
  Args:
    filename: The name of a file containing a GraphDef pbtxt (text-formatted
      `tf.GraphDef` protocol buffer data).
  """
  with tf.gfile.FastGFile(filename, 'r') as f:
    graph_def = tf.GraphDef()
 
    file_content = f.read()
 
    # Merges the human-readable string in `file_content` into `graph_def`.
    text_format.Merge(file_content, graph_def)
    tf.train.write_graph( graph_def , './' , 'protobuf.pb' , as_text = False )

在image上进行画框,并保存画过框的图像

比如在进行图像检测时,如何将检测出来的图像上的目标进行画框,并将该图像保存。依次延伸到视频,对视频的每一祯进行处理:

def draw_bounding_box_on_image_array(image,
                                     ymin,
                                     xmin,
                                     ymax,
                                     xmax,
                                     color='red',
                                     thickness=4,
                                     display_str_list=(),
                                     use_normalized_coordinates=True):

  image_pil = Image.fromarray(np.uint8(image)).convert('RGB')
  draw_bounding_box_on_image(image_pil, ymin, xmin, ymax, xmax, color,
                             thickness, display_str_list,
                             use_normalized_coordinates)
  np.copyto(image, np.array(image_pil))

def draw_bounding_box_on_image(image,
                               ymin,
                               xmin,
                               ymax,
                               xmax,
                               color='red',
                               thickness=5,
                               display_str_list=(),
                               use_normalized_coordinates=True):

  draw = ImageDraw.Draw(image)
  im_width, im_height = 1, 1
  if use_normalized_coordinates:
    (left, right, top, bottom) = (xmin * im_width, xmax * im_width,
                                  ymin * im_height, ymax * im_height)
  else:
    (left, right, top, bottom) = (xmin, xmax, ymin, ymax)
  draw.line([(left, top), (left, bottom), (right, bottom),
             (right, top), (left, top)], width=thickness, fill=color)
  try:
    font = ImageFont.truetype('arial.ttf', 24)
  except IOError:

    font = ImageFont.load_default()

  # If the total height of the display strings added to the top of the bounding
  # box exceeds the top of the image, stack the strings below the bounding box
  # instead of above.
  display_str_heights = [font.getsize(ds)[1] for ds in display_str_list]
  # Each display_str has a top and bottom margin of 0.05x.
  total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights)

  if top > total_display_str_height:
    text_bottom = top
  else:
    text_bottom = bottom + total_display_str_height
  # Reverse list and print from bottom to top.
  for display_str in display_str_list[::-1]:
    text_width, text_height = font.getsize(display_str)
    margin = np.ceil(0.05 * text_height)
    draw.rectangle(
        [(left, text_bottom - text_height - 2 * margin), (left + text_width,
                                                          text_bottom)],
        fill=color)
    draw.text(
        (left + margin, text_bottom - text_height - margin),
        display_str,
        fill='black',
        font=font)
    text_bottom -= text_height - 2 * margin



def my_vis_detections(im, class_name, dets, color, thresh=0.5):
    """Draw detected bounding boxes."""
    inds = np.where(dets[:, -1] >= thresh)[0]
    if len(inds) == 0:
        return
    for i in inds:
        bbox = dets[i, :4]
        score = dets[i, -1]

        ymin, xmin, ymax, xmax = bbox[1], bbox[0], bbox[3], bbox[2]
        draw_bounding_box_on_image_array(im,
                                         ymin,
                                         xmin,
                                         ymax,
                                         xmax,
                                         color=color,
                                         thickness=4,
                                         display_str_list=['{:s} {:.3f}'.format(class_name, score)],
                                         use_normalized_coordinates=True
                                         )

def my_demo(sess, net, im):

    scores, boxes = im_detect(sess, net, im)
    im = im[:, :, (2, 1, 0)]

    CONF_THRESH = 0.8
    NMS_THRESH = 0.3
    for cls_ind, cls in enumerate(CLASSES[1:]):
        color = CLASSBBOXCOLOR[cls_ind]
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack((cls_boxes,
                          cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]

        my_vis_detections(im, cls, dets, color, thresh=CONF_THRESH)

    return im

该例子时我在已经通过faster-rcnn进行训练,通过im_detect(sess,net,im)函数后,出来的时对应的每一幅图像的目标检测结果,包括每一个目标的得分以及box。最终的cls和dets时经过赛选后的目标。通过my_vis_detextions()函数对每一类包含的目标进行画框(可以对框的颜色以及粗细进行设置)

猜你喜欢

转载自blog.csdn.net/qq26983255/article/details/81455364