Почему старые запущенные картинки объединяют друг друга? - PullRequest
1 голос
/ 24 февраля 2020

Я использую flask micro service и matplotlib и opencv2, которые представляют изображения. Я делаю некоторые операции. Когда я выбираю другую обувь. Все старые изображения и текущее изображение интегрированы друг с другом. Где проблема в том, что служба matplotlib или opencv или flask. Когда я закрою программу, начните снова. Сначала я могу найти хорошее решение. Мне нужно что-нибудь refre sh или что-то? Результат Решения: первая и вторая операция. Почему первый остается вторым? Если я оперирую третьим башмаком, то будут интегрированы остальные. Кто-нибудь был раньше? (Мой код удалить фон изображения)

First Image operation result enter image description here

Мой сервисный код:

@app.route("/upload", methods=["POST"])
def upload():

    upload_dir = os.path.join(APP_ROOT, "uploads/")


    if not os.path.isdir(upload_dir):
        os.mkdir(upload_dir)

    print("File :",request.files.getlist("file"))   
    destination = ""
    img_name = ""
    for img in request.files.getlist("file"):

        img_name = img.filename
        destination = "/".join([upload_dir, img_name])
        img.save(destination)

    ######################################### Object Detection ############################################
    ######################################### Object Detection ############################################
    ######################################### Object Detection ############################################

    # This is needed since the notebook is stored in the object_detection folder.
    sys.path.append("../")
    os.chdir("/home/hamzaygrtc/Desktop/tensorflow1/models/research/object_detection/utils")
    CWD_PATH = os.getcwd()
    sys.path.append(CWD_PATH)
    print(sys.path)

    print(CWD_PATH)
    import label_map_util
    import visualization_utils as vis_util

    # Name of the directory containing the object detection module we're using
    MODEL_NAME = 'inference_graph'
    #IMAGE_NAME = 'images.jpeg'

    # Grab path to current working directory
    CWD_PATH = os.getcwd()

    # Path to frozen detection graph .pb file, which contains the model that is used
    # for object detection.
    os.chdir("/home/hamzaygrtc/Desktop/tensorflow1/models/research/object_detection/")
    CWD_PATH = os.getcwd()
    sys.path.append(CWD_PATH)

    PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,'frozen_inference_graph.pb')
    print("laasdasd ",PATH_TO_CKPT)
    os.chdir("/home/hamzaygrtc/Desktop/tensorflow1/models/research/object_detection/")
    CWD_PATH = os.getcwd()
    sys.path.append(CWD_PATH)
    # Path to label map file
    PATH_TO_LABELS = os.path.join(CWD_PATH,'training','labelmap.pbtxt')

    # Path to image
    #PATH_TO_IMAGE = os.path.join(CWD_PATH,IMAGE_NAME)
    PATH_TO_IMAGE = destination

    # Number of classes the object detector can identify
    NUM_CLASSES = 1

    # Load the label map.
    # Label maps map indices to category names, so that when our convolution
    # network predicts `5`, we know that this corresponds to `king`.
    # Here we use internal utility functions, but anything that returns a
    # dictionary mapping integers to appropriate string labels would be fine
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    # Load the Tensorflow model into memory.
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

        sess = tf.Session(graph=detection_graph)

    # Define input and output tensors (i.e. data) for the object detection classifier

    # Input tensor is the image
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    # Output tensors are the detection boxes, scores, and classes
    # Each box represents a part of the image where a particular object was detected
    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

    # Each score represents level of confidence for each of the objects.
    # The score is shown on the result image, together with the class label.
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')

    # Number of objects detected
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')

    # Load image using OpenCV and
    # expand image dimensions to have shape: [1, None, None, 3]
    # i.e. a single-column array, where each item in the column has the pixel RGB value
    image = cv2.imread(PATH_TO_IMAGE)
    image_expanded = np.expand_dims(image, axis=0)

    # Perform the actual detection by running the model with the image as input
    (boxes, scores, classes, num) = sess.run(
        [detection_boxes, detection_scores, detection_classes, num_detections],
        feed_dict={image_tensor: image_expanded})

    # Draw the results of the detection (aka 'visulaize the results')

    vis_util.visualize_boxes_and_labels_on_image_array(
        image,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=8,
        min_score_thresh=0.80)

    min_score_thresh = 0.8
    bboxes = boxes[scores > min_score_thresh]

    #get image size
    im_height ,im_width,channel= image.shape
    final_box = []
    for box in bboxes:
        ymin, xmin, ymax, xmax = box
        final_box.append([xmin * im_width, xmax * im_width, ymin * im_height, ymax * im_height])

    y=int(final_box[0][2])
    x=int(final_box[0][0]-20)
    h=int(final_box[0][3])
    w=int(final_box[0][1])
    oldImage = cv2.imread(PATH_TO_IMAGE)
    crop = oldImage[y:y+h, x:x+w]


    height, width, channels = crop.shape
    #crop = cv2.resize(crop, (width*2,height*2)) 
    cv2.imwrite(img_name ,crop)
    print("LASDasdasdasdsaDDasdasdasdasdasdasdasdasd")
    print("LASDasdasdasdsaDDasdasdasdasdasdasdasdasd")
    print("LASDasdasdasdsaDDasdasdasdasdasdasdasdasd")



    '''
    cv2.imshow('Image', crop)

    print(final_box)
    # All the results have been drawn on image. Now display the image.
    cv2.imshow('Object detector', image)

    while True:
        k = cv2.waitKey(10)&0xFF
        if k == 27 or k == ord("q"):
            break
    # Clean up
    cv2.destroyAllWindows()
    '''
    ######################################### Object Detection ############################################
    ######################################### Object Detection ############################################
    ######################################### Object Detection ############################################


    ######################################### Remove Background ############################################
    ######################################### Remove Background ############################################
    ######################################### Remove Background ############################################




    # Define the helper function
    def decode_segmap(image, source, nc=21):

      label_colors = np.array([(0, 0, 0),  # 0=background
                   # 1=aeroplane, 2=bicycle, 3=bird, 4=boat, 5=bottle
                   (128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128),
                   # 6=bus, 7=car, 8=cat, 9=chair, 10=cow
                   (0, 128, 128), (128, 128, 128), (64, 0, 0), (192, 0, 0), (64, 128, 0),
                   # 11=dining table, 12=dog, 13=horse, 14=motorbike, 15=person
                   (192, 128, 0), (64, 0, 128), (192, 0, 128), (64, 128, 128), (192, 128, 128),
                   # 16=potted plant, 17=sheep, 18=sofa, 19=train, 20=tv/monitor
                   (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128)])

      r = np.zeros_like(image).astype(np.uint8)
      g = np.zeros_like(image).astype(np.uint8)
      b = np.zeros_like(image).astype(np.uint8)


      for l in range(0, nc):
        idx = image == l
        r[idx] = label_colors[l, 0]
        g[idx] = label_colors[l, 1]
        b[idx] = label_colors[l, 2]

      rgb = np.stack([r, g, b], axis=2)

      # Load the foreground input image 
      foreground = cv2.imread(source)

      # Change the color of foreground image to RGB 
      # and resize image to match shape of R-band in RGB output map
      foreground = cv2.cvtColor(foreground, cv2.COLOR_BGR2RGB)
      foreground = cv2.resize(foreground,(r.shape[1],r.shape[0]))

      # Create a background array to hold white pixels
      # with the same size as RGB output map
      background = 255 * np.ones_like(rgb).astype(np.uint8)

      # Convert uint8 to float
      foreground = foreground.astype(float)
      background = background.astype(float)

      # Create a binary mask of the RGB output map using the threshold value 0
      th, alpha = cv2.threshold(np.array(rgb),0,255, cv2.THRESH_BINARY)

      # Apply a slight blur to the mask to soften edges
      alpha = cv2.GaussianBlur(alpha, (7,7),0)

      # Normalize the alpha mask to keep intensity between 0 and 1
      alpha = alpha.astype(float)/255

      # Multiply the foreground with the alpha matte
      foreground = cv2.multiply(alpha, foreground)  

      # Multiply the background with ( 1 - alpha )
      background = cv2.multiply(1.0 - alpha, background)  

      # Add the masked foreground and background
      outImage = cv2.add(foreground, background)

      # Return a normalized output image for display
      return outImage/255

    def segment(net, path, img_name,show_orig=True, dev='cpu'):
      img = Image.open(path)
      if show_orig: plt.imshow(img); plt.axis('off'); plt.show()
      # Comment the Resize and CenterCrop for better inference results
      trf = T.Compose([#T.Resize(200), 
                       #T.CenterCrop(284), 
                       T.ToTensor(), 
                       T.Normalize(mean = [0.485, 0.456, 0.406], 
                                   std = [0.229, 0.224, 0.225])])
      inp = trf(img).unsqueeze(0).to(dev)
      out = net.to(dev)(inp)['out']
      om = torch.argmax(out.squeeze(), dim=0).detach().cpu().numpy()

      rgb = decode_segmap(om, path)

      plt.imshow(rgb)
      plt.gca().set_axis_off()
      plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, 
                  hspace = 0, wspace = 0)

      print("ABOVVVVVVVVVV" , img_name)
      plt.margins(0,0)
      plt.gca().xaxis.set_major_locator(plt.NullLocator())
      plt.gca().yaxis.set_major_locator(plt.NullLocator())
      plt.axis('off'); plt.savefig(img_name,dpi=30,fbbox_inches = 'tight',
        pad_inches = 0)
      plt.show()
      time.sleep(2.0)

      return rgb
    dlab = models.segmentation.deeplabv3_resnet101(pretrained=1).eval()
    #fcn = models.segmentation.fcn_resnet101(pretrained=True).eval()
    rgb = segment(dlab, './'+str(img_name), img_name,show_orig=False)


    ######################################### Remove Background ############################################
    ######################################### Remove Background ############################################
    ######################################### Remove Background ############################################

    #inference
    #result = np.array(train_images_paths)[simple_inference(model, session, train_vectors, os.path.join(upload_dir, img_name), cfg.IMAGE_SIZE)]
    result = np.array(train_images_paths)[simple_inference(model, session, train_vectors, os.path.join("/home/hamzaygrtc/Desktop/tensorflow1/models/research/object_detection/", img_name), cfg.IMAGE_SIZE)]
    print(os.path.join("/home/hamzaygrtc/Desktop/tensorflow1/models/research/object_detection/", img_name))
    result_final = []

    for img in result:
        result_final.append("images/"+img.split("/")[-1]) #example: dataset/train/0_shoes.png -> [dataset, train, 0_shoes.png] -> [-1] = 0_shoes.png
    print("Result : ",result_final) 

    os.chdir("/home/hamzaygrtc/Desktop/tensorflow1/models/research/object_detection/")
    CWD_PATH = os.getcwd()
    sys.path.append(CWD_PATH)
    os.chdir("/home/hamzaygrtc/Desktop/tensorflow1/models/research/object_detection/Bitirme-1/Bitirme-1/code/uploads/")
    CWD_PATH = os.getcwd()
    sys.path.append(CWD_PATH)

    plt.imshow(rgb)
    plt.gca().set_axis_off()
    plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, 
              hspace = 0, wspace = 0)

    global index2
    index2 = index2 + 1

    plt.margins(0,0)
    plt.gca().xaxis.set_major_locator(plt.NullLocator())
    plt.gca().yaxis.set_major_locator(plt.NullLocator())
    plt.axis('off'); plt.savefig(img_name+str(index2)+".jpg",dpi=30,fbbox_inches = 'tight',
    pad_inches = 0)
    plt.show()
    time.sleep(2.0)

    return render_template("result.html", image_name=img_name+str(index2)+".jpg", result_paths=result_final[:-2]) #added [:-2] just to have equal number of images in the result page per row


...