Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,57 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import cv2
|
3 |
-
import
|
4 |
-
import
|
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 |
-
def
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
gui = gr.Blocks()
|
60 |
-
with gui:
|
61 |
-
gr.Markdown("# Live Video AI Assistant")
|
62 |
-
with gr.Row():
|
63 |
-
video_component = gr.Video()
|
64 |
-
threading.Thread(target=video_feed, daemon=True).start()
|
65 |
-
prompt = gr.Textbox(label="Enter your question")
|
66 |
-
response = gr.Textbox(label="AI Response")
|
67 |
-
btn = gr.Button("Ask")
|
68 |
-
btn.click(process_image, inputs=prompt, outputs=response)
|
69 |
-
|
70 |
-
gui.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import threading
|
4 |
+
import torch
|
5 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
# Initialize the webcam
|
9 |
+
cap = cv2.VideoCapture(0)
|
10 |
+
|
11 |
+
# Load the Hugging Face model and processor
|
12 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
13 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-vqa-base").to("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
+
|
15 |
+
def query_the_image(query: str, image_data: bytes):
|
16 |
+
try:
|
17 |
+
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
18 |
+
inputs = processor(image, query, return_tensors="pt").to(model.device)
|
19 |
+
output = model.generate(**inputs)
|
20 |
+
answer = processor.decode(output[0], skip_special_tokens=True)
|
21 |
+
return answer
|
22 |
+
except Exception as e:
|
23 |
+
return f"Error: {e}"
|
24 |
+
|
25 |
+
def get_frame():
|
26 |
+
ret, frame = cap.read()
|
27 |
+
if not ret:
|
28 |
+
return None
|
29 |
+
_, buffer = cv2.imencode('.jpg', frame)
|
30 |
+
return buffer.tobytes()
|
31 |
+
|
32 |
+
def process_image(prompt):
|
33 |
+
frame_data = get_frame()
|
34 |
+
if frame_data:
|
35 |
+
return query_the_image(prompt, frame_data)
|
36 |
+
return "Error capturing image"
|
37 |
+
|
38 |
+
def video_feed():
|
39 |
+
while True:
|
40 |
+
ret, frame = cap.read()
|
41 |
+
if ret:
|
42 |
+
yield cv2.imencode('.jpg', frame)[1].tobytes()
|
43 |
+
else:
|
44 |
+
break
|
45 |
+
|
46 |
+
gui = gr.Blocks()
|
47 |
+
with gui:
|
48 |
+
gr.Markdown("# Live Video AI Assistant")
|
49 |
+
with gr.Row():
|
50 |
+
video_component = gr.Video()
|
51 |
+
threading.Thread(target=video_feed, daemon=True).start()
|
52 |
+
prompt = gr.Textbox(label="Enter your question")
|
53 |
+
response = gr.Textbox(label="AI Response")
|
54 |
+
btn = gr.Button("Ask")
|
55 |
+
btn.click(process_image, inputs=prompt, outputs=response)
|
56 |
+
|
57 |
+
gui.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|