如何在python代码中提供输入文件位置和输出位置

时间:2019-08-22 11:16:57

标签: python

我想将一个图像文件转换为另一个图像文件,我的代码会做到这一点,但是为此,我必须在终端中提供源文件位置和目标文件位置,而不是这样做,我想在python代码中提及这两个源代码和目标文件名。

    import os
    import glob
    import time;
    from io import BytesIO

    import numpy as np
    from PIL import Image

    import tensorflow as tf
    import sys
    import datetime

    date_string = time.strftime("%Y-%m-%d-%H:%M")


    class DeepLabModel(object):
      """Class to load deeplab model and run inference."""

      INPUT_TENSOR_NAME = 'ImageTensor:0'
      OUTPUT_TENSOR_NAME = 'SemanticPredictions:0'
      INPUT_SIZE = 513
      FROZEN_GRAPH_NAME = 'frozen_inference_graph'

      def __init__(self, tarball_path):
        """Creates and loads pretrained deeplab model."""
        self.graph = tf.Graph()

        graph_def = None
        graph_def = tf.GraphDef.FromString(open(tarball_path + "/frozen_inference_graph.pb", "rb").read()) 

        if graph_def is None:
          raise RuntimeError('Cannot find inference graph in tar archive.')

        with self.graph.as_default():
          tf.import_graph_def(graph_def, name='')

        self.sess = tf.Session(graph=self.graph)

      def run(self, image):
        """Runs inference on a single image.

        Args:
          image: A PIL.Image object, raw input image.

        Returns:
          resized_image: RGB image resized from original input image.
          seg_map: Segmentation map of `resized_image`.
        """
        start = datetime.datetime.now()

        width, height = image.size
        resize_ratio = 1.0 * self.INPUT_SIZE / max(width, height)
        target_size = (int(resize_ratio * width), int(resize_ratio * height))
        resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS)
        batch_seg_map = self.sess.run(
            self.OUTPUT_TENSOR_NAME,
            feed_dict={self.INPUT_TENSOR_NAME: [np.asarray(resized_image)]})
        seg_map = batch_seg_map[0]

        end = datetime.datetime.now()

        diff = end - start
        print("Time taken to evaluate segmentation is : " + str(diff))

        return resized_image, seg_map

    def drawSegment(baseImg, matImg):
      width, height = baseImg.size
      dummyImg = np.zeros([height, width, 4], dtype=np.uint8)
      for x in range(width):
                for y in range(height):
                    color = matImg[y,x]
                    (r,g,b) = baseImg.getpixel((x,y))
                    if color == 0:
                        dummyImg[y,x,3] = 0
                    else :
                        dummyImg[y,x] = [r,g,b,255]
      img = Image.fromarray(dummyImg)
      img.save(outputFilePath)


    inputFilePath = sys.argv[1]
    outputFilePath = sys.argv[2]

    if inputFilePath is None or outputFilePath is None:
      print("Bad parameters. Please specify input file path and output file path")
      exit()

    modelType = "mobile_net_model"
    if len(sys.argv) > 3 and sys.argv[3] == "1":
      modelType = "xception_model"

    MODEL = DeepLabModel(modelType)
    print('model loaded successfully : ' + modelType)

    def run_visualization(filepath):
      """Inferences DeepLab model and visualizes result."""
      try:
        print("Trying to open : " + sys.argv[1])
        # f = open(sys.argv[1])
        jpeg_str = open(filepath, "rb").read()
        orignal_im = Image.open(BytesIO(jpeg_str))
      except IOError:
        print('Cannot retrieve image. Please check file: ' + filepath)
        return

      print('running deeplab on image %s...' % filepath)
      resized_im, seg_map = MODEL.run(orignal_im)

      # vis_segmentation(resized_im, seg_map)
      drawSegment(resized_im, seg_map)

    run_visualization(inputFilePath)

这是我的代码

当我在终端中发出此命令时,此代码有效。

   python abcd.py input_file/front1566192748.jpg output_file/testing1.png

相反,我想在python代码中同时提及输入文件和输出文件。

要运行我喜欢使用的代码

 python abcd.py

仅。

谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

替换以下两行:

inputFilePath = sys.argv[1]
outputFilePath = sys.argv[2]

使用

inputFilePath = 'input_file/front1566192748.jpg'
outputFilePath = 'output_file/testing1.png'

您已经完成。

编辑:您还必须在其余代码中删除对sys.argv [1]和sys.argv [2]的所有引用,以免出错。