asamasach commited on
Commit
403d6e9
·
1 Parent(s): 0c1e41c

show case

Browse files
Files changed (3) hide show
  1. LICENSE +4 -0
  2. app.py +114 -0
  3. requirements.txt +5 -0
LICENSE ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ Copyright (c) 2025 SMARTFALCON-AI.com
2
+ All Rights Reserved.
3
+
4
+ This model is proprietary. Any usage, distribution, or reproduction requires explicit permission from the copyright holder.
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import onnxruntime as ort
3
+ import numpy as np
4
+ import cv2
5
+ from huggingface_hub import hf_hub_download
6
+
7
+ # Private models from your account
8
+ MODELS = {
9
+ "Dental Implant": "smartfalcon-ai/Dental-Implant-Defect-Detection",
10
+ "Data Matrix": "smartfalcon-ai/Data-Matrix-Defect-Detection",
11
+ "Ball Pen": "smartfalcon-ai/Ball-Pen-Defect-Detection",
12
+ "Knit Up": "smartfalcon-ai/Knit-Up-Defect-Detection",
13
+ "Knit Back": "smartfalcon-ai/Knit-Back-Defect-Detection",
14
+ "Jean Back": "smartfalcon-ai/Jean-Back-Defect-Detection",
15
+ "Jean Up": "smartfalcon-ai/Jean-Up-Defect-Detection",
16
+ "Tire Cord": "smartfalcon-ai/Tire-Cord-Defect-Detection"
17
+ }
18
+
19
+ # Keep loaded sessions in memory
20
+ sessions = {}
21
+
22
+ def get_session(model_name):
23
+ if model_name not in sessions:
24
+ model_path = hf_hub_download(repo_id=MODELS[model_name], filename="best.onnx", use_auth_token=True)
25
+ sessions[model_name] = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
26
+ return sessions[model_name]
27
+
28
+ IMG_SIZE = 640
29
+ IOU_THRESHOLD = 0.45 # Fixed NMS
30
+
31
+ # Preprocessing
32
+ def preprocess(img):
33
+ h, w = img.shape[:2]
34
+ img_resized = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
35
+ img_resized = img_resized.astype(np.float32) / 255.0
36
+ img_resized = img_resized.transpose(2, 0, 1) # CHW
37
+ img_resized = np.expand_dims(img_resized, 0)
38
+ return img_resized, w, h
39
+
40
+ # Convert [cx,cy,w,h] -> [x1,y1,x2,y2]
41
+ def xywh2xyxy(x):
42
+ y = np.copy(x)
43
+ y[:,0] = x[:,0] - x[:,2]/2
44
+ y[:,1] = x[:,1] - x[:,3]/2
45
+ y[:,2] = x[:,0] + x[:,2]/2
46
+ y[:,3] = x[:,1] + x[:,3]/2
47
+ return y
48
+
49
+ # Non-max suppression
50
+ def non_max_suppression(preds, conf_thres=0.25, iou_thres=0.45):
51
+ preds = preds[0]
52
+ preds = preds[preds[:,4] > conf_thres]
53
+ if preds.shape[0]==0:
54
+ return []
55
+
56
+ boxes = xywh2xyxy(preds[:, :4])
57
+ scores = preds[:,4]
58
+ class_scores = preds[:,5:]
59
+ cls_ids = np.argmax(class_scores, axis=1)
60
+ cls_conf = class_scores.max(axis=1)
61
+ final_scores = scores * cls_conf
62
+
63
+ indices = cv2.dnn.NMSBoxes(
64
+ bboxes=boxes.tolist(),
65
+ scores=final_scores.tolist(),
66
+ score_threshold=conf_thres,
67
+ nms_threshold=iou_thres
68
+ )
69
+ if len(indices)==0:
70
+ return []
71
+
72
+ indices = indices.flatten()
73
+ output=[]
74
+ for idx in indices:
75
+ x1, y1, x2, y2 = boxes[idx]
76
+ output.append([x1, y1, x2, y2, final_scores[idx], cls_ids[idx]])
77
+ return output
78
+
79
+ # Inference
80
+ def infer(image, model_name, conf_threshold):
81
+ session = get_session(model_name)
82
+ img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
83
+ blob, orig_w, orig_h = preprocess(img_bgr)
84
+ preds = session.run(None, {"images": blob})[0]
85
+ detections = non_max_suppression(preds, conf_threshold, IOU_THRESHOLD)
86
+
87
+ for det in detections:
88
+ x1, y1, x2, y2, score, cls_id = det
89
+ x1 = int(x1 / IMG_SIZE * orig_w)
90
+ y1 = int(y1 / IMG_SIZE * orig_h)
91
+ x2 = int(x2 / IMG_SIZE * orig_w)
92
+ y2 = int(y2 / IMG_SIZE * orig_h)
93
+
94
+ label = f"{int(cls_id)}:{score:.2f}"
95
+ cv2.rectangle(img_bgr, (x1, y1), (x2, y2), (0,255,0), 2)
96
+ cv2.putText(img_bgr, label, (x1, y1-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 2)
97
+
98
+ return cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
99
+
100
+ # Gradio UI
101
+ demo = gr.Interface(
102
+ fn=infer,
103
+ inputs=[
104
+ gr.Image(type="numpy"),
105
+ gr.Dropdown(list(MODELS.keys()), label="Select Model"),
106
+ gr.Slider(minimum=0.0, maximum=1.0, value=0.25, step=0.01, label="Confidence Threshold")
107
+ ],
108
+ outputs=gr.Image(type="numpy"),
109
+ title="Industrial Defect Detection",
110
+ description="Upload an image, select the defect model, and adjust the confidence threshold."
111
+ )
112
+
113
+ if __name__ == "__main__":
114
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ onnxruntime
3
+ numpy
4
+ opencv-python
5
+ huggingface_hub