Beispiel #1
0
    def classify(self, path):
        imgPaths = os.listdir(path)
        cnt = 0
        for imgPath in imgPaths:
            tot = 0
            img = read_imgfile(os.path.join(path, imgPath), self.imgW,
                               self.imgH)
            humans = self.getHumans(img)
            imgcv = cv2.imread(os.path.join(path, imgPath))
            res, delta = draw_humans(imgcv, humans)
            tot += delta

            if (len(humans) == 0):
                imgp90 = cv2.flip(cv2.transpose(img), 1)
                humans = self.getHumans(imgp90)
                imgcvp90 = cv2.flip(cv2.transpose(imgcv), 1)
                res, delta = draw_humans(imgcvp90, humans)
                delta = len(humans) - delta
                tot += delta

                imgn90 = cv2.flip(cv2.transpose(img), 0)
                humans = self.getHumans(imgn90)
                imgcvn90 = cv2.flip(cv2.transpose(imgcv), 0)
                res, delta = draw_humans(imgcvn90, humans)
                delta = len(humans) - delta
                tot += delta
            if tot > 0:
                cnt += 1

        print(cnt)
        if cnt >= self.th * len(os.listdir(path)):
            return 1
        return 0
Beispiel #2
0
def cb_showimg(img, preprocessed, heatMat, pafMat, humans, show_process=False):
    global fps_time

    # display
    image = img
    image_h, image_w = image.shape[:2]
    image = draw_humans(image, humans)

    scale = 480.0 / image_h
    newh, neww = 480, int(scale * image_w + 0.5)

    image = cv2.resize(image, (neww, newh), interpolation=cv2.INTER_AREA)

    if show_process:
        process_img = CocoPoseLMDB.display_image(preprocessed,
                                                 heatMat,
                                                 pafMat,
                                                 as_numpy=True)
        process_img = cv2.resize(process_img, (640, 480),
                                 interpolation=cv2.INTER_AREA)

        canvas = np.zeros([480, 640 + neww, 3], dtype=np.uint8)
        canvas[:, :640] = process_img
        canvas[:, 640:] = image
    else:
        canvas = image

    cv2.putText(canvas, "FPS: %f" % (1.0 / (time.time() - fps_time)), (10, 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
    cv2.imshow('openpose', canvas)

    fps_time = time.time()
    def detect_parts(self, image):
        start = time.time()

        imgpath = self._download_image(image)
        image, thumbnail, input_width, input_height = self.read_img(
            imgpath, 368, 368)
        start = print_time("image loaded in: ", start)

        if not SmartBodyCrop.initialized:
            print("Loading the model...")
            self.load_graph_def()

        with tf.Session() as sess:
            inputs = tf.get_default_graph().get_tensor_by_name('inputs:0')
            heatmaps_tensor = tf.get_default_graph().get_tensor_by_name(
                'Mconv7_stage6_L2/BiasAdd:0')
            pafs_tensor = tf.get_default_graph().get_tensor_by_name(
                'Mconv7_stage6_L1/BiasAdd:0')

            heatMat, pafMat = sess.run([heatmaps_tensor, pafs_tensor],
                                       feed_dict={inputs: image})

            start = print_time("tf session executed in: ", start)

            humans = estimate_pose(heatMat[0], pafMat[0])
            start = print_time("pose estimated in: ", start)

            # display
            img1 = draw_humans(thumbnail, humans)
            return img1
Beispiel #4
0
    def detect_parts(self, imgpath):
        img1_raw = Image.open(imgpath)
        img1 = np.asarray(img1_raw)
        input_width, input_height = img1.shape[0], img1.shape[1]
        tf.reset_default_graph()

        graph_def = graph_pb2.GraphDef()
        with open('models/optimized_openpose.pb', 'rb') as f:
            graph_def.ParseFromString(f.read())
        tf.import_graph_def(graph_def, name='')

        inputs = tf.get_default_graph().get_tensor_by_name('inputs:0')
        heatmaps_tensor = tf.get_default_graph().get_tensor_by_name(
            'Mconv7_stage6_L2/BiasAdd:0')
        pafs_tensor = tf.get_default_graph().get_tensor_by_name(
            'Mconv7_stage6_L1/BiasAdd:0')

        image = self.read_img(imgpath, input_width, input_height)

        with tf.Session() as sess:
            heatMat, pafMat = sess.run([heatmaps_tensor, pafs_tensor],
                                       feed_dict={inputs: image})

            heatMat, pafMat = heatMat[0], pafMat[0]
            humans = estimate_pose(heatMat, pafMat)
            # display
            image_h, image_w = img1.shape[:2]
            img1 = draw_humans(img1_raw, humans)

            scale = 480.0 / image_h
            newh, neww = 480, int(scale * image_w + 0.5)

            return img1
Beispiel #5
0
def compute_pose_frame(input_image, sess):
    # Build the network to produce the poses:
    image = cv2.resize(input_image,
                       dsize=(input_height, input_width),
                       interpolation=cv2.INTER_CUBIC)
    global first_time
    if first_time:
        # load pretrained weights
        #print(first_time)
        s = "%dx%d" % (input_node.shape[2], input_node.shape[1])
        ckpts = "./third_party/tf-openpose/models/trained/mobilenet_" + s + "/model-release"
        vars_in_checkpoint = tf.train.list_variables(ckpts)
        var_rest = []
        for el in vars_in_checkpoint:
            var_rest.append(el[0])
        variables = tf.contrib.slim.get_variables_to_restore()
        var_list = [v for v in variables if v.name.split(":")[0] in var_rest]

        loader = tf.train.Saver(var_list=var_list)

        loader.restore(sess, ckpts)

        first_time = False

    # Compute and draw the pose:
    vec = sess.run(net.get_output(name="concat_stage7"),
                   feed_dict={"image:0": [image]})
    pafMat, heatMat = sess.run([
        net.get_output(name=last_layer.format(stage=stage_level, aux=1)),
        net.get_output(name=last_layer.format(stage=stage_level, aux=2))
    ],
                               feed_dict={"image:0": [image]})
    heatMat, pafMat = heatMat[0], pafMat[0]
    humans = estimate_pose(heatMat, pafMat)
    pose_image = np.zeros(tuple(image.shape), dtype=np.uint8)
    pose_image = draw_humans(pose_image, humans)

    # Extract argmax along axis 2:
    heatMat = np.amax(heatMat, axis=2)
    pafMat = np.amax(pafMat, axis=2)

    # Resize input_image, heatMat, pafMat to match pose_image:
    heatMat = cv2.resize(heatMat,
                         dsize=(input_height, input_width),
                         interpolation=cv2.INTER_CUBIC)
    pafMat = cv2.resize(pafMat,
                        dsize=(input_height, input_width),
                        interpolation=cv2.INTER_CUBIC)

    # Make sure there is a third dimension:
    heatMat = np.expand_dims(heatMat, axis=2)
    pafMat = np.expand_dims(pafMat, axis=2)

    # Concatenate input_image, pose_image, heatMat and pafMat:
    pose_image = np.concatenate((image, pose_image, heatMat, pafMat), axis=2)

    return pose_image
Beispiel #6
0
def display(image, humans, heatMat, pafMat):
    print("hahha")
    image_h, image_w = image.shape[:2]
    image = draw_humans(image, humans)

    scale = 480.0 / image_h
    newh, neww = 480, int(scale * image_w + 0.5)

    process_img = CocoPoseLMDB.display_image(image,
                                             heatMat,
                                             pafMat,
                                             as_numpy=True)
    image = cv2.resize(image, (neww, newh), interpolation=cv2.INTER_AREA)

    convas = np.zeros([480, 640 + neww, 3], dtype=np.uint8)
    convas[:, :640] = process_img
    convas[:, 640:] = image

    cv2.imshow('result', convas)
    cv2.waitKey(0)
Beispiel #7
0
def video_write(img, humans):
    # display
    canvas = draw_humans(img, humans)
    stand_degrees = analyse.cal_degrees(humans)
    # print(stand_degrees)

    # show text
    y0, dy = 50, 25
    i = 0
    for person in stand_degrees:
        text = "person:"
        y = y0 + dy * i
        cv2.putText(canvas, text, (5, y), cv2.FONT_HERSHEY_SIMPLEX, 1,
                    (0, 255, 0), 2)
        i = i + 1
        for degree_name, degree in person.items():
            text = "{} = {}°".format(degree_name, analyse.cos_2_degree(degree))
            y = y0 + dy * i
            cv2.putText(canvas, text, (5, y), cv2.FONT_HERSHEY_SIMPLEX, 1,
                        (0, 255, 0), 2)
            i = i + 1
    return canvas
def compute_pose_frame(input_image, sess):
    image = cv2.resize(input_image, dsize = (input_height, input_width), interpolation = cv2.INTER_CUBIC)
    global first_time
    if first_time:
        print(first_time)
        s = '%dx%d' % (input_node.shape[2], input_node.shape[1])
        ckpts = './openpose/models/trained/mobilenet_' + s + '/model-release'
        vars_in_checkpoint = tf.train.list_variables(ckpts)
        var_rest = []
        for el in vars_in_checkpoint:
            var_rest.append(el[0])
        variables = tf.contrib.slim.get_variables_to_restore()
        var_list = [v for v in variables if v.name.split(':')[0] in var_rest]

        loader = tf.train.Saver(var_list = var_list)

        loader.restore(sess, ckpts)

        first_time = False

    vec = sess.run(net.get_output(name = 'concat_stage7'), feed_dict = {'image:0': [image]})
    run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
    run_metadata = tf.runMetadata()
    pafMat, heatMat, sess.run([
        net.get_output(name=last_layer.format(stage=stage_level, aux=1)),
        net.get_output(name=last_layer.format(stage=stage_level, aux=2))
        ], 
        feed_dict = {'image:0': [image]}, options = run_options, run_metadata = run_metadata)
    tl=timeline.Timeline(run_metadata.step_stats)
    ctf = tl.generate_chrome_trace_format()
    heatMat, pafMat = heatMat[0], pafMat[0]
    humans = estimate_pose(heatMat, pafMat)
    pose_image = np.zeros(tuple(image.shape), dtype = np.uint8)
    pose_image = draw_humans(pose_image,humans)

    return pose_image
Beispiel #9
0
        logging.info('pose+')
        a = time.time()
        humans = estimate_pose(heatMat, pafMat)
        logging.info('pose- elapsed_time={}'.format(time.time() - a))

        logging.info('image={} heatMap={} pafMat={}'.format(
            image.shape, heatMat.shape, pafMat.shape))
        process_img = CocoPoseLMDB.display_image(image,
                                                 heatMat,
                                                 pafMat,
                                                 as_numpy=True)

        # display
        image = cv2.imread(args.imgpath)
        image_h, image_w = image.shape[:2]
        image = draw_humans(image, humans)

        scale = 480.0 / image_h
        newh, neww = 480, int(scale * image_w + 0.5)

        image = cv2.resize(image, (neww, newh), interpolation=cv2.INTER_AREA)

        convas = np.zeros([480, 640 + neww, 3], dtype=np.uint8)
        convas[:, :640] = process_img
        convas[:, 640:] = image

        cv2.imwrite(args.imgout, convas)
        cv2.waitKey(0)

        tf.train.write_graph(sess.graph_def, '.', 'graph-tmp.pb', as_text=True)
Beispiel #10
0
def get_predicted_imgs_and_thetas(imgs, trace_person):
    predicted_img = []
    human_thetas = {}
    human_tmp = {}
    frame_count = 0
    trace_human_id = None
    count = 0
    for img in imgs:
        count += 1
        if count % 50 == 0:
            print("processed img:{}".format(count))
        humans = get_prediction(img)
        # 对预测出来的进行删减
        processed_humans = []
        for human in humans:
            # 颈部如果未找到直接舍去
            if 1 not in human.keys():
                continue
            # 如果颈部,鼻子,一只耳朵能够被找到,且存在六个以上点,则保留此数据
            elif 0 in human.keys() and 1 in human.keys() and (
                    16 in human.keys()
                    or 17 in human.keys()) and len(human.keys()) > 6:
                processed_humans.append(human)
            elif 0 not in human.keys() and 2 not in human.keys(
            ) and 5 not in human.keys():
                continue
            elif 9 not in human.keys() and 10 not in human.keys(
            ) and 12 not in human.keys() and 13 not in human.keys():
                continue
            elif len(human) < 6:
                continue
            else:
                processed_humans.append(human)
        # 在原始数据上标出点并连接,保存
        canvas = draw_humans(img, processed_humans)
        predicted_img.append(canvas)
        former_human_count = len(human_tmp)
        present_human_count = len(processed_humans)
        # 预测出有人物的第一帧图直接加入空字典
        if human_tmp == {}:
            count = 0
            for human in processed_humans:
                # 进行初始化
                human_tmp[count] = []
                for i in range(0, frame_count):
                    human_tmp[count].append(None)
                human_tmp[count].append(human)
                # 计算角度
                human_thetas[count] = {}
                theta_dict = analyse.cal_degrees(human)
                # 对所有可能的角度数据进行初始化
                for key in common.CalDegree:
                    human_thetas[count][key] = []
                    # 为之前没有人物的帧均加入None
                    for i in range(0, frame_count):
                        human_thetas[count][key].append(None)
                    if key in theta_dict.keys():
                        human_thetas[count][key].append(theta_dict[key])
                    else:
                        human_thetas[count][key].append(None)
                count = count + 1
        # 不是第一帧对前一帧的内容进行分析
        else:
            # 使用处理过的预测数据在前一帧的基础上为每一个人添加内容
            for human_id, human_list in human_tmp.items():
                human = None
                # 寻找最后一个最近的非None的数据
                for former_human in human_list[::-1]:
                    if former_human is not None:
                        human = former_human
                        break
                # 在能找到一个非None的前数据时
                if human is not None:
                    # 以找到的已存数据的最近一帧与新加入的进行对比添加人物至合适位置
                    neck_point = analyse.get_point(human, 1)
                    radius = -1
                    # 如果找到鼻子点,以颈部到鼻子为半径,颈部为原点,在前一帧如果找到点,则判断为同一个人,如果均存在则取距离较近的一个
                    if 0 in human.keys():
                        nose_point = analyse.get_point(human, 0)
                        radius = analyse.get_distance(nose_point, neck_point)
                    # 如果找不到鼻子,则以左肩或者右肩到颈部的举例作为半径
                    elif 5 in human.keys():
                        l_shoulder = analyse.get_point(human, 5)
                        radius = analyse.get_distance(neck_point, l_shoulder)
                    elif 2 in human.keys():
                        r_shoulder = analyse.get_point(human, 2)
                        radius = analyse.get_distance(neck_point, r_shoulder)
                    # 寻找并分析得到合适的人并添加
                    to_add = None
                    min_distance = 1.
                    for processed_human in processed_humans:
                        neck_distance = analyse.get_distance(
                            analyse.get_point(processed_human, 1),
                            analyse.get_point(human, 1))
                        if radius != -1:
                            if neck_distance < min_distance and neck_distance < 0.1:
                                to_add = processed_human
                                min_distance = neck_distance
                        # if former_human_count < present_human_count:
                        #     if neck_distance < min_distance and neck_distance < radius:
                        #         to_add = processed_human
                        #         min_distance = neck_distance
                        # else:
                        #     if neck_distance < min_distance:
                        #         to_add = processed_human
                        #         min_distance = neck_distance
                    if to_add is not None:
                        processed_humans.remove(to_add)
                    human_tmp[human_id].append(to_add)
                    # 添加角度数据
                    theta_dict = analyse.cal_degrees(to_add)
                    for key in common.CalDegree:
                        if theta_dict is None:
                            human_thetas[human_id][key].append(None)
                        elif key in theta_dict.keys():
                            human_thetas[human_id][key].append(theta_dict[key])
                        else:
                            human_thetas[human_id][key].append(None)
                # else:
                #     human_tmp.pop(human_id)
                #     human_thetas.pop(human_id)
                #     continue
            # 如果有新的人物加入,在这里进行添加
            for residual_human in processed_humans:
                new_id = max(human_tmp.keys()) + 1
                # 添加人物
                human_tmp[new_id] = []
                for i in range(0, frame_count):
                    human_tmp[new_id].append(None)
                human_tmp[new_id].append(residual_human)
                # 添加计算的角度数据
                human_thetas[new_id] = {}
                theta_dict = analyse.cal_degrees(residual_human)
                for key in common.CalDegree:
                    human_thetas[new_id][key] = []
                    for i in range(0, frame_count):
                        human_thetas[new_id][key].append(None)
                    if key in theta_dict.keys():
                        human_thetas[new_id][key].append(theta_dict[key])
                    else:
                        human_thetas[new_id][key].append(None)
        frame_count = frame_count + 1
    # 寻找需要追踪的人
    human_ids = list(human_tmp)
    if trace_person is not None:
        for i in range(0, len(imgs)):
            for human_id in human_ids:
                head_point = analyse.get_point(human_tmp[human_id][i], 0)
                if head_point is not None:
                    result = face_detect.recognize_person(
                        trace_person, imgs[i], head_point)
                    if result:
                        trace_human_id = human_id
                        return predicted_img, human_thetas, human_tmp, trace_human_id
                    else:
                        human_ids.remove(human_id)
    return predicted_img, human_thetas, human_tmp, trace_human_id
Beispiel #11
0
    def execute(self):
        self.stdscr.nodelay(True)

        self.info("Initializing PoseNet...")
        self.net = pose_net.create()
        if not pose_net.initialize(self.net):
            raise RuntimeError("Failed to initialize PoseNet")
        if not pose_net.load_weights(self.net, "KerasPoseNet_weights.bin"):
            raise RuntimeError(
                "Failed to load weights from KerasPoseNet_weights.bin")
        if not pose_net.commit(self.net):
            raise RuntimeError("Failed to commit PoseNet")
        self.info("Successfully initialized PoseNet")

        if not self.open_video_capture():
            return

        width = 432
        height = 368
        out_width = width // 8
        out_height = height // 8
        peaks_chw = np.zeros((18, out_height, out_width), dtype=np.float32)
        heatmap_hwc = np.zeros((out_height, out_width, 18), dtype=np.float32)
        paf_hwc = np.zeros((out_height, out_width, 38), dtype=np.float32)
        input_data_hwc = np.zeros((height, width, 3), dtype=np.uint8)

        frame = np.zeros(
            [get_screen_height(), get_screen_width(), 3], dtype=np.uint8)

        image_original = None
        image_resized = None
        image_resized_bgr = None
        while True:
            if image_original is None:
                ret_val, image_original = self.cap.read()
            else:
                ret_val, image_original = self.cap.read(image_original)
            if not ret_val:
                self.info("End of %s reached, will reopen",
                          self.capture_source)
                self.cap.release()
                self.cap = None
                image_original = None
                image_resized = None
                image_resized_bgr = None
                t0 = time.time()
                import gc
                gc.collect()
                self.info("Garbage collection completed in %.3f sec",
                          time.time() - t0)
                if not self.open_video_capture():
                    return
                continue

            if image_resized is None:
                orig_w = image_original.shape[1]
                orig_h = image_original.shape[0]
                dst_w = width
                dst_h = orig_h * dst_w // orig_w
                if dst_h > height:
                    dst_h = height
                    dst_w = orig_w * dst_h // orig_h
                image_resized = np.zeros((dst_h, dst_w, 3), dtype=np.uint8)
                image_resized_bgr = np.zeros((dst_h, dst_w, 3), dtype=np.uint8)

            t00 = time.time()

            t0pre = t00
            cv2.resize(image_original, (dst_w, dst_h), image_resized_bgr)
            cv2.cvtColor(image_resized_bgr,
                         cv2.COLOR_BGR2RGB,
                         dst=image_resized)

            # import pydevd
            # pydevd.settrace("172.16.40.212")

            if input_data_hwc.shape != image_resized.shape:
                input_data_hwc[:] = 0
                y_offs = (height - image_resized.shape[0]) >> 1
                x_offs = (width - image_resized.shape[1]) >> 1
                input_data_hwc[y_offs:y_offs + image_resized.shape[0],
                               x_offs:x_offs + image_resized.shape[1]] = \
                    image_resized
            else:
                input_data_hwc[:] = image_resized
            input_data_whc16 = (np.moveaxis(input_data_hwc, [0, 1, 2],
                                            [1, 0, 2]).astype(np.float32) -
                                128.0).astype(np.float16)

            pose_net.put_input(self.net,
                               np.ascontiguousarray(input_data_whc16))
            dt_pre = time.time() - t0pre
            self.info("Preprocessed input in %.3f sec", dt_pre)

            t0 = time.time()
            pose_net.run_network(self.net)
            dt_inference = time.time() - t0
            self.info("Inference in %.3f sec", dt_inference)
            output_whc8 = pose_net.get_final_output(self.net)

            t0pp = time.time()
            t0 = t0pp
            extract_heatmap_peaks(array_address(output_whc8),
                                  array_address(peaks_chw),
                                  self.peaks_threshold)
            self.info("heatmap peaks extracted in %.3f sec", time.time() - t0)

            t0 = time.time()
            peaks_hwc = np.moveaxis(peaks_chw, [0, 1, 2], [2, 0, 1])
            data56 = output_whc8[:out_width * out_height * 56].\
                reshape(56 // 8, out_width, out_height, 8)
            for ch in range(0, 18):
                heatmap_hwc[:, :,
                            ch] = np.moveaxis(data56[ch // 8, :, :, ch % 8],
                                              [0, 1], [1, 0])
            for ch in range(19, 56):
                paf_hwc[:, :,
                        ch - 19] = np.moveaxis(data56[ch // 8, :, :, ch % 8],
                                               [0, 1], [1, 0])
            self.info("Repacked numpy arrays in %.3f sec", time.time() - t0)

            t0 = time.time()
            humans = estimate_paf(peaks_hwc, heatmap_hwc, paf_hwc)
            self.info("Extracted %d humans in %.3f sec", len(humans),
                      time.time() - t0)
            dt_pp = time.time() - t0pp

            t0 = time.time()
            draw_humans(image_resized_bgr, humans, imgcopy=False)
            self.info("Drawn humans in %.3f sec", time.time() - t0)

            t0 = time.time()
            frame[:] = 0
            x0 = (frame.shape[1] - image_resized_bgr.shape[1]) // 2
            y0 = (frame.shape[0] - image_resized_bgr.shape[0]) // 4
            frame[y0:y0 + image_resized_bgr.shape[0],
                  x0:x0 + image_resized_bgr.shape[1]] = image_resized_bgr

            text_color = (16, 147, 222)  # bgr
            text_scale = 1.0
            text_thickness = 2
            text = "CONV: %.0f msec" % (0.001 *
                                        pose_net.get_conv_usec(self.net))
            sz, b = cv2.getTextSize(text, FONT, text_scale, text_thickness)
            h = sz[1] + sz[1] // 2
            left = x0
            top = y0 + image_resized_bgr.shape[0] + h
            cv2.putText(frame, text, (left, top), FONT, text_scale, text_color,
                        text_thickness)
            top += h

            text = "CONV+CPU+MemSync: %.0f msec" % (1000.0 * dt_inference)
            sz, b = cv2.getTextSize(text, FONT, text_scale, text_thickness)
            cv2.putText(frame, text, (left, top), FONT, text_scale, text_color,
                        text_thickness)
            top += h

            text = "Post-processing: %.0f msec" % (1000.0 * dt_pp)
            sz, b = cv2.getTextSize(text, FONT, text_scale, text_thickness)
            cv2.putText(frame, text, (left, top), FONT, text_scale, text_color,
                        text_thickness)
            top += h

            text = "Pre-processing: %.0f msec" % (1000.0 * dt_pre)
            sz, b = cv2.getTextSize(text, FONT, text_scale, text_thickness)
            cv2.putText(frame, text, (left, top), FONT, text_scale, text_color,
                        text_thickness)
            top += h

            text = "Total: %.0f msec" % (1000.0 * (time.time() - t00))
            sz, b = cv2.getTextSize(text, FONT, text_scale, text_thickness)
            cv2.putText(frame, text, (left, top), FONT, text_scale, text_color,
                        text_thickness)

            self.info("Filled framebuffer in %.3f sec", time.time() - t0)

            self.info("Total time: %.3f", time.time() - t00)

            self.fb.update(frame)

            paused = False
            while True:
                c = self.stdscr.getch()
                if c == -1:
                    if paused:
                        continue
                    break
                if c == ord(' '):
                    if paused:
                        paused = False
                        self.info("Unpaused")
                        continue
                    paused = True
                    self.info("Paused")
                    continue
                if c == 27:
                    self.cap.release()
                    return
Beispiel #12
0
        run_metadata = tf.RunMetadata()
        pafMat, heatMat = sess.run(
            [
                net.get_output(name=last_layer.format(stage=args.stage_level, aux=1)),
                net.get_output(name=last_layer.format(stage=args.stage_level, aux=2))
            ], feed_dict={'image:0': [image]}, options=run_options, run_metadata=run_metadata
        )
        logging.info('inference- elapsed_time={}'.format(time.time() - a))

        heatMat, pafMat = heatMat[0], pafMat[0]

        logging.info('pose+')
        a = time.time()
        humans = estimate_pose(heatMat, pafMat)
        logging.info('pose- elapsed_time={}'.format(time.time() - a))

        # display
        imagecv = cv2.imread(args.imgpath)
        image_h, image_w = imagecv.shape[:2]
        image, delta = draw_humans(imagecv, humans)
        print (delta)

        # scale = 480.0 / image_h
        # newh, neww = 480, int(scale * image_w + 0.5)

        # image = cv2.resize(image, (neww, newh), interpolation=cv2.INTER_AREA)

        cv2.imshow('result', image)
        cv2.waitKey(0)

        tf.train.write_graph(sess.graph_def, '.', 'graph-tmp.pb', as_text=True)
Beispiel #13
0
def inference(imgpath, j):
    # input_width = 656
    # input_height = 368

    input_width = 352
    input_height = 288

    t0 = time.time()

    tf.compat.v1.reset_default_graph()

    from tensorflow.core.framework import graph_pb2
    graph_def = graph_pb2.GraphDef()
    # Download model from https://www.dropbox.com/s/2dw1oz9l9hi9avg/optimized_openpose.pb
    with open('models/optimized_openpose.pb', 'rb') as f:
        graph_def.ParseFromString(f.read())
    tf.import_graph_def(graph_def, name='')

    t1 = time.time()
    # print(t1 - t0)

    inputs = tf.compat.v1.get_default_graph().get_tensor_by_name('inputs:0')
    heatmaps_tensor = tf.compat.v1.get_default_graph().get_tensor_by_name(
        'Mconv7_stage6_L2/BiasAdd:0')
    pafs_tensor = tf.compat.v1.get_default_graph().get_tensor_by_name(
        'Mconv7_stage6_L1/BiasAdd:0')

    t2 = time.time()
    # print(t2 - t1)

    image = read_imgfile(imgpath, input_width, input_height)

    t3 = time.time()
    # print(t3 - t2)

    with tf.compat.v1.Session() as sess:
        heatMat, pafMat = sess.run([heatmaps_tensor, pafs_tensor],
                                   feed_dict={inputs: image})

        t4 = time.time()
        # print(t4 - t3)

        heatMat, pafMat = heatMat[0], pafMat[0]

        humans = estimate_pose(heatMat, pafMat)

        # coordinates for left hand, head, right hand etc. indexes: 0,2,3,4,5,6,7
        head_coco_idx = 0
        right_shoulder_idx = 2
        right_elbow_idx = 3
        right_hand_idx = 4
        left_shoulder_idx = 5
        left_elbow_idx = 6
        left_hand_idx = 7

        for human in humans:
            if human[right_hand_idx] is None:
                break

            head_coords = human[head_coco_idx][1]
            right_shoulder_coords = human[right_shoulder_idx][1]
            right_elbow_coords = human[right_elbow_idx][1]
            right_hand_coords = human[right_hand_idx][1]
            left_shoulder_coords = human[left_shoulder_idx][1]
            left_elbow_coords = human[left_elbow_idx][1]
            left_hand_coords = human[left_hand_idx][1]

            fields = [
                head_coords, right_shoulder_coords, right_elbow_coords,
                right_hand_coords, left_shoulder_coords, left_elbow_coords,
                left_hand_coords
            ]

        # with open(r'test.csv', 'a') as f:
        # writer = csv.writer(f)
        # writer.writerow(fields)
        # end of printing

        # display
        image = cv2.imread(imgpath)
        image_h, image_w = image.shape[:2]
        image = draw_humans(image, humans)

        scale = 480.0 / image_h
        newh, neww = 480, int(scale * image_w + 0.5)

        image = cv2.resize(image, (neww, newh), interpolation=cv2.INTER_AREA)

        ### Uncomment below to show image with skeleton
        # cv2.imshow('result', image)

        ##### save image with coordinates
        cv2.imwrite('./Outputs/openpos/result_' + str(j) + '.png', image)

        t5 = time.time()
        # print(t5 - t4)
        cv2.waitKey(0)

        return fields
Beispiel #14
0
            humans1 = estimate_pose(heatMat1, pafMat1)

            backbone_feature2 = sess2.run(outputs2, feed_dict={inputs2: image})
            heatMat2, pafMat2 = sess2.run(
                [heatmaps_tensor2, pafs_tensor2],
                feed_dict={outputs2: backbone_feature2})

            heatMat2, pafMat2 = heatMat2[0], pafMat2[0]

            humans2 = estimate_pose(heatMat2, pafMat2)

            # display
            image = img
            image_h, image_w = image.shape[:2]
            image1 = draw_humans(image, humans1)
            image2 = draw_humans(image, humans2)

            scale = 480.0 / image_h
            newh, neww = 480, int(scale * image_w + 0.5)

            image1 = cv2.resize(image1, (neww, newh),
                                interpolation=cv2.INTER_AREA)
            image2 = cv2.resize(image2, (neww, newh),
                                interpolation=cv2.INTER_AREA)

            cv2.putText(image1, "FPS: %f" % (1.0 / (time.time() - fps_time)),
                        (10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0),
                        2)

            fps_time = time.time()