Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,39 +3,40 @@ import cv2
|
|
| 3 |
from PIL import Image
|
| 4 |
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
| 5 |
import torch
|
| 6 |
-
import numpy as np
|
| 7 |
-
import tempfile
|
| 8 |
|
| 9 |
-
# Load
|
| 10 |
processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xl")
|
| 11 |
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xl")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
def describe_live_frame():
|
| 15 |
-
cap = cv2.VideoCapture(0) #
|
| 16 |
if not cap.isOpened():
|
| 17 |
-
return "Cannot access camera."
|
| 18 |
|
| 19 |
ret, frame = cap.read()
|
| 20 |
cap.release()
|
| 21 |
if not ret:
|
| 22 |
-
return "Failed to capture frame."
|
| 23 |
|
|
|
|
| 24 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 25 |
image = Image.fromarray(frame_rgb)
|
| 26 |
|
|
|
|
| 27 |
inputs = processor(images=image, return_tensors="pt")
|
| 28 |
generated_ids = model.generate(**inputs, max_new_tokens=50)
|
| 29 |
caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
|
|
|
| 30 |
return image, caption
|
| 31 |
|
| 32 |
-
#
|
| 33 |
with gr.Blocks() as demo:
|
| 34 |
-
gr.Markdown("## 🧠 Live Scene
|
| 35 |
-
btn = gr.Button("Capture & Describe Scene")
|
| 36 |
img_output = gr.Image(label="Captured Frame")
|
| 37 |
-
|
| 38 |
|
| 39 |
-
btn.click(fn=describe_live_frame, inputs=[], outputs=[img_output,
|
| 40 |
|
| 41 |
demo.launch()
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
| 5 |
import torch
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Load BLIP-2 FLAN-T5 model (CPU-compatible)
|
| 8 |
processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xl")
|
| 9 |
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xl")
|
| 10 |
|
| 11 |
+
# Function to capture frame and generate caption
|
| 12 |
def describe_live_frame():
|
| 13 |
+
cap = cv2.VideoCapture(0) # 0 = default webcam
|
| 14 |
if not cap.isOpened():
|
| 15 |
+
return None, "❌ Cannot access camera. Try reconnecting or use a different device."
|
| 16 |
|
| 17 |
ret, frame = cap.read()
|
| 18 |
cap.release()
|
| 19 |
if not ret:
|
| 20 |
+
return None, "❌ Failed to capture frame."
|
| 21 |
|
| 22 |
+
# Convert OpenCV frame to PIL
|
| 23 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 24 |
image = Image.fromarray(frame_rgb)
|
| 25 |
|
| 26 |
+
# Run BLIP-2 captioning
|
| 27 |
inputs = processor(images=image, return_tensors="pt")
|
| 28 |
generated_ids = model.generate(**inputs, max_new_tokens=50)
|
| 29 |
caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
| 30 |
+
|
| 31 |
return image, caption
|
| 32 |
|
| 33 |
+
# Gradio interface
|
| 34 |
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("## 🧠 Live Scene Captioning (Simulated Real-Time)\nBLIP-2 FLAN-T5 – CPU Friendly")
|
| 36 |
+
btn = gr.Button("📸 Capture & Describe Scene")
|
| 37 |
img_output = gr.Image(label="Captured Frame")
|
| 38 |
+
text_output = gr.Textbox(label="Scene Description")
|
| 39 |
|
| 40 |
+
btn.click(fn=describe_live_frame, inputs=[], outputs=[img_output, text_output])
|
| 41 |
|
| 42 |
demo.launch()
|