コード例 #1
0
ファイル: detect.py プロジェクト: hender14/Raspberry-Pi4
def main(mode, tiny, iou_threshold, confidence_threshold, path):
  class_names, n_classes = load_class_names()
  if tiny:
    model = YOLOv3_tiny(n_classes=n_classes,
                        iou_threshold=iou_threshold,
                        confidence_threshold=confidence_threshold)
  else:
    model = YOLOv3(n_classes=n_classes,
                  iou_threshold=iou_threshold,
                  confidence_threshold=confidence_threshold)
  inputs = tf.placeholder(tf.float32, [1, *model.input_size, 3])
  detections = model(inputs)
  saver = tf.train.Saver(tf.global_variables(scope=model.scope))

  with tf.Session() as sess:
    saver.restore(sess, './weights/model-tiny.ckpt' if tiny else './weights/model.ckpt')

    if mode == 'image':
      image = load_image(path, input_size=model.input_size)
      result = sess.run(detections, feed_dict={inputs: image})
      draw_boxes(path, boxes_dict=result[0], class_names=class_names, input_size=model.input_size)
      return

    elif mode == 'video':
      cv2.namedWindow("Detections")
      video = cv2.VideoCapture(path)
      fourcc = int(video.get(cv2.CAP_PROP_FOURCC))
      fps = video.get(cv2.CAP_PROP_FPS)
      frame_size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))
      out = cv2.VideoWriter('./detections/video_output.mp4', fourcc, fps, frame_size)
      print("Video being saved at \"" + './detections/video_output.mp4' + "\"")
      print("Press 'q' to quit")
      while True:
        retval, frame = video.read()
        if not retval:
          break
        resized_frame = cv2.resize(frame, dsize=tuple((x) for x in model.input_size[::-1]), interpolation=cv2.INTER_NEAREST)
        result = sess.run(detections, feed_dict={inputs: [resized_frame]})
        draw_boxes_frame(frame, frame_size, result, class_names, model.input_size)
        cv2.imshow("Detections", frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord('q'):
            break
        out.write(frame)
      cv2.destroyAllWindows()
      video.release()
      return

    elif mode == 'webcam':
      while True:
        frame, addr = receive()
        frame_size = (frame.shape[1], frame.shape[0])
        resized_frame = cv2.resize(frame, dsize=tuple((x) for x in model.input_size[::-1]), interpolation=cv2.INTER_NEAREST)
        result = sess.run(detections, feed_dict={inputs: [resized_frame]})
        draw_boxes_frame(frame, frame_size, result, class_names, model.input_size)
        jpgstring = cv2.imencode(".jpg", frame)
        packet = jpgstring[1].tostring()
        udpServSock.sendto(packet, addr)
      return
コード例 #2
0
ファイル: yolov3_model.py プロジェクト: YEEN6/Yolnir
 def run_model(self, frame):
     if frame is None:
         print("No image! Wait a Seconds...!")
         return frame
     else:
         #####TF MODEL#####
         image = Image.fromarray(frame)
         img_resized = np.array(image.resize(size=tuple(self.SIZE)),
                                dtype=np.float32)
         img_resized = img_resized / 255
         boxes, scores, labels = self.sess.run(self.output_tensors,
                                               feed_dict={
                                                   self.input_tensor:
                                                   np.expand_dims(
                                                       img_resized, axis=0)
                                               })
         image, bbox_list = utils.draw_boxes(image,
                                             boxes,
                                             scores,
                                             labels,
                                             self.classes,
                                             self.SIZE,
                                             show=False,
                                             target=self.TARGET)
         self.result = np.asarray(image)
         self.calculate_pitch_yaw_vertical(bbox_list)
         return self.result
コード例 #3
0
 def save_result(self, path, PIL_image=None, boxes=None, scores=None, labels=None):
     PIL_image = PIL_image or self.last_PIL_image
     boxes = boxes or self.last_boxes
     scores = scores or self.last_scores
     labels = labels or self.last_labels
     PIL_image_result = utils.draw_boxes(PIL_image, boxes, scores, labels, self.classes, [self.IMAGE_H, self.IMAGE_W], show=False)
     PIL_image_result.save(path)
コード例 #4
0
 def visualize_result(self, PIL_image=None, boxes=None, scores=None, labels=None):
     PIL_image = PIL_image or self.last_PIL_image
     boxes = boxes or self.last_boxes
     scores = scores or self.last_scores
     labels = labels or self.last_labels
     PIL_image_result = utils.draw_boxes(PIL_image, boxes, scores, labels, self.classes, [self.IMAGE_H, self.IMAGE_W])
     np_image = np.array(PIL_image_result, dtype=np.int32)
     # plt.figure(figsize=(10, 10))
     plt.imshow(np_image)
     plt.show()
コード例 #5
0
    def visualize_result(self, PIL_image=None, boxes=None, scores=None, labels=None):
        PIL_image = PIL_image or self.last_PIL_image
        boxes = boxes or self.last_boxes
        scores = scores or self.last_scores
        labels = labels or self.last_labels
        PIL_image_result = utils.draw_boxes(PIL_image, boxes, scores, labels, self.classes, [self.IMAGE_H, self.IMAGE_W])
        np_image = np.array(PIL_image_result, dtype=np.int32)
        # plt.figure(figsize=(10, 10))
        plt.imshow(np_image)
        plt.show()


# if __name__ == '__main__':
#     yolo = YoloTest()
#     boxes, scores, labels = yolo.predict_from_path("./data/demo_data/car1.jpg")
#
#     print(boxes, scores, labels)
#     yolo.visualize_result()
コード例 #6
0
                               dtype=np.float32)
        img_resized = img_resized / 255.
        prev_time = time.time()

        boxes, scores, probs = sess.run(
            output_tensors,
            feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)})
        boxes, scores, labels, _ = utils.cpu_nms(boxes,
                                                 scores,
                                                 probs,
                                                 num_classes,
                                                 score_thresh=0.4,
                                                 iou_thresh=0.5)
        image = utils.draw_boxes(image,
                                 boxes,
                                 scores,
                                 labels,
                                 classes, (IMAGE_H, IMAGE_W),
                                 show=False)

        curr_time = time.time()
        exec_time = curr_time - prev_time
        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)
コード例 #7
0
import numpy as np
import matplotlib.pyplot as plt
import os

os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
# 在推断模式中,将使用running_mean和running_variance作为bn层的mean和variance
tf.keras.backend.set_learning_phase(False)
# 加载模型
model = tf.saved_model.load('saved_model/overfit_model-1000e')

# 80个类别的名称
class_name = read_class_name('./model_data/coco_classes_chinese.txt')
# 图片名称
file = 'car2.png'
# 图片放缩成608,608,像素值归一化
image, image_data, image_shape = preprocess_image(img_path='train-set/origin/' + file)
# 进行目标检测算法
res_class, res_score, res_boxes = detect(image_data, model)
if len(res_boxes) > 0:
    # image_shape 为原图大小高宽格式,
    # 还原成相对于原图的位置
    res_boxes = res_boxes * np.tile(list(image_shape), 2)
    # 把框画在图片上
    draw_boxes(image, res_score, res_boxes, res_class, class_name, generate_colors(class_name))
    plt.imshow(image)
    plt.show()
    image.save('train-set/detected/'+file, quality=100)
else:
    print('未检测到任何目标')

コード例 #8
0
#
#   Editor      : VIM
#   File name   : f**k.py
#   Author      : YunYang1994
#   Created date: 2019-01-23 10:21:50
#   Description :
#
#================================================================

import numpy as np
import tensorflow as tf
from PIL import Image
from core import utils


IMAGE_H, IMAGE_W = 416, 416
classes = utils.read_coco_names('./data/raccoon.names')
num_classes = len(classes)
image_path = "./raccoon_dataset/images/raccoon-182.jpg"  # 181,
img = Image.open(image_path)
img_resized = np.array(img.resize(size=(IMAGE_W, IMAGE_H)), dtype=np.float32)
img_resized = img_resized / 255.
cpu_nms_graph = tf.Graph()

input_tensor, output_tensors = utils.read_pb_return_tensors(cpu_nms_graph, "./checkpoint/yolov3_cpu_nms.pb",
                                           ["Placeholder:0", "concat_9:0", "mul_6:0"])
with tf.Session(graph=cpu_nms_graph) as sess:
    boxes, scores = sess.run(output_tensors, feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)})
    boxes, scores, labels = utils.cpu_nms(boxes, scores, num_classes, score_thresh=0.3, iou_thresh=0.5)
    image = utils.draw_boxes(img, boxes, scores, labels, classes, [IMAGE_H, IMAGE_W], show=True)
コード例 #9
0
ファイル: test.py プロジェクト: InvokerLiu/tensorflow-yolov3
saver = tf.train.Saver()
saver.restore(sess, "data/SVHN/checkpoint5/yolov3.ckpt-4000")

acc = 0
STEPS = 13068

for step in range(STEPS):
    run_items = sess.run([y_pred, y_true], feed_dict={is_training: False})
    if step == 5:
        acc = utils.compute_accuracy(run_items[0], run_items[1])

    y_pred_data = run_items[0]
    pred_boxes = y_pred_data[0][0]
    pred_confs = y_pred_data[1][0]
    pred_probs = y_pred_data[2][0]

    pred_boxes, pred_scores, pred_labels = utils.cpu_nms(pred_boxes, pred_confs * pred_probs, NUM_CLASSES,
                                                         score_thresh=0.3, iou_thresh=0.5)

    img = Image.open("data/SVHN/PaddingTest/" + str(step + 1) + ".png")
    image = utils.draw_boxes(img, pred_boxes, pred_scores, pred_labels, CLASSES, [IMAGE_H, IMAGE_W], show=False)
    # if acc == 1:
    #     image.save("data/SVHN/RightRecognition/" + str(step + 1) + ".png")
    # else:
    #     image.save("data/SVHN/WrongRecognition/" + str(step + 1) + ".png")
    print("=> STEP %10d [VALID]:\tacc:%7.4f" % (step+1, acc))
    acc += acc * BATCH_SIZE

acc /= 13068
print("精度为%7.4f" % acc)
コード例 #10
0
                               dtype=np.float32)
        img_resized = img_resized / 255.
        prev_time = time.time()

        boxes, scores = sess.run(
            output_tensors,
            feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)})
        boxes, scores, labels = utils.cpu_nms(boxes,
                                              scores,
                                              num_classes,
                                              score_thresh=0.4,
                                              iou_thresh=0.5)
        image = utils.draw_boxes(boxes,
                                 scores,
                                 labels,
                                 image,
                                 classes,
                                 SIZE,
                                 show=False)

        curr_time = time.time()
        exec_time = curr_time - prev_time
        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)
コード例 #11
0
def main(mode, tiny, iou_threshold, confidence_threshold, path):
    rectangleColor = (0, 255, 0)
    frameCounter = 0
    currentCarID = 0
    fps = 0

    carTracker = {}
    carNumbers = {}
    carLocation1 = {}
    carLocation2 = {}
    speed = [None] * 1000

    class_names, n_classes = load_class_names()
    if tiny:
        model = YOLOv3_tiny(n_classes=n_classes,
                            iou_threshold=iou_threshold,
                            confidence_threshold=confidence_threshold)
    else:
        model = YOLOv3(n_classes=n_classes,
                       iou_threshold=iou_threshold,
                       confidence_threshold=confidence_threshold)
    inputs = tf.placeholder(tf.float32, [1, *model.input_size, 3])
    detections = model(inputs)
    saver = tf.train.Saver(tf.global_variables(scope=model.scope))

    with tf.Session() as sess:
        saver.restore(
            sess, './weights/model-tiny.ckpt' if tiny else './weights/model.ckpt')

        if mode == 'image':
            image = load_image(path, input_size=model.input_size)
            result = sess.run(detections, feed_dict={inputs: image})
            draw_boxes(
                path, boxes_dict=result[0], class_names=class_names, input_size=model.input_size)
            return

        cv2.namedWindow("Detections")
        
        
        print("Video being saved at \"" + './detections/video_output.mp4' + "\"")
        print("Press 'q' to quit")
        pathIn = './data/PETS_2000_Frames/'
        files = [f for f in os.listdir(pathIn)]
        files.sort(key= lambda x: int(x.split('.')[0].split('_')[1]))
        frame=cv2.imread(pathIn+files[0])
        frame_size = frame.shape[:2][::-1]
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        
        out = cv2.VideoWriter(
            './detections/video_output.mp4', fourcc, 20, frame_size)
        for i in range(len(files)):
            frame = cv2.imread(pathIn+files[i])
            resized_frame = cv2.resize(frame, dsize=tuple(
                (x) for x in model.input_size[::-1]), interpolation=cv2.INTER_NEAREST)
            result = sess.run(detections, feed_dict={inputs: [resized_frame]})
            draw_boxes_frame(frame, frame_size, result,
                             class_names, model.input_size)
            start_time = time.time()
              # rc, image = video.read()
            if type(frame) == type(None):
              break

            frame = cv2.resize(frame, frame_size)
            resultImage = frame

            frameCounter = frameCounter + 1

            carIDtoDelete = []

            for carID in carTracker.keys():
                trackingQuality = carTracker[carID].update(frame)

                if trackingQuality < 7:
                    carIDtoDelete.append(carID)

            for carID in carIDtoDelete:
                print('Removing carID ' + str(carID) + \
                      ' from list of trackers.')
                print('Removing carID ' + str(carID) + ' previous location.')
                print('Removing carID ' + str(carID) + ' current location.')
                carTracker.pop(carID, None)
                carLocation1.pop(carID, None)
                carLocation2.pop(carID, None)

            if not (frameCounter % 10):
                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                cars = carCascade.detectMultiScale(
                    gray, 1.1, 13, 18, (24, 24))

                for (_x, _y, _w, _h) in cars:
                    x = int(_x)
                    y = int(_y)
                    w = int(_w)
                    h = int(_h)

                    x_bar = x + 0.5 * w
                    y_bar = y + 0.5 * h

                    matchCarID = None

                    for carID in carTracker.keys():
                        trackedPosition = carTracker[carID].get_position()

                        t_x = int(trackedPosition.left())
                        t_y = int(trackedPosition.top())
                        t_w = int(trackedPosition.width())
                        t_h = int(trackedPosition.height())

                        t_x_bar = t_x + 0.5 * t_w
                        t_y_bar = t_y + 0.5 * t_h

                        if ((t_x <= x_bar <= (t_x + t_w)) and (t_y <= y_bar <= (t_y + t_h)) and (x <= t_x_bar <= (x + w)) and (y <= t_y_bar <= (y + h))):
                            matchCarID = carID

                    if matchCarID is None:
                        print('Creating new tracker ' + str(currentCarID))

                        tracker = dlib.correlation_tracker()
                        tracker.start_track(
                            frame, dlib.rectangle(x, y, x + w, y + h))

                        carTracker[currentCarID] = tracker
                        carLocation1[currentCarID] = [x, y, w, h]

                        currentCarID = currentCarID + 1

            for carID in carTracker.keys():
                trackedPosition = carTracker[carID].get_position()
                t_x = int(trackedPosition.left())
                t_y = int(trackedPosition.top())
                t_w = int(trackedPosition.width())
                t_h = int(trackedPosition.height())
                carLocation2[carID] = [t_x, t_y, t_w, t_h]

            end_time = time.time()

            if not (end_time == start_time):
                # print("Reached at 168") 
                fps = 1.0/(end_time - start_time)
            for i in carLocation1.keys():
                if frameCounter % 1 == 0:
                    [x1, y1, w1, h1] = carLocation1[i]
                    [x2, y2, w2, h2] = carLocation2[i]

                    # print 'previous location: ' + str(carLocation1[i]) + ', current location: ' + str(carLocation2[i])
                    carLocation1[i] = [x2, y2, w2, h2]
                    # print("Reached at 177")

                    # print 'new previous location: ' + str(carLocation1[i])
                    if [x1, y1, w1, h1] != [x2, y2, w2, h2]:
                        # print("Reached at 181") 
                        if (speed[i] == None or speed[i] == 0):
                            speed[i] = estimateSpeed(
                                [x1, y1, w1, h1], [x2, y2, w2, h2])

                        # if y1 > 275 and y1 < 285:
                        if speed[i] != None:
                            print(str(int(speed[i])) + " km/hr")
                            cv2.putText(resultImage, str(int(speed[i])) + " km/hr", (int(x1 + w1/2), int(y1-5)), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2)
            cv2.imshow("Detections", resultImage)
            key = cv2.waitKey(1) & 0xFF
            if key == ord('q'):
                break
            out.write(resultImage)
        cv2.destroyAllWindows()
コード例 #12
0
#
#================================================================

from PIL import Image
import numpy as np
from core import utils

classes = utils.read_coco_names('./data/coco.names')

data = open('./data/train_data/quick_train_data.txt', 'r').readlines()

example = data[5].split(' ')

image_path = example[0]
boxes_num = len(example[1:]) // 5

bboxes = np.zeros([boxes_num, 4], dtype=np.float64)
labels = np.zeros([
    boxes_num,
], dtype=np.int32)

for i in range(boxes_num):
    labels[i] = example[1 + i * 5]
    bboxes[i] = [float(x) for x in example[2 + i * 5:6 + i * 5]]

scores = np.array([1] * boxes_num)

image = Image.open(image_path)

utils.draw_boxes(bboxes, scores, labels, image, classes, image.size)
コード例 #13
0
import cv2
from PIL import Image
import numpy as np

input_image = "../data/train_dome_data/images/raccoon-4.jpg"
image = Image.open(input_image)
# image = cv2.imread(input_image)
# image = Image.fromarray(image)
image_resize = cv2.resize(np.array(image) / 255., (416, 416))
image_place = tf.placeholder(dtype=tf.float32, shape=(None, 416, 416, 3))
CLASSES = utils.read_coco_names('../data/raccoon.names')
ANCHORE = utils.get_anchors("../data/raccoon_anchors.txt", 416, 416)
model = yolov3.yolov3(len(CLASSES), ANCHORE)
with tf.variable_scope('yolov3'):
    pred_feature_map = model.forward(
        image_place, is_training=False)  #执行前向传播得到3个尺度的feature map
    pred = model.predict(pred_feature_map)  #根据feature map进行预测
sess = tf.Session()
saver = tf.train.Saver()
model_dir = tf.train.latest_checkpoint("../data/train_dome_data/model/")
saver.restore(sess, model_dir)
boxes, confs, prods = sess.run(
    pred, feed_dict={image_place:
                     np.expand_dims(image_resize,
                                    0)})  #根据预测结果和获取目标预测框,目标类别 以及是否有object
boxes, confs, prods = utils.cpu_nms(boxes, confs * prods,
                                    len(CLASSES))  #根据非最大值抑制算法的原则  筛选出最优的候选框
utils.draw_boxes(image, boxes, confs, prods, CLASSES, (416, 416),
                 "../data/font/HuaWenXinWei-1.ttf")  #在输出图片上绘制目标框,并输出标签识别率结果
print(boxes, confs, prods)
コード例 #14
0
# nms on GPU
input_tensor, output_tensors = utils.read_pb_return_tensors(
    gpu_nms_graph, "./checkpoint/yolov3_gpu_nms.pb",
    ["Placeholder:0", "concat_10:0", "concat_11:0", "concat_12:0"])
with tf.Session(graph=gpu_nms_graph) as sess:
    for i in range(5):
        start = time.time()
        boxes, scores, labels = sess.run(
            output_tensors,
            feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)})
        print("=> nms on gpu the number of boxes= %d  time=%.2f ms" %
              (len(boxes), 1000 * (time.time() - start)))
    image = utils.draw_boxes(boxes,
                             scores,
                             labels,
                             img,
                             classes,
                             SIZE,
                             show=True)
# nms on CPU
input_tensor, output_tensors = utils.read_pb_return_tensors(
    cpu_nms_graph, "./checkpoint/yolov3_cpu_nms.pb",
    ["Placeholder:0", "concat_9:0", "mul_9:0"])
with tf.Session(graph=cpu_nms_graph) as sess:
    for i in range(5):
        start = time.time()
        boxes, scores = sess.run(
            output_tensors,
            feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)})
        boxes, scores, labels = utils.cpu_nms(boxes,
                                              scores,
コード例 #15
0
ファイル: quick_test.py プロジェクト: wkvanderveen/hwr
    def test(self, source_dir=None, image_path=None, show=False):
        if source_dir:
            image_name = choice(os.listdir(source_dir))
            image_path = os.path.join(source_dir, image_name)

        img = cv2.imread(image_path, 0)
        img = cv2.resize(src=img, dsize=(0, 0), fx=1 / 2, fy=1 / 2)
        if img.shape[0] < self.img_h and img.shape[1] < self.img_w:
            img = cv2.copyMakeBorder(src=img,
                                     top=(self.img_h - img.shape[0]) // 2,
                                     bottom=(self.img_h - img.shape[0]) // 2,
                                     left=(self.img_w - img.shape[1]) // 2,
                                     right=(self.img_w - img.shape[1]) // 2,
                                     borderType=cv2.BORDER_CONSTANT,
                                     value=[255, 255, 255])

        img = cv2.resize(img, (self.img_w, self.img_h))
        img = expand_dims(img, axis=2)

        classes = os.listdir(self.orig_letters)

        cpu_nms_graph = tf.Graph()

        input_tensor, output_tensors = utils.read_pb_return_tensors(
            cpu_nms_graph,
            os.path.join(self.checkpoint_dir, "yolov3_cpu_nms.pb"),
            ["Placeholder:0", "concat_5:0", "mul_2:0"])

        with tf.Session(graph=cpu_nms_graph) as sess:
            boxes, scores = sess.run(
                output_tensors,
                feed_dict={input_tensor: np.expand_dims(img, axis=0)})
            boxes = boxes.reshape(-1, 4)
            scores = scores.reshape(-1, self.num_classes)

            min_wh, max_wh = -10000, 10000
            min_ratio = 1 / 4  # 0 -- 1

            mask = np.logical_and(boxes[:, 0] >= min_wh, boxes[:, 0] <= max_wh)
            mask = np.logical_and(mask, boxes[:, 1] >= min_wh)
            mask = np.logical_and(mask, boxes[:, 2] >= min_wh)
            mask = np.logical_and(mask, boxes[:, 3] >= min_wh)
            mask = np.logical_and(mask, boxes[:, 1] <= max_wh)
            mask = np.logical_and(mask, boxes[:, 2] <= max_wh)
            mask = np.logical_and(mask, boxes[:, 3] <= max_wh)
            mask = np.logical_and(mask, boxes[:, 0] < boxes[:, 2])
            mask = np.logical_and(mask, boxes[:, 1] < boxes[:, 3])

            boxes = boxes[mask]
            scores = scores[mask]

            h = abs(boxes[:, 2] - boxes[:, 0])
            w = abs(boxes[:, 3] - boxes[:, 1])

            mask = np.logical_and(w / h > min_ratio, h / w > min_ratio)

            boxes = boxes[mask]
            scores = scores[mask]

            if self.filters:
                # Harder filters
                print(f"Test: Boxes before filtering:\t{boxes.shape[0]}")

                mask = np.logical_and(boxes[:, 0] >= 0,
                                      boxes[:, 0] <= img.shape[1])
                mask = np.logical_and(mask, boxes[:, 1] >= 0)
                mask = np.logical_and(mask, boxes[:, 2] >= 0)
                mask = np.logical_and(mask, boxes[:, 3] >= 0)
                mask = np.logical_and(mask, boxes[:, 1] <= img.shape[0])
                mask = np.logical_and(mask, boxes[:, 2] <= img.shape[1])
                mask = np.logical_and(mask, boxes[:, 3] <= img.shape[0])
                mask = np.logical_and(mask, boxes[:, 0] < boxes[:, 2])
                mask = np.logical_and(mask, boxes[:, 1] < boxes[:, 3])
                mask = np.logical_and(
                    mask,
                    abs(boxes[:, 2] - boxes[:, 0]) >= self.size_threshold[0])
                mask = np.logical_and(
                    mask,
                    abs(boxes[:, 3] - boxes[:, 1]) >= self.size_threshold[1])

                boxes = boxes[mask]
                scores = scores[mask]

                print(f"Test: Boxes after filtering:\t{boxes.shape[0]}")

                if boxes.shape[0] == 0:
                    print(
                        f"Try changing the filters/thresholds in the parameters."
                    )

            boxes, scores, labels = utils.cpu_nms(boxes=boxes,
                                                  scores=scores,
                                                  num_classes=self.num_classes,
                                                  max_boxes=self.max_boxes)

            (image,
             results) = utils.draw_boxes(img,
                                         boxes,
                                         scores,
                                         labels,
                                         classes, [self.img_h, self.img_w],
                                         show=show,
                                         size_threshold=self.size_threshold)

        return results
コード例 #16
0
                   BATCH_SIZE,
                   shuffle=SHUFFLE_SIZE,
                   multi_image_size=False)
testset = dataset(parser, TEST_TFRECORD, BATCH_SIZE, shuffle=None)
example = trainset.get_next()

images, *y_true = example
model = yolov3.yolov3(NUM_CLASSES, ANCHORS, basic_net=MobilenetV2)

with tf.variable_scope('yolov3'):
    model.set_anchor(images)
    pred_feature_map = model.forward(images, is_training=False)
    y_pred = model.predict(pred_feature_map)
saver = tf.train.Saver()
with tf.Session() as sess:
    saver.restore(sess, "./checkpoint/yolov3.ckpt-25000")
    run_items = sess.run([images, y_pred])
    for i in range(8):
        image = run_items[0][i]
        pred_boxes = run_items[1][0][i:i + 1]
        pred_confs = run_items[1][1][i:i + 1]
        pred_probs = run_items[1][2][i:i + 1]
        pred_boxes, pred_scores, pred_labels = utils.cpu_nms(
            pred_boxes, pred_confs * pred_probs, len(CLASSES))
        im = utils.draw_boxes(image * 255,
                              pred_boxes,
                              pred_scores,
                              pred_labels,
                              CLASSES, (416, 416),
                              show=True)
コード例 #17
0
                iou_thresh=self.iou_thresh)

        return {'boxes': boxes, 'probs': probs, 'labels': labels}

    def _process_image(self, img):
        """
        Image pre-processing
        :param img: Single image records
        :type img: [HxWx3] numpy array
        :return: Pre-processed image
        :rtype: [self.img_h x self.img_w x 3] numpy array
        """

        img_resized = np.array(np.resize(img, [self.img_h, self.img_w, 3]),
                               dtype=np.float32)
        img_resized = img_resized / 255.
        return img_resized


if __name__ == "__main__":
    model = ObjectRecognition(use_gpu=False)
    image_path = "data/OpenImages/test/Apple/0da61cd490c57814.jpg"
    img = np.asarray(Image.open(image_path))
    predictions = model.predict_image(img)
    image = utils.draw_boxes(img,
                             predictions['boxes'],
                             predictions['scores'],
                             predictions['labels'],
                             model.classes, [model.img_h, model.img_w],
                             show=True)
コード例 #18
0
import tensorflow as tf
from core import utils, yolov3
import cv2
from PIL import Image
import numpy as np

input_image = "../data/raccoon_data/images/raccoon-4.jpg"
image = Image.open(input_image)
# image = cv2.imread(input_image)
# image = Image.fromarray(image)
image_resize = cv2.resize(np.array(image) / 255., (416, 416))
image_place = tf.placeholder(dtype=tf.float32, shape=(None, 416, 416, 3))
CLASSES = utils.read_coco_names('../data/objects.names')
ANCHORE = utils.get_anchors("../data/objects.txt", 416, 416)
model = yolov3.yolov3(len(CLASSES), ANCHORE)
with tf.variable_scope('yolov3'):
    pred_feature_map = model.forward(image_place, is_training=False)
    pred = model.predict(pred_feature_map)
sess = tf.Session()
saver = tf.train.Saver()
model_dir = tf.train.latest_checkpoint("../data/raccoon_data/model/")
saver.restore(sess, model_dir)
boxes, confs, prods = sess.run(pred, feed_dict={image_place: np.expand_dims(image_resize, 0)})
boxes, confs, prods = utils.cpu_nms(boxes, confs * prods, len(CLASSES))
utils.draw_boxes(image, boxes, confs, prods, CLASSES, (416, 416), "../data/font/HuaWenXinWei-1.ttf")
print(boxes, confs, prods)
コード例 #19
0
    with tf.variable_scope("yoloAt"):
        p_feature_map = model.forward(inputs_image, is_training=False)

    sess.run(tf.global_variables_initializer())

    saver = tf.train.Saver(var_list=tf.global_variables())
    saver.restore(sess, Config.CKPT)
    print(Config.F_Yellow + "starting predicting" + Config.F_Init)
    for pic in os.listdir('./data/test_pic'):
        img = Image.open('./data/test_pic/' + pic)
        img_resized = np.array(img.resize(size=(Config.IMAGE_H,
                                                Config.IMAGE_W)),
                               dtype=np.float32)
        img_resized = img_resized / 255
        boxes, confs, probs = sess.run(
            model.predict(p_feature_map),
            feed_dict={inputs_image: np.expand_dims(img_resized, axis=0)})
        scores = confs * probs
        boxes, scores, labels = utils.cpu_nms(boxes, scores,
                                              Config.NUM_CLASSES)
        image = utils.draw_boxes(img,
                                 boxes,
                                 scores,
                                 labels,
                                 Config.CLASSES,
                                 [Config.IMAGE_H, Config.IMAGE_W],
                                 show=False)
        image.save('./jpg/pre_{}'.format(pic))
        print("finished predicting {}".format(Config.F_Red + pic +
                                              Config.F_Init))