Segizu commited on
Commit
5ef1757
·
1 Parent(s): f5df7cc

Image2caption simple

Browse files
Files changed (4) hide show
  1. __pycache__/utils.cpython-39.pyc +0 -0
  2. app.py +32 -0
  3. requirements.txt +4 -0
  4. utils.py +28 -0
__pycache__/utils.cpython-39.pyc ADDED
Binary file (1.4 kB). View file
 
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import streamlit as st
3
+ from utils import ImageCaptioningModel
4
+ import tempfile
5
+
6
+ # Initialize the BLIP Image Captioning model
7
+ captioning_model = ImageCaptioningModel()
8
+
9
+ # Streamlit UI
10
+ st.title("🖼️ Image Captioning with BLIP")
11
+
12
+ st.write("Upload an image and the model will generate a description.")
13
+
14
+ # Upload Image
15
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
16
+
17
+ if uploaded_file is not None:
18
+ # Display uploaded image
19
+ st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
20
+
21
+ # Save file temporarily
22
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
23
+ temp_file.write(uploaded_file.getbuffer())
24
+ temp_file_path = temp_file.name
25
+
26
+ # Generate caption
27
+ with st.spinner("Generating caption..."):
28
+ caption = captioning_model.generate_caption(temp_file_path)
29
+
30
+ # Show caption result
31
+ st.success("Generated Caption:")
32
+ st.write(f"**{caption}**")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ Pillow
4
+ streamlit
utils.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # utils.py
2
+ from transformers import BlipProcessor, BlipForConditionalGeneration
3
+ from PIL import Image
4
+ import torch
5
+
6
+ class ImageCaptioningModel:
7
+ def __init__(self, model_name="Salesforce/blip-image-captioning-base"):
8
+ """
9
+ Initialize BLIP Image Captioning model.
10
+ """
11
+ self.processor = BlipProcessor.from_pretrained(model_name)
12
+ self.model = BlipForConditionalGeneration.from_pretrained(model_name)
13
+ self.model.eval()
14
+
15
+ def generate_caption(self, image_path):
16
+ """
17
+ Generate a caption for the given image.
18
+ :param image_path: Path to the input image
19
+ :return: Generated caption (string)
20
+ """
21
+ image = Image.open(image_path).convert("RGB")
22
+ inputs = self.processor(images=image, return_tensors="pt")
23
+
24
+ with torch.no_grad():
25
+ output = self.model.generate(**inputs)
26
+
27
+ caption = self.processor.tokenizer.decode(output[0], skip_special_tokens=True)
28
+ return caption