-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLIP_CAM.py
191 lines (156 loc) · 6.76 KB
/
BLIP_CAM.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import cv2
from transformers import AutoProcessor, AutoModelForImageTextToText
import torch
import logging
import time
from PIL import Image
import sys
from threading import Thread, Lock
from queue import Queue
def setup_logging():
"""Configure logging with basic formatting"""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
return logging.getLogger(__name__)
class CaptionGenerator:
def __init__(self, processor, model, device):
self.processor = processor
self.model = model
self.device = device
self.current_caption = f"Initializing caption... ({device.upper()})"
self.caption_queue = Queue(maxsize=1)
self.lock = Lock()
self.running = True
self.thread = Thread(target=self._caption_worker)
self.thread.daemon = True
self.thread.start()
def _caption_worker(self):
while self.running:
try:
if not self.caption_queue.empty():
frame = self.caption_queue.get()
caption = self._generate_caption(frame)
with self.lock:
self.current_caption = caption
except Exception as e:
logging.error(f"Caption worker error: {str(e)}")
time.sleep(0.1) # Prevent busy waiting
def _generate_caption(self, image):
try:
# Resize to 640x480 (or any other size)
image_resized = cv2.resize(image, (640, 480))
# Convert to RGB
rgb_image = cv2.cvtColor(image_resized, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(rgb_image)
# Process the image for captioning
inputs = self.processor(images=pil_image, return_tensors="pt")
inputs = {name: tensor.to(self.device) for name, tensor in inputs.items()}
with torch.no_grad():
outputs = self.model.generate(
**inputs,
max_length=30,
num_beams=5,
num_return_sequences=1
)
caption = self.processor.batch_decode(outputs, skip_special_tokens=True)[0].strip()
return f"BLIP: {caption} ({self.device.upper()})"
except Exception as e:
logging.error(f"Caption generation error: {str(e)}")
return f"BLIP: Caption generation failed ({self.device.upper()})"
def update_frame(self, frame):
if self.caption_queue.empty():
try:
self.caption_queue.put_nowait(frame.copy())
except:
pass # Queue is full, skip this frame
def get_caption(self):
with self.lock:
return self.current_caption
def stop(self):
self.running = False
self.thread.join()
def get_gpu_usage():
"""Get the GPU memory usage and approximate utilization"""
if torch.cuda.is_available():
memory_allocated = torch.cuda.memory_allocated() / (1024 ** 2) # MB
memory_total = torch.cuda.get_device_properties(0).total_memory / (1024 ** 2) # MB
memory_used_percent = (memory_allocated / memory_total) * 100
gpu_info = f"GPU Memory Usage: {memory_used_percent:.2f}% | Allocated: {memory_allocated:.2f} MB / {memory_total:.2f} MB"
return gpu_info
else:
return "GPU not available"
def load_models():
"""Load BLIP model"""
try:
blip_processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
blip_model = AutoModelForImageTextToText.from_pretrained("Salesforce/blip-image-captioning-large")
device = 'cuda' if torch.cuda.is_available() else 'cpu'
if device == 'cuda':
# Set GPU memory usage limit to 90%
torch.cuda.set_per_process_memory_fraction(0.9)
blip_model = blip_model.to('cuda')
return blip_processor, blip_model, device
except Exception as e:
logging.error(f"Failed to load models: {str(e)}")
return None, None, None
def live_stream_with_caption(processor, model, device, display_width=1280, display_height=720):
"""Stream webcam feed with live captions and FPS"""
cap = cv2.VideoCapture(1)
if not cap.isOpened():
cap = cv2.VideoCapture(0)
if not cap.isOpened():
logger.error("Failed to access webcam.")
return
cap.set(cv2.CAP_PROP_FRAME_WIDTH, display_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, display_height)
logger.info(f"Webcam feed started successfully using {device.upper()}.")
caption_generator = CaptionGenerator(processor, model, device)
prev_time = time.time() # Track time to calculate FPS
try:
while True:
ret, frame = cap.read()
if not ret:
logger.error("Failed to read frame from webcam.")
break
# Update caption and track FPS
caption_generator.update_frame(frame)
current_caption = caption_generator.get_caption()
# Get GPU memory usage
gpu_info = get_gpu_usage()
# Calculate FPS
curr_time = time.time()
fps = 1 / (curr_time - prev_time)
prev_time = curr_time
# Break caption into lines if it overflows
max_width = 40 # Adjust max width for caption as needed
caption_lines = [current_caption[i:i + max_width] for i in range(0, len(current_caption), max_width)]
y_offset = 40
for line in caption_lines:
cv2.putText(frame, line, (20, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
y_offset += 30
# Display GPU memory usage and FPS
cv2.putText(frame, gpu_info, (20, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 1)
y_offset += 30
cv2.putText(frame, f"FPS: {fps:.2f}", (20, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 1)
# Display the video frame
cv2.imshow("BLIP: Unified Vision-Language Captioning", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except KeyboardInterrupt:
logger.info("Stream interrupted by user.")
finally:
caption_generator.stop()
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
logger = setup_logging()
logger.info("Loading BLIP model...")
blip_processor, blip_model, device = load_models()
if None in (blip_processor, blip_model):
logging.error("Failed to load the BLIP model. Exiting.")
sys.exit(1)
logger.info(f"Using {device.upper()} for inference.")
logger.info("Starting live stream with BLIP captioning and FPS display...")
live_stream_with_caption(blip_processor, blip_model, device)