def detection(model, config): # Tracker if config.USE_TRACKER: import sys sys.path.append(os.getcwd() + '/stuff/kcf') import KCF tracker = KCF.kcftracker(False, True, False, False) tracker_counter = 0 track = False print("> Building Graph") # tf Session Config tf_config = model.tf_config detection_graph = model.detection_graph category_index = model.category_index with detection_graph.as_default(): with tf.Session(graph=detection_graph, config=tf_config) as sess: # start Videostream vs = WebcamVideoStream(config.VIDEO_INPUT, config.WIDTH, config.HEIGHT).start() # Define Input and Ouput tensors tensor_dict = model.get_tensordict([ 'num_detections', 'detection_boxes', 'detection_scores', 'detection_classes', 'detection_masks' ]) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Mask Transformations if 'detection_masks' in tensor_dict: # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size. detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0]) detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0]) real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32) detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1]) detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1]) detection_masks_reframed = reframe_box_masks_to_image_masks( detection_masks, detection_boxes, vs.real_height, vs.real_width) detection_masks_reframed = tf.cast( tf.greater(detection_masks_reframed, 0.5), tf.uint8) # Follow the convention by adding back the batch dimension tensor_dict['detection_masks'] = tf.expand_dims( detection_masks_reframed, 0) if config.SPLIT_MODEL: score_out = detection_graph.get_tensor_by_name( 'Postprocessor/convert_scores:0') expand_out = detection_graph.get_tensor_by_name( 'Postprocessor/ExpandDims_1:0') score_in = detection_graph.get_tensor_by_name( 'Postprocessor/convert_scores_1:0') expand_in = detection_graph.get_tensor_by_name( 'Postprocessor/ExpandDims_1_1:0') # Threading score = model.score expand = model.expand gpu_worker = SessionWorker("GPU", detection_graph, tf_config) cpu_worker = SessionWorker("CPU", detection_graph, tf_config) gpu_opts = [score_out, expand_out] cpu_opts = [ tensor_dict['detection_boxes'], tensor_dict['detection_scores'], tensor_dict['detection_classes'], tensor_dict['num_detections'] ] fps = FPS(config.FPS_INTERVAL).start() masks = None print('> Starting Detection') while vs.isActive(): # Detection if not (config.USE_TRACKER and track): if config.SPLIT_MODEL: # split model in seperate gpu and cpu session threads if gpu_worker.is_sess_empty(): # read video frame, expand dimensions and convert to rgb frame = vs.read() # put new queue gpu_feeds = {image_tensor: vs.expanded()} if config.VISUALIZE: gpu_extras = frame # for visualization frame else: gpu_extras = None gpu_worker.put_sess_queue(gpu_opts, gpu_feeds, gpu_extras) g = gpu_worker.get_result_queue() if g is None: # gpu thread has no output queue. ok skip, let's check cpu thread. pass else: # gpu thread has output queue. score, expand, frame = g["results"][0], g[ "results"][1], g["extras"] if cpu_worker.is_sess_empty(): # When cpu thread has no next queue, put new queue. # else, drop gpu queue. cpu_feeds = { score_in: score, expand_in: expand } cpu_extras = frame cpu_worker.put_sess_queue( cpu_opts, cpu_feeds, cpu_extras) c = cpu_worker.get_result_queue() if c is None: # cpu thread has no output queue. ok, nothing to do. continue continue # If CPU RESULT has not been set yet, no fps update else: boxes, scores, classes, num, frame = c["results"][ 0], c["results"][1], c["results"][2], c[ "results"][3], c["extras"] else: # default session frame = vs.read() output_dict = sess.run( tensor_dict, feed_dict={image_tensor: vs.expanded()}) num = output_dict['num_detections'][0] classes = output_dict['detection_classes'][0] boxes = output_dict['detection_boxes'][0] scores = output_dict['detection_scores'][0] if 'detection_masks' in output_dict: masks = output_dict['detection_masks'][0] # reformat detection num = int(num) boxes = np.squeeze(boxes) classes = np.squeeze(classes).astype(np.uint8) scores = np.squeeze(scores) # Visualization vis = visualize_objectdetection( frame, boxes, classes, scores, masks, category_index, fps._glob_numFrames, config.MAX_FRAMES, fps.fps_local(), config.PRINT_INTERVAL, config.PRINT_TH, config.OD_MODEL_NAME + config._DEV + config._OPT, config.VISUALIZE) if not vis: break # Activate Tracker if config.USE_TRACKER and num <= config.NUM_TRACKERS: tracker_frame = frame track = True first_track = True # Tracking else: frame = vs.read() if first_track: trackers = [] tracker_boxes = boxes for box in boxes[~np.all(boxes == 0, axis=1)]: tracker.init( conv_detect2track(box, vs.real_width, vs.real_height), tracker_frame) trackers.append(tracker) first_track = False for idx, tracker in enumerate(trackers): tracker_box = tracker.update(frame) tracker_boxes[idx, :] = conv_track2detect( tracker_box, vs.real_width, vs.real_height) vis = visualize_objectdetection( frame, tracker_boxes, classes, scores, masks, category_index, fps._glob_numFrames, config.MAX_FRAMES, fps.fps_local(), config.PRINT_INTERVAL, config.PRINT_TH, config.OD_MODEL_NAME + config._DEV + config._OPT, config.VISUALIZE) if not vis: break tracker_counter += 1 #tracker_frame = frame if tracker_counter >= config.TRACKER_FRAMES: track = False tracker_counter = 0 fps.update() # End everything vs.stop() fps.stop() if config.SPLIT_MODEL: gpu_worker.stop() cpu_worker.stop()
def detection(model,config): # Tf Session tf_config = model.tf_config detection_graph = model.detection_graph category_index = model.category_index print("> Building Graph") with detection_graph.as_default(): with tf.Session(graph=detection_graph,config=tf_config) as sess: # start Videostream # Define Input and Ouput tensors tensor_dict = model.get_tensordict(['num_detections', 'detection_boxes', 'detection_scores','detection_classes', 'detection_masks']) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Mask Transformations if 'detection_masks' in tensor_dict: # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size. detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0]) detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0]) real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32) detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1]) detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1]) detection_masks_reframed = reframe_box_masks_to_image_masks( detection_masks, detection_boxes, config.HEIGHT, config.WIDTH) detection_masks_reframed = tf.cast(tf.greater(detection_masks_reframed, 0.5), tf.uint8) # Follow the convention by adding back the batch dimension tensor_dict['detection_masks'] = tf.expand_dims(detection_masks_reframed, 0) if config.SPLIT_MODEL: score_out = detection_graph.get_tensor_by_name('Postprocessor/convert_scores:0') expand_out = detection_graph.get_tensor_by_name('Postprocessor/ExpandDims_1:0') score_in = detection_graph.get_tensor_by_name('Postprocessor/convert_scores_1:0') expand_in = detection_graph.get_tensor_by_name('Postprocessor/ExpandDims_1_1:0') # Threading score = model.score expand = model.expand # Timeliner if config.WRITE_TIMELINE: options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() timeliner = TimeLiner() else: options = tf.RunOptions(trace_level=tf.RunOptions.NO_TRACE) run_metadata = False images = load_images(config.IMAGE_PATH,config.LIMIT_IMAGES) timer = Timer().start() print('> Starting Detection') for image in images: if config.SPLIT_MODEL: # split model in seperate gpu and cpu session threads masks = None # No Mask Detection possible yet frame = cv2.resize(cv2.imread(image),(config.WIDTH,config.HEIGHT)) frame_expanded = np.expand_dims(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), axis=0) timer.tic() # GPU Session score, expand = sess.run([score_out, expand_out], feed_dict={image_tensor: frame_expanded}, options=options, run_metadata=run_metadata) timer.tictic() if config.WRITE_TIMELINE: timeliner.write_timeline(run_metadata.step_stats, '{}/timeline_{}_SM1.json'.format( config.RESULT_PATH,config.OD_DISPLAY_NAME)) timer.tic() # CPU Session boxes, scores, classes, num = sess.run( [tensor_dict['detection_boxes'], tensor_dict['detection_scores'], tensor_dict['detection_classes'], tensor_dict['num_detections']], feed_dict={score_in:score, expand_in: expand}, options=options, run_metadata=run_metadata) timer.toc() if config.WRITE_TIMELINE: timeliner.write_timeline(run_metadata.step_stats, '{}/timeline_{}_SM2.json'.format( config.RESULT_PATH,config.OD_DISPLAY_NAME)) else: # default session frame = cv2.resize(cv2.imread(image),(config.WIDTH,config.HEIGHT)) frame_expanded = np.expand_dims(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), axis=0) timer.tic() output_dict = sess.run(tensor_dict, feed_dict={image_tensor: frame_expanded}, options=options, run_metadata=run_metadata) timer.toc() if config.WRITE_TIMELINE: timeliner.write_timeline(run_metadata.step_stats, '{}/timeline_{}.json'.format( config.RESULT_PATH,config.OD_DISPLAY_NAME)) num = output_dict['num_detections'][0] classes = output_dict['detection_classes'][0] boxes = output_dict['detection_boxes'][0] scores = output_dict['detection_scores'][0] if 'detection_masks' in output_dict: masks = output_dict['detection_masks'][0] else: masks = None # reformat detection num = int(num) boxes = np.squeeze(boxes) classes = np.squeeze(classes).astype(np.uint8) scores = np.squeeze(scores) # Visualization vis = visualize_objectdetection(frame,boxes,classes,scores,masks,category_index,timer.get_frame(), config.MAX_FRAMES,timer.get_fps(),config.PRINT_INTERVAL,config.PRINT_TH, config.OD_DISPLAY_NAME,config.VISUALIZE,config.VIS_FPS,config.DISCO_MOE,config.ALPHA) if not vis: break if config.SAVE_RESULT: cv2.imwrite('{}/{}_{}.jpg'.format(config.RESULT_PATH,timer.get_frame(),config.OD_DISPLAY_NAME),frame) cv2.destroyAllWindows() timer.stop()
def detection(model, config): # Tf Session tf_config = model.tf_config detection_graph = model.detection_graph category_index = model.category_index print("> Building Graph") with detection_graph.as_default(): with tf.Session(graph=detection_graph, config=tf_config) as sess: # start Videostream # Define Input and Ouput tensors tensor_dict = model.get_tensordict([ 'num_detections', 'detection_boxes', 'detection_scores', 'detection_classes', 'detection_masks' ]) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Mask Transformations if 'detection_masks' in tensor_dict: # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size. detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0]) detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0]) real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32) detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1]) detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1]) detection_masks_reframed = reframe_box_masks_to_image_masks( detection_masks, detection_boxes, config.HEIGHT, config.WIDTH) detection_masks_reframed = tf.cast( tf.greater(detection_masks_reframed, 0.5), tf.uint8) # Follow the convention by adding back the batch dimension tensor_dict['detection_masks'] = tf.expand_dims( detection_masks_reframed, 0) if config.SPLIT_MODEL: score_out = detection_graph.get_tensor_by_name( 'Postprocessor/convert_scores:0') expand_out = detection_graph.get_tensor_by_name( 'Postprocessor/ExpandDims_1:0') score_in = detection_graph.get_tensor_by_name( 'Postprocessor/convert_scores_1:0') expand_in = detection_graph.get_tensor_by_name( 'Postprocessor/ExpandDims_1_1:0') # Threading score = model.score expand = model.expand # Timeliner if config.WRITE_TIMELINE: options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() timeliner = TimeLiner() else: options = tf.RunOptions(trace_level=tf.RunOptions.NO_TRACE) run_metadata = False images = load_images(config.IMAGE_PATH, config.LIMIT_IMAGES) timer = Timer().start() print('> Starting Detection') for image in images: if config.SPLIT_MODEL: # split model in seperate gpu and cpu session threads masks = None # No Mask Detection possible yet frame = cv2.resize(cv2.imread(image), (config.WIDTH, config.HEIGHT)) frame_expanded = np.expand_dims(cv2.cvtColor( frame, cv2.COLOR_BGR2RGB), axis=0) timer.tic() # GPU Session score, expand = sess.run( [score_out, expand_out], feed_dict={image_tensor: frame_expanded}, options=options, run_metadata=run_metadata) timer.tictic() if config.WRITE_TIMELINE: timeliner.write_timeline( run_metadata.step_stats, 'test_results/timeline_{}{}{}{}.json'.format( config.OD_MODEL_NAME, '_SM1', config._DEV, config._OPT)) timer.tic() # CPU Session boxes, scores, classes, num = sess.run( [ tensor_dict['detection_boxes'], tensor_dict['detection_scores'], tensor_dict['detection_classes'], tensor_dict['num_detections'] ], feed_dict={ score_in: score, expand_in: expand }, options=options, run_metadata=run_metadata) timer.toc() if config.WRITE_TIMELINE: timeliner.write_timeline( run_metadata.step_stats, 'test_results/timeline_{}{}{}{}.json'.format( config.OD_MODEL_NAME, '_SM2', config._DEV, config._OPT)) else: # default session frame = cv2.resize(cv2.imread(image), (config.WIDTH, config.HEIGHT)) frame_expanded = np.expand_dims(cv2.cvtColor( frame, cv2.COLOR_BGR2RGB), axis=0) timer.tic() output_dict = sess.run( tensor_dict, feed_dict={image_tensor: frame_expanded}, options=options, run_metadata=run_metadata) timer.toc() if config.WRITE_TIMELINE: timeliner.write_timeline( run_metadata.step_stats, 'test_results/timeline_{}{}{}.json'.format( config.OD_MODEL_NAME, config._DEV, config._OPT)) num = output_dict['num_detections'][0] classes = output_dict['detection_classes'][0] boxes = output_dict['detection_boxes'][0] scores = output_dict['detection_scores'][0] if 'detection_masks' in output_dict: masks = output_dict['detection_masks'][0] else: masks = None # reformat detection num = int(num) boxes = np.squeeze(boxes) classes = np.squeeze(classes).astype(np.uint8) scores = np.squeeze(scores) # Visualization vis = visualize_objectdetection( frame, boxes, classes, scores, masks, category_index, timer.get_frame(), config.MAX_FRAMES, timer.get_fps(), config.PRINT_INTERVAL, config.PRINT_TH, config.OD_MODEL_NAME + config._DEV + config._OPT, config.VISUALIZE) if not vis: break cv2.destroyAllWindows() timer.stop()
def detection(model,config): # Tracker if config.USE_TRACKER: import sys sys.path.append(os.getcwd()+'/rod/kcf') import KCF tracker = KCF.kcftracker(False, True, False, False) tracker_counter = 0 track = False print("> Building Graph") # tf Session Config tf_config = model.tf_config detection_graph = model.detection_graph category_index = model.category_index with detection_graph.as_default(): with tf.Session(graph=detection_graph,config=tf_config) as sess: # start Videostream vs = WebcamVideoStream(config.VIDEO_INPUT,config.WIDTH,config.HEIGHT).start() # Define Input and Ouput tensors tensor_dict = model.get_tensordict(['num_detections', 'detection_boxes', 'detection_scores','detection_classes', 'detection_masks']) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Mask Transformations if 'detection_masks' in tensor_dict: # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size. detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0]) detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0]) real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32) detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1]) detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1]) detection_masks_reframed = reframe_box_masks_to_image_masks( detection_masks, detection_boxes, vs.real_height, vs.real_width) detection_masks_reframed = tf.cast(tf.greater(detection_masks_reframed, 0.5), tf.uint8) # Follow the convention by adding back the batch dimension tensor_dict['detection_masks'] = tf.expand_dims(detection_masks_reframed, 0) if config.SPLIT_MODEL: score_out = detection_graph.get_tensor_by_name('Postprocessor/convert_scores:0') expand_out = detection_graph.get_tensor_by_name('Postprocessor/ExpandDims_1:0') score_in = detection_graph.get_tensor_by_name('Postprocessor/convert_scores_1:0') expand_in = detection_graph.get_tensor_by_name('Postprocessor/ExpandDims_1_1:0') # Threading score = model.score expand = model.expand gpu_worker = SessionWorker("GPU",detection_graph,tf_config) cpu_worker = SessionWorker("CPU",detection_graph,tf_config) gpu_opts = [score_out, expand_out] cpu_opts = [tensor_dict['detection_boxes'], tensor_dict['detection_scores'], tensor_dict['detection_classes'], tensor_dict['num_detections']] fps = FPS(config.FPS_INTERVAL).start() masks = None print('> Starting Detection') while vs.isActive(): # Detection if not (config.USE_TRACKER and track): if config.SPLIT_MODEL: # split model in seperate gpu and cpu session threads if gpu_worker.is_sess_empty(): # read video frame, expand dimensions and convert to rgb frame = vs.read() # put new queue gpu_feeds = {image_tensor: vs.expanded()} if config.VISUALIZE: gpu_extras = frame # for visualization frame else: gpu_extras = None gpu_worker.put_sess_queue(gpu_opts,gpu_feeds,gpu_extras) g = gpu_worker.get_result_queue() if g is None: # gpu thread has no output queue. ok skip, let's check cpu thread. pass else: # gpu thread has output queue. score,expand,frame = g["results"][0],g["results"][1],g["extras"] if cpu_worker.is_sess_empty(): # When cpu thread has no next queue, put new queue. # else, drop gpu queue. cpu_feeds = {score_in: score, expand_in: expand} cpu_extras = frame cpu_worker.put_sess_queue(cpu_opts,cpu_feeds,cpu_extras) c = cpu_worker.get_result_queue() if c is None: # cpu thread has no output queue. ok, nothing to do. continue continue # If CPU RESULT has not been set yet, no fps update else: boxes, scores, classes, num, frame = c["results"][0],c["results"][1],c["results"][2],c["results"][3],c["extras"] else: # default session frame = vs.read() output_dict = sess.run(tensor_dict, feed_dict={image_tensor: vs.expanded()}) num = output_dict['num_detections'][0] classes = output_dict['detection_classes'][0] boxes = output_dict['detection_boxes'][0] scores = output_dict['detection_scores'][0] if 'detection_masks' in output_dict: masks = output_dict['detection_masks'][0] # reformat detection num = int(num) boxes = np.squeeze(boxes) classes = np.squeeze(classes).astype(np.uint8) scores = np.squeeze(scores) # Visualization vis = visualize_objectdetection(frame,boxes,classes,scores,masks,category_index,fps._glob_numFrames, config.MAX_FRAMES,fps.fps_local(),config.PRINT_INTERVAL,config.PRINT_TH, config.OD_DISPLAY_NAME,config.VISUALIZE,config.VIS_FPS,config.DISCO_MODE,config.ALPHA) if not vis: break # Activate Tracker if config.USE_TRACKER and num <= config.NUM_TRACKERS: tracker_frame = frame track = True first_track = True # Tracking else: frame = vs.read() if first_track: trackers = [] tracker_boxes = boxes for box in boxes[~np.all(boxes == 0, axis=1)]: tracker.init(conv_detect2track(box,vs.real_width, vs.real_height), tracker_frame) trackers.append(tracker) first_track = False for idx,tracker in enumerate(trackers): tracker_box = tracker.update(frame) tracker_boxes[idx,:] = conv_track2detect(tracker_box, vs.real_width, vs.real_height) vis = visualize_objectdetection(frame,tracker_boxes,classes,scores,masks,category_index,fps._glob_numFrames, config.MAX_FRAMES,fps.fps_local(),config.PRINT_INTERVAL,config.PRINT_TH, config.OD_DISPLAY_NAME,config.VISUALIZE,config.VIS_FPS,config.DISCO_MODE,config.ALPHA) if not vis: break tracker_counter += 1 #tracker_frame = frame if tracker_counter >= config.TRACKER_FRAMES: track = False tracker_counter = 0 fps.update() # End everything vs.stop() fps.stop() if config.SPLIT_MODEL: gpu_worker.stop() cpu_worker.stop()