brxerq commited on
Commit
7327cc1
1 Parent(s): 4aad7a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -71
app.py CHANGED
@@ -7,8 +7,20 @@ from PIL import Image
7
 
8
  # Load the TensorFlow Lite model
9
  MODEL_DIR = 'model'
10
- GRAPH_NAME = 'detect.tflite'
11
- LABELMAP_NAME = 'labelmap.txt'
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  pkg = importlib.util.find_spec('tflite_runtime')
14
  if pkg:
@@ -18,80 +30,87 @@ else:
18
  from tensorflow.lite.python.interpreter import Interpreter
19
  from tensorflow.lite.python.interpreter import load_delegate
20
 
21
- PATH_TO_CKPT = os.path.join(MODEL_DIR, GRAPH_NAME)
22
- PATH_TO_LABELS = os.path.join(MODEL_DIR, LABELMAP_NAME)
23
-
24
  # Load the label map
25
- with open(PATH_TO_LABELS, 'r') as f:
26
- labels = [line.strip() for line in f.readlines()]
27
-
28
- if labels[0] == '???':
29
- del(labels[0])
30
-
31
- # Load the TensorFlow Lite model
32
- interpreter = Interpreter(model_path=PATH_TO_CKPT)
33
- interpreter.allocate_tensors()
34
-
35
- input_details = interpreter.get_input_details()
36
- output_details = interpreter.get_output_details()
37
- height = input_details[0]['shape'][1]
38
- width = input_details[0]['shape'][2]
39
- floating_model = (input_details[0]['dtype'] == np.float32)
40
-
41
- input_mean = 127.5
42
- input_std = 127.5
43
-
44
- outname = output_details[0]['name']
45
- if ('StatefulPartitionedCall' in outname):
46
- boxes_idx, classes_idx, scores_idx = 1, 3, 0
47
- else:
48
- boxes_idx, classes_idx, scores_idx = 0, 1, 2
49
-
50
- def perform_detection(image, interpreter, labels):
51
- imH, imW, _ = image.shape
52
- image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
53
- image_resized = cv2.resize(image_rgb, (width, height))
54
- input_data = np.expand_dims(image_resized, axis=0)
55
-
56
- if floating_model:
57
- input_data = (np.float32(input_data) - input_mean) / input_std
58
-
59
- interpreter.set_tensor(input_details[0]['index'], input_data)
60
- interpreter.invoke()
61
-
62
- boxes = interpreter.get_tensor(output_details[boxes_idx]['index'])[0]
63
- classes = interpreter.get_tensor(output_details[classes_idx]['index'])[0]
64
- scores = interpreter.get_tensor(output_details[scores_idx]['index'])[0]
65
-
66
- detections = []
67
- for i in range(len(scores)):
68
- if ((scores[i] > 0.5) and (scores[i] <= 1.0)):
69
- ymin = int(max(1, (boxes[i][0] * imH)))
70
- xmin = int(max(1, (boxes[i][1] * imW)))
71
- ymax = int(min(imH, (boxes[i][2] * imH)))
72
- xmax = int(min(imW, (boxes[i][3] * imW)))
73
-
74
- cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (10, 255, 0), 2)
75
- object_name = labels[int(classes[i])]
76
- label = '%s: %d%%' % (object_name, int(scores[i] * 100))
77
- labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2)
78
- label_ymin = max(ymin, labelSize[1] + 10)
79
- cv2.rectangle(image, (xmin, label_ymin - labelSize[1] - 10), (xmin + labelSize[0], label_ymin + baseLine - 10), (255, 255, 255), cv2.FILLED)
80
- cv2.putText(image, label, (xmin, label_ymin - 7), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
81
-
82
- detections.append([object_name, scores[i], xmin, ymin, xmax, ymax])
83
- return image
 
 
 
 
 
 
 
 
 
 
84
 
85
  def resize_image(image, size=640):
86
  return cv2.resize(image, (size, size))
87
 
88
- def detect_image(input_image):
89
  image = np.array(input_image)
90
  resized_image = resize_image(image, size=640) # Resize input image
91
- result_image = perform_detection(resized_image, interpreter, labels)
92
  return Image.fromarray(result_image)
93
 
94
- def detect_video(input_video):
95
  cap = cv2.VideoCapture(input_video)
96
  frames = []
97
 
@@ -101,7 +120,7 @@ def detect_video(input_video):
101
  break
102
 
103
  resized_frame = resize_image(frame, size=640) # Resize each frame
104
- result_frame = perform_detection(resized_frame, interpreter, labels)
105
  frames.append(result_frame)
106
 
107
  cap.release()
@@ -124,17 +143,19 @@ def detect_video(input_video):
124
  app = gr.Blocks()
125
 
126
  with app:
 
 
 
127
  with gr.Tab("Image Detection"):
128
  gr.Markdown("Upload an image for object detection")
129
  image_input = gr.Image(type="pil", label="Upload an image")
130
  image_output = gr.Image(type="pil", label="Detection Result")
131
- gr.Button("Submit").click(fn=detect_image, inputs=image_input, outputs=image_output)
132
 
133
  with gr.Tab("Video Detection"):
134
  gr.Markdown("Upload a video for object detection")
135
  video_input = gr.Video(label="Upload a video")
136
  video_output = gr.Video(label="Detection Result")
137
- gr.Button("Submit").click(fn=detect_video, inputs=video_input, outputs=video_output)
138
 
139
  app.launch()
140
-
 
7
 
8
  # Load the TensorFlow Lite model
9
  MODEL_DIR = 'model'
10
+ MODEL_DIRS = {
11
+ 'Multi-class model': 'model',
12
+ 'Empty class': 'model_2',
13
+ 'Misalignment class': 'model_3'
14
+ }
15
+
16
+ # Function to load model based on selection
17
+ def load_model(model_name):
18
+ selected_model_dir = MODEL_DIRS.get(model_name, MODEL_DIR)
19
+ graph_name = 'detect.tflite' if model_name == 'Multi-class model' else f'detect_{model_name.lower().replace(" ", "_")}.tflite'
20
+ labelmap_name = 'labelmap.txt' if model_name == 'Multi-class model' else f'labelmap_{model_name.lower().replace(" ", "_")}.txt'
21
+ path_to_ckpt = os.path.join(selected_model_dir, graph_name)
22
+ path_to_labels = os.path.join(selected_model_dir, labelmap_name)
23
+ return path_to_ckpt, path_to_labels
24
 
25
  pkg = importlib.util.find_spec('tflite_runtime')
26
  if pkg:
 
30
  from tensorflow.lite.python.interpreter import Interpreter
31
  from tensorflow.lite.python.interpreter import load_delegate
32
 
 
 
 
33
  # Load the label map
34
+ def load_labels(path_to_labels):
35
+ with open(path_to_labels, 'r') as f:
36
+ labels = [line.strip() for line in f.readlines()]
37
+
38
+ if labels[0] == '???':
39
+ del(labels[0])
40
+
41
+ return labels
42
+
43
+ def load_interpreter(model_path):
44
+ interpreter = Interpreter(model_path=model_path)
45
+ interpreter.allocate_tensors()
46
+ return interpreter
47
+
48
+ class ModelDetector:
49
+ def __init__(self, model_name):
50
+ self.model_path, self.label_path = load_model(model_name)
51
+ self.labels = load_labels(self.label_path)
52
+ self.interpreter = load_interpreter(self.model_path)
53
+
54
+ input_details = self.interpreter.get_input_details()
55
+ output_details = self.interpreter.get_output_details()
56
+ self.height = input_details[0]['shape'][1]
57
+ self.width = input_details[0]['shape'][2]
58
+ self.floating_model = (input_details[0]['dtype'] == np.float32)
59
+
60
+ self.input_mean = 127.5
61
+ self.input_std = 127.5
62
+
63
+ outname = output_details[0]['name']
64
+ if ('StatefulPartitionedCall' in outname):
65
+ self.boxes_idx, self.classes_idx, self.scores_idx = 1, 3, 0
66
+ else:
67
+ self.boxes_idx, self.classes_idx, self.scores_idx = 0, 1, 2
68
+
69
+ def perform_detection(self, image):
70
+ imH, imW, _ = image.shape
71
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
72
+ image_resized = cv2.resize(image_rgb, (self.width, self.height))
73
+ input_data = np.expand_dims(image_resized, axis=0)
74
+
75
+ if self.floating_model:
76
+ input_data = (np.float32(input_data) - self.input_mean) / self.input_std
77
+
78
+ self.interpreter.set_tensor(self.interpreter.get_input_details()[0]['index'], input_data)
79
+ self.interpreter.invoke()
80
+
81
+ boxes = self.interpreter.get_tensor(self.interpreter.get_output_details()[self.boxes_idx]['index'])[0]
82
+ classes = self.interpreter.get_tensor(self.interpreter.get_output_details()[self.classes_idx]['index'])[0]
83
+ scores = self.interpreter.get_tensor(self.interpreter.get_output_details()[self.scores_idx]['index'])[0]
84
+
85
+ detections = []
86
+ for i in range(len(scores)):
87
+ if ((scores[i] > 0.5) and (scores[i] <= 1.0)):
88
+ ymin = int(max(1, (boxes[i][0] * imH)))
89
+ xmin = int(max(1, (boxes[i][1] * imW)))
90
+ ymax = int(min(imH, (boxes[i][2] * imH)))
91
+ xmax = int(min(imW, (boxes[i][3] * imW)))
92
+
93
+ cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (10, 255, 0), 2)
94
+ object_name = self.labels[int(classes[i])]
95
+ label = '%s: %d%%' % (object_name, int(scores[i] * 100))
96
+ labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2)
97
+ label_ymin = max(ymin, labelSize[1] + 10)
98
+ cv2.rectangle(image, (xmin, label_ymin - labelSize[1] - 10), (xmin + labelSize[0], label_ymin + baseLine - 10), (255, 255, 255), cv2.FILLED)
99
+ cv2.putText(image, label, (xmin, label_ymin - 7), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
100
+
101
+ detections.append([object_name, scores[i], xmin, ymin, xmax, ymax])
102
+ return image
103
 
104
  def resize_image(image, size=640):
105
  return cv2.resize(image, (size, size))
106
 
107
+ def detect_image(input_image, model_detector):
108
  image = np.array(input_image)
109
  resized_image = resize_image(image, size=640) # Resize input image
110
+ result_image = model_detector.perform_detection(resized_image)
111
  return Image.fromarray(result_image)
112
 
113
+ def detect_video(input_video, model_detector):
114
  cap = cv2.VideoCapture(input_video)
115
  frames = []
116
 
 
120
  break
121
 
122
  resized_frame = resize_image(frame, size=640) # Resize each frame
123
+ result_frame = model_detector.perform_detection(resized_frame)
124
  frames.append(result_frame)
125
 
126
  cap.release()
 
143
  app = gr.Blocks()
144
 
145
  with app:
146
+ gr.Label("Select Model:")
147
+ model_selector = gr.Dropdown(choices=list(MODEL_DIRS.keys()), label="Multi-class model")
148
+
149
  with gr.Tab("Image Detection"):
150
  gr.Markdown("Upload an image for object detection")
151
  image_input = gr.Image(type="pil", label="Upload an image")
152
  image_output = gr.Image(type="pil", label="Detection Result")
153
+ gr.Button("Submit").click(fn=detect_image, inputs=[image_input, model_selector], outputs=image_output)
154
 
155
  with gr.Tab("Video Detection"):
156
  gr.Markdown("Upload a video for object detection")
157
  video_input = gr.Video(label="Upload a video")
158
  video_output = gr.Video(label="Detection Result")
159
+ gr.Button("Submit").click(fn=detect_video, inputs=[video_input, model_selector], outputs=video_output)
160
 
161
  app.launch()