Spaces:
Build error
Build error
File size: 1,399 Bytes
1c5a277 a3895ed 1c5a277 a3895ed 1c5a277 a3895ed 1c5a277 f9d091a 1c5a277 f9d091a 0932151 1c5a277 a3895ed 1c5a277 a3895ed 1c5a277 a3895ed 1c5a277 |
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 |
# app.py
import gradio as gr
import torch
from transformers import BlipProcessor, BlipForConditionalGeneration
import cv2
from PIL import Image
# Load BLIP captioning model
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
device = torch.device("cpu")
model.to(device)
# Live webcam captioning generator
def webcam_caption():
cap = cv2.VideoCapture(0) # open webcam
while True:
ret, frame = cap.read()
if not ret:
break
# Convert OpenCV frame (BGR) to RGB PIL Image
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = Image.fromarray(frame_rgb)
# Generate caption
inputs = processor(images=image, return_tensors="pt").to(device)
out = model.generate(**inputs, max_new_tokens=50)
caption = processor.decode(out[0], skip_special_tokens=True)
yield frame_rgb, caption
cap.release()
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("## 🎥 Live Webcam BLIP Captioning (CPU)")
video = gr.Image(label="Webcam Stream")
text = gr.Textbox(label="Caption")
demo.load(
fn=webcam_caption,
inputs=None,
outputs=[video, text],
every=2 # call generator every 2 sec (adjust if you want)
)
demo.launch()
|