Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ 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)
|
|
@@ -11,7 +12,7 @@ cap = cv2.VideoCapture(0)
|
|
| 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")
|
|
@@ -21,20 +22,20 @@ def query_the_image(query: str, image_data: bytes):
|
|
| 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()
|
|
|
|
| 4 |
import torch
|
| 5 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 6 |
from PIL import Image
|
| 7 |
+
import spaces
|
| 8 |
|
| 9 |
# Initialize the webcam
|
| 10 |
cap = cv2.VideoCapture(0)
|
|
|
|
| 12 |
# Load the Hugging Face model and processor
|
| 13 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
| 14 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-vqa-base").to("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
+
@spaces.GPU
|
| 16 |
def query_the_image(query: str, image_data: bytes):
|
| 17 |
try:
|
| 18 |
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
|
|
|
| 22 |
return answer
|
| 23 |
except Exception as e:
|
| 24 |
return f"Error: {e}"
|
| 25 |
+
@spaces.GPU
|
| 26 |
def get_frame():
|
| 27 |
ret, frame = cap.read()
|
| 28 |
if not ret:
|
| 29 |
return None
|
| 30 |
_, buffer = cv2.imencode('.jpg', frame)
|
| 31 |
return buffer.tobytes()
|
| 32 |
+
@spaces.GPU
|
| 33 |
def process_image(prompt):
|
| 34 |
frame_data = get_frame()
|
| 35 |
if frame_data:
|
| 36 |
return query_the_image(prompt, frame_data)
|
| 37 |
return "Error capturing image"
|
| 38 |
+
@spaces.GPU
|
| 39 |
def video_feed():
|
| 40 |
while True:
|
| 41 |
ret, frame = cap.read()
|