|
| 1 | +import cv2 |
| 2 | +import multiprocessing |
| 3 | +import time |
| 4 | + |
| 5 | +import os |
| 6 | +import numpy as np |
| 7 | +import tensorflow as tf |
| 8 | + |
| 9 | +from utils import FPS |
| 10 | +from object_detection.utils import label_map_util |
| 11 | +from object_detection.utils import visualization_utils as vis_util |
| 12 | + |
| 13 | +CWD_PATH = os.getcwd() |
| 14 | + |
| 15 | +# Path to frozen detection graph. This is the actual model that is used for the object detection. |
| 16 | +MODEL_NAME = 'ssd_mobilenet_v1_coco_11_06_2017' |
| 17 | +PATH_TO_CKPT = os.path.join(CWD_PATH, 'object_detection', MODEL_NAME, 'frozen_inference_graph.pb') |
| 18 | + |
| 19 | +# List of the strings that is used to add correct label for each box. |
| 20 | +PATH_TO_LABELS = os.path.join(CWD_PATH, 'object_detection', 'data', 'mscoco_label_map.pbtxt') |
| 21 | + |
| 22 | +NUM_CLASSES = 90 |
| 23 | + |
| 24 | +# Loading label map |
| 25 | +label_map = label_map_util.load_labelmap(PATH_TO_LABELS) |
| 26 | +categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, |
| 27 | + use_display_name=True) |
| 28 | +category_index = label_map_util.create_category_index(categories) |
| 29 | + |
| 30 | + |
| 31 | +def detect_objects(image_np, sess, detection_graph): |
| 32 | + # Expand dimensions since the model expects images to have shape: [1, None, None, 3] |
| 33 | + image_np_expanded = np.expand_dims(image_np, axis=0) |
| 34 | + image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') |
| 35 | + |
| 36 | + # Each box represents a part of the image where a particular object was detected. |
| 37 | + boxes = detection_graph.get_tensor_by_name('detection_boxes:0') |
| 38 | + |
| 39 | + # Each score represent how level of confidence for each of the objects. |
| 40 | + # Score is shown on the result image, together with the class label. |
| 41 | + scores = detection_graph.get_tensor_by_name('detection_scores:0') |
| 42 | + classes = detection_graph.get_tensor_by_name('detection_classes:0') |
| 43 | + num_detections = detection_graph.get_tensor_by_name('num_detections:0') |
| 44 | + |
| 45 | + # Actual detection. |
| 46 | + (boxes, scores, classes, num_detections) = sess.run( |
| 47 | + [boxes, scores, classes, num_detections], |
| 48 | + feed_dict={image_tensor: image_np_expanded}) |
| 49 | + |
| 50 | + # Visualization of the results of a detection. |
| 51 | + vis_util.visualize_boxes_and_labels_on_image_array( |
| 52 | + image_np, |
| 53 | + np.squeeze(boxes), |
| 54 | + np.squeeze(classes).astype(np.int32), |
| 55 | + np.squeeze(scores), |
| 56 | + category_index, |
| 57 | + use_normalized_coordinates=True, |
| 58 | + line_thickness=8) |
| 59 | + |
| 60 | + return image_np |
| 61 | + |
| 62 | + |
| 63 | +def blend_non_transparent(face_img, overlay_img): |
| 64 | + # Let's find a mask covering all the non-black (foreground) pixels |
| 65 | + # NB: We need to do this on grayscale version of the image |
| 66 | + gray_overlay = cv2.cvtColor(overlay_img, cv2.COLOR_BGR2GRAY) |
| 67 | + overlay_mask = cv2.threshold(gray_overlay, 1, 255, cv2.THRESH_BINARY)[1] |
| 68 | + |
| 69 | + # Let's shrink and blur it a little to make the transitions smoother... |
| 70 | + overlay_mask = cv2.erode(overlay_mask, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))) |
| 71 | + overlay_mask = cv2.blur(overlay_mask, (3, 3)) |
| 72 | + |
| 73 | + # And the inverse mask, that covers all the black (background) pixels |
| 74 | + background_mask = 255 - overlay_mask |
| 75 | + |
| 76 | + # Turn the masks into three channel, so we can use them as weights |
| 77 | + overlay_mask = cv2.cvtColor(overlay_mask, cv2.COLOR_GRAY2BGR) |
| 78 | + background_mask = cv2.cvtColor(background_mask, cv2.COLOR_GRAY2BGR) |
| 79 | + |
| 80 | + # Create a masked out face image, and masked out overlay |
| 81 | + # We convert the images to floating point in range 0.0 - 1.0 |
| 82 | + face_part = (face_img * (1 / 255.0)) * (background_mask * (1 / 255.0)) |
| 83 | + overlay_part = (overlay_img * (1 / 255.0)) * (overlay_mask * (1 / 255.0)) |
| 84 | + |
| 85 | + # And finally just add them together, and rescale it back to an 8bit integer image |
| 86 | + return np.uint8(cv2.addWeighted(face_part, 255.0, overlay_part, 255.0, 0.0)) |
| 87 | + |
| 88 | + |
| 89 | +def main_process(input, output): |
| 90 | + while True: |
| 91 | + time.sleep(0.5) |
| 92 | + image = input.get() |
| 93 | + output.put(image) |
| 94 | + |
| 95 | + |
| 96 | +def child_process(input, output): |
| 97 | + # Load a (frozen) Tensorflow model into memory. |
| 98 | + detection_graph = tf.Graph() |
| 99 | + with detection_graph.as_default(): |
| 100 | + od_graph_def = tf.GraphDef() |
| 101 | + with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: |
| 102 | + serialized_graph = fid.read() |
| 103 | + od_graph_def.ParseFromString(serialized_graph) |
| 104 | + tf.import_graph_def(od_graph_def, name='') |
| 105 | + |
| 106 | + sess = tf.Session(graph=detection_graph) |
| 107 | + |
| 108 | + while True: |
| 109 | + image = input.get() |
| 110 | + image2 = detect_objects(image, sess, detection_graph) |
| 111 | + result = blend_non_transparent(image, image2) |
| 112 | + output.put(result) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + input = multiprocessing.Queue(5) |
| 117 | + output = multiprocessing.Queue(5) |
| 118 | + |
| 119 | + main_process = multiprocessing.Process(target=main_process, args=(input, output)) |
| 120 | + main_process.daemon = True |
| 121 | + child_process = multiprocessing.Process(target=child_process, args=(input, output)) |
| 122 | + child_process.daemon = False |
| 123 | + |
| 124 | + main_process.start() |
| 125 | + child_process.start() |
| 126 | + |
| 127 | + video_capture = cv2.VideoCapture(0) |
| 128 | + video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 480) |
| 129 | + video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 360) |
| 130 | + |
| 131 | + while True: |
| 132 | + _, frame = video_capture.read() |
| 133 | + |
| 134 | + input.put(frame) |
| 135 | + |
| 136 | + cv2.imshow('Video', output.get()) |
| 137 | + |
| 138 | + if cv2.waitKey(1) & 0xFF == ord('q'): |
| 139 | + break |
0 commit comments