brxerq commited on
Commit
0a231a1
1 Parent(s): c695cc2

Update model_1.py

Browse files
Files changed (1) hide show
  1. model_1.py +7 -34
model_1.py CHANGED
@@ -1,11 +1,10 @@
 
1
  import os
2
  import cv2
3
  import numpy as np
4
  import importlib.util
5
- import gradio as gr
6
  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'
@@ -13,22 +12,18 @@ LABELMAP_NAME = 'labelmap.txt'
13
  pkg = importlib.util.find_spec('tflite_runtime')
14
  if pkg:
15
  from tflite_runtime.interpreter import Interpreter
16
- from tflite_runtime.interpreter import load_delegate
17
  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
 
@@ -42,12 +37,12 @@ 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))
@@ -65,12 +60,12 @@ def perform_detection(image, interpreter, labels):
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))
@@ -82,13 +77,9 @@ def perform_detection(image, interpreter, labels):
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):
@@ -100,8 +91,7 @@ def detect_video(input_video):
100
  if not ret:
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()
@@ -120,20 +110,3 @@ def detect_video(input_video):
120
  out.release()
121
 
122
  return output_video_path
123
-
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()
 
1
+ # model_1.py
2
  import os
3
  import cv2
4
  import numpy as np
5
  import importlib.util
 
6
  from PIL import Image
7
 
 
8
  MODEL_DIR = 'model'
9
  GRAPH_NAME = 'detect.tflite'
10
  LABELMAP_NAME = 'labelmap.txt'
 
12
  pkg = importlib.util.find_spec('tflite_runtime')
13
  if pkg:
14
  from tflite_runtime.interpreter import Interpreter
 
15
  else:
16
  from tensorflow.lite.python.interpreter import Interpreter
 
17
 
18
  PATH_TO_CKPT = os.path.join(MODEL_DIR, GRAPH_NAME)
19
  PATH_TO_LABELS = os.path.join(MODEL_DIR, LABELMAP_NAME)
20
 
 
21
  with open(PATH_TO_LABELS, 'r') as f:
22
  labels = [line.strip() for line in f.readlines()]
23
 
24
  if labels[0] == '???':
25
  del(labels[0])
26
 
 
27
  interpreter = Interpreter(model_path=PATH_TO_CKPT)
28
  interpreter.allocate_tensors()
29
 
 
37
  input_std = 127.5
38
 
39
  outname = output_details[0]['name']
40
+ if 'StatefulPartitionedCall' in outname:
41
  boxes_idx, classes_idx, scores_idx = 1, 3, 0
42
  else:
43
  boxes_idx, classes_idx, scores_idx = 0, 1, 2
44
 
45
+ def perform_detection(image):
46
  imH, imW, _ = image.shape
47
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
48
  image_resized = cv2.resize(image_rgb, (width, height))
 
60
 
61
  detections = []
62
  for i in range(len(scores)):
63
+ if scores[i] > 0.5:
64
  ymin = int(max(1, (boxes[i][0] * imH)))
65
  xmin = int(max(1, (boxes[i][1] * imW)))
66
  ymax = int(min(imH, (boxes[i][2] * imH)))
67
  xmax = int(min(imW, (boxes[i][3] * imW)))
68
+
69
  cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (10, 255, 0), 2)
70
  object_name = labels[int(classes[i])]
71
  label = '%s: %d%%' % (object_name, int(scores[i] * 100))
 
77
  detections.append([object_name, scores[i], xmin, ymin, xmax, ymax])
78
  return image
79
 
 
 
 
80
  def detect_image(input_image):
81
  image = np.array(input_image)
82
+ result_image = perform_detection(image)
 
83
  return Image.fromarray(result_image)
84
 
85
  def detect_video(input_video):
 
91
  if not ret:
92
  break
93
 
94
+ result_frame = perform_detection(frame)
 
95
  frames.append(result_frame)
96
 
97
  cap.release()
 
110
  out.release()
111
 
112
  return output_video_path