Spaces:
Sleeping
Sleeping
popboat1 commited on
Commit ·
11890f3
1
Parent(s): 8450fd0
Optimize video stream payload and fix WebSocket CORS policy
Browse files- src/api/api.py +11 -17
src/api/api.py
CHANGED
|
@@ -8,6 +8,7 @@ import base64
|
|
| 8 |
import math
|
| 9 |
import os
|
| 10 |
import asyncio
|
|
|
|
| 11 |
|
| 12 |
gpus = tf.config.list_physical_devices('GPU')
|
| 13 |
if gpus:
|
|
@@ -22,7 +23,7 @@ app = FastAPI()
|
|
| 22 |
app.add_middleware(
|
| 23 |
CORSMiddleware,
|
| 24 |
allow_origins=["*"],
|
| 25 |
-
allow_credentials=
|
| 26 |
allow_methods=["*"],
|
| 27 |
allow_headers=["*"],
|
| 28 |
)
|
|
@@ -81,15 +82,9 @@ def generate_feature_grid(feature_map, max_features=64):
|
|
| 81 |
grid_image[y_start:y_end, x_start:x_end] = channel_img
|
| 82 |
|
| 83 |
grid_image = np.uint8(grid_image)
|
| 84 |
-
|
| 85 |
colored_grid = cv2.applyColorMap(grid_image, cv2.COLORMAP_VIRIDIS)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
alpha_channel = grid_image # Use the raw grayscale intensity as the alpha map
|
| 89 |
-
|
| 90 |
-
transparent_grid = cv2.merge((b_channel, g_channel, r_channel, alpha_channel))
|
| 91 |
-
|
| 92 |
-
_, buffer = cv2.imencode('.png', transparent_grid)
|
| 93 |
return base64.b64encode(buffer).decode('utf-8')
|
| 94 |
|
| 95 |
@app.post("/predict")
|
|
@@ -103,14 +98,12 @@ async def predict_image(file: UploadFile = File(...)):
|
|
| 103 |
img_batch = np.expand_dims(img_normalized, axis=0)
|
| 104 |
|
| 105 |
activations = feature_extractor.predict(img_batch)
|
| 106 |
-
|
| 107 |
predictions = model.predict(img_batch)
|
| 108 |
class_idx = np.argmax(predictions[0])
|
| 109 |
|
| 110 |
layer_data = []
|
| 111 |
for i, activation in enumerate(activations):
|
| 112 |
b64_image = generate_feature_grid(activation)
|
| 113 |
-
|
| 114 |
layer_data.append({
|
| 115 |
"layer_index": i + 1,
|
| 116 |
"shape": activation.shape[1:],
|
|
@@ -142,17 +135,17 @@ async def predict_video_stream(websocket: WebSocket):
|
|
| 142 |
img_normalized = img_resized.astype(np.float32) / 255.0
|
| 143 |
img_batch = np.expand_dims(img_normalized, axis=0)
|
| 144 |
|
| 145 |
-
activations = feature_extractor
|
| 146 |
-
predictions = model
|
| 147 |
-
class_idx = np.argmax(predictions[0])
|
| 148 |
|
| 149 |
layer_data = []
|
| 150 |
for i, activation in enumerate(activations):
|
| 151 |
-
b64_image = generate_feature_grid(activation)
|
| 152 |
layer_data.append({
|
| 153 |
"layer_index": i + 1,
|
| 154 |
"shape": activation.shape[1:],
|
| 155 |
-
"texture_b64": f"data:image/
|
| 156 |
})
|
| 157 |
|
| 158 |
await websocket.send_json({
|
|
@@ -163,8 +156,9 @@ async def predict_video_stream(websocket: WebSocket):
|
|
| 163 |
await asyncio.sleep(0.01)
|
| 164 |
|
| 165 |
except WebSocketDisconnect:
|
| 166 |
-
print("WebSocket Disconnected")
|
| 167 |
except Exception as e:
|
| 168 |
-
print(f"WebSocket
|
|
|
|
| 169 |
|
| 170 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
|
|
|
| 8 |
import math
|
| 9 |
import os
|
| 10 |
import asyncio
|
| 11 |
+
import traceback
|
| 12 |
|
| 13 |
gpus = tf.config.list_physical_devices('GPU')
|
| 14 |
if gpus:
|
|
|
|
| 23 |
app.add_middleware(
|
| 24 |
CORSMiddleware,
|
| 25 |
allow_origins=["*"],
|
| 26 |
+
allow_credentials=False,
|
| 27 |
allow_methods=["*"],
|
| 28 |
allow_headers=["*"],
|
| 29 |
)
|
|
|
|
| 82 |
grid_image[y_start:y_end, x_start:x_end] = channel_img
|
| 83 |
|
| 84 |
grid_image = np.uint8(grid_image)
|
|
|
|
| 85 |
colored_grid = cv2.applyColorMap(grid_image, cv2.COLORMAP_VIRIDIS)
|
| 86 |
|
| 87 |
+
_, buffer = cv2.imencode('.jpg', colored_grid, [cv2.IMWRITE_JPEG_QUALITY, 60])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
return base64.b64encode(buffer).decode('utf-8')
|
| 89 |
|
| 90 |
@app.post("/predict")
|
|
|
|
| 98 |
img_batch = np.expand_dims(img_normalized, axis=0)
|
| 99 |
|
| 100 |
activations = feature_extractor.predict(img_batch)
|
|
|
|
| 101 |
predictions = model.predict(img_batch)
|
| 102 |
class_idx = np.argmax(predictions[0])
|
| 103 |
|
| 104 |
layer_data = []
|
| 105 |
for i, activation in enumerate(activations):
|
| 106 |
b64_image = generate_feature_grid(activation)
|
|
|
|
| 107 |
layer_data.append({
|
| 108 |
"layer_index": i + 1,
|
| 109 |
"shape": activation.shape[1:],
|
|
|
|
| 135 |
img_normalized = img_resized.astype(np.float32) / 255.0
|
| 136 |
img_batch = np.expand_dims(img_normalized, axis=0)
|
| 137 |
|
| 138 |
+
activations = feature_extractor(img_batch, training=False)
|
| 139 |
+
predictions = model(img_batch, training=False)
|
| 140 |
+
class_idx = np.argmax(predictions[0].numpy())
|
| 141 |
|
| 142 |
layer_data = []
|
| 143 |
for i, activation in enumerate(activations):
|
| 144 |
+
b64_image = generate_feature_grid(activation.numpy())
|
| 145 |
layer_data.append({
|
| 146 |
"layer_index": i + 1,
|
| 147 |
"shape": activation.shape[1:],
|
| 148 |
+
"texture_b64": f"data:image/jpeg;base64,{b64_image}"
|
| 149 |
})
|
| 150 |
|
| 151 |
await websocket.send_json({
|
|
|
|
| 156 |
await asyncio.sleep(0.01)
|
| 157 |
|
| 158 |
except WebSocketDisconnect:
|
| 159 |
+
print("WebSocket Disconnected Cleanly")
|
| 160 |
except Exception as e:
|
| 161 |
+
print(f"WebSocket Crashed:")
|
| 162 |
+
traceback.print_exc()
|
| 163 |
|
| 164 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|