apailang commited on
Commit
bf4597b
β€’
1 Parent(s): c5d7db5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -65
app.py CHANGED
@@ -40,7 +40,7 @@ def load_model(model_repo_id):
40
  def predict(pilimg):
41
 
42
  image_np = pil_image_as_numpy_array(pilimg)
43
- return predict2(image_np)
44
 
45
  def predict2(image_np):
46
 
@@ -68,12 +68,8 @@ def predict2(image_np):
68
 
69
  return result_pil_img2
70
 
71
- def predict3(pilimg):
72
 
73
- image_np = pil_image_as_numpy_array(pilimg)
74
- return predict4(image_np)
75
-
76
- def predict4(image_np):
77
 
78
  results = detection_model2(image_np)
79
 
@@ -99,50 +95,50 @@ def predict4(image_np):
99
 
100
  return result_pil_img4
101
 
102
- def detect_video(video):
103
- # Create a video capture object
104
- cap = cv2.VideoCapture(video)
105
-
106
- # Process frames in a loop
107
- while cap.isOpened():
108
- ret, frame = cap.read()
109
- if not ret:
110
- break
111
-
112
- # Expand dimensions since model expects images to have shape: [1, None, None, 3]
113
- image_np_expanded = np.expand_dims(frame, axis=0)
114
-
115
- # Run inference
116
- output_dict = model(image_np_expanded)
117
-
118
- # Extract detections
119
- boxes = output_dict['detection_boxes'][0].numpy()
120
- scores = output_dict['detection_scores'][0].numpy()
121
- classes = output_dict['detection_classes'][0].numpy().astype(np.int64)
122
-
123
- # Draw bounding boxes and labels
124
- image_np_with_detections = viz_utils.visualize_boxes_and_labels_on_image_array(
125
- frame,
126
- boxes,
127
- classes,
128
- scores,
129
- category_index,
130
- use_normalized_coordinates=True,
131
- max_boxes_to_draw=20,
132
- min_score_thresh=.5,
133
- agnostic_mode=False)
134
-
135
- # Yield the processed frame
136
- yield image_np_with_detections
137
-
138
- # Release resources
139
- cap.release()
140
 
141
  a = os.path.join(os.path.dirname(__file__), "data/c_base_detected.mp4") # Video
142
  b = os.path.join(os.path.dirname(__file__), "data/c_tuned_detected.mp4") # Video
143
 
144
- def video_demo(video1, video2):
145
- return [video1, video2]
146
 
147
  label_id_offset = 0
148
  REPO_ID = "apailang/mytfodmodel"
@@ -173,24 +169,22 @@ test12 = os.path.join(os.path.dirname(__file__), "data/test12.jpeg")
173
  base_image = gr.Interface(
174
  fn=predict,
175
  inputs=[gr.Image(type="pil"),gr.Slider(minimum=0.01, maximum=0.99, value=0.6 ,label="Threshold(WIP)",info="[not in used]to set prediction confidence threshold")],
176
- outputs=gr.Image(type="pil"),
177
- title="Luffy and Chopper face detection (Base mobile net model)",
178
- description="Upload a Image for prediction or click on below examples. Prediction confident >38%",
179
- examples=[[test1],[test2],[test3],[test4],[test5],[test6],[test7],[test8],[test9],[test10],[test11],[test12],],
180
- cache_examples=True
181
- )#.launch(share=True)
182
-
183
- tuned_image = gr.Interface(
184
- fn=predict3,
185
- inputs=gr.Image(type="pil"),
186
- outputs=gr.Image(type="pil"),
187
- title="Luffy and Chopper face detection (tuned mobile net model)",
188
- description="Upload a Image for prediction or click on below examples. Mobile net tuned with data Augmentation. Prediction confident >38%",
189
  examples=[[test1],[test2],[test3],[test4],[test5],[test6],[test7],[test8],[test9],[test10],[test11],[test12],],
190
  cache_examples=True
191
  )#.launch(share=True)
192
 
193
-
 
 
 
 
 
 
 
 
194
 
195
  # a = os.path.join(os.path.dirname(__file__), "data/a.mp4") # Video
196
  # b = os.path.join(os.path.dirname(__file__), "data/b.mp4") # Video
@@ -211,19 +205,18 @@ tuned_image = gr.Interface(
211
  # )
212
 
213
 
214
-
215
  video = gr.Interface(
216
  fn=lambda x,y: [x,y], #video_demo,
217
  inputs=[gr.Video(label="base model Video"),gr.Video(label="tuned model Video")],
218
- outputs=[gr.Video(label="base model"), gr.Video(label="Tuned model")],
219
  examples=[
220
  [a, b]
221
  ],
222
- title="Comparing base vs tuned detected video",
223
- description="using SSD mobile net V2 320x320. Model has been customed trained to detect Character of Luffy and Chopper"
224
  )
225
 
226
- demo = gr.TabbedInterface([base_image,tuned_image, video], ["Image (Base Model)","Image (Tuned Model)", "Display Detected Video"])
227
 
228
 
229
  if __name__ == "__main__":
 
40
  def predict(pilimg):
41
 
42
  image_np = pil_image_as_numpy_array(pilimg)
43
+ return predict2(image_np),predict3(image_np)
44
 
45
  def predict2(image_np):
46
 
 
68
 
69
  return result_pil_img2
70
 
 
71
 
72
+ def predict3(image_np):
 
 
 
73
 
74
  results = detection_model2(image_np)
75
 
 
95
 
96
  return result_pil_img4
97
 
98
+ # def detect_video(video):
99
+ # # Create a video capture object
100
+ # cap = cv2.VideoCapture(video)
101
+
102
+ # # Process frames in a loop
103
+ # while cap.isOpened():
104
+ # ret, frame = cap.read()
105
+ # if not ret:
106
+ # break
107
+
108
+ # # Expand dimensions since model expects images to have shape: [1, None, None, 3]
109
+ # image_np_expanded = np.expand_dims(frame, axis=0)
110
+
111
+ # # Run inference
112
+ # output_dict = model(image_np_expanded)
113
+
114
+ # # Extract detections
115
+ # boxes = output_dict['detection_boxes'][0].numpy()
116
+ # scores = output_dict['detection_scores'][0].numpy()
117
+ # classes = output_dict['detection_classes'][0].numpy().astype(np.int64)
118
+
119
+ # # Draw bounding boxes and labels
120
+ # image_np_with_detections = viz_utils.visualize_boxes_and_labels_on_image_array(
121
+ # frame,
122
+ # boxes,
123
+ # classes,
124
+ # scores,
125
+ # category_index,
126
+ # use_normalized_coordinates=True,
127
+ # max_boxes_to_draw=20,
128
+ # min_score_thresh=.5,
129
+ # agnostic_mode=False)
130
+
131
+ # # Yield the processed frame
132
+ # yield image_np_with_detections
133
+
134
+ # # Release resources
135
+ # cap.release()
136
 
137
  a = os.path.join(os.path.dirname(__file__), "data/c_base_detected.mp4") # Video
138
  b = os.path.join(os.path.dirname(__file__), "data/c_tuned_detected.mp4") # Video
139
 
140
+ # def video_demo(video1, video2):
141
+ # return [video1, video2]
142
 
143
  label_id_offset = 0
144
  REPO_ID = "apailang/mytfodmodel"
 
169
  base_image = gr.Interface(
170
  fn=predict,
171
  inputs=[gr.Image(type="pil"),gr.Slider(minimum=0.01, maximum=0.99, value=0.6 ,label="Threshold(WIP)",info="[not in used]to set prediction confidence threshold")],
172
+ outputs=[gr.Image(type="pil",label="Base Model"),gr.Image(type="pil",label="Tuned Model")],
173
+ title="Luffy and Chopper Head detection. SSD mobile net V2 320x320",
174
+ description="Upload a Image for prediction or click on below examples. Prediction confident >38% will be shown in dectected images. Threshold slider is WIP",
 
 
 
 
 
 
 
 
 
 
175
  examples=[[test1],[test2],[test3],[test4],[test5],[test6],[test7],[test8],[test9],[test10],[test11],[test12],],
176
  cache_examples=True
177
  )#.launch(share=True)
178
 
179
+ # tuned_image = gr.Interface(
180
+ # fn=predict3,
181
+ # inputs=gr.Image(type="pil"),
182
+ # outputs=gr.Image(type="pil"),
183
+ # title="Luffy and Chopper face detection on images. Result comparison of base vs tuned SSD mobile net V2 320x320",
184
+ # description="Upload a Image for prediction or click on below examples. Mobile net tuned with data Augmentation. Prediction confident >38%",
185
+ # examples=[[test1],[test2],[test3],[test4],[test5],[test6],[test7],[test8],[test9],[test10],[test11],[test12],],
186
+ # cache_examples=True
187
+ # )#.launch(share=True)
188
 
189
  # a = os.path.join(os.path.dirname(__file__), "data/a.mp4") # Video
190
  # b = os.path.join(os.path.dirname(__file__), "data/b.mp4") # Video
 
205
  # )
206
 
207
 
 
208
  video = gr.Interface(
209
  fn=lambda x,y: [x,y], #video_demo,
210
  inputs=[gr.Video(label="base model Video"),gr.Video(label="tuned model Video")],
211
+ outputs=[gr.Video(label="Base model inferenced video"), gr.Video(label="Tuned model inferenced video")],
212
  examples=[
213
  [a, b]
214
  ],
215
+ title="Luffy and Chopper face detection on video Result comparison of base vs tuned SSD mobile net V2 320x320",
216
+ description="Model has been customed trained to detect Character of Luffy and Chopper with Prediction confident >10%. Videos are pre-inferenced to reduce load time. (Browser zoom out to view right columne - top (base model inference) & bottom(tuned model inference)) "
217
  )
218
 
219
+ demo = gr.TabbedInterface([base_image, video], ["Images", "Video"])
220
 
221
 
222
  if __name__ == "__main__":