Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting stream.py code for single image #7

Open
syedali1621 opened this issue Oct 16, 2020 · 14 comments
Open

Converting stream.py code for single image #7

syedali1621 opened this issue Oct 16, 2020 · 14 comments

Comments

@syedali1621
Copy link

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

@syedali1621
Copy link
Author

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)

@seth814
Copy link
Owner

seth814 commented Oct 18, 2020

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.

@syedali1621
Copy link
Author

Figure_1 (1)

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.
Figure_1

@seth814
Copy link
Owner

seth814 commented Oct 18, 2020

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.

@syedali1621
Copy link
Author

Receiving below error.
module 'numpy' has no attribute 'expand_dim'

@seth814
Copy link
Owner

seth814 commented Oct 20, 2020

Should be np.expand_dims. I edited the original comment.

@syedali1621
Copy link
Author

Thank you Now this error.
Invalid number of channels in input image: 'VScn::contains(scn)'where 'scn' is 2

@seth814
Copy link
Owner

seth814 commented Oct 20, 2020

Has something to do with the image channels. I guess the error is something with an invalid shape going into cvtColor, but idk.

@syedali1621
Copy link
Author

syedali1621 commented Oct 20, 2020

Figure_1R6
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 256
256 like.

8684721

@seth814
Copy link
Owner

seth814 commented Oct 20, 2020

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.

@syedali1621
Copy link
Author

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)

@seth814
Copy link
Owner

seth814 commented Oct 20, 2020

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.

@seth814
Copy link
Owner

seth814 commented Oct 25, 2020

@syedali1621
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants