File size: 3,968 Bytes
1e3316b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
import pickle
import requests
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
from keras.src.utils import pad_sequences
from matplotlib import pyplot as plt
from keras.models import load_model
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.preprocessing.text import Tokenizer
from PIL import Image

def load_model_from_path(model_path):
    model_link=os.path.abspath(model_path)
    if os.path.exists(model_link):
        try:
            model = load_model(model_link)
            print(f"Model from {model_link} loaded successfully!")
            return model
        except Exception as e:
            print(f"Error loading model from {model_link}: {e}")
    else:
        print(f"File not found: {model_link}")
    return None

def tokenizer_load(path):
    with open(path, 'rb') as file:
         tokenizer = pickle.load(file)
    return tokenizer

def download_image(url, save_path):
    try:
        response = requests.get(url, stream=True, timeout=10)
        response.raise_for_status()  # Raise an error for bad responses (4xx and 5xx)
        with open(save_path, 'wb') as file:
            for chunk in response.iter_content(1024):
                file.write(chunk)
        return save_path
    except Exception as e:
        print(f"Error downloading image {url}: {e}")
        return None


def extract_image_features_one(model, img_path):
    try:
        if img_path.startswith("http"):
            temp_path = "temp_image.jpg"
            img_path = download_image(img_path, temp_path)
            if img_path is None:
                return None

        if not os.path.exists(img_path):
            print(f"Error: Image path does not exist - {img_path}")
            return None

        image = load_img(img_path, target_size=(224, 224))
        img_array = img_to_array(image)
        img_array = np.expand_dims(img_array, axis=0)
        img_array = preprocess_input(img_array)

        feature = model.predict(img_array, verbose=0)

        if feature is None:
            print(f"Error: Model returned None for image - {img_path}")

        return feature
    except Exception as e:
        print(f"Exception in feature extraction: {e}")
        return None
    finally:
        if temp_path and os.path.exists(temp_path):
            os.remove(temp_path)


def idx_to_word(integer,tokenizer):
    for word ,index in tokenizer.word_index.items():
        if index == integer:
            return word
    return None

def extract_captions(mapping):
    captions_list = []
    for key in mapping:
        captions_list.extend(mapping[key])
    return captions_list


def prepare_tokenizer(captions_list):
    tokenizer = Tokenizer()
    tokenizer.fit_on_texts(captions_list)
    vocab_size = len(tokenizer.word_index) + 1
    return tokenizer, vocab_size


def calculate_max_length(captions_list):
    return max(len(caption.split()) for caption in captions_list)

def predict_caption(model, image, tokenizer, max_length):
        in_text = 'startseq'
        for i in range(max_length):
            sequence = tokenizer.texts_to_sequences([in_text])[0]
            sequence = pad_sequences([sequence], maxlen=max_length, padding='post')
            yhat = model.predict([image, sequence], verbose=0)
            yhat = np.argmax(yhat)
            word = idx_to_word(yhat, tokenizer)
            if word is None:
                break
            in_text += " " + word
            if word == 'endseq':
                break
        return in_text

def generate_caption(image_path,vgg16_model,model,tokenizer):
    features_image = extract_image_features_one(vgg16_model, image_path)
    if features_image is None:
       print("Error: No features extracted from the image.")
    y_pred = predict_caption(model, features_image, tokenizer, 18)
    return y_pred