Update app.py
Browse files
app.py
CHANGED
@@ -15,6 +15,24 @@ def create_monitor_interface():
|
|
15 |
def __init__(self, model_name: str = "llama-3.2-90b-vision-preview"):
|
16 |
self.client = Groq(api_key=api_key)
|
17 |
self.model_name = model_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
def analyze_frame(self, frame: np.ndarray) -> str:
|
20 |
if frame is None:
|
@@ -25,12 +43,15 @@ def create_monitor_interface():
|
|
25 |
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
|
26 |
elif len(frame.shape) == 3 and frame.shape[2] == 4:
|
27 |
frame = cv2.cvtColor(frame, cv2.COLOR_RGBA2RGB)
|
|
|
|
|
|
|
28 |
|
29 |
frame_pil = PILImage.fromarray(frame)
|
30 |
|
31 |
-
# Convert to base64
|
32 |
buffered = io.BytesIO()
|
33 |
-
frame_pil.save(buffered, format="JPEG")
|
34 |
img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
35 |
|
36 |
try:
|
|
|
15 |
def __init__(self, model_name: str = "llama-3.2-90b-vision-preview"):
|
16 |
self.client = Groq(api_key=api_key)
|
17 |
self.model_name = model_name
|
18 |
+
self.max_image_size = (512, 512) # Maximum dimensions
|
19 |
+
self.jpeg_quality = 50 # Reduced JPEG quality
|
20 |
+
|
21 |
+
def resize_image(self, image):
|
22 |
+
"""Resize image while maintaining aspect ratio"""
|
23 |
+
height, width = image.shape[:2]
|
24 |
+
|
25 |
+
# Calculate aspect ratio
|
26 |
+
aspect = width / height
|
27 |
+
|
28 |
+
if width > height:
|
29 |
+
new_width = min(self.max_image_size[0], width)
|
30 |
+
new_height = int(new_width / aspect)
|
31 |
+
else:
|
32 |
+
new_height = min(self.max_image_size[1], height)
|
33 |
+
new_width = int(new_height * aspect)
|
34 |
+
|
35 |
+
return cv2.resize(image, (new_width, new_height))
|
36 |
|
37 |
def analyze_frame(self, frame: np.ndarray) -> str:
|
38 |
if frame is None:
|
|
|
43 |
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
|
44 |
elif len(frame.shape) == 3 and frame.shape[2] == 4:
|
45 |
frame = cv2.cvtColor(frame, cv2.COLOR_RGBA2RGB)
|
46 |
+
|
47 |
+
# Resize image
|
48 |
+
frame = self.resize_image(frame)
|
49 |
|
50 |
frame_pil = PILImage.fromarray(frame)
|
51 |
|
52 |
+
# Convert to base64 with compression
|
53 |
buffered = io.BytesIO()
|
54 |
+
frame_pil.save(buffered, format="JPEG", quality=self.jpeg_quality, optimize=True)
|
55 |
img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
56 |
|
57 |
try:
|