Spaces:
Sleeping
Sleeping
jovian
commited on
Commit
·
49c91ff
1
Parent(s):
40d2aea
stitching
Browse files- app.py +113 -3
- requirements.txt +1 -1
- stitching +1 -0
app.py
CHANGED
|
@@ -6,7 +6,11 @@ from sahi import AutoDetectionModel
|
|
| 6 |
from PIL import Image
|
| 7 |
import plotly.graph_objects as go
|
| 8 |
import torch
|
| 9 |
-
import spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 12 |
|
|
@@ -209,7 +213,7 @@ def upload_image(image):
|
|
| 209 |
"""Process the uploaded image (if needed) and display it."""
|
| 210 |
return image
|
| 211 |
|
| 212 |
-
|
| 213 |
def apply_detection(image):
|
| 214 |
"""Run object detection on the uploaded image and return the annotated image."""
|
| 215 |
# Convert image from PIL to NumPy array
|
|
@@ -299,6 +303,54 @@ def generate_confidence_bar_chart(annotations):
|
|
| 299 |
|
| 300 |
|
| 301 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
|
| 304 |
|
|
@@ -517,7 +569,7 @@ with gr.Blocks() as demo:
|
|
| 517 |
pittings_confidence_bar_chart = gr.Plot(label="Pittings Confidence Score Distribution")
|
| 518 |
|
| 519 |
|
| 520 |
-
|
| 521 |
with gr.Row(visible=False) as confidence_btn_row:
|
| 522 |
confidence_chart_btn = gr.Button("Generate Confidence Chart", variant="primary")
|
| 523 |
confidence_chart_btn.click(
|
|
@@ -525,6 +577,54 @@ with gr.Blocks() as demo:
|
|
| 525 |
inputs=output_annotations, # Pass the annotations
|
| 526 |
outputs=[nicks_confidence_bar_chart,dents_confidence_bar_chart,scratches_confidence_bar_chart,pittings_confidence_bar_chart]
|
| 527 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
|
| 529 |
# Login row, initially visible
|
| 530 |
with gr.Row(visible=True) as login_row:
|
|
@@ -553,6 +653,11 @@ with gr.Blocks() as demo:
|
|
| 553 |
gr.update(visible=login_state), # Show frequency_btn_row
|
| 554 |
gr.update(visible=login_state), #Show Confidence chart
|
| 555 |
gr.update(visible=login_state), #Show Confidence btn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
gr.update(visible=not login_state) # for login
|
| 557 |
),
|
| 558 |
inputs=login_successful,
|
|
@@ -564,6 +669,11 @@ with gr.Blocks() as demo:
|
|
| 564 |
frequency_btn_row,
|
| 565 |
confidence_bar_chart_row,
|
| 566 |
confidence_btn_row,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
login_row]
|
| 568 |
)
|
| 569 |
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
import plotly.graph_objects as go
|
| 8 |
import torch
|
| 9 |
+
#import spaces
|
| 10 |
+
|
| 11 |
+
import os
|
| 12 |
+
import shutil
|
| 13 |
+
import subprocess
|
| 14 |
|
| 15 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 16 |
|
|
|
|
| 213 |
"""Process the uploaded image (if needed) and display it."""
|
| 214 |
return image
|
| 215 |
|
| 216 |
+
#@spaces.GPU
|
| 217 |
def apply_detection(image):
|
| 218 |
"""Run object detection on the uploaded image and return the annotated image."""
|
| 219 |
# Convert image from PIL to NumPy array
|
|
|
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
+
# Directory to save images
|
| 307 |
+
img_dir = "./stitching/img_dir/"
|
| 308 |
+
output_dir = "./"
|
| 309 |
+
os.makedirs(img_dir, exist_ok=True)
|
| 310 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 311 |
+
|
| 312 |
+
# Function to handle the stitching process
|
| 313 |
+
def save_and_stitch(first_image, second_image, third_image, fourth_image):
|
| 314 |
+
# Save images to `img_dir`
|
| 315 |
+
images = [first_image, second_image, third_image, fourth_image]
|
| 316 |
+
for idx, img in enumerate(images):
|
| 317 |
+
if img is not None:
|
| 318 |
+
file_path = os.path.join(img_dir, f"Image_{idx + 1}.jpg")
|
| 319 |
+
img.save(file_path, format="JPEG")
|
| 320 |
+
|
| 321 |
+
# Execute the stitching command for all image files in the folder
|
| 322 |
+
command = f"stitch {img_dir}/Image_*.jpg"
|
| 323 |
+
try:
|
| 324 |
+
subprocess.run(command, shell=True, check=True)
|
| 325 |
+
|
| 326 |
+
# Load the result image from ./stitching/result.jpg
|
| 327 |
+
result_image_path = os.path.join(output_dir, "result.jpg")
|
| 328 |
+
if os.path.exists(result_image_path):
|
| 329 |
+
print("found")
|
| 330 |
+
return Image.open(result_image_path)
|
| 331 |
+
else:
|
| 332 |
+
print("not found")
|
| 333 |
+
return None # If result image doesn't exist, return None
|
| 334 |
+
|
| 335 |
+
except subprocess.CalledProcessError as e:
|
| 336 |
+
print(f"Error executing command: {str(e)}")
|
| 337 |
+
return None
|
| 338 |
+
|
| 339 |
+
# Function to clear the img_dir
|
| 340 |
+
def clear_img_dir():
|
| 341 |
+
for file_name in os.listdir(img_dir):
|
| 342 |
+
file_path = os.path.join(img_dir, file_name)
|
| 343 |
+
try:
|
| 344 |
+
if os.path.isfile(file_path):
|
| 345 |
+
os.remove(file_path)
|
| 346 |
+
elif os.path.isdir(file_path):
|
| 347 |
+
os.rmdir(file_path) # For directories, remove them
|
| 348 |
+
except Exception as e:
|
| 349 |
+
print(f"Error deleting file {file_name}: {str(e)}")
|
| 350 |
+
return "Images cleared from img_dir!"
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
|
| 354 |
|
| 355 |
|
| 356 |
|
|
|
|
| 569 |
pittings_confidence_bar_chart = gr.Plot(label="Pittings Confidence Score Distribution")
|
| 570 |
|
| 571 |
|
| 572 |
+
#Gradio row for confidence bar chart
|
| 573 |
with gr.Row(visible=False) as confidence_btn_row:
|
| 574 |
confidence_chart_btn = gr.Button("Generate Confidence Chart", variant="primary")
|
| 575 |
confidence_chart_btn.click(
|
|
|
|
| 577 |
inputs=output_annotations, # Pass the annotations
|
| 578 |
outputs=[nicks_confidence_bar_chart,dents_confidence_bar_chart,scratches_confidence_bar_chart,pittings_confidence_bar_chart]
|
| 579 |
)
|
| 580 |
+
|
| 581 |
+
|
| 582 |
+
|
| 583 |
+
|
| 584 |
+
|
| 585 |
+
with gr.Row(visible=False) as upload_image_stitching:
|
| 586 |
+
first_image_stitching = gr.Image(type="pil", label="Select Image 1")
|
| 587 |
+
second_image_stitching = gr.Image(type="pil", label="Select Image 2")
|
| 588 |
+
third_image_stitching = gr.Image(type="pil", label="Select Image 3")
|
| 589 |
+
fourth_image_stitching = gr.Image(type="pil", label="Select Image 4")
|
| 590 |
+
|
| 591 |
+
# Row for result output
|
| 592 |
+
with gr.Row(visible=False) as result_output_block:
|
| 593 |
+
result_output = gr.Image(type="pil",label="Stitched Output Image")
|
| 594 |
+
|
| 595 |
+
# Row for buttons
|
| 596 |
+
with gr.Row(visible=False) as stitching_btn:
|
| 597 |
+
apply_stitching_btn = gr.Button("Apply Stitching",variant="primary")
|
| 598 |
+
# Button click actions
|
| 599 |
+
apply_stitching_btn.click(
|
| 600 |
+
save_and_stitch,
|
| 601 |
+
inputs=[
|
| 602 |
+
first_image_stitching,
|
| 603 |
+
second_image_stitching,
|
| 604 |
+
third_image_stitching,
|
| 605 |
+
fourth_image_stitching,
|
| 606 |
+
],
|
| 607 |
+
outputs=result_output,
|
| 608 |
+
)
|
| 609 |
+
|
| 610 |
+
|
| 611 |
+
# Row for displaying status
|
| 612 |
+
with gr.Row(visible=False) as display_img_dir_status:
|
| 613 |
+
status_text = gr.Textbox(label="Status")
|
| 614 |
+
|
| 615 |
+
|
| 616 |
+
# Row for clearing images from img_dir
|
| 617 |
+
with gr.Row(visible=False) as clear_img_btn:
|
| 618 |
+
clear_img_dir_btn = gr.Button("Clear Images from img_dir",variant="primary")
|
| 619 |
+
clear_img_dir_btn.click(
|
| 620 |
+
clear_img_dir,
|
| 621 |
+
inputs=[],
|
| 622 |
+
outputs=status_text
|
| 623 |
+
)
|
| 624 |
+
|
| 625 |
+
|
| 626 |
+
|
| 627 |
+
|
| 628 |
|
| 629 |
# Login row, initially visible
|
| 630 |
with gr.Row(visible=True) as login_row:
|
|
|
|
| 653 |
gr.update(visible=login_state), # Show frequency_btn_row
|
| 654 |
gr.update(visible=login_state), #Show Confidence chart
|
| 655 |
gr.update(visible=login_state), #Show Confidence btn
|
| 656 |
+
gr.update(visible=login_state), #Show upload image stitching
|
| 657 |
+
gr.update(visible=login_state), #Show stitched result output
|
| 658 |
+
gr.update(visible=login_state), #Show stitching btn
|
| 659 |
+
gr.update(visible=login_state), #Show display image dir status
|
| 660 |
+
gr.update(visible=login_state), #Show clear image btn
|
| 661 |
gr.update(visible=not login_state) # for login
|
| 662 |
),
|
| 663 |
inputs=login_successful,
|
|
|
|
| 669 |
frequency_btn_row,
|
| 670 |
confidence_bar_chart_row,
|
| 671 |
confidence_btn_row,
|
| 672 |
+
upload_image_stitching,
|
| 673 |
+
result_output_block,
|
| 674 |
+
stitching_btn,
|
| 675 |
+
display_img_dir_status,
|
| 676 |
+
clear_img_btn,
|
| 677 |
login_row]
|
| 678 |
)
|
| 679 |
|
requirements.txt
CHANGED
|
@@ -7,4 +7,4 @@ sahi==0.11.18
|
|
| 7 |
pillow
|
| 8 |
plotly==5.24.1
|
| 9 |
ultralytics==8.3.24
|
| 10 |
-
|
|
|
|
| 7 |
pillow
|
| 8 |
plotly==5.24.1
|
| 9 |
ultralytics==8.3.24
|
| 10 |
+
stitching
|
stitching
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Subproject commit b890591b524bed72685dc9de2468db6f89a921ce
|