def __init__(self):
     self.__self_dir_path = dirname(realpath(__file__))
     self.__thread = None
     self.__pause_resume = None
     self.__tf_sess = tf.Session(config=self.__config)
     self.__in_pipe = Pipe()
     self.__threading = threading
Esempio n. 2
0
    def __init__(self, index=None):
        if index is None:
            index = 0

        self.__system = PySpin.System.GetInstance()
        self.__cam_list = self.__system.GetCameras()
        self.__cam = self.__cam_list.GetByIndex(index)
        # self.__pixel_format = self.__cam.PixelFormat
        # print(self.__pixel_format.GetCurrentEntry())
        # self.__pixel_format = PySpin.PixelFormat_BGR8
        # print(self.__pixel_format)
        # self.__access_mode = self.__cam.PixelFormat.GetAccessMode
        self.__cam.Init()
        self.__nodemap = self.__cam.GetNodeMap()
        self.configure_custom_image_settings(self.__nodemap)
        # self.__cam.DeInit()
        # self.__cam.Init()
        self.__cam.AcquisitionMode.SetValue(PySpin.AcquisitionMode_Continuous)
        print(self.__cam.EncoderMode)

        self.__node_acquisition_framerate = PySpin.CFloatPtr(
            self.__nodemap.GetNode('AcquisitionFrameRate'))
        self.__framerate_to_set = self.__node_acquisition_framerate.GetValue()

        print(self.__framerate_to_set)

        self.__cam.BeginAcquisition()

        self.__out_pipe = Pipe(self.__out_pipe_process)
        self.__thread = None
Esempio n. 3
0
 def __init__(self, flush_pipe_on_read=False):
     self.__detector = None
     self.__face_size = 64
     self.__flush_pipe_on_read = flush_pipe_on_read
     self.__thread = None
     self.__in_pipe = Pipe(self.__in_pipe_process)
     self.__out_pipe = Pipe(self.__out_pipe_process)
Esempio n. 4
0
 def __init__(self, graph_prefix=None, flush_pipe_on_read=False):
     self.__dict__.update(self._defaults)  # set up default values
     # self.__dict__.update(kwargs) # and update with user overrides
     YOLOObjectDetectionAPI.class_names, self.__category_dict = self._get_class(
     )
     YOLOObjectDetectionAPI.anchors = self._get_anchors()
     self.__graph_prefix = graph_prefix
     self.__flush_pipe_on_read = flush_pipe_on_read
     self.__thread = None
     self.__in_pipe = Pipe(self.__in_pipe_process)
     self.__out_pipe = Pipe(self.__out_pipe_process)
Esempio n. 5
0
    def __init__(self, graph_prefix='', flush_pipe_on_read=False):
        self.__flush_pipe_on_read = flush_pipe_on_read

        self.__model_path = "/home/developer/PycharmProjects/SecureIt/obj_tracking/sort_deep/resources/networks/mars-small128.pb"

        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)

        if not graph_prefix:
            self.__graph_prefix = ''
        else:
            self.__graph_prefix = graph_prefix + '/'
Esempio n. 6
0
    def __init__(self, graph_prefix='', flush_pipe_on_read=False):
        self.__flush_pipe_on_read = flush_pipe_on_read
        self.__feature_dim = (2048)
        self.__image_shape = (224, 224, 3)

        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)

        if not graph_prefix:
            self.__graph_prefix = ''
        else:
            self.__graph_prefix = graph_prefix + '/'
Esempio n. 7
0
    def __init__(self, graph_prefix='', flush_pipe_on_read=False):
        self.__flush_pipe_on_read = flush_pipe_on_read

        self.__model_path = pretrained_path.get() + "/mars-small128.pb"

        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)

        if not graph_prefix:
            self.__graph_prefix = ''
        else:
            self.__graph_prefix = graph_prefix + '/'
Esempio n. 8
0
    def __init__(self, max_age=120, min_hits=3, flush_pipe_on_read=False):
        """
        Sets key parameters for SORT
        """
        self.max_age = max_age
        self.min_hits = min_hits
        self.trackers = []
        self.frame_count = 0

        self.__flush_pipe_on_read = flush_pipe_on_read

        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__outpass_pipe = Pipe(self.__out_pipe_process)
    def __init__(self, max_age=10000, min_hits=5, flush_pipe_on_read=False):
        self.max_age = max_age
        self.min_hits = min_hits
        self.trackers = []
        self.frame_count = 0
        self.__bg_frame = None
        self.__bg_gray = None

        self.__flush_pipe_on_read = flush_pipe_on_read

        self.__feature_dim = (2048)
        self.__image_shape = (224, 224, 3)

        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)
    def __init__(self, model_name=PRETRAINED_ssd_mobilenet_v1_coco_2017_11_17, image_shape=None,
                 graph_prefix=None, flush_pipe_on_read=False):
        self.__category_index, self.__category_dict = self.__fetch_category_indices()
        self.__path_to_frozen_graph = self.__fetch_model_path(model_name)
        self.__flush_pipe_on_read = flush_pipe_on_read
        self.__image_shape = image_shape

        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)

        self.__run_session_on_thread = False

        if not graph_prefix:
            self.__graph_prefix = ''
        else:
            self.__graph_prefix = graph_prefix + '/'
Esempio n. 11
0
def detect_objects(cap, pipe, detector, default):
    if not default:
        ret_pipe = Pipe()
    else:
        ret_pipe = None

    def start_cam():
        while True:
            ret, image = cap.read()
            if not ret:
                continue
            inference = Inference(image.copy(), return_pipe=ret_pipe)
            detector.get_in_pipe().push_wait()
            detector.get_in_pipe().push(inference)

    Thread(target=start_cam).start()
    while True:
        if not default:
            ret, inference = ret_pipe.pull(True)
            if not ret:
                ret_pipe.pull_wait()
            else:
                ret_pipe.flush()
        else:
            detector.getOutPipe().pull_wait()
            ret, inference = detector.getOutPipe().pull(True)
        if ret:
            i_dets = inference.get_result()
            pipe.push(i_dets.get_annotated())
Esempio n. 12
0
class SessionRunner:
    __config = tf.ConfigProto()
    __config.gpu_options.allow_growth = True
    __counter = 0

    def __init__(self, threading=False):
        self.__self_dir_path = dirname(realpath(__file__))
        self.__thread = None
        self.__pause_resume = None
        self.__sess = tf.Session(config=self.__config)
        self.__in_pipe = Pipe()
        self.__threading = threading

    def get_in_pipe(self):
        return self.__in_pipe

    def get_session(self):
        return self.__sess

    def start(self):
        if self.__thread is None:
            self.__pause_resume = threading.Event()
            self.__thread = Thread(target=self.__start)
            self.__thread.start()

    def stop(self):
        if self.__thread is not None:
            self.__thread = None

    def __start(self):
        while self.__thread:
            ret, pull = self.__in_pipe.pull()
            if ret:
                if self.__threading:
                    Thread(target=self.__exec, args=(pull, )).start()
                else:
                    self.__exec(pull)
            else:
                self.__in_pipe.wait()

    def __exec(self, pull):
        job_fnc, args_dict = pull
        with self.__sess.as_default():
            with self.__sess.graph.as_default():
                job_fnc(args_dict)
Esempio n. 13
0
    def __init__(self,
                 session_runner,
                 model_name=PRETRAINED_mars_small128,
                 graph_prefix=None):
        self.__path_to_frozen_graph = self.__fetch_model_path(model_name)

        if not graph_prefix:
            self.__graph_prefix = ''
        else:
            self.__graph_prefix = graph_prefix + '/'

        self.__session_runner = session_runner
        self.__tf_sess = session_runner.get_session()
        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)

        self.init_graph()
    def __init__(self,
                 image_shape=None,
                 graph_prefix=None,
                 flush_pipe_on_read=False):
        self.__category_index, self.__category_dict = self.__fetch_category_indices(
        )
        self.__path_to_frozen_graph = self.__fetch_model_path()
        self.__flush_pipe_on_read = flush_pipe_on_read
        self.__image_shape = image_shape

        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)

        if not graph_prefix:
            self.__graph_prefix = ''
        else:
            self.__graph_prefix = graph_prefix + '/'
Esempio n. 15
0
    def get_default(name):
        global fs
        if not fs:
            fs = FlaskMovieAPI()
            Thread(target=fs.get_app().run, args=("0.0.0.0", 9999)).start()

        if not name in fs.__routes_pipe:
            pipe = Pipe()
            fs.create(name, pipe)

        return fs.__routes_pipe[name]
    def __init__(self, max_age=10000, min_hits=5, flush_pipe_on_read=False, use_detection_mask=False, conf_path=None):
        self.max_age = max_age
        self.min_hits = min_hits
        self.trackers = []
        self.frame_count = 0
        self.__bg_frame = None
        self.__bg_gray = None
        self.__conf_path = conf_path
        self.__flush_pipe_on_read = flush_pipe_on_read

        # self.__feature_dim = (128)
        # self.__image_shape = (128, 64, 3)

        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)

        self.__use_detection_mask = use_detection_mask
        self.__zones = None
        if self.__conf_path is not None:
            self.__zones = Zone.create_zones_from_conf(self.__conf_path)
class SessionRunner:
    __config = tf.ConfigProto(log_device_placement=False)
    __config.gpu_options.allow_growth = True
    __counter = 0

    def __init__(self):
        self.__self_dir_path = dirname(realpath(__file__))
        self.__thread = None
        self.__pause_resume = None
        self.__tf_sess = tf.Session(config=self.__config)
        self.__in_pipe = Pipe()
        self.__threading = threading

    def get_in_pipe(self):
        return self.__in_pipe

    def get_session(self):
        return self.__tf_sess

    def start(self):
        if self.__thread is None:
            self.__pause_resume = threading.Event()
            self.__thread = Thread(target=self.__start)
            self.__thread.start()

    def stop(self):
        if self.__thread is not None:
            self.__thread = None

    def __start(self):
        while self.__thread:
            ret, sess_fnc = self.__in_pipe.pull()
            if ret:
                if type(sess_fnc) is not SessionRunnable:
                    raise Exception("Pipe elements must be a SessionFunction")
                sess_fnc.execute(self.__tf_sess)
            else:
                self.__in_pipe.wait()
Esempio n. 18
0
    def __init__(self,
                 input_size,
                 labels,
                 max_box_per_image,
                 anchors,
                 flush_pipe_on_read=False):
        self.__flush_pipe_on_read = flush_pipe_on_read
        self.__model_path = model_path.get()
        self.__model = None
        self.__input_size = input_size
        self.__labels = list(labels)
        self.__nb_class = len(self.__labels)
        self.__nb_box = len(anchors) // 2
        self.__class_wt = np.ones(self.__nb_class, dtype='float32')
        self.__anchors = anchors
        self.__max_box_per_image = max_box_per_image
        self.__session_runner = None
        self.__tf_sess = None

        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)
        self.__run_session_on_thread = False
Esempio n. 19
0
    def __init__(self, model_name=PRETRAINED_ssd_mobilenet_v1_coco_2017_11_17):
        self.dir_path = dirname(realpath(__file__))

        self.model_path = self.dir_path + '/object_detection/pretrained/'
        self.model_file = model_name + '.tar.gz'
        self.download_base = 'http://download.tensorflow.org/models/object_detection/'
        self.path_to_frozen_graph = self.model_path + model_name + '/frozen_inference_graph.pb'
        path_to_labels = os.path.join(self.dir_path + '/object_detection/data',
                                      'mscoco_label_map.pbtxt')
        self.class_count = 90
        if not path.exists(self.path_to_frozen_graph):
            self.__download()

        self.__load()
        self.label_map = label_map_util.load_labelmap(path_to_labels)
        self.categories = label_map_util.convert_label_map_to_categories(
            self.label_map,
            max_num_classes=self.class_count,
            use_display_name=True)
        self.category_index = label_map_util.create_category_index(
            self.categories)

        self.inPipe = Pipe()
        self.outPipe = Pipe()
Esempio n. 20
0

tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created')

tcp_socket.bind((HOST, TCP_PORT))
print('Socket bind complete')
tcp_socket.listen(10)
print('Socket now listening')
conn, addr = tcp_socket.accept()

images = []
data = b''
payload_size = struct.calcsize("L")

pipe = Pipe()


def start_tcp_server():
    global data
    global pipe
    while True:
        while len(data) < payload_size:
            data += conn.recv(4096)
        packed_msg_size = data[:payload_size]

        data = data[payload_size:]
        msg_size = struct.unpack("L", packed_msg_size)[0]

        while len(data) < msg_size:
            data += conn.recv(4096)
Esempio n. 21
0
class ResNet50ExtractorAPI:
    def __init__(self, graph_prefix='', flush_pipe_on_read=False):
        self.__flush_pipe_on_read = flush_pipe_on_read
        self.__feature_dim = (2048)
        self.__image_shape = (224, 224, 3)

        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)

        if not graph_prefix:
            self.__graph_prefix = ''
        else:
            self.__graph_prefix = graph_prefix + '/'

    def __preprocess(self, original):
        preprocessed = cv2.resize(original,
                                  tuple(self.__image_shape[:2][::-1]))
        preprocessed = resnet50.preprocess_input(preprocessed)
        return preprocessed

    def __in_pipe_process(self, inference):
        images = inference.get_input()
        if type(images) is not list:
            images = [images]
        data = []
        for img in images:
            data.append(self.__preprocess(img))
        data = np.array(data)
        inference.set_data(data)
        return inference

    def __out_pipe_process(self, result):
        result, inference = result
        inference.set_result(result)
        return inference

    def get_in_pipe(self):
        return self.__in_pipe

    def get_out_pipe(self):
        return self.__out_pipe

    def use_session_runner(self, session_runner):
        self.__session_runner = session_runner
        K.set_session(session_runner.get_session())
        self.__tf_sess = K.get_session()

        with self.__tf_sess.as_default():
            self.__model = resnet50.ResNet50(weights='imagenet',
                                             include_top=False,
                                             pooling='avg')

    def run(self):
        if self.__thread is None:
            self.__thread = Thread(target=self.__run)
            self.__thread.start()

    def __run(self):
        while self.__thread:

            if self.__in_pipe.is_closed():
                self.__out_pipe.close()
                return

            ret, inference = self.__in_pipe.pull(self.__flush_pipe_on_read)
            if ret:
                self.__session_runner.get_in_pipe().push(
                    (self.__job, inference))
            else:
                self.__in_pipe.wait()

    def __job(self, inference):
        try:
            self.__out_pipe.push(
                (self.__model.predict(inference.get_data()), inference))
        except:
            self.__out_pipe.push((np.zeros((0, self.__feature_dim),
                                           np.float32), inference))
class OFISTObjectTrackingAPI:
    def __init__(self, max_age=10000, min_hits=5, flush_pipe_on_read=False):
        self.max_age = max_age
        self.min_hits = min_hits
        self.trackers = []
        self.frame_count = 0
        self.__bg_frame = None
        self.__bg_gray = None

        self.__flush_pipe_on_read = flush_pipe_on_read

        self.__feature_dim = (2048)
        self.__image_shape = (224, 224, 3)

        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)

    number = 0

    def __extract_image_patch(self, image, bbox, patch_shape):

        sx, sy, ex, ey = np.array(bbox).astype(np.int)

        # dx = ex-sx
        # dx = int(.25*dx)

        # dy = ey-sy
        # dy = int(.6*dy)

        dx = 0
        dy = 0

        image = image[sy:ey - dy, sx + dx:ex - dx]
        image = cv2.resize(image, tuple(patch_shape[::-1]))

        # img_yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
        # img_yuv[:, :, 0] = cv2.equalizeHist(img_yuv[:, :, 0])
        # image = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)

        image[0] = cv2.equalizeHist(image[0])
        image[1] = cv2.equalizeHist(image[1])
        image[2] = cv2.equalizeHist(image[2])

        return image

    def __in_pipe_process(self, inference):
        i_dets = inference.get_input()
        frame = i_dets.get_image()
        classes = i_dets.get_classes()
        boxes = i_dets.get_boxes_tlbr(normalized=False)
        masks = i_dets.get_masks()
        bboxes = []

        scores = i_dets.get_scores()
        for i in range(len(classes)):
            if classes[i] == i_dets.get_category('person') and scores[i] > .75:
                bboxes.append(
                    [boxes[i][1], boxes[i][0], boxes[i][3], boxes[i][2]])
        patches = []

        # if self.__bg_frame is None:
        #     self.__bg_frame = frame
        #     self.__bg_gray = cv2.cvtColor(self.__bg_frame, cv2.COLOR_BGR2GRAY)
        #     self.__bg_gray = cv2.GaussianBlur(self.__bg_gray, (5, 5), 0)
        #
        # gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # gray_frame = cv2.GaussianBlur(gray_frame, (5, 5), 0)
        # difference = cv2.absdiff(self.__bg_gray, gray_frame)
        # ret, mask = cv2.threshold(difference, 50, 255, cv2.THRESH_BINARY)
        # mask = np.stack((mask, mask, mask), axis=2)
        #
        # image = np.multiply(frame, mask)
        #
        # inference.get_meta_dict()['mask'] = mask
        # inference.get_meta_dict()['diff_img']=image

        image = frame
        # blur = cv2.GaussianBlur(image, (5, 5), 0)
        # image = cv2.addWeighted(image, 1.5, image, -0.5, 0)
        # image = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS)
        for i in range(len(bboxes)):
            box = bboxes[i]
            # mask = masks[i]
            # mask = np.stack((mask, mask, mask), axis=2)
            # image = np.multiply(frame, mask)
            patch = self.__extract_image_patch(image, box,
                                               self.__image_shape[:2])
            if patch is None:
                print("WARNING: Failed to extract image patch: %s." % str(box))
                patch = np.random.uniform(0., 255.,
                                          self.__image_shape).astype(np.uint8)
            patches.append(patch)

        inference.set_data(patches)
        inference.get_meta_dict()['bboxes'] = bboxes
        return inference

    def __out_pipe_process(self, inference):
        f_vecs = inference.get_result()

        # print(f_vecs.shape)
        inference = inference.get_meta_dict()['inference']
        bboxes = inference.get_meta_dict()['bboxes']
        self.frame_count = min(self.frame_count + 1, 1000)

        matched, unmatched_dets, unmatched_trks = KNNTracker.associate_detections_to_trackers(
            f_vecs, self.trackers, bboxes, distance_threshold=0.575)
        if bboxes:

            # # update matched trackers with assigned detections
            # for t, trk in enumerate(self.trackers):
            #     if (t not in unmatched_trks):
            #         d = matched[np.where(matched[:, 1] == t)[0], 0][0]
            #         trk.update(bboxes[d], f_vecs[d])  ## for dlib re-intialize the trackers ?!

            # update matched trackers with assigned detections
            for trk in self.trackers:
                if (trk.get_id() not in unmatched_trks):
                    d = matched[np.where(matched[:, 1] == trk.get_id())[0],
                                0][0]
                    trk.update(
                        bboxes[d],
                        f_vecs[d])  ## for dlib re-intialize the trackers ?!

            # create and initialise new trackers for unmatched detections
            for i in unmatched_dets:
                trk = KNNTracker(bboxes[i], f_vecs[i])
                self.trackers.append(trk)

        i = len(self.trackers)
        ret = []
        for trk in reversed(self.trackers):
            d = trk.get_bbox()
            if (trk.get_hit_streak() >=
                    self.min_hits):  # or self.frame_count <= self.min_hits):
                ret.append(
                    np.concatenate(
                        ([int(i) for i in d], [trk.get_id()])).reshape(
                            1, -1))  # +1 as MOT benchmark requires positive
            i -= 1
            # remove dead tracklet
            if (trk.get_time_since_update() > self.max_age):
                self.trackers.pop(i)

        if (len(ret) > 0):
            inference.set_result(np.concatenate(ret))
        else:
            inference.set_result(np.empty((0, 5)))
        return inference

    def get_in_pipe(self):
        return self.__in_pipe

    def get_out_pipe(self):
        return self.__out_pipe

    def use_session_runner(self, session_runner):
        self.__session_runner = session_runner
        # self.__encoder = ResNet50ExtractorAPI("", True)
        self.__encoder = MarsExtractorAPI(flush_pipe_on_read=True)
        self.__encoder.use_session_runner(session_runner)
        self.__enc_in_pipe = self.__encoder.get_in_pipe()
        self.__enc_out_pipe = self.__encoder.get_out_pipe()
        self.__encoder.run()

    def run(self):
        if self.__thread is None:
            self.__thread = Thread(target=self.__run)
            self.__thread.start()

    def __run(self):
        while self.__thread:

            if self.__in_pipe.is_closed():
                self.__out_pipe.close()
                return

            ret, inference = self.__in_pipe.pull(self.__flush_pipe_on_read)
            if ret:
                self.__job(inference)
            else:
                self.__in_pipe.wait()

    def __job(self, inference):
        self.__enc_in_pipe.push(
            Inference(inference.get_data(),
                      meta_dict={'inference': inference},
                      return_pipe=self.__out_pipe))
Esempio n. 23
0
class MarsExtractorAPI:
    def __init__(self, graph_prefix='', flush_pipe_on_read=False):
        self.__flush_pipe_on_read = flush_pipe_on_read

        self.__model_path = pretrained_path.get() + "/mars-small128.pb"

        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)

        if not graph_prefix:
            self.__graph_prefix = ''
        else:
            self.__graph_prefix = graph_prefix + '/'

    def get_input_shape(self):
        return self.__image_shape

    def __preprocess(self, original):
        preprocessed = cv2.resize(original,
                                  tuple(self.__image_shape[:2][::-1]))
        # preprocessed = resnet50.preprocess_input(preprocessed)
        return preprocessed

    def __in_pipe_process(self, inference):
        images = inference.get_input()
        if type(images) is not list:
            images = [images]
        data = []
        for img in images:
            data.append(self.__preprocess(img))
        # data = np.array(data)
        inference.set_data(data)
        return inference

    def __out_pipe_process(self, result):
        result, inference = result
        inference.set_result(result)
        return inference

    def get_in_pipe(self):
        return self.__in_pipe

    def get_out_pipe(self):
        return self.__out_pipe

    def use_session_runner(self, session_runner):
        self.__session_runner = session_runner
        self.__tf_sess = session_runner.get_session()

        with self.__tf_sess.graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(self.__model_path, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name=self.__graph_prefix)

        self.__input_var = tf.get_default_graph().get_tensor_by_name(
            self.__graph_prefix + "images:0")
        self.__output_var = tf.get_default_graph().get_tensor_by_name(
            self.__graph_prefix + "features:0")

        assert len(self.__output_var.get_shape()) == 2
        assert len(self.__input_var.get_shape()) == 4

        self.__feature_dim = self.__output_var.get_shape().as_list()[-1]
        self.__image_shape = self.__input_var.get_shape().as_list()[1:]
        # print(self.__image_shape)

    def run(self):
        if self.__thread is None:
            self.__thread = Thread(target=self.__run)
            self.__thread.start()

    def __run(self):
        while self.__thread:

            if self.__in_pipe.is_closed():
                self.__out_pipe.close()
                return

            ret, inference = self.__in_pipe.pull(self.__flush_pipe_on_read)
            if ret:
                self.__session_runner.get_in_pipe().push(
                    SessionRunnable(self.__job, inference))
            else:
                self.__in_pipe.pull_wait()

    def __job(self, inference):
        x = inference.get_data()
        if len(x) > 0:
            self.__out_pipe.push(
                (self.__tf_sess.run(self.__output_var,
                                    feed_dict={self.__input_var:
                                               x}), inference))
        else:
            self.__out_pipe.push((np.zeros((0, self.__feature_dim),
                                           np.float32), inference))
Esempio n. 24
0
class TFObjectDetectionAPI(ObjectDetectorInterface):
    outPipe = None
    inPipe = None

    objectTypes = {
        0: None,
        1: 'person',
        2: 'bicycle',
        3: 'car',
        4: 'motorcycle',
        5: 'airplane',
        6: 'bus',
        7: 'train',
        8: 'truck',
        9: 'boat',
        10: 'traffic light',
        11: 'fire hydrant',
        13: 'stop sign',
        14: 'parking meter',
        15: 'bench',
        16: 'bird',
        17: 'cat',
        18: 'dog',
        19: 'horse',
        20: 'sheep',
        21: 'cow',
        22: 'elephant',
        23: 'bear',
        24: 'zebra',
        25: 'giraffe',
        27: 'backpack',
        28: 'umbrella',
        31: 'handbag',
        32: 'tie',
        33: 'suitcase',
        34: 'frisbee',
        35: 'skis',
        36: 'snowboard',
        37: 'sports ball',
        38: 'kite',
        39: 'baseball bat',
        40: 'baseball glove',
        41: 'skateboard',
        42: 'surfboard',
        43: 'tennis racket',
        44: 'bottle',
        46: 'wine glass',
        47: 'cup',
        48: 'fork',
        49: 'knife',
        50: 'spoon',
        51: 'bowl',
        52: 'banana',
        53: 'apple',
        54: 'sandwich',
        55: 'orange',
        56: 'broccoli',
        57: 'carrot',
        58: 'hot dog',
        59: 'pizza',
        60: 'donut',
        61: 'cake',
        62: 'chair',
        63: 'couch',
        64: 'potted plant',
        65: 'bed',
        67: 'dining table',
        70: 'toilet',
        72: 'tv',
        73: 'laptop',
        74: 'mouse',
        75: 'remote',
        76: 'keyboard',
        77: 'cell phone',
        78: 'microwave',
        79: 'oven',
        80: 'toaster',
        81: 'sink',
        82: 'refrigerator',
        84: 'book',
        85: 'clock',
        86: 'vase',
        87: 'scissors',
        88: 'teddy bear',
        89: 'hair drier',
        90: 'toothbrush',
    }

    thread = None

    def __init__(self, model_name=PRETRAINED_ssd_mobilenet_v1_coco_2017_11_17):
        self.dir_path = dirname(realpath(__file__))

        self.model_path = self.dir_path + '/object_detection/pretrained/'
        self.model_file = model_name + '.tar.gz'
        self.download_base = 'http://download.tensorflow.org/models/object_detection/'
        self.path_to_frozen_graph = self.model_path + model_name + '/frozen_inference_graph.pb'
        path_to_labels = os.path.join(self.dir_path + '/object_detection/data',
                                      'mscoco_label_map.pbtxt')
        self.class_count = 90
        if not path.exists(self.path_to_frozen_graph):
            self.__download()

        self.__load()
        self.label_map = label_map_util.load_labelmap(path_to_labels)
        self.categories = label_map_util.convert_label_map_to_categories(
            self.label_map,
            max_num_classes=self.class_count,
            use_display_name=True)
        self.category_index = label_map_util.create_category_index(
            self.categories)

        self.inPipe = Pipe()
        self.outPipe = Pipe()

    def start(self):
        if self.thread is None:
            self.thread = Thread(target=self.__start)
            self.thread.start()

    def stop(self):
        if self.thread is not None:
            self.thread.stop()
            self.thread = None

    def getInPipe(self):
        return self.inPipe

    def getOutPipe(self):
        return self.outPipe

    def __download(self):
        try:
            os.mkdir(self.model_path)
        except:
            pass
        opener = urllib.request.URLopener()
        opener.retrieve(self.download_base + self.model_file,
                        self.model_path + self.model_file)
        tar_file = tarfile.open(self.model_path + self.model_file)
        for file in tar_file.getmembers():
            file_name = os.path.basename(file.name)
            if 'frozen_inference_graph.pb' in file_name:
                tar_file.extract(file, self.model_path)

    def __load(self):
        self.detection_graph = tf.Graph()
        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(self.path_to_frozen_graph, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

    def __start(self, types=None):

        with self.detection_graph.as_default():
            with tf.Session(graph=self.detection_graph) as sess:

                #######################################################Crowd#######################################

                # new_saver = tf.train.import_meta_graph("/home/allahbaksh/crowd_count-tf-B/src/model.ckpt.meta")
                # new_saver.restore(sess, tf.train.latest_checkpoint("/home/allahbaksh/crowd_count-tf-B/src/"))
                # graph = tf.get_default_graph()
                # op_to_restore = graph.get_tensor_by_name("add_12:0")
                # x = graph.get_tensor_by_name('Placeholder:0')
                #
                # fps = 25
                #
                # counter = 0
                avg = 0
                #
                # crowd_count = []

                #######################################################Crowd#######################################

                while True:
                    ret, image_np = self.inPipe.pull()
                    if not ret:
                        continue

                    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                    image_np_expanded = np.expand_dims(image_np, axis=0)
                    image_tensor = self.detection_graph.get_tensor_by_name(
                        'image_tensor:0')
                    # Each box represents a part of the image where a particular object was detected.
                    boxes = self.detection_graph.get_tensor_by_name(
                        'detection_boxes:0')
                    # Each score represent how level of confidence for each of the objects.
                    # Score is shown on the result image, together with the class label.
                    scores = self.detection_graph.get_tensor_by_name(
                        'detection_scores:0')
                    classes = self.detection_graph.get_tensor_by_name(
                        'detection_classes:0')
                    num_detections = self.detection_graph.get_tensor_by_name(
                        'num_detections:0')
                    # Actual detection.
                    (boxes, scores, classes, num_detections) = sess.run(
                        [boxes, scores, classes, num_detections],
                        feed_dict={image_tensor: image_np_expanded})

                    decisionInstances = []
                    height, width = image_np.shape[0], image_np.shape[1]

                    boxes = np.squeeze(boxes)
                    classes = np.squeeze(classes)
                    scores = np.squeeze(scores)

                    for i in range(len(classes)):
                        y_tl, x_tl, y_br, x_br = boxes[i] * [
                            height, width, height, width
                        ]

                        try:
                            objType = self.objectTypes[classes[i]]
                            if types is not None:
                                if objType not in types:
                                    continue
                        except:
                            if types is not None:
                                continue
                            objType = 'undefined'
                        decisionInstances.append(
                            DecisionInstance(
                                InstanceType(objType, classes[i]), scores[i],
                                InferenceBounds(x_tl, y_tl, x_br, y_br)))

                    annotatedImage = image_np.copy()
                    vis_util.visualize_boxes_and_labels_on_image_array(
                        annotatedImage,
                        boxes,
                        classes.astype(np.int32),
                        scores,
                        self.category_index,
                        use_normalized_coordinates=True,
                        line_thickness=2)

                    #######################################################Crowd#######################################

                    # img = image_np.copy()
                    # img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
                    # img = np.array(img)
                    # img = (img - 127.5) / 128
                    #
                    # x_in = np.reshape(img, (1, img.shape[0], img.shape[1], 1))
                    # x_in = np.float32(x_in)
                    # y_pred = []
                    # y_pred = sess.run(op_to_restore, feed_dict={x: x_in})
                    # sum = np.absolute(np.int32(np.sum(y_pred)))
                    #
                    # crowd_count.append(sum)
                    # if len(crowd_count) > 20:
                    #     avg = int(np.average(crowd_count) * 0.5)
                    #     crowd_count = []
                    #
                    # # if counter <= fps:
                    # #     avg += sum
                    # # else:
                    # #     counter = 0
                    # #     avg = np.int32(avg / fps)
                    # #     print("AVG ###########################################",  avg)
                    # #     avg = 0
                    # # counter+=1

                    #######################################################Crowd#######################################

                    inference = Inference(image_np, decisionInstances,
                                          annotatedImage, avg)
                    self.outPipe.push(inference)
Esempio n. 25
0
    session = tf.Session()

    image = cv2.resize(image, tuple(image_shape[:2][::-1]))
    image = np.expand_dims(image, axis=0)

    # K.set_session(session)

    extractor = ResNet50ExtractorAPI('rn50_api', True)
    ip = extractor.get_in_pipe()
    # op = extractor.get_out_pipe()
    extractor.use_session_runner(session_runner)

    session_runner.start()
    extractor.run()

    ret_pipe = Pipe()

    # for i in range(1000):
    i = 0
    while True:
        ret, image = cap.read()
        if not ret:
            continue
        ip.push(Inference(image, ret_pipe, {}))

        ret, feature_inference = ret_pipe.pull()
        if ret:
            print(feature_inference.get_result().shape)
        else:
            ret_pipe.wait()
Esempio n. 26
0
class YOLOObjectDetectionAPI():
    _defaults = {
        "model_path": pretrained_path.get() + '/yolo_v3.h5',
        "anchors_path": data_path.get() + '/yolo_anchors.txt',
        "classes_path": data_path.get() + '/coco.names',
        "score": 0.3,
        "iou": 0.45,
        "model_image_size": (416, 416),
        "gpu_num": 1,
    }

    @staticmethod
    def __get_dir_path():
        return dirname(realpath(__file__))

    @classmethod
    def get_defaults(cls, n):
        if n in cls._defaults:
            return cls._defaults[n]
        else:
            return "Unrecognized attribute name '" + n + "'"

    def __init__(self, graph_prefix=None, flush_pipe_on_read=False):
        self.__dict__.update(self._defaults)  # set up default values
        # self.__dict__.update(kwargs) # and update with user overrides
        YOLOObjectDetectionAPI.class_names, self.__category_dict = self._get_class(
        )
        YOLOObjectDetectionAPI.anchors = self._get_anchors()
        self.__graph_prefix = graph_prefix
        self.__flush_pipe_on_read = flush_pipe_on_read
        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)

        self.__run_session_on_thread = False

    def use_threading(self, run_on_thread=True):
        self.__run_session_on_thread = run_on_thread

    def use_session_runner(self, session_runner):
        self.__session_runner = session_runner
        K.set_session(session_runner.get_session())
        self.__tf_sess = K.get_session()
        self.boxes, self.scores, self.classes = self.generate()

    def __in_pipe_process(self, inference):

        image = inference.get_input()
        pil_image = Image.fromarray(image)

        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                pil_image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (pil_image.width - (pil_image.width % 32),
                              pil_image.height - (pil_image.height % 32))
            boxed_image = letterbox_image(pil_image, new_image_size)
        data = np.array(boxed_image, dtype='float32')

        data /= 255.
        data = np.expand_dims(data, 0)  # Add batch dimension.

        inference.set_data(data)
        inference.get_meta_dict()['PIL'] = pil_image
        return inference
        # return image

    def __out_pipe_process(self, result):
        (out_boxes, out_classes, out_scores), inference = result
        out_boxes = np.array([[0 if y < 0 else y for y in x]
                              for x in out_boxes])
        result = InferedDetections(inference.get_input(),
                                   len(out_boxes),
                                   out_boxes,
                                   out_classes,
                                   out_scores,
                                   masks=None,
                                   is_normalized=False,
                                   get_category_fnc=self.get_category,
                                   annotator=self.annotate)
        inference.set_result(result)
        return inference

    def get_in_pipe(self):
        return self.__in_pipe

    def get_out_pipe(self):
        return self.__out_pipe

    def _get_class(self):
        dir_path = YOLOObjectDetectionAPI.__get_dir_path()
        classes_path = os.path.expanduser(self.classes_path)
        with open(classes_path) as f:
            class_names = f.readlines()
        class_names = [c.strip() for c in class_names]
        category_dict = {}
        for id, name in enumerate(class_names):
            category_dict[id] = name
            category_dict[name] = id
        return class_names, category_dict

    def _get_anchors(self):
        dir_path = YOLOObjectDetectionAPI.__get_dir_path()
        anchors_path = os.path.expanduser(self.anchors_path)
        with open(anchors_path) as f:
            anchors = f.readline()
        anchors = [float(x) for x in anchors.split(',')]
        return np.array(anchors).reshape(-1, 2)

    def generate(self):
        dir_path = YOLOObjectDetectionAPI.__get_dir_path()
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith(
            '.h5'), 'Keras model or weights must be a .h5 file.'

        # Load model, or construct model and load weights.
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        is_tiny_version = num_anchors == 6  # default setting
        try:
            self.yolo_model = load_model(model_path, compile=False)
        except:
            self.yolo_model = tiny_yolo_body(Input(shape=(None, None, 3)), num_anchors // 2, num_classes) \
                if is_tiny_version else yolo_body(Input(shape=(None, None, 3)), num_anchors // 3, num_classes)
            self.yolo_model.load_weights(
                self.model_path)  # make sure model, anchors and classes match
        else:
            assert self.yolo_model.layers[-1].output_shape[-1] == \
                   num_anchors / len(self.yolo_model.output) * (num_classes + 5), \
                'Mismatch between model and given anchor and class sizes'

        # print('{} model, anchors, and classes loaded.'.format(model_path))

        # Generate colors for drawing bounding boxes.
        hsv_tuples = [(x / len(self.class_names), 1., 1.)
                      for x in range(len(self.class_names))]
        YOLOObjectDetectionAPI.colors = list(
            map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        YOLOObjectDetectionAPI.colors = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                YOLOObjectDetectionAPI.colors))
        np.random.seed(10101)  # Fixed seed for consistent colors across runs.
        np.random.shuffle(YOLOObjectDetectionAPI.colors
                          )  # Shuffle colors to decorrelate adjacent classes.
        np.random.shuffle(YOLOObjectDetectionAPI.colors
                          )  # Shuffle colors to decorrelate adjacent classes.
        np.random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        if self.gpu_num >= 2:
            self.yolo_model = multi_gpu_model(self.yolo_model,
                                              gpus=self.gpu_num)

        boxes, scores, classes = yolo_eval(self.yolo_model.output,
                                           self.anchors,
                                           len(self.class_names),
                                           self.input_image_shape,
                                           score_threshold=self.score,
                                           iou_threshold=self.iou)
        return boxes, scores, classes

    def freeze_session(self,
                       session,
                       keep_var_names=None,
                       output_names=None,
                       clear_devices=True):
        """
        Freezes the state of a session into a pruned computation graph.

        Creates a new computation graph where variable nodes are replaced by
        constants taking their current value in the session. The new graph will be
        pruned so subgraphs that are not necessary to compute the requested
        outputs are removed.
        @param session The TensorFlowsion(detected_boxes, confidence_threshold=FLAGS.conf_threshold,
                                         iou_threshold=FLAGS.iou_threshold)

    draw_boxes(filtered_boxes, img, classes, (FLAGS.size, FLAGS.size))

    img.save(FLAGS.output_img) session to be frozen.
        @param keep_var_names A list of variable names that should not be frozen,
                              or None to freeze all the variables in the graph.
        @param output_names Names of the relevant graph outputs.
        @param clear_devices Remove the device directives from the graph for better portability.
        @return The frozen graph definition.
        """
        from tensorflow.python.framework.graph_util import convert_variables_to_constants
        graph = session.graph
        with graph.as_default():
            freeze_var_names = list(
                set(v.op.name
                    for v in tf.global_variables()).difference(keep_var_names
                                                               or []))
            output_names = output_names or []
            output_names += [v.op.name for v in tf.global_variables()]
            input_graph_def = graph.as_graph_def()
            if clear_devices:
                for node in input_graph_def.node:
                    node.device = ""
            frozen_graph = convert_variables_to_constants(
                session, input_graph_def, output_names, freeze_var_names)
            return frozen_graph

    def run(self):
        if self.__thread is None:
            self.__thread = Thread(target=self.__run)
            self.__thread.start()

    def __run(self):
        while self.__thread:
            if self.__in_pipe.is_closed():
                self.__out_pipe.close()
                return

            ret, inference = self.__in_pipe.pull(self.__flush_pipe_on_read)
            if ret:
                self.__session_runner.get_in_pipe().push(
                    SessionRunnable(
                        self.__job,
                        inference,
                        run_on_thread=self.__run_session_on_thread))
            else:
                self.__in_pipe.pull_wait()

    def __job(self, inference):
        data = inference.get_data()
        pil_image = inference.get_meta_dict()['PIL']
        # image = inference.get_input()
        out_boxes, out_scores, out_classes = self.__tf_sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: data,
                self.input_image_shape: [pil_image.size[1], pil_image.size[0]],
            })

        self.__out_pipe.push(((out_boxes, out_classes, out_scores), inference))

    def close_session(self):
        self.__tf_sess.close()

    @staticmethod
    def annotate(inference):
        annotated = inference.image.copy()
        image = Image.fromarray(inference.get_image())
        font = ImageFont.truetype(font='arial.ttf',
                                  size=np.floor(3e-2 * image.size[1] +
                                                0.5).astype('int32'))
        thickness = 1  #(image.size[0] + image.size[1]) // 300
        for i, c in reversed(list(enumerate(inference.get_classes()))):
            predicted_class = YOLOObjectDetectionAPI.class_names[c]
            box = inference.get_boxes_tlbr(normalized=False)[i]
            score = inference.get_scores()[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            # print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=YOLOObjectDetectionAPI.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=YOLOObjectDetectionAPI.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw
        return np.array(image)

    def get_category(self, category):
        return self.__category_dict[category]
Esempio n. 27
0
class Sort:
    def __init__(self, max_age=120, min_hits=3, flush_pipe_on_read=False):
        """
        Sets key parameters for SORT
        """
        self.max_age = max_age
        self.min_hits = min_hits
        self.trackers = []
        self.frame_count = 0

        self.__flush_pipe_on_read = flush_pipe_on_read

        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__outpass_pipe = Pipe(self.__out_pipe_process)

    def __in_pipe_process(self, inference):
        detections = inference.get_boxes_tlbr(normalized=False)
        frame = inference.get_input()
        classes = inference.get_classes()
        person_detections = []
        scores = inference.get_scores()
        for i in range(len(classes)):
            if classes[i] == inference.get_category(
                    'person') and scores[i] > .5:
                person_detections.append([
                    detections[i][1], detections[i][0], detections[i][3],
                    detections[i][2]
                ])
        return (frame, person_detections)

    def __out_pipe_process(self, inference):
        image, f_vecs, bboxes = inference

        if bboxes:
            matched, unmatched_dets, unmatched_trks = Tracker.associate_detections_to_trackers(
                f_vecs, self.trackers)

            # update matched trackers with assigned detections
            for t, trk in enumerate(self.trackers):
                if (t not in unmatched_trks):
                    d = matched[np.where(matched[:, 1] == t)[0], 0][0]
                    trk.update(
                        bboxes[d],
                        f_vecs[d])  ## for dlib re-intialize the trackers ?!

            # create and initialise new trackers for unmatched detections
            for i in unmatched_dets:
                trk = Tracker(bboxes[i], f_vecs[i])
                self.trackers.append(trk)

        i = len(self.trackers)
        ret = []
        for trk in reversed(self.trackers):
            # if bboxes == []:
            # trk.update([], img)
            d = trk.get_bbox()
            if (trk.get_hit_streak() >= self.min_hits
                    or self.frame_count <= self.min_hits):
                ret.append(np.concatenate((d, [trk.get_id()])).reshape(
                    1, -1))  # +1 as MOT benchmark requires positive
            i -= 1
            # remove dead tracklet
            if (trk.get_time_since_update() > self.max_age):
                self.trackers.pop(i)
        print(len(self.trackers))
        if (len(ret) > 0):
            return np.concatenate(ret), image
        return np.empty((0, 5)), image

    def get_in_pipe(self):
        return self.__in_pipe

    def get_out_pipe(self):
        return self.__out_pipe

    def use_session_runner(self, model, session_runner):
        self.__session_runner = session_runner
        # K.set_session(session_runner.get_session())
        # self.__tf_sess = K.get_session()
        self.__image_encoder = ImageEncoder(model, session_runner)

    def run(self):
        if self.__thread is None:
            self.__thread = Thread(target=self.__run)
            self.__thread.start()

    def __run(self):
        while self.__thread:

            if self.__in_pipe.is_closed():
                self.__out_pipe.close()
                return

            ret, data_tuple = self.__in_pipe.pull(self.__flush_pipe_on_read)
            if ret:
                self.data_tuple = data_tuple
                self.__session_runner.add_job(self.__job())
            else:
                self.__in_pipe.pull_wait()

    def __job(self):
        f_vecs = self.__image_encoder.extract_features(self.data_tuple[0],
                                                       self.data_tuple[1])
        self.__out_pipe.push((self.data_tuple[0], f_vecs, self.data_tuple[1]))

    def update(self, image, bboxes):
        """
Esempio n. 28
0
class ImageEncoder:
    @staticmethod
    def __fetch_model_path(model_name):
        return "../resources/networks/" + model_name

    def __init__(self,
                 session_runner,
                 model_name=PRETRAINED_mars_small128,
                 graph_prefix=None):
        self.__path_to_frozen_graph = self.__fetch_model_path(model_name)

        if not graph_prefix:
            self.__graph_prefix = ''
        else:
            self.__graph_prefix = graph_prefix + '/'

        self.__session_runner = session_runner
        self.__tf_sess = session_runner.get_session()
        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)

        self.init_graph()

    def get_in_pipe(self):
        return self.__in_pipe

    def get_out_pipe(self):
        return self.__out_pipe

    def __in_pipe_process(self, obj):
        return obj

    def __out_pipe_process(self, obj):
        return obj

    def init_graph(self):

        with self.__tf_sess.graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(self.__path_to_frozen_graph, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name=self.__graph_prefix)

        self.input_var = self.__tf_sess.graph.get_tensor_by_name(
            self.__graph_prefix + 'images:0')
        self.output_var = self.__tf_sess.graph.get_tensor_by_name(
            self.__graph_prefix + 'features:0')

        assert len(self.output_var.get_shape()) == 2
        assert len(self.input_var.get_shape()) == 4

        self.feature_dim = self.output_var.get_shape().as_list()[-1]
        self.image_shape = self.input_var.get_shape().as_list()[1:]

    def run(self):
        if self.__thread is None:
            self.__thread = Thread(target=self.__run)
            self.__thread.start()

    def __run(self):
        while self.__thread:

            if self.__in_pipe.is_closed():
                self.__out_pipe.close()
                return

            ret, data_x = self.__in_pipe.pull()
            if ret:
                self.data_x = data_x
                self.__session_runner.add_job(self.__job())
            else:
                self.__in_pipe.pull_wait()

    def __job(self):
        __tracking_features = np.zeros((len(self.data_x), self.feature_dim),
                                       np.float32)
        self._run_in_batches(
            lambda x: self.__tf_sess.run(self.output_var, feed_dict=x),
            {self.input_var: self.data_x},
            __tracking_features,
            batch_size=32)
        self.__out_pipe.push(__tracking_features)

    def _run_in_batches(self, f, data_dict, out, batch_size):
        data_len = len(out)
        num_batches = int(data_len / batch_size)

        s, e = 0, 0
        for i in range(num_batches):
            s, e = i * batch_size, (i + 1) * batch_size
            batch_data_dict = {k: v[s:e] for k, v in data_dict.items()}
            out[s:e] = f(batch_data_dict)
        if e < len(out):
            batch_data_dict = {k: v[e:] for k, v in data_dict.items()}
            out[e:] = f(batch_data_dict)
            if not ret:
                continue
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + cv2.imencode('.jpg', image)[1].tostring() + b'\r\n')

    def gen_age_api(self):
        while True:
            age_in_pipe.wait()
            ret, image = age_in_pipe.pull()
            if not ret:
                continue
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + cv2.imencode('.jpg', image)[1].tostring() + b'\r\n')


image_in_pipe = Pipe()
tracking_in_pipe = Pipe()
age_in_pipe = Pipe()
flag_in_pipe = Pipe()
zone_detection_in_pipe = Pipe()
object_retail=Pipeline()
dict=None
@app.route('/live_stock_feed')
def live_stock_feed():
    return Response(object_retail.gen_analysis(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/live_tracking_feed')
def live_tracking_feed():
    return Response(object_retail.gen_tracking(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')
Esempio n. 30
0
class AgeDetection(object):
    '''
    Path to the trained model path for age_detection
    '''
    WRN_WEIGHTS_PATH = age_model_path.get() + '/age_model.h5'

    def __init__(self, flush_pipe_on_read=False):
        self.__detector = None
        self.__face_size = 64
        self.__flush_pipe_on_read = flush_pipe_on_read
        self.__thread = None
        self.__in_pipe = Pipe(self.__in_pipe_process)
        self.__out_pipe = Pipe(self.__out_pipe_process)

    def crop_face(self, imgarray, section, margin=40, size=64):
        """
        :param imgarray: full image
        :param section: face detected area (x, y, w, h)
        :param margin: add some margin to the face detected area to include a full head
        :param size: the result image resolution with be (size x size)
        :return: resized image in numpy array with shape (size x size x 3)
        """
        img_h, img_w, _ = imgarray.shape
        if section is None:
            section = [0, 0, img_w, img_h]
        (x, y, w, h) = section
        margin = int(min(w, h) * margin / 100)
        x_a = x - margin
        y_a = y - margin
        x_b = x + w + margin
        y_b = y + h + margin
        if x_a < 0:
            x_b = min(x_b - x_a, img_w - 1)
            x_a = 0
        if y_a < 0:
            y_b = min(y_b - y_a, img_h - 1)
            y_a = 0
        if x_b > img_w:
            x_a = max(x_a - (x_b - img_w), 0)
            x_b = img_w
        if y_b > img_h:
            y_a = max(y_a - (y_b - img_h), 0)
            y_b = img_h
        cropped = imgarray[y_a:y_b, x_a:x_b]
        resized_img = cv2.resize(cropped, (size, size),
                                 interpolation=cv2.INTER_AREA)
        resized_img = np.array(resized_img)
        return resized_img, (x_a, y_a, x_b - x_a, y_b - y_a)

    def __in_pipe_process(self, inference):
        '''
        :param inference: inference object with input frame
        :return: inference: inference object with age_inference object containing face bbox details,
                data condaining np_array of all faces detected
        '''
        frame = inference.get_input()
        faces = self.__detector.detect_faces(frame)
        faces = list_filter(faces, 0.90)
        # print("faces " ,faces)
        bboxes = []
        face_imgs = np.empty(
            (len(faces), self.__face_size, self.__face_size, 3))
        for i, face in enumerate(faces):
            face_img, cropped = self.crop_face(frame,
                                               face[:4],
                                               margin=40,
                                               size=self.__face_size)
            (x, y, w, h) = cropped
            bboxes.append([x, y, x + w, y + h])
            # face_img = (face_img/255)-0.5
            # cv2.rectangle(self.image_bounding_boxes, (x, y), (x + w, y + h), (255, 200, 0), 2)
            face_imgs[i, :, :, :] = face_img
        age_inference = AgeInference(frame, bboxes=bboxes)
        inference.set_result(age_inference)
        inference.set_data(face_imgs)
        return inference

    def __out_pipe_process(self, inference):
        '''
        :param inference: inference object with faces np array in data,
                age_inference object with bboxes in result
        :return: inference: inference object with age_inference object in result after processing
                    face np array from data for age, gender and ethnicity
        '''
        # placeholder for cropped faces
        results, inference = inference
        age_inference = inference.get_result()
        if results is not None:
            predicted_genders = results[0]
            ages = np.arange(0, 101).reshape(101, 1)
            predicted_ages = results[1].dot(ages).flatten()
            predicted_ethnicity = np.argmax(results[2], axis=1)
            age_inference.set_ages(predicted_ages)
            age_inference.set_genders(predicted_genders)
            age_inference.set_ethnicity(predicted_ethnicity)
        inference.set_result(age_inference)
        return inference

    def get_in_pipe(self):
        return self.__in_pipe

    def get_out_pipe(self):
        return self.__out_pipe

    def use_threading(self, run_on_thread=True):
        self.__run_session_on_thread = run_on_thread

    def use_session_runner(self, session_runner):
        self.__session_runner = session_runner
        K.set_session(self.__session_runner.get_session())
        self.__tf_sess = K.get_session()
        self.__detector = MTCNN(session_runner=self.__session_runner)

        with self.__tf_sess.as_default():
            self.__model = WideResNet(self.__face_size)()
            self.__model.load_weights(AgeDetection.WRN_WEIGHTS_PATH)

    def run(self):
        if self.__thread is None:
            self.__thread = Thread(target=self.__run)
            self.__thread.start()

    def __run(self):
        while self.__thread:

            if self.__in_pipe.is_closed():
                self.__out_pipe.close()
                return

            ret, inference = self.__in_pipe.pull(self.__flush_pipe_on_read)
            if ret:
                self.__session_runner.get_in_pipe().push(
                    SessionRunnable(self.__job, inference))
            else:
                self.__in_pipe.pull_wait()

    def __job(self, inference):
        '''
        :param inference: run the model on data from inference object and push it to out_pipe
        :return:
        '''
        try:
            self.__out_pipe.push(
                (self.__model.predict(inference.get_data()), inference))
        except:
            self.__out_pipe.push((None, inference))