Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,50 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import cv2
|
| 3 |
-
import tempfile
|
| 4 |
from PIL import Image
|
| 5 |
-
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
| 6 |
-
import torch
|
| 7 |
-
import os
|
| 8 |
|
| 9 |
-
# Load BLIP
|
| 10 |
-
processor =
|
| 11 |
-
model =
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
inputs = processor(images=image, return_tensors="pt")
|
| 16 |
-
generated_ids = model.generate(**inputs, max_new_tokens=50)
|
| 17 |
-
caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
|
| 18 |
-
return caption
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
success, frame = cap.read()
|
| 27 |
-
if not success:
|
| 28 |
break
|
| 29 |
-
if count % interval == 0:
|
| 30 |
-
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 31 |
-
frames.append((count, Image.fromarray(frame_rgb)))
|
| 32 |
-
count += 1
|
| 33 |
-
cap.release()
|
| 34 |
-
return frames
|
| 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 |
-
gr.Interface(
|
| 62 |
-
fn=handle_upload,
|
| 63 |
-
inputs=gr.File(label="Upload Image or Video"),
|
| 64 |
-
outputs=gr.Textbox(label="Scene Descriptions"),
|
| 65 |
-
title="π§ Scene Understanding AI β BLIP-2 (Image + Video)",
|
| 66 |
-
description="Upload a photo or video. The AI will describe the scene(s) using BLIP-2 (FLAN-T5). Works on CPU."
|
| 67 |
-
).launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 6 |
import cv2
|
|
|
|
| 7 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Load BLIP captioning model
|
| 10 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 11 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 12 |
|
| 13 |
+
device = torch.device("cpu")
|
| 14 |
+
model.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Live webcam captioning generator
|
| 17 |
+
def webcam_caption():
|
| 18 |
+
cap = cv2.VideoCapture(0) # open webcam
|
| 19 |
+
while True:
|
| 20 |
+
ret, frame = cap.read()
|
| 21 |
+
if not ret:
|
|
|
|
|
|
|
| 22 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Convert OpenCV frame (BGR) to RGB PIL Image
|
| 25 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 26 |
+
image = Image.fromarray(frame_rgb)
|
| 27 |
+
|
| 28 |
+
# Generate caption
|
| 29 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 30 |
+
out = model.generate(**inputs, max_new_tokens=50)
|
| 31 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 32 |
+
|
| 33 |
+
yield frame_rgb, caption
|
| 34 |
+
|
| 35 |
+
cap.release()
|
| 36 |
|
| 37 |
+
# Gradio interface
|
| 38 |
+
with gr.Blocks() as demo:
|
| 39 |
+
gr.Markdown("## π₯ Live Webcam BLIP Captioning (CPU)")
|
| 40 |
+
video = gr.Image(label="Webcam Stream")
|
| 41 |
+
text = gr.Textbox(label="Caption")
|
| 42 |
|
| 43 |
+
demo.load(
|
| 44 |
+
fn=webcam_caption,
|
| 45 |
+
inputs=None,
|
| 46 |
+
outputs=[video, text],
|
| 47 |
+
every=2 # call generator every 2 sec (adjust if you want)
|
| 48 |
+
)
|
| 49 |
|
| 50 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|