Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,7 +41,6 @@
|
|
| 41 |
# demo.launch()
|
| 42 |
|
| 43 |
|
| 44 |
-
|
| 45 |
import gradio as gr
|
| 46 |
import cv2
|
| 47 |
import numpy as np
|
|
@@ -50,27 +49,24 @@ import time
|
|
| 50 |
import gc
|
| 51 |
from inspector_engine import AdvancedBlockInspector
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
inspector = AdvancedBlockInspector(yolo_model_path='yolo26n-obb.onnx')
|
| 56 |
|
| 57 |
def inspect(image):
|
|
|
|
| 58 |
if image is None:
|
| 59 |
return None, {"error": "No image uploaded"}
|
| 60 |
|
| 61 |
try:
|
| 62 |
start_time = time.time()
|
| 63 |
|
| 64 |
-
#
|
| 65 |
frame = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
# The engine handles internal lazy loading of YOLO/Torch on first call
|
| 69 |
result = inspector.inspect_block(frame)
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
# Your engine requires (image, saddle_rois, result_details)
|
| 73 |
-
# result.saddle_results contains the status for each saddle
|
| 74 |
vis_frame = frame.copy()
|
| 75 |
if hasattr(inspector, 'last_saddles') and result.saddle_results:
|
| 76 |
vis_frame = inspector.visualize_results(
|
|
@@ -79,26 +75,26 @@ def inspect(image):
|
|
| 79 |
result.saddle_results
|
| 80 |
)
|
| 81 |
|
| 82 |
-
#
|
| 83 |
vis_rgb = cv2.cvtColor(vis_frame, cv2.COLOR_BGR2RGB)
|
| 84 |
|
| 85 |
-
#
|
| 86 |
res_dict = result.to_dict()
|
| 87 |
res_dict['server_side_time_ms'] = (time.time() - start_time) * 1000
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
|
| 91 |
-
del frame
|
| 92 |
gc.collect()
|
| 93 |
|
| 94 |
return vis_rgb, res_dict
|
| 95 |
-
|
| 96 |
except Exception as e:
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
return None, {"error": str(e)}
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
# Adding 'api_name' allows your Render App to call /predict directly
|
| 102 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 103 |
gr.Markdown("# TMTL Industrial Inspector")
|
| 104 |
gr.Markdown("Remote AI Inference Engine for Saddle Defect Detection")
|
|
@@ -111,18 +107,26 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 111 |
with gr.Column():
|
| 112 |
output_img = gr.Image(type="numpy", label="AI Visualization")
|
| 113 |
output_json = gr.JSON(label="Detailed Analysis")
|
| 114 |
-
|
|
|
|
| 115 |
btn.click(
|
| 116 |
fn=inspect,
|
| 117 |
inputs=input_img,
|
| 118 |
-
outputs=[output_img, output_json]
|
| 119 |
-
api_name="predict" # CRITICAL: This allows Render to connect
|
| 120 |
)
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
)
|
|
|
|
| 41 |
# demo.launch()
|
| 42 |
|
| 43 |
|
|
|
|
| 44 |
import gradio as gr
|
| 45 |
import cv2
|
| 46 |
import numpy as np
|
|
|
|
| 49 |
import gc
|
| 50 |
from inspector_engine import AdvancedBlockInspector
|
| 51 |
|
| 52 |
+
# Initialize engine with lazy loading
|
| 53 |
+
inspector = AdvancedBlockInspector(yolo_model_path='yolo26n-obb.pt')
|
|
|
|
| 54 |
|
| 55 |
def inspect(image):
|
| 56 |
+
"""Main inspection function"""
|
| 57 |
if image is None:
|
| 58 |
return None, {"error": "No image uploaded"}
|
| 59 |
|
| 60 |
try:
|
| 61 |
start_time = time.time()
|
| 62 |
|
| 63 |
+
# Convert Gradio (RGB) to OpenCV (BGR)
|
| 64 |
frame = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 65 |
|
| 66 |
+
# Process image
|
|
|
|
| 67 |
result = inspector.inspect_block(frame)
|
| 68 |
|
| 69 |
+
# Visualization
|
|
|
|
|
|
|
| 70 |
vis_frame = frame.copy()
|
| 71 |
if hasattr(inspector, 'last_saddles') and result.saddle_results:
|
| 72 |
vis_frame = inspector.visualize_results(
|
|
|
|
| 75 |
result.saddle_results
|
| 76 |
)
|
| 77 |
|
| 78 |
+
# Convert back to RGB for Gradio
|
| 79 |
vis_rgb = cv2.cvtColor(vis_frame, cv2.COLOR_BGR2RGB)
|
| 80 |
|
| 81 |
+
# Prepare JSON data
|
| 82 |
res_dict = result.to_dict()
|
| 83 |
res_dict['server_side_time_ms'] = (time.time() - start_time) * 1000
|
| 84 |
|
| 85 |
+
# Memory cleanup
|
| 86 |
+
del frame, vis_frame
|
|
|
|
| 87 |
gc.collect()
|
| 88 |
|
| 89 |
return vis_rgb, res_dict
|
| 90 |
+
|
| 91 |
except Exception as e:
|
| 92 |
+
import traceback
|
| 93 |
+
error_msg = f"Error: {str(e)}\n{traceback.format_exc()}"
|
| 94 |
+
print(error_msg)
|
| 95 |
return None, {"error": str(e)}
|
| 96 |
|
| 97 |
+
# Create Gradio Interface
|
|
|
|
| 98 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 99 |
gr.Markdown("# TMTL Industrial Inspector")
|
| 100 |
gr.Markdown("Remote AI Inference Engine for Saddle Defect Detection")
|
|
|
|
| 107 |
with gr.Column():
|
| 108 |
output_img = gr.Image(type="numpy", label="AI Visualization")
|
| 109 |
output_json = gr.JSON(label="Detailed Analysis")
|
| 110 |
+
|
| 111 |
+
# Wire up the button - FIXED: removed api_name from click event
|
| 112 |
btn.click(
|
| 113 |
fn=inspect,
|
| 114 |
inputs=input_img,
|
| 115 |
+
outputs=[output_img, output_json]
|
|
|
|
| 116 |
)
|
| 117 |
+
|
| 118 |
+
# Add API endpoint properly - CRITICAL FIX
|
| 119 |
+
demo.load(api_name=False) # Disable auto API name on load
|
| 120 |
|
| 121 |
if __name__ == "__main__":
|
| 122 |
+
demo.queue(
|
| 123 |
+
max_size=10,
|
| 124 |
+
default_concurrency_limit=4 # Limit concurrent requests
|
| 125 |
+
).launch(
|
| 126 |
+
server_name="0.0.0.0",
|
| 127 |
+
server_port=7860,
|
| 128 |
+
show_error=True,
|
| 129 |
+
share=False,
|
| 130 |
+
# CRITICAL: Enable API mode for external calls
|
| 131 |
+
show_api=True
|
| 132 |
)
|