def segmentation(): global target_size w = 640 h = 480 vs = WebcamVideoStream(0, w, h).start() fake2 = pyfakewebcam.FakeWebcam('/dev/video2', w, h) resize_ratio = 1.0 * 513 / max(vs.real_width, vs.real_height) target_size = (int(resize_ratio * vs.real_width), int(resize_ratio * vs.real_height)) fps = FPS2(5).start() print("Starting...") logo_img = cv2.imread("logo-uds.png") name = "Webcam-Replacement" cv2.namedWindow(name, 16) # 16 = WINDOW_GUI_NORMAL, disable right click cv2.setMouseCallback(name, toggleCam) global showCam showCam = True while vs.isActive(): if showCam: image = cv2.resize(vs.read(), target_size) image[100:100 + logo_img.shape[0], 50:50 + logo_img.shape[1]] = logo_img ir = cv2.resize(image, (vs.real_width, vs.real_height)) ir2 = cv2.cvtColor(ir, cv2.COLOR_BGR2RGB) fake2.schedule_frame(ir2) cv2.imshow(name, ir) key = cv2.waitKey(1) if key & 0xFF == ord('q'): break if key & 0xFF == 27: break if key & 0xFF == ord('c'): showCam = not showCam if cv2.getWindowProperty(name, 0) < 0: break if cv2.getWindowProperty(name, cv2.WND_PROP_VISIBLE) < 1: break fps.update() fps.stop() vs.stop() cv2.destroyAllWindows()
def segmentation(model, config): detection_graph = model.detection_graph # fixed input sizes as model needs resize either way vs = WebcamVideoStream(config.CV_INPUT, 640, 480).start() resize_ratio = 1.0 * 513 / max(vs.real_width, vs.real_height) target_size = (int(resize_ratio * vs.real_width), int(resize_ratio * vs.real_height)) #(513, 384) tf_config = model.tf_config fps = FPS(config.FPS_INTERVAL).start() print("> Starting Segmentaion") with detection_graph.as_default(): with tf.Session(graph=detection_graph, config=tf_config) as sess: while vs.isActive(): frame = vs.resized(target_size) batch_seg_map = sess.run( 'SemanticPredictions:0', feed_dict={ 'ImageTensor:0': [cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)] }) seg_map = batch_seg_map[0] #boxes = [] #labels = [] map_labeled = measure.label(seg_map, connectivity=1) for region in measure.regionprops(map_labeled): if region.area > config.MINAREA: box = region.bbox label = config.LABEL_NAMES[seg_map[tuple( region.coords[0])]] #boxes.append(box) #labels.append(label) if config.VISUALIZE: draw_single_box_on_image(frame, box, label) vis = visualize_deeplab( frame, seg_map, 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 fps.update() fps.stop() vs.stop()
def detection(detectionGraph, category_index, score, expand): print('[INFO] Building Graph...') config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=logDevice) config.gpu_options.allow_growth = allowMemGrth with detectionGraph.as_default(): with tf.Session(graph=detectionGraph, config=config) as sess: # Define Input and Ouput tensors image_tensor = detectionGraph.get_tensor_by_name('image_tensor:0') detection_boxes = detectionGraph.get_tensor_by_name( 'detection_boxes:0') detection_scores = detectionGraph.get_tensor_by_name( 'detection_scores:0') detection_classes = detectionGraph.get_tensor_by_name( 'detection_classes:0') num_detections = detectionGraph.get_tensor_by_name( 'num_detections:0') score_out = detectionGraph.get_tensor_by_name( 'Postprocessor/convert_scores:0') expand_out = detectionGraph.get_tensor_by_name( 'Postprocessor/ExpandDims_1:0') score_in = detectionGraph.get_tensor_by_name( 'Postprocessor/convert_scores_1:0') expand_in = detectionGraph.get_tensor_by_name( 'Postprocessor/ExpandDims_1_1:0') # Threading gpu_worker = SessionWorker('GPU', detectionGraph, config) cpu_worker = SessionWorker('CPU', detectionGraph, config) gpu_opts = [score_out, expand_out] cpu_opts = [ detection_boxes, detection_scores, detection_classes, num_detections ] gpu_counter = 0 cpu_counter = 0 # -----Start Video Stream----- fps = FPS(fpsInterval).start() videoStream = WebcamVideoStream(videoInput, width, height).start() counts = 0 print('[INFO] Press q to Exit') print('[INFO] Starting Detection...') while videoStream.isActive(): # -----actual Detection----- # split model in seperate gpu and cpu session threads if gpu_worker.sessEmpty(): # read video frame, expand dimensions and convert to # rgb image = videoStream.read() img_copy = image.copy() image = cv2.resize(image, dsize=(400, 200)) image_expanded = np.expand_dims(cv2.cvtColor( image, cv2.COLOR_BGR2RGB), axis=0) # put new queue gpu_feeds = {image_tensor: image_expanded} if visualize: gpu_extras = image # for visualization frame else: gpu_extras = None gpu_worker.insertSessQueue(gpu_opts, gpu_feeds, gpu_extras) g = gpu_worker.getResultQueue() if g is None: # if gpu thread has no output queue gpu_counter += 1 else: # if gpu thread has output queue. gpu_counter = 0 score, expand, image = g['results'][0], g['results'][1], g[ 'extras'] if cpu_worker.sessEmpty(): # if cpu thread has no next put new queue # else drop gpu queue cpu_feeds = {score_in: score, expand_in: expand} cpu_extras = image cpu_worker.insertSessQueue(cpu_opts, cpu_feeds, cpu_extras) c = cpu_worker.getResultQueue() if c is None: # cpu thread has no output queue cpu_counter += 1 time.sleep(0.005) continue # If CPU RESULT has not been set yet, no fps update else: cpu_counter = 0 boxes, scores, classes, num, image = c['results'][0], c[ 'results'][1], c['results'][2], c['results'][3], c[ 'extras'] # -----Visualization----- image = cv2.resize(img_copy, dsize=(800, 500)) if visualize: vis_util.visualize_boxes_and_labels_on_image_array( image, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=3) if visFPS: cv2.putText(image, 'fps: {}'.format(fps.fpsLocal()), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (77, 255, 9), 2) cv2.imshow('object_detection', image) if cv2.waitKey(1) & 0xFF == ord('q'): # press q to exit break fps.update() counts += 1 # cv2.waitKey(30) # close thread and video stream gpu_worker.stop() cpu_worker.stop() fps.stop() videoStream.stop() cv2.destroyAllWindows() print('[INFO] elapsed time (total): {:.2f}s'.format(fps.elapsed())) print('[INFO] Average FPS: {:.2f}'.format(fps.fps()))
def detection(model, config): # Tracker if config.USE_TRACKER: import sys sys.path.append(ROOT_DIR + '/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.CV_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 segmentation(detection_graph): global bg_img global bg_imgs global target_size w = 640 h = 480 vs = WebcamVideoStream(0, w, h).start() fake2 = pyfakewebcam.FakeWebcam('/dev/video2', w, h) resize_ratio = 1.0 * 513 / max(vs.real_width, vs.real_height) target_size = (int(resize_ratio * vs.real_width), int(resize_ratio * vs.real_height)) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True fps = FPS2(5).start() bg_imgs = loadBg(".") bg_img = bg_imgs[0] print("Starting...") name = "Webcam-Replacement" cv2.namedWindow(name, 16) # 16 = WINDOW_GUI_NORMAL, disable right click cv2.setMouseCallback(name, nextBg) global img_num img_num = 0 showCam = False with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: while vs.isActive(): image = cv2.resize(vs.read(), target_size) batch_seg_map = sess.run( 'SemanticPredictions:0', feed_dict={ 'ImageTensor:0': [cv2.cvtColor(image, cv2.COLOR_BGR2RGB)] }) seg_map = batch_seg_map[0] seg_map[seg_map != 15] = 0 bg_copy = bg_img.copy() mask = (seg_map == 15) if showCam: bg_copy = image else: bg_copy[mask] = image[mask] seg_image = np.stack((seg_map, seg_map, seg_map), axis=-1).astype(np.uint8) gray = cv2.cvtColor(seg_image, cv2.COLOR_BGR2GRAY) ir = cv2.resize(bg_copy, (vs.real_width, vs.real_height)) ir2 = cv2.cvtColor(ir, cv2.COLOR_BGR2RGB) fake2.schedule_frame(ir2) cv2.imshow(name, ir) key = cv2.waitKey(1) if key & 0xFF == ord('q'): break if key & 0xFF == 27: break if key & 0xFF == ord('c'): showCam = not showCam if key & 0xFF == ord('o'): openImg() if cv2.getWindowProperty(name, 0) < 0: break if cv2.getWindowProperty(name, cv2.WND_PROP_VISIBLE) < 1: break fps.update() fps.stop() vs.stop() cv2.destroyAllWindows()