Spaces:
Sleeping
Sleeping
Create image_caption.py
Browse files- image_caption.py +103 -0
image_caption.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import os
|
| 4 |
+
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
|
| 5 |
+
import torch
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import io
|
| 8 |
+
import google.generativeai as genai
|
| 9 |
+
|
| 10 |
+
class Caption:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
|
| 13 |
+
self.api_key = 'AIzaSyAFG94rVbm9eWepO5uPGsMha8XJ-sHbMdA'
|
| 14 |
+
genai.configure(api_key=self.api_key)
|
| 15 |
+
self.model = genai.GenerativeModel(model_name="gemini-pro-vision")
|
| 16 |
+
# self.model = VisionEncoderDecoderModel.from_pretrained(
|
| 17 |
+
# "nlpconnect/vit-gpt2-image-captioning"
|
| 18 |
+
# )
|
| 19 |
+
# self.feature_extractor = ViTImageProcessor.from_pretrained(
|
| 20 |
+
# "nlpconnect/vit-gpt2-image-captioning"
|
| 21 |
+
# )
|
| 22 |
+
# self.tokenizer = AutoTokenizer.from_pretrained(
|
| 23 |
+
# "nlpconnect/vit-gpt2-image-captioning"
|
| 24 |
+
# )
|
| 25 |
+
|
| 26 |
+
# # device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 27 |
+
# self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 28 |
+
# self.model.to(self.device)
|
| 29 |
+
# self.max_length = 16
|
| 30 |
+
# self.num_beams = 4
|
| 31 |
+
# self.gen_kwargs = {"max_length": self.max_length, "num_beams": self.num_beams}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def predict_step(self,image_paths):
|
| 35 |
+
images = []
|
| 36 |
+
|
| 37 |
+
for image_path in image_paths:
|
| 38 |
+
i_image = Image.open(image_path)
|
| 39 |
+
if i_image.mode != "RGB":
|
| 40 |
+
i_image = i_image.convert(mode="RGB")
|
| 41 |
+
|
| 42 |
+
images.append(i_image)
|
| 43 |
+
|
| 44 |
+
pixel_values = self.feature_extractor(images=images, return_tensors="pt").pixel_values
|
| 45 |
+
pixel_values = pixel_values.to(self.device)
|
| 46 |
+
|
| 47 |
+
output_ids = self.model.generate(pixel_values, **self.gen_kwargs)
|
| 48 |
+
|
| 49 |
+
preds = self.tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
| 50 |
+
preds = [pred.strip() for pred in preds]
|
| 51 |
+
return preds
|
| 52 |
+
|
| 53 |
+
def predict_from_memory(self, image_buffers):
|
| 54 |
+
images = []
|
| 55 |
+
|
| 56 |
+
for image_buffer in image_buffers:
|
| 57 |
+
# Ensure the buffer is positioned at the start
|
| 58 |
+
if isinstance(image_buffer, io.BytesIO):
|
| 59 |
+
image_buffer.seek(0)
|
| 60 |
+
try:
|
| 61 |
+
i_image = Image.open(image_buffer)
|
| 62 |
+
if i_image.mode != "RGB":
|
| 63 |
+
i_image = i_image.convert("RGB")
|
| 64 |
+
images.append(i_image)
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"Failed to process image buffer: {str(e)}")
|
| 67 |
+
continue
|
| 68 |
+
|
| 69 |
+
return self.process_images(images)
|
| 70 |
+
|
| 71 |
+
def process_images(self, images):
|
| 72 |
+
pixel_values = self.feature_extractor(images=images, return_tensors="pt").pixel_values
|
| 73 |
+
pixel_values = pixel_values.to(self.device)
|
| 74 |
+
output_ids = self.model.generate(pixel_values, **self.gen_kwargs)
|
| 75 |
+
preds = self.tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
| 76 |
+
preds = [pred.strip() for pred in preds]
|
| 77 |
+
return preds
|
| 78 |
+
|
| 79 |
+
def predict_image_caption_gemini(self,img):
|
| 80 |
+
prompt = "Describe the main focus of this image in detail."
|
| 81 |
+
response = self.model.generate_content([prompt, img], stream=True)
|
| 82 |
+
response.resolve()
|
| 83 |
+
print("Derived data",response.text)
|
| 84 |
+
return response.text
|
| 85 |
+
|
| 86 |
+
def get_args(self):
|
| 87 |
+
parser = argparse.ArgumentParser()
|
| 88 |
+
parser.add_argument( "-i",
|
| 89 |
+
"--input_img_paths",
|
| 90 |
+
type=str,
|
| 91 |
+
default="farmer.jpg",
|
| 92 |
+
help="img for caption")
|
| 93 |
+
|
| 94 |
+
args = parser.parse_args()
|
| 95 |
+
|
| 96 |
+
return args
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
model = Caption()
|
| 100 |
+
args = model.get_args()
|
| 101 |
+
image_paths = []
|
| 102 |
+
image_paths.append(args.input_img_paths)
|
| 103 |
+
print(model.predict_step(image_paths))
|