Alex Chan commited on
Commit
d3fbb91
1 Parent(s): 999c5c9

cleaned up scripts

Browse files
Files changed (2) hide show
  1. Code/Inference.py +2 -80
  2. app.py +1 -1
Code/Inference.py CHANGED
@@ -120,7 +120,6 @@ def DLCInference(Crop,dlc_liveObj,CropSize):
120
 
121
  def VisualizeAll(frame, box, DLCPredict2D,MeanConfidence,ScaleBBox, imsize):
122
  """Visualize all stuff"""
123
- # colourList = [(255,255,0),(255,0 ,255),(128,0,128),(203,192,255),(0, 255, 255),(255, 0 , 0 ),(63,133,205),(0,255,0),(0,0,255)]
124
  colourList = [(0,255,255),(255,0 ,255),(128,0,128),(255,192,203),(255, 255, 0),(0, 0 , 255 ),(205,133,63),(0,255,0),(255,0,0)]
125
 
126
  ##Order: Lshoulder, Rshoulder, topKeel,botKeel,Tail,Beak,Nose,Leye,Reye
@@ -131,23 +130,8 @@ def VisualizeAll(frame, box, DLCPredict2D,MeanConfidence,ScaleBBox, imsize):
131
  cv2.circle(frame,roundPoint,1,colourList[x], 5)
132
  PlotPoints.append(roundPoint)
133
 
134
-
135
- ##change box to XYWH to scale down
136
- # bbox = [box[0],box[1],box[2]-box[0],box[3]-box[1]]
137
- # ScaleWidth = (bbox[2]/ScaleBBox)/2
138
- # ScaleHeight = (bbox[3]/ScaleBBox)/2
139
- # ###based on ScaleBBox, scale back down bounding box for plotting
140
- # x1 = round(bbox[0]+ScaleWidth) if round(bbox[0]+ScaleWidth)>0 else 0
141
- # y1 = round(bbox[1]+ScaleHeight)if round(bbox[1]+ScaleHeight)>0 else 0
142
- # x2 = round(bbox[0]+bbox[2]-ScaleWidth) if round(bbox[0]+bbox[2]-ScaleWidth) < imsize[0] else imsize[0]
143
- # y2 = round(bbox[1]+bbox[3]-ScaleHeight)if round(bbox[1]+bbox[3]-ScaleHeight) < imsize[1] else imsize[1]
144
- # box = [x1,y1,x2,y2]
145
  cv2.rectangle(frame,(round(box[0]),round(box[1])),(round(box[2]),round(box[3])),[0,0,255],3)
146
 
147
- #plot mean confidence
148
- # import ipdb;ipdb.set_trace()
149
- # font = cv2.FONT_HERSHEY_SIMPLEX
150
- # cv2.putText(frame,str(round(MeanConfidence,3)),(round(box[0]),round(box[1])),font,2,[255,0,0],2)
151
 
152
  return frame, PlotPoints
153
 
@@ -201,75 +185,13 @@ def Inference(frame,predictor,dlc_liveObj,ScaleBBox=1,Dilate=5,DLCThreshold=0.3)
201
  BirdCrop = Crop[y1:y2,x1:x2] #bbox is XYWH
202
 
203
  DLCPredict2D= DLCInference(BirdCrop,dlc_liveObj,CropSize)
204
- MeanConfidence = DLCPredict2D[:,2].mean()
205
 
206
- if MeanConfidence > DLCThreshold: #if mean keypoint confidence is higher than this threshold, consider bird
207
- bbox.append(MeanConfidence)
208
- frame, PlotPoints = VisualizeAll(frame, bbox, DLCPredict2D,MeanConfidence,ScaleBBox,imsize)
209
 
210
  if BirdBBox.shape[0] == 0:
211
  DLCPredict2D= DLCInference(InferFrame,dlc_liveObj,CropSize)
212
- MeanConfidence = DLCPredict2D[:,2].mean()
213
  bbox = [0,0,0,0]
214
- if MeanConfidence > DLCThreshold: #if mean keypoint confidence is higher than this threshold, consider bird
215
- frame, PlotPoints = VisualizeAll(frame, bbox, DLCPredict2D,MeanConfidence,ScaleBBox,imsize)
216
 
217
  return frame
218
 
219
-
220
-
221
-
222
- if __name__ == "__main__":
223
- # VidPath = "/media/alexchan/Extreme SSD/WorkDir/Pigeon3DTrack/ManualClickTrials/Videos/Cam1_C0008.MP4_Trimmed.mp4"
224
- VidPath = "/media/alexchan/Extreme SSD/SampleDatasets/PigeonsEverywhere/2022-12-01_Pilot_bottom_control.mp4"
225
-
226
- OutDir = "/media/alexchan/Extreme SSD/WorkDir/Pigeon3DTrack/OutdoorTracking/Final2D"
227
-
228
- ExportModelPath = "/media/alexchan/Extreme SSD/WorkDir/Pigeon3DTrack/Weights/DLC_Weights/N6000_DLC_Mask/exported-models/DLC_DLC_Segmented_resnet_50_iteration-0_shuffle-1"
229
- # ExportModelPath = "/media/alexchan/Extreme SSD/WorkDir/Pigeon3DTrack/Weights/DLC_Weights/DLC_PigeonSuperModel_imgaug_efficientnet-b0_iteration-0_shuffle-2"
230
-
231
- if not os.path.isdir(ExportModelPath):
232
- #need to export model first
233
- # DLC_Config = "/media/alexchan/Extreme SSD/SampleDatasets/ImageTrainingData/N5000/DLC_Seg_Aug/config.yaml"
234
-
235
- dlc.export_model(DLC_Config)
236
- else:
237
- print("model already exported!")
238
-
239
- CropSize = (320,320)
240
- # WeightPath = "/home/alexchan/Documents/Pigeon3DTrack/Data/YOLO_Weights/yolov8m.pt"
241
- # VidPath= "/media/alexchan/Extreme SSD/PigeonOutdoors/28032023/Cam1_C0004.MP4"
242
- # import ipdb;ipdb.set_trace()
243
- # YOLOModel = YOLO(YOLOPath)
244
- device = "cuda"
245
- # device = "cpu"
246
-
247
- ###Detectron:
248
- cfg = get_cfg()
249
- # add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
250
- cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml"))
251
- cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7 # set threshold for this model
252
- # Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well
253
- cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml")
254
- predictor = DefaultPredictor(cfg)
255
-
256
-
257
- ##DLC:
258
- dlc_proc = Processor()
259
- dlc_liveObj = DLCLive(ExportModelPath, processor=dlc_proc)
260
-
261
- RunInference(predictor,dlc_liveObj, device,OutDir,
262
- VidPath,CropSize,startFrame=0,
263
- TotalFrames =900, ScaleBBox=1.3,
264
- DLCThreshold = 0, Dilate=5)
265
-
266
-
267
-
268
- ###DLC tests
269
- # DLC_Config = "/media/alexchan/Extreme SSD/SampleDatasets/ImageTrainingData/N5000/DLC/config.yaml"
270
- # Video = "/media/alexchan/Extreme SSD/SampleDatasets/ImageTrainingData/N5000/DLC/videos/Video1.mp4"
271
- # dlc.analyze_videos(DLC_Config,[Video])
272
- # dlc.export_model(DLC_Config)
273
-
274
- # import pandas as pd
275
- # pd.read_hdf("/media/alexchan/Extreme SSD/SampleDatasets/ImageTrainingData/N5000/DLC/videos/Video1DLC_resnet50_DLC20230401-173058shuffle1_3000.h5")
 
120
 
121
  def VisualizeAll(frame, box, DLCPredict2D,MeanConfidence,ScaleBBox, imsize):
122
  """Visualize all stuff"""
 
123
  colourList = [(0,255,255),(255,0 ,255),(128,0,128),(255,192,203),(255, 255, 0),(0, 0 , 255 ),(205,133,63),(0,255,0),(255,0,0)]
124
 
125
  ##Order: Lshoulder, Rshoulder, topKeel,botKeel,Tail,Beak,Nose,Leye,Reye
 
130
  cv2.circle(frame,roundPoint,1,colourList[x], 5)
131
  PlotPoints.append(roundPoint)
132
 
 
 
 
 
 
 
 
 
 
 
 
133
  cv2.rectangle(frame,(round(box[0]),round(box[1])),(round(box[2]),round(box[3])),[0,0,255],3)
134
 
 
 
 
 
135
 
136
  return frame, PlotPoints
137
 
 
185
  BirdCrop = Crop[y1:y2,x1:x2] #bbox is XYWH
186
 
187
  DLCPredict2D= DLCInference(BirdCrop,dlc_liveObj,CropSize)
 
188
 
189
+ frame, PlotPoints = VisualizeAll(frame, bbox, DLCPredict2D,MeanConfidence,ScaleBBox,imsize)
 
 
190
 
191
  if BirdBBox.shape[0] == 0:
192
  DLCPredict2D= DLCInference(InferFrame,dlc_liveObj,CropSize)
 
193
  bbox = [0,0,0,0]
194
+ frame, PlotPoints = VisualizeAll(frame, bbox, DLCPredict2D,MeanConfidence,ScaleBBox,imsize)
 
195
 
196
  return frame
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import sys
2
  import subprocess
3
- # subprocess.check_call([sys.executable, '-m', 'pip', 'install','git+https://github.com/facebookresearch/detectron2.git'])
4
 
5
  import numpy as np
6
  import gradio as gr
 
1
  import sys
2
  import subprocess
3
+ subprocess.check_call([sys.executable, '-m', 'pip', 'install','git+https://github.com/facebookresearch/detectron2.git'])
4
 
5
  import numpy as np
6
  import gradio as gr