Sumit commited on
Commit
d6d1de8
·
1 Parent(s): 48f525f

updated the code for fmcg inspection

Browse files
app.py CHANGED
@@ -24,11 +24,11 @@ def run_detect(img, conf):
24
 
25
  return annotated, status, df
26
 
27
- def run_qc(img, threshold_pct):
28
  if img is None:
29
  return "", "⚠️ Please upload an image.", ""
30
 
31
- res = classify_qc(img, threshold=threshold_pct/100.0)
32
 
33
  warning_text = ""
34
  if res.get("warning"):
@@ -40,7 +40,7 @@ def run_qc(img, threshold_pct):
40
  source = res.get("source", "Unknown")
41
 
42
  if status == "QC Approved":
43
- result_text = f"### ✅ QC Status: **{status}** · Defect: **{defect}** · Confidence: {confidence}%"
44
  else:
45
  result_text = f"### ❌ QC Status: **{status}** · Defect: **{defect}** · Confidence: {confidence}%"
46
 
@@ -74,6 +74,7 @@ with gr.Blocks(title="Yugensys Vision AI — Manufacturing") as demo:
74
  with gr.Row():
75
  with gr.Column(scale=1):
76
  img_input2 = gr.Image(type="pil", label="Upload a product image")
 
77
  thresh_slider = gr.Slider(minimum=0, maximum=100, value=80, step=1, label="EfficientNet Confidence Threshold (Triggers YOLO Fallback)")
78
  btn2 = gr.Button("Run QC Inspection", variant="primary")
79
  with gr.Column(scale=1):
@@ -81,7 +82,7 @@ with gr.Blocks(title="Yugensys Vision AI — Manufacturing") as demo:
81
  result_output = gr.Markdown()
82
  source_output = gr.Markdown()
83
 
84
- btn2.click(fn=run_qc, inputs=[img_input2, thresh_slider], outputs=[warning_output, result_output, source_output])
85
 
86
  if __name__ == "__main__":
87
  demo.launch()
 
24
 
25
  return annotated, status, df
26
 
27
+ def run_qc(img, product_type, threshold_pct):
28
  if img is None:
29
  return "", "⚠️ Please upload an image.", ""
30
 
31
+ res = classify_qc(img, product_type=product_type, threshold=threshold_pct/100.0)
32
 
33
  warning_text = ""
34
  if res.get("warning"):
 
40
  source = res.get("source", "Unknown")
41
 
42
  if status == "QC Approved":
43
+ result_text = f"### ✅ QC Status: **{status}** · Defect: **None** · Confidence: {confidence}%"
44
  else:
45
  result_text = f"### ❌ QC Status: **{status}** · Defect: **{defect}** · Confidence: {confidence}%"
46
 
 
74
  with gr.Row():
75
  with gr.Column(scale=1):
76
  img_input2 = gr.Image(type="pil", label="Upload a product image")
77
+ product_dropdown = gr.Dropdown(choices=["fmcg_inspection", "steel_inspection"], value="fmcg_inspection", label="Product Type")
78
  thresh_slider = gr.Slider(minimum=0, maximum=100, value=80, step=1, label="EfficientNet Confidence Threshold (Triggers YOLO Fallback)")
79
  btn2 = gr.Button("Run QC Inspection", variant="primary")
80
  with gr.Column(scale=1):
 
82
  result_output = gr.Markdown()
83
  source_output = gr.Markdown()
84
 
85
+ btn2.click(fn=run_qc, inputs=[img_input2, product_dropdown, thresh_slider], outputs=[warning_output, result_output, source_output])
86
 
87
  if __name__ == "__main__":
88
  demo.launch()
inference/classify.py CHANGED
@@ -62,18 +62,18 @@ def predict_yolo(image_path, yolo_model):
62
 
63
  from functools import lru_cache
64
 
65
- @lru_cache(maxsize=1)
66
- def get_efficientnet():
67
  project_root = Path(__file__).parent.parent
68
- eff_path = project_root / "models/poc2/efficientnet_b0/best.pt"
69
  if not eff_path.exists():
70
  return None, None, None
71
  return load_efficientnet(eff_path)
72
 
73
- @lru_cache(maxsize=1)
74
- def get_yolo():
75
  project_root = Path(__file__).parent.parent
76
- yolo_path = project_root / "models/poc2/yolov11_cls/best.pt"
77
  if not yolo_path.exists():
78
  return None
79
  return YOLO(str(yolo_path))
@@ -112,13 +112,13 @@ def predict_efficientnet_pil(img, model, config, idx_to_class, min_no_defect_pro
112
 
113
  return predicted_class, confidence, original_pred, original_conf
114
 
115
- def classify_qc(img, force_model=None, threshold=0.80, min_no_defect_prob=0.0):
116
  """
117
  Main entry point for Streamlit. Takes a PIL Image, runs the dual-inference
118
  pipeline, and returns the formatted dictionary.
119
  """
120
- eff_model, eff_config, eff_idx_to_class = get_efficientnet()
121
- yolo_model = get_yolo()
122
 
123
  orig_pred = None
124
  orig_conf = None
@@ -161,16 +161,18 @@ def classify_qc(img, force_model=None, threshold=0.80, min_no_defect_prob=0.0):
161
 
162
  # Normalize class names from different models/datasets
163
  class_mapping = {
164
- "ok_front": "no_defect",
165
- "ok front": "no_defect",
166
- "defective front": "def_front",
167
- "defective_front": "def_front",
 
 
168
  }
169
  if final_class in class_mapping:
170
  final_class = class_mapping[final_class]
171
 
172
  # Map to EXACT status UI expects
173
- if final_class == "no_defect":
174
  status = "QC Approved"
175
  else:
176
  status = "QC Rejected"
 
62
 
63
  from functools import lru_cache
64
 
65
+ @lru_cache(maxsize=2)
66
+ def get_efficientnet(product_type="fmcg_inspection"):
67
  project_root = Path(__file__).parent.parent
68
+ eff_path = project_root / f"models/poc2/{product_type}/efficientnet_b0/best.pt"
69
  if not eff_path.exists():
70
  return None, None, None
71
  return load_efficientnet(eff_path)
72
 
73
+ @lru_cache(maxsize=2)
74
+ def get_yolo(product_type="fmcg_inspection"):
75
  project_root = Path(__file__).parent.parent
76
+ yolo_path = project_root / f"models/poc2/{product_type}/yolov11_cls/best.pt"
77
  if not yolo_path.exists():
78
  return None
79
  return YOLO(str(yolo_path))
 
112
 
113
  return predicted_class, confidence, original_pred, original_conf
114
 
115
+ def classify_qc(img, product_type="fmcg_inspection", force_model=None, threshold=0.80, min_no_defect_prob=0.0):
116
  """
117
  Main entry point for Streamlit. Takes a PIL Image, runs the dual-inference
118
  pipeline, and returns the formatted dictionary.
119
  """
120
+ eff_model, eff_config, eff_idx_to_class = get_efficientnet(product_type)
121
+ yolo_model = get_yolo(product_type)
122
 
123
  orig_pred = None
124
  orig_conf = None
 
161
 
162
  # Normalize class names from different models/datasets
163
  class_mapping = {
164
+ "ok_front": "non-defective",
165
+ "ok front": "non-defective",
166
+ "defective front": "defective",
167
+ "defective_front": "defective",
168
+ "no_defect": "non-defective",
169
+ "def_front": "defective"
170
  }
171
  if final_class in class_mapping:
172
  final_class = class_mapping[final_class]
173
 
174
  # Map to EXACT status UI expects
175
+ if final_class == "non-defective":
176
  status = "QC Approved"
177
  else:
178
  status = "QC Rejected"
models/poc2/fmcg_inspection/efficientnet_b0/best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d578f0d37118062d374a7286872839b5f6c40ff840d5a0ca0300a0d4e552cd48
3
+ size 16316197
models/poc2/fmcg_inspection/yolov11_cls/best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c443c56e0f4fe12e7fcc92d5b7c3fa69cb72b2988d9e87116281a1e98cc81301
3
+ size 3186370