Пример #1
0
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import tensorflow as tf
#from keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
#from keras.applications.vgg16 import VGG16,preprocess_input, decode_predictions
#from keras.applications.vgg19 import VGG19,preprocess_input, decode_predictions
from keras.applications.xception import Xception,preprocess_input, decode_predictions


#model = ResNet50(weights='/usr/local/lib/python2.7/dist-packages/keras/models/resnet50_weights_tf_dim_ordering_tf_kernels.h5')
#model = VGG16(weights='/usr/local/lib/python2.7/dist-packages/keras/models/vgg16_weights.h5')
#model = VGG19(weights='/usr/local/lib/python2.7/dist-packages/keras/models/vgg19_weights.h5')
model = Xception(weights='/usr/local/lib/python2.7/dist-packages/keras/models/xception_weights_tf_dim_ordering_tf_kernels.h5')


model._make_predict_function()
graph = tf.get_default_graph()
target_size = (224, 224)

rospy.init_node('classify', anonymous=True)
#These should be combined into a single message
pub = rospy.Publisher('object_detected', String, queue_size = 1)
bridge = CvBridge()

msg_string = String()


def callback(image_msg):

    #First convert the image to OpenCV image 
    cv_image = bridge.imgmsg_to_cv2(image_msg, desired_encoding="passthrough")
Пример #2
0
def generate_desc(model, tokenizer, photo, max_length):
    in_text = 'start'
    for i in range(max_length):
        sequence = tokenizer.texts_to_sequences([in_text])[0]
        sequence = pad_sequences([sequence], maxlen=max_length)
        pred = model.predict([photo, sequence], verbose=0)
        pred = np.argmax(pred)
        word = word_for_id(pred, tokenizer)
        if word is None:
            break
        in_text += ' ' + word
        if word == 'end':
            break
    return in_text


max_length = 32
tokenizer = load(open("tokenizer.p", "rb"))
model = load_model('model_9.h5')
model._make_predict_function()
xception_model = Xception(include_top=False, pooling="avg")
xception_model._make_predict_function()


def caption_this_image(input_img):
    photo = extract_features(input_img, xception_model)
    descriptionss = generate_desc(model, tokenizer, photo, max_length)
    print("\n\n")
    return descriptionss