Spaces:
Running
Running
tweak
Browse files
app.py
CHANGED
|
@@ -12,7 +12,6 @@ classes = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight
|
|
| 12 |
API_KEY = os.environ.get("ROBOFLOW_API_KEY")
|
| 13 |
ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
|
| 14 |
|
| 15 |
-
|
| 16 |
def RoboFlowGetOutlineBoxesPIL(pil_img):
|
| 17 |
client = InferenceHTTPClient(
|
| 18 |
api_url="https://detect.roboflow.com",
|
|
@@ -27,14 +26,20 @@ def RoboFlowGetOutlineBoxesPIL(pil_img):
|
|
| 27 |
use_cache=True # cache workflow definition for 15 minutes
|
| 28 |
)
|
| 29 |
return result
|
| 30 |
-
|
| 31 |
-
def localOutlineBox(image):
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
print("model exists")
|
| 35 |
else:
|
| 36 |
print("model is not available")
|
| 37 |
-
model = YOLO(
|
| 38 |
yolo_ret = model(image, verbose=False)
|
| 39 |
useful_ret = yolo_ret[0]
|
| 40 |
names = model.names
|
|
@@ -294,13 +299,13 @@ def online_process_image(image):
|
|
| 294 |
json_str = json.dumps(kept_box_info, indent=2)
|
| 295 |
return boxed_img, textual, json_str
|
| 296 |
|
| 297 |
-
def process_image(image):
|
| 298 |
print("start processing image")
|
| 299 |
if image is None:
|
| 300 |
# Ensure we always return 3 values to prevent errors.
|
| 301 |
return None, "", ""
|
| 302 |
#pil_image = image.copy() if hasattr(image, 'copy') else Image.fromarray(image)
|
| 303 |
-
all_box_info = localOutlineBox(image)
|
| 304 |
kept_box_info = filter_overlapping_boxes(all_box_info)
|
| 305 |
del all_box_info
|
| 306 |
boxed_img = drawWithAllBox_info(image, kept_box_info)
|
|
@@ -323,7 +328,15 @@ with gr.Blocks() as demo:
|
|
| 323 |
# --- Main Application (initially hidden) ---
|
| 324 |
with gr.Column(visible=False) as main_app:
|
| 325 |
img_input = gr.Image(type="pil", label="Upload Image")
|
| 326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
img_out = gr.Image(type="pil", label="Image with Boxes")
|
| 328 |
text_out = gr.Textbox(label="Textual Description", lines=8)
|
| 329 |
json_state = gr.State("")
|
|
@@ -344,9 +357,9 @@ with gr.Blocks() as demo:
|
|
| 344 |
f.write(json_str)
|
| 345 |
return f.name
|
| 346 |
|
| 347 |
-
def _process_and_prepare_download(image):
|
| 348 |
"""Processes the image, creates the JSON file, and updates the UI."""
|
| 349 |
-
boxed_img, textual, json_str = process_image(image)
|
| 350 |
filepath = create_json_file(json_str)
|
| 351 |
|
| 352 |
# Use the legacy gr.update() for compatibility with older Gradio versions.
|
|
@@ -370,7 +383,7 @@ with gr.Blocks() as demo:
|
|
| 370 |
|
| 371 |
run_btn.click(
|
| 372 |
_process_and_prepare_download,
|
| 373 |
-
inputs=img_input,
|
| 374 |
# The output now includes the download button itself.
|
| 375 |
outputs=[img_out, text_out, json_state, download_btn]
|
| 376 |
)
|
|
|
|
| 12 |
API_KEY = os.environ.get("ROBOFLOW_API_KEY")
|
| 13 |
ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
|
| 14 |
|
|
|
|
| 15 |
def RoboFlowGetOutlineBoxesPIL(pil_img):
|
| 16 |
client = InferenceHTTPClient(
|
| 17 |
api_url="https://detect.roboflow.com",
|
|
|
|
| 26 |
use_cache=True # cache workflow definition for 15 minutes
|
| 27 |
)
|
| 28 |
return result
|
| 29 |
+
|
| 30 |
+
def localOutlineBox(image, selected_model="yolo_accurate"):
|
| 31 |
+
model_paths = {
|
| 32 |
+
"yolo_accurate": "./vt_dataset_yolov12_v7_weights.pt",
|
| 33 |
+
"yolo_extra_large": "./yolo_extra_large_weights.pt"
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
model_path = model_paths.get(selected_model, "./vt_dataset_yolov12_v7_weights.pt")
|
| 37 |
+
model_path = "./vt_dataset_yolov12_v7_weights.pt"
|
| 38 |
+
if os.path.exists(model_path):
|
| 39 |
print("model exists")
|
| 40 |
else:
|
| 41 |
print("model is not available")
|
| 42 |
+
model = YOLO(model_path)
|
| 43 |
yolo_ret = model(image, verbose=False)
|
| 44 |
useful_ret = yolo_ret[0]
|
| 45 |
names = model.names
|
|
|
|
| 299 |
json_str = json.dumps(kept_box_info, indent=2)
|
| 300 |
return boxed_img, textual, json_str
|
| 301 |
|
| 302 |
+
def process_image(image, selected_model = "yolo_accurate"):
|
| 303 |
print("start processing image")
|
| 304 |
if image is None:
|
| 305 |
# Ensure we always return 3 values to prevent errors.
|
| 306 |
return None, "", ""
|
| 307 |
#pil_image = image.copy() if hasattr(image, 'copy') else Image.fromarray(image)
|
| 308 |
+
all_box_info = localOutlineBox(image, selected_model)
|
| 309 |
kept_box_info = filter_overlapping_boxes(all_box_info)
|
| 310 |
del all_box_info
|
| 311 |
boxed_img = drawWithAllBox_info(image, kept_box_info)
|
|
|
|
| 328 |
# --- Main Application (initially hidden) ---
|
| 329 |
with gr.Column(visible=False) as main_app:
|
| 330 |
img_input = gr.Image(type="pil", label="Upload Image")
|
| 331 |
+
|
| 332 |
+
with gr.Row():
|
| 333 |
+
model_list = gr.Dropdown(
|
| 334 |
+
choices=["yolo_accurate", "yolo_extra_large"],
|
| 335 |
+
value="yolo_accurate",
|
| 336 |
+
label="Select Model",
|
| 337 |
+
info="Choose the YOLO model for detection"
|
| 338 |
+
)
|
| 339 |
+
run_btn = gr.Button("Run Detection")
|
| 340 |
img_out = gr.Image(type="pil", label="Image with Boxes")
|
| 341 |
text_out = gr.Textbox(label="Textual Description", lines=8)
|
| 342 |
json_state = gr.State("")
|
|
|
|
| 357 |
f.write(json_str)
|
| 358 |
return f.name
|
| 359 |
|
| 360 |
+
def _process_and_prepare_download(image, selected_model):
|
| 361 |
"""Processes the image, creates the JSON file, and updates the UI."""
|
| 362 |
+
boxed_img, textual, json_str = process_image(image, selected_model)
|
| 363 |
filepath = create_json_file(json_str)
|
| 364 |
|
| 365 |
# Use the legacy gr.update() for compatibility with older Gradio versions.
|
|
|
|
| 383 |
|
| 384 |
run_btn.click(
|
| 385 |
_process_and_prepare_download,
|
| 386 |
+
inputs=[img_input, model_list],
|
| 387 |
# The output now includes the download button itself.
|
| 388 |
outputs=[img_out, text_out, json_state, download_btn]
|
| 389 |
)
|