eho69 commited on
Commit
f26282e
·
verified ·
1 Parent(s): 1be4124

detecton endpoints

Browse files
Files changed (1) hide show
  1. app.py +25 -17
app.py CHANGED
@@ -250,7 +250,6 @@
250
  # if __name__ == "__main__":
251
  # demo.launch()
252
 
253
-
254
  import gradio as gr
255
  import cv2
256
  import numpy as np
@@ -360,20 +359,24 @@ class EnginePartDetector:
360
  try:
361
  with open(self.TEMPLATE_FILE, "rb") as f:
362
  data = pickle.load(f)
363
- # Support legacy format if needed, but here we assume the new format
364
- if isinstance(data, dict):
365
- # If old format was {name: {"features": feat, "roi": roi}}
366
- # we convert it to {name: [feat]}
 
 
 
 
 
 
 
 
367
  self.classes = {}
368
  self.class_rois = {}
369
  for k, v in data.items():
370
  if isinstance(v, dict) and "features" in v:
371
  self.classes[k] = [v["features"]]
372
  self.class_rois[k] = v.get("roi")
373
- else:
374
- self.classes[k] = v
375
- else:
376
- self.classes = {}
377
  logger.info(f"Loaded {len(self.classes)} class(es).")
378
  except Exception as e:
379
  logger.error(f"Data load failed: {e}")
@@ -381,12 +384,12 @@ class EnginePartDetector:
381
 
382
  def _persist_data(self) -> None:
383
  try:
 
384
  with open(self.TEMPLATE_FILE, "wb") as f:
385
- pickle.dump(self.classes, f)
386
- # Separately save ROIs if needed, but for now we just persist classes
387
- # In a real app we'd save ROIs too. Let's include them in a combined dict.
388
- with open("class_data.pkl", "wb") as f:
389
- pickle.dump({"classes": self.classes, "rois": self.class_rois}, f)
390
  except Exception as e:
391
  logger.error(f"Data save failed: {e}")
392
 
@@ -628,7 +631,7 @@ custom_css = """
628
  .footer { text-align: center; margin-top: 2rem; color: #666; }
629
  """
630
 
631
- with gr.Blocks(title="Engine Part CV System", theme=gr.themes.Soft(), css=custom_css) as demo:
632
  gr.Markdown("""
633
  <div class="header">
634
  <h1>🔧 Engine Part CV System</h1>
@@ -694,7 +697,7 @@ with gr.Blocks(title="Engine Part CV System", theme=gr.themes.Soft(), css=custom
694
  return detector.list_templates(), detector.get_template_roi(first_name)
695
  return "No classes trained yet.", None
696
 
697
- refresh_btn.click(fn=update_library_preview, outputs=[template_list, library_roi_view])
698
  demo.load(fn=update_library_preview, outputs=[template_list, library_roi_view])
699
 
700
  gr.Markdown("""
@@ -705,4 +708,9 @@ with gr.Blocks(title="Engine Part CV System", theme=gr.themes.Soft(), css=custom
705
  """)
706
 
707
  if __name__ == "__main__":
708
- demo.launch(share=False, show_error=True)
 
 
 
 
 
 
250
  # if __name__ == "__main__":
251
  # demo.launch()
252
 
 
253
  import gradio as gr
254
  import cv2
255
  import numpy as np
 
359
  try:
360
  with open(self.TEMPLATE_FILE, "rb") as f:
361
  data = pickle.load(f)
362
+
363
+ if isinstance(data, dict):
364
+ # Check if it's the new combined format
365
+ if "classes" in data and "rois" in data:
366
+ self.classes = data["classes"]
367
+ self.class_rois = data["rois"]
368
+ # Check if it's the temporary refactored format
369
+ elif all(isinstance(v, list) for v in data.values()):
370
+ self.classes = data
371
+ self.class_rois = {}
372
+ # Legacy support: {name: {"features": feat, "roi": roi}}
373
+ else:
374
  self.classes = {}
375
  self.class_rois = {}
376
  for k, v in data.items():
377
  if isinstance(v, dict) and "features" in v:
378
  self.classes[k] = [v["features"]]
379
  self.class_rois[k] = v.get("roi")
 
 
 
 
380
  logger.info(f"Loaded {len(self.classes)} class(es).")
381
  except Exception as e:
382
  logger.error(f"Data load failed: {e}")
 
384
 
385
  def _persist_data(self) -> None:
386
  try:
387
+ # Save everything in one file to ensure ROIs are preserved
388
  with open(self.TEMPLATE_FILE, "wb") as f:
389
+ pickle.dump({
390
+ "classes": self.classes,
391
+ "rois": self.class_rois
392
+ }, f)
 
393
  except Exception as e:
394
  logger.error(f"Data save failed: {e}")
395
 
 
631
  .footer { text-align: center; margin-top: 2rem; color: #666; }
632
  """
633
 
634
+ with gr.Blocks(title="Engine Part CV System") as demo:
635
  gr.Markdown("""
636
  <div class="header">
637
  <h1>🔧 Engine Part CV System</h1>
 
697
  return detector.list_templates(), detector.get_template_roi(first_name)
698
  return "No classes trained yet.", None
699
 
700
+ refresh_btn.click(fn=update_library_preview, outputs=[template_list, library_roi_view], api_name="list_classes")
701
  demo.load(fn=update_library_preview, outputs=[template_list, library_roi_view])
702
 
703
  gr.Markdown("""
 
708
  """)
709
 
710
  if __name__ == "__main__":
711
+ demo.launch(
712
+ share=False,
713
+ show_error=True,
714
+ theme=gr.themes.Soft(),
715
+ css=custom_css
716
+ )