Esempio n. 1
0
def freeze_graph_test(pb_path, outnode):
    with tf.Graph().as_default():
        output_graph_def = tf.GraphDef()

        with open(pb_path, "rb") as f:
            output_graph_def.ParseFromString(f.read())
        for node in output_graph_def.node:
            if node.op == 'Conv2D' and 'explicit_paddings' in node.attr:
                del node.attr['explicit_paddings']
            if node.op == 'ResizeNearestNeighbor' and 'half_pixel_centers' in node.attr:
                del node.attr['half_pixel_centers']
        tf.import_graph_def(output_graph_def, name="")
        with tf.Session() as sess:
            summary_writer = tf.summary.FileWriter('mylog/new2')
            summary_writer.add_graph(tf.get_default_graph())
            sess.run(tf.global_variables_initializer())
            img = cv2.imread('004650.jpg')
            originimg = img
            orishape = img.shape
            img = tools.img_preprocess2(img, None, (INPUTSIZE, INPUTSIZE),
                                        False)[np.newaxis, ...]
            img = img.astype(np.float32)

            outbox = sess.graph.get_tensor_by_name('YoloV3/output/boxconcat:0')
            inputdata = sess.graph.get_tensor_by_name("input/input_data:0")
            outbox = sess.run(outbox, feed_dict={inputdata: img})
            outbox = np.array(postprocess(outbox, INPUTSIZE, orishape[:2]))
            originimg = tools.draw_bbox(originimg, outbox, CLASSES)
            cv2.imwrite('004650_detected.jpg', originimg)
Esempio n. 2
0
    def detect_video(self, video_path):
        import cv2
        vid = cv2.VideoCapture(video_path)
        output_path = './output/result.mp4'
        if not vid.isOpened():
            raise IOError("Couldn't open webcam or video")
        video_FourCC = int(vid.get(cv2.CAP_PROP_FOURCC))
        video_fps = vid.get(cv2.CAP_PROP_FPS)
        video_size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)),
                      int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)))
        isOutput = True if output_path != "" else False
        if isOutput:
            print("!!! TYPE:", type(output_path), type(video_FourCC),
                  type(video_fps), type(video_size))
            out = cv2.VideoWriter(output_path, video_FourCC, video_fps,
                                  video_size)
        accum_time = 0
        curr_fps = 0
        fps = "FPS: ??"
        #prev_time = timer()
        while True:
            return_value, frame = vid.read()
            #image = Image.fromarray(frame)

            original_image = np.copy(frame)
            prev_time = timer()
            bboxes = self.get_bbox(frame)
            curr_time = timer()
            image = tools.draw_bbox(original_image, bboxes, self._classes)

            #image = self.detect_image(frame)
            result = np.asarray(image)
            #curr_time = timer()
            exec_time = curr_time - prev_time
            #prev_time = curr_time
            accum_time = accum_time + exec_time
            curr_fps = curr_fps + 1
            print("exec_time : {}".format(exec_time))
            if accum_time > 1:
                accum_time = accum_time - 1
                fps = "FPS: " + str(curr_fps)
                curr_fps = 0
            cv2.putText(result,
                        text=fps,
                        org=(3, 15),
                        fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                        fontScale=0.50,
                        color=(255, 0, 0),
                        thickness=2)
            cv2.namedWindow("result", cv2.WINDOW_NORMAL)
            cv2.imshow("result", result)
            if isOutput:
                out.write(result)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        self.__sess.close()
Esempio n. 3
0
    def detect_image(self, image):
        original_image = np.copy(image)
        start = timer()
        bboxes = self.get_bbox(image)
        end = timer()
        print("running time : {} ms".format(end - start))

        image = tools.draw_bbox(original_image, bboxes, self._classes)
        self.__sess.close()
        return image
Esempio n. 4
0
 def detect_image(self, image):
     original_image = np.copy(image)
     bboxes = self.get_bbox(image)
     image = tools.draw_bbox(original_image, bboxes, self._classes)
     self.__sess.close()
     return image
Esempio n. 5
0
image_path   = "./data/kite.jpg"
check_dir    = "./saved_model/"

original_image      = cv2.imread(image_path)
original_image      = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
original_image_size = original_image.shape[:2]

image_data = tools.preprocess_data(np.copy(original_image), [input_size, input_size])
image_data = image_data[np.newaxis, ...].astype(np.float32)

model = YOLOv3()
model.load_weights(filepath=cfg.YOLO.SAVE_MODEL_DIR + "saved_model")

feature_maps = model.predict(image_data)
decoded_tensor = []
for i, conv_tensor in enumerate(feature_maps):
    pred_tensor = loss.decode(conv_tensor, i)
    decoded_tensor.append(pred_tensor)

pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in decoded_tensor]
pred_bbox = tf.concat(pred_bbox, axis=0)

bboxes = tools.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.3)
bboxes = nms(bboxes, 0.45, method='nms')

image = tools.draw_bbox(original_image, bboxes, show_label=True)
image = Image.fromarray(image)
image.show()


    prev_time = time.time()

    feature_maps = model.predict(image_data)
    pred_bbox = []
    for i, conv_tensor in enumerate(feature_maps):
        pred_tensor = loss.decode(conv_tensor, i)
        pred_bbox.append(pred_tensor)

    curr_time = time.time()
    exec_time = curr_time - prev_time

    pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox]
    pred_bbox = tf.concat(pred_bbox, axis=0)
    bboxes = tools.postprocess_boxes(pred_bbox, frame_size, input_size, 0.3)
    bboxes = nms(bboxes, 0.45, method='nms')
    image = tools.draw_bbox(frame, bboxes)
    result = np.asarray(image)
    info = "time: %.2f ms" % (1000 * exec_time)
    cv2.putText(result,
                text=info,
                org=(50, 70),
                fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                fontScale=1,
                color=(255, 0, 0),
                thickness=2)
    cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
    result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    cv2.imshow("result", result)
    if cv2.waitKey(1) & 0xFF == ord('q'): break