Semantic-Shapes
Semantic-Shapes copied to clipboard
Converting stream.py code for single image
Respected sir: I am trying to convert stream.py code for single image but i am facing many issues can you please provide code for image ?
Thank you
Kindly check my code for the single image it is working but the result is not good can you check. Am I missing something in the code or this code is okay?
import tensorflow as tf tf.logging.set_verbosity(tf.logging.ERROR) import os import time import contextlib import numpy as np import cv2 import matplotlib.pyplot as plt
from utils import VideoStream from tensorflow.keras.models import load_model from tensorflow.keras.utils import to_categorical from models import preprocess_input, dice from config import imshape, model_name, n_classes from utils import add_masks, crf with contextlib.redirect_stdout(None): import pygame
model = load_model(os.path.join('models', model_name+'.model'), custom_objects={'dice': dice})
img = cv2.imread('075189.png')
plt.imshow(img)
img = cv2.resize(img,(256,256)) img = np.reshape(img,[1,256,256,3])
[0.8, 0.2]
classes = model.predict(img) print(classes.shape) #plt.imshow(classes[0, :, :, 0]) plt.imshow(classes[0, :, :, 1]) plt.show() #cv2.imshow('image',img) #cv2.waitKey(0) #cv2.destroyAllWindows()
print (classes)
What do you mean by result is not good? Like it's plotting something, but the output doesn't look correct? Attach an image or something.
Thank you for your reply I am attaching result image now results are good but it is not showing RBG result I am segmenting the neck area of T-shirts.
I'm assuming you only have one class then. If I just follow along with the code in stream.py...
img.shape = (256, 256, 3)
tmp = np.expand_dims(img, axis=0)
roi_pred = model.predict(tmp)
roi_mask = roi_pred.squeeze() * 255.0
roi_mask = cv2.cvtColor(roi_mask, cv2.COLOR_GRAY2RGB)
roi_mask = np.array(roi_mask, dtype=np.uint8)
roi_mask = cv2.addWeighted(img, 1.0, roi_mask, 1.0, 0)
plt.imshow(roi_mask)
plt.show()
Let me know if that works. If it does, please post a picture. If it doesn't post the error code.
Receiving below error. module 'numpy' has no attribute 'expand_dim'
Should be np.expand_dims. I edited the original comment.
Thank you Now this error. Invalid number of channels in input image: 'VScn::contains(scn)'where 'scn' is 2
Has something to do with the image channels. I guess the error is something with an invalid shape going into cvtColor, but idk.
Somehow i got the RGB result with mask area remove from image but it is showing some noise on image background. Do you have any idea why there is noise? I trained with 256256 images should I train with bigger size images to remove noise?
How to train with bigger size images when I try it gives an error that the image size should be 256256 like.
Training with a bigger image won't do anything. You can maybe try some of these: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html
Can probably remove the edge at the very top.
Thank you yes i am trying them they are improving the results thank you. Also, I need to train on 1024*1024 i changed the values in config.py but receiving below error.
ValueError: Error when checking input: expected input_1 to have shape (1024, 1024, 3) but got array with shape (256, 256, 3)
I probably missed setting a dimension somewhere. Problem is I don't have time to look into this at the moment. Just look at the stack trace or look for where I have 256 in the code and find the conflicts.
Made a notebook for this.
https://github.com/seth814/Semantic-Shapes/blob/master/Single%20Image%20Multi%20Class.ipynb
Thank you for the code but i am receiving below error with this code.
File "E:\aithe\projects\virtualfitting\dev\semanticshapes2\utils.py", line 80, in add_masks val = pred[:,:,i].astype(np.uint8) IndexError: index 2 is out of bounds for axis 2 with size 2