tensorflow 映像資料處理(二)

來源:互聯網
上載者:User

____tz_zs


映像片段截取,映像大小調整,映像翻轉以及色彩調整的整個映像預先處理過程

案例來源《TensorFlow實戰Google深度學習架構》


原圖



處理後的圖片








# -*- coding: utf-8 -*-"""@author: tz_zs的圖片預先處理範例"""import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt# 隨機處理的順序可以進一步降低無關因素的影響def distort_color(image, color_ordering=0):    if color_ordering == 0:        # 隨機亮度        image = tf.image.random_brightness(image, max_delta=32. / 255.)        # 隨機飽和度        image = tf.image.random_saturation(image, lower=0.5, upper=1.5)        # 隨機色相        image = tf.image.random_hue(image, max_delta=0.2)        # 隨機對比        image = tf.image.random_contrast(image, lower=0.5, upper=1.5)    elif color_ordering == 1:        image = tf.image.random_saturation(image, lower=0.5, upper=1.5)        image = tf.image.random_brightness(image, max_delta=32. / 255.)        image = tf.image.random_contrast(image, lower=0.5, upper=1.5)        image = tf.image.random_hue(image, max_delta=0.2)    return tf.clip_by_value(image, 0.0, 1.0)def preprocess_for_train(image, height, width, bbox):    # 如果沒有提供注釋框,則關注整個映像    if bbox is None:        bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])  # [[[ 0.  0.  1.  1.]]]    # 轉換映像張量的類型    if image.dtype != tf.float32:        image = tf.image.convert_image_dtype(image, dtype=tf.float32)    # 隨機截取映像    # print(tf.shape(image).eval())  # [232 320   3]    bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(tf.shape(image), bounding_boxes=bbox)    distorted_image = tf.slice(image, bbox_begin, bbox_size)    # 大小    distorted_image = tf.image.resize_images(distorted_image, [height, width], method=np.random.randint(4))    # 翻轉    distorted_image = tf.image.random_flip_left_right(distorted_image)    # 隨機色彩    distorted_image = distort_color(distorted_image, np.random.randint(2))    return distorted_imageimage_raw_data = tf.gfile.FastGFile("picture.jpg", "rb").read()with tf.Session() as sess:    img_data = tf.image.decode_jpeg(image_raw_data)    boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])    for i in range(6):        result = preprocess_for_train(img_data, 299, 299, boxes)        plt.imshow(result.eval())        plt.show()









聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.