jovian commited on
Commit
69c5037
·
1 Parent(s): 29b883b
Files changed (2) hide show
  1. app.py +120 -6
  2. requirements.txt +1 -0
app.py CHANGED
@@ -8,6 +8,14 @@ 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
 
13
 
@@ -15,7 +23,7 @@ class Detection:
15
 
16
  def __init__(self):
17
  # Set the model path and confidence threshold
18
- yolov8_model_path = "./model/best.pt" # Update to your model path
19
 
20
  # Initialize the AutoDetectionModel
21
  self.model = AutoDetectionModel.from_pretrained(
@@ -97,7 +105,7 @@ class Detection:
97
  # Populate the category_areas dictionary
98
  for annotation in annotations:
99
  category_name = annotation['category_name']
100
- area = annotation['bbox'][2] * annotation['bbox'][3] # Width * Height
101
  if category_name in category_areas:
102
  category_areas[category_name].append(area)
103
 
@@ -299,6 +307,54 @@ def generate_confidence_bar_chart(annotations):
299
 
300
 
301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
 
303
 
304
 
@@ -335,11 +391,11 @@ with gr.Blocks() as demo:
335
  }
336
 
337
 
338
- .gradio-container-5-9-0 .prose * {
339
  color: #083484;
340
  }
341
 
342
- .gradio-container-5-9-0 .prose :first-child {
343
  margin-top: 85px
344
  }
345
 
@@ -421,7 +477,7 @@ with gr.Blocks() as demo:
421
  font-family: 'Montserrat',sans-serif;
422
  }
423
 
424
- .gradio-container-5-9-0 .prose h1, .gradio-container-5-9-0 .prose h2, .gradio-container-5-9-0 .prose h3, .gradio-container-5-9-0 .prose h4, .gradio-container-5-9-0 .prose h5 {
425
  margin: var(--spacing-xxl) 0 var(--spacing-lg);
426
  font-weight: var(--prose-header-text-weight);
427
  line-height: 1.3;
@@ -517,7 +573,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 +581,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 +657,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 +673,11 @@ with gr.Blocks() as demo:
564
  frequency_btn_row,
565
  confidence_bar_chart_row,
566
  confidence_btn_row,
 
 
 
 
 
567
  login_row]
568
  )
569
 
 
8
  import torch
9
  import spaces
10
 
11
+ import os
12
+ import shutil
13
+ import subprocess
14
+
15
+ import os
16
+ import shutil
17
+ import subprocess
18
+
19
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
20
 
21
 
 
23
 
24
  def __init__(self):
25
  # Set the model path and confidence threshold
26
+ yolov8_model_path = "./model/best_100epochs_latest.pt" # Update to your model path
27
 
28
  # Initialize the AutoDetectionModel
29
  self.model = AutoDetectionModel.from_pretrained(
 
105
  # Populate the category_areas dictionary
106
  for annotation in annotations:
107
  category_name = annotation['category_name']
108
+ area = annotation['area']
109
  if category_name in category_areas:
110
  category_areas[category_name].append(area)
111
 
 
307
 
308
 
309
 
310
+ # Directory to save images
311
+ img_dir = "./stitching/img_dir/"
312
+ output_dir = "./"
313
+ os.makedirs(img_dir, exist_ok=True)
314
+ os.makedirs(output_dir, exist_ok=True)
315
+
316
+ # Function to handle the stitching process
317
+ def save_and_stitch(first_image, second_image, third_image, fourth_image):
318
+ # Save images to `img_dir`
319
+ images = [first_image, second_image, third_image, fourth_image]
320
+ for idx, img in enumerate(images):
321
+ if img is not None:
322
+ file_path = os.path.join(img_dir, f"Image_{idx + 1}.jpg")
323
+ img.save(file_path, format="JPEG")
324
+
325
+ # Execute the stitching command for all image files in the folder
326
+ command = f"stitch {img_dir}/Image_*.jpg"
327
+ try:
328
+ subprocess.run(command, shell=True, check=True)
329
+
330
+ # Load the result image from ./stitching/result.jpg
331
+ result_image_path = os.path.join(output_dir, "result.jpg")
332
+ if os.path.exists(result_image_path):
333
+ print("found")
334
+ return Image.open(result_image_path)
335
+ else:
336
+ print("not found")
337
+ return None # If result image doesn't exist, return None
338
+
339
+ except subprocess.CalledProcessError as e:
340
+ print(f"Error executing command: {str(e)}")
341
+ return None
342
+
343
+ # Function to clear the img_dir
344
+ def clear_img_dir():
345
+ for file_name in os.listdir(img_dir):
346
+ file_path = os.path.join(img_dir, file_name)
347
+ try:
348
+ if os.path.isfile(file_path):
349
+ os.remove(file_path)
350
+ elif os.path.isdir(file_path):
351
+ os.rmdir(file_path) # For directories, remove them
352
+ except Exception as e:
353
+ print(f"Error deleting file {file_name}: {str(e)}")
354
+ return "Images cleared from img_dir!"
355
+
356
+
357
+
358
 
359
 
360
 
 
391
  }
392
 
393
 
394
+ .gradio-container-5-4-0 .prose * {
395
  color: #083484;
396
  }
397
 
398
+ .gradio-container-5-4-0 .prose :first-child {
399
  margin-top: 85px
400
  }
401
 
 
477
  font-family: 'Montserrat',sans-serif;
478
  }
479
 
480
+ .gradio-container-5-4-0 .prose h1, .gradio-container-5-4-0 .prose h2, .gradio-container-5-4-0 .prose h3, .gradio-container-5-4-0 .prose h4, .gradio-container-5-4-0 .prose h5 {
481
  margin: var(--spacing-xxl) 0 var(--spacing-lg);
482
  font-weight: var(--prose-header-text-weight);
483
  line-height: 1.3;
 
573
  pittings_confidence_bar_chart = gr.Plot(label="Pittings Confidence Score Distribution")
574
 
575
 
576
+ #Gradio row for confidence bar chart
577
  with gr.Row(visible=False) as confidence_btn_row:
578
  confidence_chart_btn = gr.Button("Generate Confidence Chart", variant="primary")
579
  confidence_chart_btn.click(
 
581
  inputs=output_annotations, # Pass the annotations
582
  outputs=[nicks_confidence_bar_chart,dents_confidence_bar_chart,scratches_confidence_bar_chart,pittings_confidence_bar_chart]
583
  )
584
+
585
+
586
+
587
+
588
+
589
+ with gr.Row(visible=False) as upload_image_stitching:
590
+ first_image_stitching = gr.Image(type="pil", label="Select Image 1")
591
+ second_image_stitching = gr.Image(type="pil", label="Select Image 2")
592
+ third_image_stitching = gr.Image(type="pil", label="Select Image 3")
593
+ fourth_image_stitching = gr.Image(type="pil", label="Select Image 4")
594
+
595
+ # Row for result output
596
+ with gr.Row(visible=False) as result_output_block:
597
+ result_output = gr.Image(type="pil",label="Stitched Output Image")
598
+
599
+ # Row for buttons
600
+ with gr.Row(visible=False) as stitching_btn:
601
+ apply_stitching_btn = gr.Button("Apply Stitching",variant="primary")
602
+ # Button click actions
603
+ apply_stitching_btn.click(
604
+ save_and_stitch,
605
+ inputs=[
606
+ first_image_stitching,
607
+ second_image_stitching,
608
+ third_image_stitching,
609
+ fourth_image_stitching,
610
+ ],
611
+ outputs=result_output,
612
+ )
613
+
614
+
615
+ # Row for displaying status
616
+ with gr.Row(visible=False) as display_img_dir_status:
617
+ status_text = gr.Textbox(label="Status")
618
+
619
+
620
+ # Row for clearing images from img_dir
621
+ with gr.Row(visible=False) as clear_img_btn:
622
+ clear_img_dir_btn = gr.Button("Clear Images from img_dir",variant="primary")
623
+ clear_img_dir_btn.click(
624
+ clear_img_dir,
625
+ inputs=[],
626
+ outputs=status_text
627
+ )
628
+
629
+
630
+
631
+
632
 
633
  # Login row, initially visible
634
  with gr.Row(visible=True) as login_row:
 
657
  gr.update(visible=login_state), # Show frequency_btn_row
658
  gr.update(visible=login_state), #Show Confidence chart
659
  gr.update(visible=login_state), #Show Confidence btn
660
+ gr.update(visible=login_state), #Show upload image stitching
661
+ gr.update(visible=login_state), #Show stitched result output
662
+ gr.update(visible=login_state), #Show stitching btn
663
+ gr.update(visible=login_state), #Show display image dir status
664
+ gr.update(visible=login_state), #Show clear image btn
665
  gr.update(visible=not login_state) # for login
666
  ),
667
  inputs=login_successful,
 
673
  frequency_btn_row,
674
  confidence_bar_chart_row,
675
  confidence_btn_row,
676
+ upload_image_stitching,
677
+ result_output_block,
678
+ stitching_btn,
679
+ display_img_dir_status,
680
+ clear_img_btn,
681
  login_row]
682
  )
683
 
requirements.txt CHANGED
@@ -1,3 +1,4 @@
 
1
  --extra-index-url https://download.pytorch.org/whl/cu124
2
  torch
3
  torchvision
 
1
+ git+https://github.com/OpenStitching/stitching.git@main#egg=stitching
2
  --extra-index-url https://download.pytorch.org/whl/cu124
3
  torch
4
  torchvision