Exemplo n.º 1
0
def main(img_path):
    # path_file = FLAGS.path_file
    #
    # for count, foldername in enumerate(os.listdir(path_file)):
    #     print(count, foldername)
    #     for count, filename in enumerate(os.listdir(path_file + '/' + foldername)):
    #         print(count, filename)
    #         dst_name = filename.split('/')[-1].split('.jpg')[0] + '_crop' + '.jpg'
    #         path_src_file = path_file + '/' + foldername + '/' + filename
    #         path_dst_file = path_file + '/' + foldername + '/' + dst_name
    #         print(path_src_file)

    config = ConfigProto()
    #config.gpu_options.per_process_gpu_memory_fraction = 0.4
    #session = InteractiveSession(config=config)
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)

    input_size = 416
    image_path = img_path
    weights = './checkpoints/yolov4-416'

    print(image_path)
    original_image = cv2.imread(image_path)
    original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

    # image_data = utils.image_preprocess(np.copy(original_image), [input_size, input_size])
    image_data = cv2.resize(original_image, (input_size, input_size))
    image_data = image_data / 255.
    # image_data = image_data[np.newaxis, ...].astype(np.float32)

    images_data = []
    for i in range(1):
        images_data.append(image_data)
    images_data = np.asarray(images_data).astype(np.float32)

    saved_model_loaded = tf.saved_model.load(weights,
                                             tags=[tag_constants.SERVING])
    infer = saved_model_loaded.signatures['serving_default']
    batch_data = tf.constant(images_data)
    pred_bbox = infer(batch_data)
    for key, value in pred_bbox.items():
        boxes = value[:, :, 0:4]
        pred_conf = value[:, :, 4:]

    boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
        boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
        scores=tf.reshape(
            pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
        max_output_size_per_class=50,
        max_total_size=50,
        iou_threshold=FLAGS.iou,
        score_threshold=FLAGS.score)
    # print(boxes.numpy()[0][0])
    #
    #print(classes.numpy()[0][0])
    # error=0
    pred_bbox = [
        boxes.numpy(),
        scores.numpy(),
        classes.numpy(),
        valid_detections.numpy()
    ]
    #rint(pred_bbox)
    image = utils.draw_bbox_and_crop(original_image, pred_bbox, size=224)
    #image = utils.draw_bbox(image_data*255, pred_bbox)
    # if error == 1:
    #     print('dog 검출 x')

    image = Image.fromarray(image.astype(np.uint8))
    image.show()
    image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
    cv2.imwrite(image_path, image)
#
# Depth Estimation from RGB Images

import numpy as np
from glob import glob
from utils import deep_utils
from utils.image_utils import depth_read, rgb_read, depth_read_kitti
from models import models
from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint
from tensorflow.keras.optimizers import Adam
import datetime
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
import segmentation_models

config = ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.9
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)


def _batchGenerator(X_filelist, y_filelist, batchSize):
    """
    Yield X and Y data when the batch is filled.
    """
    #Sort filelists to confirm they are same order
    X_filelist.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))
    y_filelist.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))
    #Shuffle order of filenames
    X_filelist, y_filelist = deep_utils.simul_shuffle(X_filelist, y_filelist)
Exemplo n.º 3
0
def training(sweep_q, worker_q):
    # GPU-initialization
    gpu_config = ConfigProto()
    gpu_config.gpu_options.per_process_gpu_memory_fraction = 0.3
    gpu_config.gpu_options.allow_growth = True
    session = InteractiveSession(config=gpu_config)

    reset_wandb_env()
    worker_data = worker_q.get()
    run_name = "{}-{}".format(worker_data.sweep_run_name, worker_data.num)
    config = worker_data.config
    train = worker_data.train
    test = worker_data.test
    num_classes = worker_data.num_classes
    x = worker_data.x
    y = worker_data.y
    run = wandb.init(
        group=worker_data.sweep_name,
        job_type=worker_data.sweep_run_name,
        name=run_name,
        config=config,
    )

    # Model
    dropout = run.config.dropout
    nodesizes = [
        run.config.node_size2, run.config.node_size3, run.config.node_size4
    ]

    model = Sequential()
    model.add(
        Bidirectional(LSTM(run.config.node_size1, return_sequences=True),
                      input_shape=(x.shape[1], x.shape[2])))
    model.add(Dropout(rate=dropout))

    for i in range(
            0, run.config.num_layers):  #number of layers ramdom between 1 an 3
        model.add(Bidirectional(LSTM(nodesizes[i], return_sequences=True)))
        model.add(Dropout(rate=dropout))

    model.add(Bidirectional(LSTM(run.config.node_size5)))
    model.add(Dropout(rate=dropout))

    model.add(Dense(num_classes, activation='softmax'))

    model.compile(loss='categorical_crossentropy',
                  optimizer=run.config.optimizer,
                  metrics=['accuracy', Precision(),
                           Recall()])
    model.summary()

    model.fit(x[train],
              y[train],
              epochs=run.config.epochs,
              batch_size=run.config.batch_size,
              validation_data=(x[test], y[test]),
              shuffle=False,
              verbose=2,
              callbacks=[WandbCallback()])

    #Test accuracy
    model_best_path = os.path.join(run.dir, "model-best.h5")
    best_model = load_model(filepath=model_best_path)
    y_eval = best_model.evaluate(x[test], y[test], verbose=0)

    #Confusion Matrix
    y_pred = best_model.predict(x[test])

    y_pred_integer = np.argmax(y_pred, axis=1)
    y_test_integer = np.argmax(y[test], axis=1)

    y_pred_name = ([worker_data.token_labels[p] for p in y_pred_integer])
    y_test_name = ([worker_data.token_labels[p] for p in y_test_integer])

    wandb.sklearn.plot_confusion_matrix(y_test_name, y_pred_name)

    #Convert to TFLite
    tflite_converter = tf.lite.TFLiteConverter.from_keras_model(best_model)
    tflite_converter.experimental_new_converter = True
    tflite_model = tflite_converter.convert()
    open(os.path.join(wandb.run.dir, "model-best.tflite"),
         "wb").write(tflite_model)

    #Finish Run
    run.log(dict(val_accuracy=y_eval[1]))
    wandb.join()
    sweep_q.put(WorkerDoneData(val_accuracy=y_eval[1]))
Exemplo n.º 4
0
from ensemblenet import StackingModel
import tensorflow.keras as keras
import numpy as np
import tensorflow as tf
from tensorflow.compat.v1 import ConfigProto, Session, RunOptions
from tensorflow.compat.v1.keras.backend import set_session

# Run Options
np.random.seed(0)  # Seed for reproducing
tf.keras.fit_verbose = 2  # Print status after every epoch
RunOptions.report_tensor_allocations_upon_oom = True  # Out-of-memory display
config = ConfigProto()
config.gpu_options.allow_growth = True  # dynamically grow GPU memory
config.log_device_placement = True
sess = Session(config=config)
set_session(sess)

num_classes = 10
# Load data and clean
((x_train, y_train), (x_test, y_test)) = keras.datasets.mnist.load_data()

(x_train, x_test) = map(lambda data: data.astype("float32") / 255,
                        (x_train, x_test))

(x_train, x_test) = map(lambda data: np.expand_dims(data, -1),
                        (x_train, x_test))

(y_train,
 y_test) = map(lambda data: keras.utils.to_categorical(data, num_classes),
               (y_train, y_test))
Exemplo n.º 5
0
from __future__ import absolute_import  # __future__: 把下一個新版本的特性匯入到當前版本
from __future__ import division
from __future__ import print_function
import abc
from abc import ABC

import six  # Python 2 and 3 compatibility library
import tensorflow as tf
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
import MultiHeadAttention
tfconfig = ConfigProto(allow_soft_placement=True)
tfconfig.gpu_options.allow_growth = True
session = InteractiveSession(config=tfconfig)

# Registry layer keys.
ATTEND_TO_ENCODER_REGISTRY_KEY = "attend_to_encoder"
ATTENTION_32_HEADS_REGISTRY_KEY = "attention_32_heads"
ATTENTION_16_HEADS_REGISTRY_KEY = "attention_16_heads"
ATTENTION_4_HEADS_REGISTRY_KEY = "attention_4_heads"
DEPTHWISE_CONV_3X1_REGISTRY_KEY = "depthwise_conv_3x1"
DEPTHWISE_CONV_5X1_REGISTRY_KEY = "depthwise_conv_5x1"
DEPTHWISE_CONV_7X1_REGISTRY_KEY = "depthwise_conv_7x1"
DILATED_CONV_3X1_REGISTRY_KEY = "dilated_conv_3x1"
DILATED_CONV_5X1_REGISTRY_KEY = "dilated_conv_5x1"
GATED_LINEAR_UNIT_REGISTRY_KEY = "gated_linear_unit"
IDENTITY_REGISTRY_KEY = "identity"
# Lightweight convolution naming convention uses "R_X" where X is the variable
# reduction factor.
LIGHTWEIGHT_CONV_3X1_R_1_REGISTRY_KEY = "lightweight_conv_3x1_r_1"
LIGHTWEIGHT_CONV_3X1_R_4_REGISTRY_KEY = "lightweight_conv_3x1_r_4"
Exemplo n.º 6
0
def main_images(m_type, m_name, logger, data_path=None, actors=[], write_output=True):
    from tensorflow.compat.v1 import ConfigProto
    from tensorflow.compat.v1 import InteractiveSession
    
    # this thing fixes something
    _config = ConfigProto()
    _config.gpu_options.allow_growth = True
    session = InteractiveSession(config=_config)
    # ---

    with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
        
        # load best model
        model = load_model(sess, m_type, m_name, logger)
        
        eye_info = HierarchicalDict(path=data_path + '/eye_data.json')

        previos_segment = ''
        current_segment = ''
        

        for data_element in tqdm(data_generator(data_path=data_path, actors=actors), ascii=True):
        # for data_element in data_generator(data_path=data_path, actors=actors):
            keys = data_element.keys
            image = data_element.item

            current_segment = keys[:-1]
            if current_segment != previos_segment:
                last_ars = [deque(maxlen=25) for _ in range(2)]

            if not eye_info.check_key(keys):
                continue

            rois_coords = eye_info[keys]['roi']
            contours = eye_info[keys]['cnt']
            aspect_ratios = eye_info[keys]['ars']

            eye1 = np.array(contours[0]).reshape((-1, 1, 2))
            eye2 = np.array(contours[1]).reshape((-1, 1, 2))
            eyes = [eye1, eye2]

            # create empty mask to draw on
            result = np.zeros(image.shape, np.uint8)
            for i, (x1, x2, y1, y2) in enumerate(rois_coords):
                prev_aspect_ratio = sum(last_ars[i]) / len(last_ars[i]) if len(last_ars[i]) else 0.35
                current_aspect_ratio = aspect_ratios[i]
                if current_aspect_ratio < 0.6 * prev_aspect_ratio:
                    continue
                last_ars[i].append(current_aspect_ratio)

                roi = image[y1 : y2, x1 : x2] # get the original eye region
                # preprocessing for the pupil detection model
                roi_gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
                shape = roi_gray.shape
                if roi_gray.shape[0] != 192:
                    roi_gray = rescale(roi_gray)

                roi_gray = gray_normalizer(roi_gray)
                roi_gray = change_channel(roi_gray, config["input_channel"])
                # ---
                
                [p] = model.predict(sess, [roi_gray])

                x, y, w = upscale_preds(p, shape)
                x, y, w = [int(item) for item in (x, y, w)]

                # draw the circle indicating a pupil
                if cv2.pointPolygonTest(eyes[i], (x + x1, y + y1), False) != -1:
                    cv2.drawContours(result, [eyes[i]], 0, (255, 255, 255), -1)
                    roi = result[y1 : y2, x1 : x2]
                    cv2.circle(roi, (x, y), 9, (0, 0, 255), -1)
                    result[y1 : y2, x1 : x2] = roi
                # ---

            previos_segment = current_segment

            if write_output:
                actor, domain, segment, idx = keys
                path = os.path.dirname(os.path.abspath(f'{data_path}/{actor}/{domain}/{segment}'))
                path = path + '/' + segment + '/original_renders/eye_regions/'
                if not os.path.exists(path):
                    os.mkdir(path)
                cv2.imwrite(f'{path}{idx}.jpg', cv2.cvtColor(result, cv2.COLOR_BGR2RGB))

    print("Done.")
def main(_argv):
    # Definition of the parameters
    max_cosine_distance = 0.4
    nn_budget = None
    nms_max_overlap = 1.0

    # initialize deep sort
    model_filename = os.getenv(
        'HOME'
    ) + '/st_mini/src/scout_mini_ros/scout_bringup/model_data/mars-small128.pb'
    encoder = gdet.create_box_encoder(model_filename, batch_size=1)
    # calculate cosine distance metric
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    # initialize tracker
    tracker = Tracker(metric)

    # load configuration for object detector
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    if use_webcam:
        video_path = FLAGS.video

    # load tflite model if flag is set
    if FLAGS.framework == 'tflite':
        interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        interpreter.allocate_tensors()
        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()
        print(input_details)
        print(output_details)
    # otherwise load standard tensorflow saved model
    else:
        saved_model_loaded = tf.saved_model.load(FLAGS.weights,
                                                 tags=[tag_constants.SERVING])
        infer = saved_model_loaded.signatures['serving_default']

    if use_webcam:
        # begin video capture
        try:
            vid = cv2.VideoCapture(int(video_path))
        except:
            vid = cv2.VideoCapture(video_path)

    # 로봇 모터 제어를 위한 초깃값 설정
    x = 0
    y = 0
    z = 0
    th = 0
    speed = 0.85
    turn = 1

    # 로봇의 최대,최소 속도 설정
    # <!--선속도--!>
    max_speed = 1.6
    min_speed = 0.7

    # <!--각속도--!>
    max_turn = 1.2
    min_turn = 1

    # 변수 추가
    cx, cy, h = 0, 0, 0
    frame_num = 0
    key = ''
    # Depth camera class 불러오기
    if not use_webcam:
        dc = DepthCamera()

    # 장애물 영역 기본값 받아오기
    default = Default_dist()

    # ROS class init
    go = scout_pub_basic()
    rate = rospy.Rate(60)

    # while video is running
    while not rospy.is_shutdown():
        if use_webcam:
            return_value, frame = vid.read()
        else:
            # depth camera 사용
            return_value, depth_frame, frame = dc.get_frame()

        if return_value:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            image = Image.fromarray(frame)
        else:
            print('Video has ended or failed, try a different video format!')
            break
        frame_num += 1

        # 좌/우 회전 한곗값 설정
        left_limit = frame.shape[1] // 2 - 70
        right_limit = frame.shape[1] // 2 + 70

        if not use_webcam:
            # 장애물 회피를 위한 ROI 디폴트 세팅하기 (현재는 10프레임만) 추가
            if frame_num < 11:
                default.default_update(depth_frame)
                continue

        print('Frame #: ', frame_num)
        #frame_size = frame.shape[:2]
        image_data = cv2.resize(frame, (input_size, input_size))
        image_data = image_data / 255.
        image_data = image_data[np.newaxis, ...].astype(np.float32)
        start_time = time.time()

        # run detections on tflite if flag is set
        if FLAGS.framework == 'tflite':
            interpreter.set_tensor(input_details[0]['index'], image_data)
            interpreter.invoke()
            pred = [
                interpreter.get_tensor(output_details[i]['index'])
                for i in range(len(output_details))
            ]
            # run detections using yolov3 if flag is set
            if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
                boxes, pred_conf = filter_boxes(pred[1],
                                                pred[0],
                                                score_threshold=0.25,
                                                input_shape=tf.constant(
                                                    [input_size, input_size]))
            else:
                boxes, pred_conf = filter_boxes(pred[0],
                                                pred[1],
                                                score_threshold=0.25,
                                                input_shape=tf.constant(
                                                    [input_size, input_size]))
        else:
            batch_data = tf.constant(image_data)
            pred_bbox = infer(batch_data)
            for _, value in pred_bbox.items():
                boxes = value[:, :, 0:4]
                pred_conf = value[:, :, 4:]

        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf,
                (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=FLAGS.iou,
            score_threshold=FLAGS.score)

        # convert data to numpy arrays and slice out unused elements
        num_objects = valid_detections.numpy()[0]
        bboxes = boxes.numpy()[0]
        bboxes = bboxes[0:int(num_objects)]
        scores = scores.numpy()[0]
        scores = scores[0:int(num_objects)]
        classes = classes.numpy()[0]
        classes = classes[0:int(num_objects)]

        # format bounding boxes from normalized ymin, xmin, ymax, xmax ---> xmin, ymin, width, height
        original_h, original_w, _ = frame.shape
        bboxes = utils.format_boxes(bboxes, original_h, original_w)

        # store all predictions in one parameter for simplicity when calling functions
        pred_bbox = [bboxes, scores, classes, num_objects]

        #fps = 1.0 / (time.time() - start_time)
        #print("1-FPS: %.2f" % fps)

        # read in all class names from config
        class_names = utils.read_class_names(cfg.YOLO.CLASSES)

        # by default allow all classes in .names file
        #allowed_classes = list(class_names.values())

        # custom allowed classes (uncomment line below to customize tracker for only people)
        allowed_classes = ['person']

        # loop through objects and use class index to get class name, allow only classes in allowed_classes list
        names = []
        deleted_indx = []
        for i in range(num_objects):
            class_indx = int(classes[i])
            class_name = class_names[class_indx]
            if class_name not in allowed_classes:
                deleted_indx.append(i)
            else:
                names.append(class_name)
        names = np.array(names)
        count = len(names)
        if FLAGS.count:
            cv2.putText(frame, "Objects being tracked: {}".format(count),
                        (5, 35), cv2.FONT_HERSHEY_COMPLEX_SMALL, 2,
                        (0, 255, 0), 2)
            print("Objects being tracked: {}".format(count))

        # delete detections that are not in allowed_classes
        bboxes = np.delete(bboxes, deleted_indx, axis=0)
        scores = np.delete(scores, deleted_indx, axis=0)

        # encode yolo detections and feed to tracker
        features = encoder(frame, bboxes)
        detections = [
            Detection(bbox, score, class_name, feature)
            for bbox, score, class_name, feature in zip(
                bboxes, scores, names, features)
        ]

        cmap = plt.get_cmap('tab20b')
        colors = [cmap(i)[:3] for i in np.linspace(0, 1, 20)]

        # run non-maxima supression
        boxs = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        classes = np.array([d.class_name for d in detections])
        indices = preprocessing.non_max_suppression(boxs, classes,
                                                    nms_max_overlap, scores)
        detections = [detections[i] for i in indices]

        # Call the tracker
        tracker.predict()
        tracker.update(detections, frame_num)

        # <st-mini 제어를 위한 Publisher code>
        go.update(x, y, z, th, speed, turn)

        # flag가 True라면 Target lost

        if tracker.lost:
            go.sendMsg(frame_num % 2)
        else:
            go.sendMsg(1)

        # 추적 알고리즘
        if len(tracker.tracks) == 0:  # 추적할 객체가 없다면 정지
            key = 'stop'
            print('There are no objects to track.')
        else:  # 추적할 객체가 있다면 동작
            for track in tracker.tracks:
                if not track.is_confirmed() or track.time_since_update > 1:
                    continue

                bbox = track.to_tlbr()
                class_name = track.get_class()

                # cx, cy 계산 추가
                w, h = int(bbox[2] - bbox[0]), int(bbox[3] - bbox[1])
                cx, cy = int(w / 2 + bbox[0]), int(h / 2 + bbox[1])

                # 사람과 로봇의 거리: person_distance
                if not use_webcam:
                    person_distance = person_dist(depth_frame, cx, cy, h)
                    print('person distance : ',
                          person_dist(depth_frame, cx, cy, h))

                # 직진 안전 구간 최대/최소값
                stable_max_dist = 2500
                stable_min_dist = 2000

                if person_distance < stable_min_dist:  # 로봇과 사람의 거리가 직진 안전 구간 최솟값보다 작을 때 정지
                    print('Too Close')
                    key = 'stop'
                else:
                    """
                    depth값 활용
                    1. Target과의 거리[전진]
                    1) 적정거리: 2.0m ~ 2.5m --> linear.x = 0.7
                    2) 위험거리: ~2.0m       --> linear.x = 0
                    3) 추격거리: 2.5m~       --> linear.x += 0.2 (적정거리가 될 때까지)

                    2. Target의 중심점을 이용해 좌우 회전
                    1) 중심점 cx, cy는 아래와 같이 구할 수 있다.
                    width = bbox[2] - bbox[0]
                    height = bbox[3] - bbox[1]
                    cx = int(width/2 + bbox[0])
                    cy = int(height/2 + bbox[1])
                    좌우 판단이기 때문에 cx만 사용.

                    2) cx값 설정 중 주의 사항
                    target이 화면 밖으로 점점 나갈수록 bbox의 좌측 상단 x좌표는 음수 혹은 frame width(여기서는 640)보다 커질 수 있다.
                    즉, cx의 값을 설정할 때 bbox[0]값이 음수 또는 640을 초과하면 좌우 회전 즉시 실시해야 함.

                    depth camera를 통해 장애물 유무를 먼저 판단하고 없다면 Target과의 거리/방향 측정 후 최종 발행값 결정.
                    """

                    # 장애물 회피용 ROI distance로 left, right string 받아오기(사람이 직진 최대 거리보다 멀때 장애물 판단)
                    # if person_distance > stable_max_dist:
                    key = obstacle_detect(default, depth_frame)
                    cv2.rectangle(frame, (cx + 10, cy - (h // 5) + 10),
                                  (cx - 10, cy - (h // 5) - 10), (255, 0, 0),
                                  5)
                    # 장애물이 없다면 사람 따라가기
                    if key == None:
                        print('key is NOT None')
                        # key,speed,turn = drive(cx, left_limit, right_limit, turn, speed)
                        # key,speed,turn = drive2(cx, left_limit, right_limit, turn, frame, speed, max_speed, max_turn)
                        # key,speed,turn = drive3(cx, left_limit, right_limit, turn, frame, speed, max_speed, min_speed, max_turn, stable_min_dist, stable_max_dist, person_distance, start_speed_down=300)
                        key, speed, turn = drive4(cx,
                                                  left_limit,
                                                  right_limit,
                                                  turn,
                                                  frame,
                                                  speed,
                                                  max_speed,
                                                  min_speed,
                                                  max_turn,
                                                  stable_min_dist,
                                                  stable_max_dist,
                                                  person_distance,
                                                  start_speed_down=300)

                # draw bbox on screen
                color = colors[int(track.track_id) % len(colors)]
                color = [i * 255 for i in color]
                cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                              (int(bbox[2]), int(bbox[3])), color, 2)
                cv2.rectangle(
                    frame, (int(bbox[0]), int(bbox[1] - 30)),
                    (int(bbox[0]) +
                     (len(class_name) + len(str(track.track_id))) * 17,
                     int(bbox[1])), color, -1)
                cv2.putText(frame, class_name + "-" + str(track.track_id),
                            (int(bbox[0]), int(bbox[1] - 10)), 0, 0.75,
                            (255, 255, 255), 2)

        # 주행 알고리즘(drive)를 거치고 나온 속도/방향을 로봇에 전달
        x, y, z, th, speed, turn = key_move(key, x, y, z, th, speed, turn)

        print('key: ', key)
        print('key_type: ', type(key))
        print('x: {}, y: {}, th: {}, speed: {}, turn: {}'.format(
            x, y, th, speed, turn))

        # 화면 중심 표시
        cv2.circle(frame, (320, 240), 10, (255, 255, 255))

        # 좌우 회전 구분선 그리기
        cv2.line(frame, (left_limit, 0), (left_limit, frame.shape[0]),
                 (255, 0, 0))
        cv2.line(frame, (right_limit, 0), (right_limit, frame.shape[0]),
                 (255, 0, 0))

        # ROS Rate sleep
        rate.sleep()
        '''
        box_center_roi = np.array((depth_frame[cy-10:cy+10, cx-10:cx+10]),dtype=np.float64)
        cv2.rectangle(frame, (cx-10, cy+10), (cx+10, cy-10), (255, 255, 255), 2)
        '''

        safe_roi = np.array([[400, 400], [240, 400], [160, 480], [480, 480]])
        #safe_roi = np.array([[240, 420], [400, 420], [480, 160], [480, 480]])
        cv2.polylines(frame, [safe_roi], True, (255, 255, 255), 2)
        cv2.rectangle(frame, (205, 445), (195, 435), (255, 0, 0), 5)
        cv2.rectangle(frame, (245, 405), (235, 395), (255, 0, 0), 5)
        cv2.rectangle(frame, (405, 405), (395, 395), (255, 0, 0), 5)
        cv2.rectangle(frame, (445, 445), (435, 435), (255, 0, 0), 5)

        # calculate frames per second of running detections
        fps = 1.0 / (time.time() - start_time)
        print("FPS: %.2f" % fps)
        # info = "time: %.2f ms" %(1000*(time.time() - start_time))
        #print(info)
        result = np.asarray(frame)
        result = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        # depth map을 칼라로 보기위함
        # depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_frame, alpha=0.03), cv2.COLORMAP_JET)

        if not FLAGS.dont_show:
            cv2.imshow("Output Video", result)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            dc.release()
            cv2.destroyAllWindows()
            break
Exemplo n.º 8
0
def main(_argv):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size

    if FLAGS.framework == 'tflite':
        interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        interpreter.allocate_tensors()
        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()
        print(input_details)
        print(output_details)
    else:
        saved_model_loaded = tf.saved_model.load(FLAGS.weights,
                                                 tags=[tag_constants.SERVING])
        infer = saved_model_loaded.signatures['serving_default']

    if FLAGS.output:
        # by default VideoCapture returns float instead of int
        width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = int(vid.get(cv2.CAP_PROP_FPS))
        codec = cv2.VideoWriter_fourcc(*FLAGS.output_format)
        out = cv2.VideoWriter(FLAGS.output, codec, fps, (width, height))

    frame_id = 0
    while not rospy.is_shutdown():
        frame = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB)
        image = Image.fromarray(frame)

        # # return_value, frame = vid.read()
        # if return_value:
        #     frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        #     image = Image.fromarray(frame)
        # else:
        #     if frame_id == vid.get(cv2.CAP_PROP_FRAME_COUNT):
        #         print("Video processing complete")
        #         break
        #     raise ValueError("No image! Try with another video format")

        frame_size = frame.shape[:2]
        image_data = cv2.resize(frame, (input_size, input_size))
        image_data = image_data / 255.
        image_data = image_data[np.newaxis, ...].astype(np.float32)
        prev_time = time.time()

        if FLAGS.framework == 'tflite':
            interpreter.set_tensor(input_details[0]['index'], image_data)
            interpreter.invoke()
            pred = [
                interpreter.get_tensor(output_details[i]['index'])
                for i in range(len(output_details))
            ]
            if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
                boxes, pred_conf = filter_boxes(pred[1],
                                                pred[0],
                                                score_threshold=0.25,
                                                input_shape=tf.constant(
                                                    [input_size, input_size]))
            else:
                boxes, pred_conf = filter_boxes(pred[0],
                                                pred[1],
                                                score_threshold=0.25,
                                                input_shape=tf.constant(
                                                    [input_size, input_size]))
        else:
            batch_data = tf.constant(image_data)
            pred_bbox = infer(batch_data)
            for key, value in pred_bbox.items():
                boxes = value[:, :, 0:4]
                pred_conf = value[:, :, 4:]

        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf,
                (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=FLAGS.iou,
            score_threshold=FLAGS.score)
        pred_bbox = [
            boxes.numpy(),
            scores.numpy(),
            classes.numpy(),
            valid_detections.numpy()
        ]

        image = utils.draw_bbox(frame, pred_bbox)

        curr_time = time.time()
        exec_time = curr_time - prev_time
        result = np.asarray(image)
        info = "time: %.2f ms" % (1000 * exec_time)
        info = "fps: %.2f" % (1. / exec_time)

        print(info)

        image_h, image_w, _ = frame.shape

        out_boxes, out_scores, out_classes, num_boxes = pred_bbox

        ROI_array_msg = ROI_array()

        for i in range(num_boxes[0]):
            if int(out_classes[0][i]) < 0 or int(
                    out_classes[0][i]) > NUM_CLASS:
                continue
            ROI_msg = ROI()
            coor = out_boxes[0][i]
            ROI_msg.min_x = int(coor[1])  #x min
            ROI_msg.min_y = int(coor[0])  #y min
            ROI_msg.Max_x = int(coor[3])  #x max
            ROI_msg.Max_y = int(coor[2])  #y max
            ROI_msg.score = float(out_scores[0][i])
            ROI_msg.object_name = str(class_name[int(out_classes[0][i])])

            ROI_array_msg.ROI_list.append(ROI_msg)

        roi_array_pub.publish(ROI_array_msg)

        result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        if not FLAGS.dis_cv2_window:
            cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
            cv2.imshow("result", result)
            if cv2.waitKey(1) & 0xFF == ord('q'): break

        if FLAGS.output:
            out.write(result)

        frame_id += 1
Exemplo n.º 9
0
if not config['Shuffle']['random']:

    SEED = config['Shuffle']['seed']
    from numpy.random import seed
    seed(SEED)
    import tensorflow as tf
    tf.random.set_seed(SEED)
    from random import seed
    seed(SEED)

#############################################SOLUCIONAR EL ERROR DE LA LIBRERIA CUDNN###################################
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

configProto = ConfigProto()
configProto.gpu_options.allow_growth = True
session = InteractiveSession(config=configProto)

#Se añade el directorio utilities a sys.path para que pueda ser usaado por el comando import
sys.path.append(os.path.join(rootdir, 'utilities'))

from os.path import join
import json
import numpy as np
from pathlib import Path

import models_Shuffle
import DataGenerators_Shuffle

from FuncionesAuxiliares import read_instance_file_txt
Exemplo n.º 10
0
def main(_argv):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    image_folder = FLAGS.image_folder
    file_list = os.listdir(image_folder)

    for process, image_name in enumerate(file_list):
        image_path = os.path.join(image_folder, image_name)
        print(image_path, '****', process + 1, '/', len(file_list))
        original_image = cv2.imread(image_path)
        original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

        # image_data = utils.image_preprocess(np.copy(original_image), [input_size, input_size])
        image_data = cv2.resize(original_image, (input_size, input_size))
        image_data = image_data / 255.
        # image_data = image_data[np.newaxis, ...].astype(np.float32)

        images_data = []
        for i in range(1):
            images_data.append(image_data)
        images_data = np.asarray(images_data).astype(np.float32)

        if FLAGS.framework == 'tflite':
            interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
            interpreter.allocate_tensors()
            input_details = interpreter.get_input_details()
            output_details = interpreter.get_output_details()
            print(input_details)
            print(output_details)
            interpreter.set_tensor(input_details[0]['index'], images_data)
            interpreter.invoke()
            pred = [
                interpreter.get_tensor(output_details[i]['index'])
                for i in range(len(output_details))
            ]
            if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
                boxes, pred_conf = filter_boxes(pred[1],
                                                pred[0],
                                                score_threshold=0.25,
                                                input_shape=tf.constant(
                                                    [input_size, input_size]))
            else:
                boxes, pred_conf = filter_boxes(pred[0],
                                                pred[1],
                                                score_threshold=0.25,
                                                input_shape=tf.constant(
                                                    [input_size, input_size]))
        else:
            saved_model_loaded = tf.saved_model.load(
                FLAGS.weights, tags=[tag_constants.SERVING])
            infer = saved_model_loaded.signatures['serving_default']
            batch_data = tf.constant(images_data)
            pred_bbox = infer(batch_data)
            for key, value in pred_bbox.items():
                boxes = value[:, :, 0:4]
                pred_conf = value[:, :, 4:]

        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf,
                (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=FLAGS.iou,
            score_threshold=FLAGS.score)
        pred_bbox = [
            boxes.numpy(),
            scores.numpy(),
            classes.numpy(),
            valid_detections.numpy()
        ]

        #######################################
        boxes = boxes.numpy().squeeze()
        scores = scores.numpy().squeeze()
        scores = np.array([scores]).T

        boxes_scores = np.concatenate((boxes, scores), axis=1)
        boxes_scores = boxes_scores[:valid_detections.numpy()[0], :]
        multiplier = np.array([
            original_image.shape[0], original_image.shape[1],
            original_image.shape[0], original_image.shape[1], 10000
        ]).T

        boxes_scores = boxes_scores * multiplier
        boxes_scores_int = np.around(boxes_scores).astype(np.int16)

        image_name = image_name.replace('.jpg', '')

        txt_path = image_folder + '/' + image_name + '.txt'
        np.savetxt(txt_path, boxes_scores_int, fmt='%i')
        # lt y,x 비율 rb y,x 비율 _score

        image = utils.draw_bbox(original_image, pred_bbox)
        # image = utils.draw_bbox(image_data*255, pred_bbox)
        image = Image.fromarray(image.astype(np.uint8))
        image.show()
        image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)

        result_path = image_folder + '/' + image_name + '.png'
        cv2.imwrite(result_path, image)
Exemplo n.º 11
0
def main(_argv):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    video_path = FLAGS.video
    # get video name by using split method
    video_name = video_path.split('/')[-1]
    video_name = video_name.split('.')[0]
    if FLAGS.framework == 'tflite':
        interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        interpreter.allocate_tensors()
        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()
        print(input_details)
        print(output_details)
    else:
        saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])
        infer = saved_model_loaded.signatures['serving_default']

    # begin video capture
    try:
        vid = cv2.VideoCapture(int(video_path))
    except:
        vid = cv2.VideoCapture(video_path)

    out = None

    if FLAGS.output:
        # by default VideoCapture returns float instead of int
        width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = int(vid.get(cv2.CAP_PROP_FPS))
        codec = cv2.VideoWriter_fourcc(*FLAGS.output_format)
        out = cv2.VideoWriter(FLAGS.output, codec, fps, (width, height))

    frame_num = 0
    while True:
        return_value, frame = vid.read()
        if return_value:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame_num += 1
            image = Image.fromarray(frame)
        else:
            print('Video has ended or failed, try a different video format!')
            break

        frame_size = frame.shape[:2]
        image_data = cv2.resize(frame, (input_size, input_size))
        image_data = image_data / 255.
        image_data = image_data[np.newaxis, ...].astype(np.float32)
        start_time = time.time()

        batch_data = tf.constant(image_data)
        pred_bbox = infer(batch_data)
        for key, value in pred_bbox.items():
            boxes = value[:, :, 0:4]
            pred_conf = value[:, :, 4:]

        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=FLAGS.iou,
            score_threshold=FLAGS.score
        )

        # format bounding boxes from normalized ymin, xmin, ymax, xmax ---> xmin, ymin, xmax, ymax
        original_h, original_w, _ = frame.shape
        bboxes = utils.format_boxes(boxes.numpy()[0], original_h, original_w)

        pred_bbox = [bboxes, scores.numpy()[0], classes.numpy()[0], valid_detections.numpy()[0]]

        # custom allowed classes (uncomment line below to allow detections for only people)
        classes = ['chair', 'person']

        # count objects found
        counted_classes = count_objects(pred_bbox, by_class = True, allowed_classes=classes)
        # loop through dict and print
        for key, value in counted_classes.items():
            print("Number of {}s: {}".format(key, value))
        image = utils.draw_bbox(frame, pred_bbox, FLAGS.info, counted_classes, allowed_classes=classes, read_plate=FLAGS.plate)
        cv2.putText(image, "Chairs Expected: 48", (5, 75),
                        cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 0, 0), 2)
        cv2.putText(image, "Students Expected: 4", (5, 50),
                        cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 0, 0), 2)

        fps = 1.0 / (time.time() - start_time)
        print("FPS: %.2f" % fps)
        cv2.putText(image, "FPS: {}".format(fps), (850, 20),
                        cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 0, 0), 2)
        result = np.asarray(image)
        cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
        result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

        if not FLAGS.dont_show:
            cv2.imshow("result", result)

        if FLAGS.output:
            out.write(result)
        if cv2.waitKey(1) & 0xFF == ord('q'): break
    cv2.destroyAllWindows()
def main(_argv):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    # image_path = FLAGS.image

    # original_image = cv2.imread(image_path)
    original_image = cv2.cvtColor(video_frame, cv2.COLOR_BGR2RGB)

    # image_data = utils.image_preprocess(np.copy(original_image), [input_size, input_size])
    image_data = cv2.resize(original_image, (input_size, input_size))
    image_data = image_data / 255.
    # image_data = image_data[np.newaxis, ...].astype(np.float32)

    images_data = []
    for i in range(1):
        images_data.append(image_data)
    images_data = np.asarray(images_data).astype(np.float32)

    if FLAGS.framework == 'tflite':
        interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        interpreter.allocate_tensors()
        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()
        print(input_details)
        print(output_details)
        interpreter.set_tensor(input_details[0]['index'], images_data)
        interpreter.invoke()
        pred = [
            interpreter.get_tensor(output_details[i]['index'])
            for i in range(len(output_details))
        ]
        if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
            boxes, pred_conf = filter_boxes(pred[1],
                                            pred[0],
                                            score_threshold=0.25,
                                            input_shape=tf.constant(
                                                [input_size, input_size]))
        else:
            boxes, pred_conf = filter_boxes(pred[0],
                                            pred[1],
                                            score_threshold=0.25,
                                            input_shape=tf.constant(
                                                [input_size, input_size]))
    else:
        saved_model_loaded = tf.saved_model.load(FLAGS.weights,
                                                 tags=[tag_constants.SERVING])
        infer = saved_model_loaded.signatures['serving_default']
        batch_data = tf.constant(images_data)
        pred_bbox = infer(batch_data)
        for key, value in pred_bbox.items():
            boxes = value[:, :, 0:4]
            pred_conf = value[:, :, 4:]

    boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
        boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
        scores=tf.reshape(
            pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
        max_output_size_per_class=50,
        max_total_size=50,
        iou_threshold=FLAGS.iou,
        score_threshold=FLAGS.score)
    final_boxes = boxes.numpy()
    final_scores = scores.numpy()
    final_classes = classes.numpy()

    array_boxes_detected = get_human_box_detection(final_boxes,
                                                   final_scores[0].tolist(),
                                                   final_classes[0].tolist(),
                                                   original_image.shape[0],
                                                   original_image.shape[1])
    print(array_boxes_detected)

    #Defining red color rgb value
    COLOR_RED = (0, 0, 255)
    for i, items in enumerate(array_boxes_detected):
        first_point = array_boxes_detected[i][0]
        second_point = array_boxes_detected[i][1]
        third_point = array_boxes_detected[i][2]
        fourth_point = array_boxes_detected[i][3]
        cv2.rectangle(original_image, (second_point, first_point),
                      (fourth_point, third_point), COLOR_RED, 2)
    image = cv2.cvtColor(np.array(original_image), cv2.COLOR_BGR2RGB)
    cv2.imshow("final", image)
    cv2.waitKey(0)
Exemplo n.º 13
0
 def create_session(self):
     print("[*] Settings config ..")
     config = ConfigProto()
     config.gpu_options.allow_growth = True
     self.session = InteractiveSession(config=config)
     print("[*] Done")
Exemplo n.º 14
0
def main(_argv):
    # Definition of the parameters
    max_cosine_distance = 0.4
    nn_budget = None
    nms_max_overlap = 1.0

    # initialize deep sort
    model_filename = 'model_data/mars-small128.pb'
    encoder = gdet.create_box_encoder(model_filename, batch_size=1)
    # calculate cosine distance metric
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    # initialize deepsort tracker
    tracker = Tracker(metric)

    # load configuration for object detector
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = 416

    # load model
    saved_model_loaded = tf.saved_model.load(FLAGS.weights,
                                             tags=[tag_constants.SERVING])
    infer = saved_model_loaded.signatures['serving_default']

    out = None

    ### csv config ###
    a = 30
    calc_stop = 0
    filename = datetime.now().strftime('%d_%m_%y')
    filename = 'data/data_log/csv_log/{}.csv'.format(filename)

    ### heatmap config ###
    heatmap = []  # heatmap which gets incremented over the day
    for i in range(len(lots)):
        numlot = [0] * len(lots[i])
        heatmap.append(numlot)

    ### server data config ###
    unique_cars = []

    ### intervall config ###
    imageProcessingInterval = 20  # default imageProcessing interval in seconds
    lastProcessed = time.time()  # last processed image
    last_round = [
    ]  # initialize last_round which stores last round taken_lot config

    # while video is running
    while True:
        # processes frame only in given time interval
        if lastProcessed + imageProcessingInterval < time.time():
            try:
                lastProcessed = time.time()

                frames = []

                vid = cv2.VideoCapture(
                    "http://96.56.250.139:8200/mjpg/video.mjpg")

                if not vid.isOpened():
                    raise IOError("We cannot open webcam", spots[s])

                return_value, frame = vid.read()
                if return_value:
                    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                    image = Image.fromarray(frame)
                else:
                    print(
                        'Video has ended or failed, try a different video format!'
                    )
                    # since the whole loop is in a try-block loop doesnt need to break
                    # which makes the script more robust for temporary camera or internet connection errors
                    #break

                # crop all camera frames to same width to add them to one window
                y = spots[0]["crop"][
                    'y']  # todo: replace '3' with 'i' for data.json loop
                x = spots[0]["crop"]['x']
                h = 600
                w = 600
                frame = frame[y:y + h, x:x + w]

                frame_size = frame.shape[:2]
                image_data = cv2.resize(frame, (input_size, input_size))
                image_data = image_data / 255.
                image_data = image_data[np.newaxis, ...].astype(np.float32)
                start_time = time.time()

                #
                batch_data = tf.constant(image_data)
                pred_bbox = infer(batch_data)
                for key, value in pred_bbox.items():
                    boxes = value[:, :, 0:4]
                    pred_conf = value[:, :, 4:]

                boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
                    boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
                    scores=tf.reshape(
                        pred_conf,
                        (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
                    max_output_size_per_class=50,
                    max_total_size=50,
                    iou_threshold=FLAGS.iou,
                    score_threshold=FLAGS.score)

                # convert data to numpy arrays and slice out unused elements
                num_objects = valid_detections.numpy()[0]
                bboxes = boxes.numpy()[0]
                bboxes = bboxes[0:int(num_objects)]
                scores = scores.numpy()[0]
                scores = scores[0:int(num_objects)]
                classes = classes.numpy()[0]
                classes = classes[0:int(num_objects)]

                # format bounding boxes from normalized ymin, xmin, ymax, xmax ---> xmin, ymin, width, height
                original_h, original_w, _ = frame.shape
                bboxes = utils.format_boxes(bboxes, original_h, original_w)

                # store all predictions in one parameter for simplicity when calling functions
                pred_bbox = [bboxes, scores, classes, num_objects]

                # read in all class names from config
                class_names = utils.read_class_names(cfg.YOLO.CLASSES)

                # by default allow all classes in .names file
                allowed_classes = list(class_names.values())

                # loop through objects and use class index to get class name
                names = []
                for i in range(num_objects):
                    class_indx = int(classes[i])
                    class_name = class_names[class_indx]
                    names.append(class_name)
                names = np.array(names)

                # encode yolo detections and feed to tracker
                features = encoder(frame, bboxes)
                detections = [
                    Detection(bbox, score, class_name, feature)
                    for bbox, score, class_name, feature in zip(
                        bboxes, scores, names, features)
                ]

                # initialize color map
                cmap = plt.get_cmap('tab20b')
                colors = [cmap(i)[:3] for i in np.linspace(0, 1, 20)]

                # run non-maxima supression
                boxs = np.array([d.tlwh for d in detections])
                scores = np.array([d.confidence for d in detections])
                classes = np.array([d.class_name for d in detections])
                indices = preprocessing.non_max_suppression(
                    boxs, classes, nms_max_overlap, scores)
                detections = [detections[i] for i in indices]

                # call the tracker
                tracker.predict()
                tracker.update(detections)

                # update tracks
                car_boxes = []  # cars as box polygon
                for track in tracker.tracks:
                    if not track.is_confirmed() or track.time_since_update > 1:
                        continue
                    bbox = track.to_tlbr()
                    class_name = track.get_class()

                    # draw bbox with tracking id on screen
                    color = colors[int(track.track_id) % len(colors)]
                    color = [i * 255 for i in color]
                    cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])),
                                  (int(bbox[2]), int(bbox[3])), color, 2)
                    cv2.rectangle(
                        frame, (int(bbox[0]), int(bbox[1] - 30)),
                        (int(bbox[0]) +
                         (len(class_name) + len(str(track.track_id))) * 17,
                         int(bbox[1])), color, -1)
                    cv2.putText(frame, class_name + "-" + str(track.track_id),
                                (int(bbox[0]), int(bbox[1] - 10)), 0, 0.5,
                                (255, 255, 255), 2)

                    xmin, ymin, xmax, ymax, car_id = int(bbox[0]), int(
                        bbox[1]), int(bbox[2]), int(bbox[3]), track.track_id
                    car = box(xmin, ymin, xmax, ymax)
                    car_boxes.append([car, car_id])

                car_boxes = np.array(car_boxes)
                taken_lots = {  # dict stores all taken lots of current analysis round
                    'lot_ids': [],
                    'car_ids': [],
                    'timespan': []
                }

                print("\n######### new round ##############")

                s = 0  # todo: add data.json-loop to loop through multiple cameras in one analysing session
                for i in range(len(
                        lots[s])):  # loop through all lots of parking space
                    # set each parking lot to 'free' as default and change if is blocked
                    lines = green
                    free = '1'
                    cords = np.array(
                        lots[s][i]['cords'], np.int32
                    )  # format coordinates to numpy array with 32-bits ints
                    x1, y1 = cords[0]
                    l = Polygon(cords)
                    lot_id = lots[s][i]['id']

                    for j in range(len(car_boxes)):  # loop through all cars
                        c = car_boxes[j][0]  # each detected car
                        car_id = car_boxes[j][1]  # each detected car
                        intsec = c.intersection(
                            l).area  # intersection of each car with this lot
                        # print(round(int))
                        r = round(intsec / l.area, 2)
                        # print(f" car-{j}: {r}")

                        ### lot is blocked ###
                        if r > .5:  # minimum percentage of how much of the lot neeeds to be blocked
                            lines = red
                            free = '0'

                            # increment heatmap index for parking-lot i
                            heatmap[s][i] += 1
                            # add car id, if new/unique to count unique visitors of day
                            if not car_id in unique_cars:
                                unique_cars.append(car_id)

                            # save taken lot information in taken_lot dict
                            taken_lots['lot_ids'].append(lot_id)
                            taken_lots['car_ids'].append(car_id)
                            # print("\nlot_id", lot_id)

                            # check if lot is still blocked by same car
                            try:
                                # car was blocking this lot already in last round
                                lot_index = last_round['lot_ids'].index(
                                    str(lot_id)
                                )  # searches for current lot in last_round
                                if car_id == last_round['car_ids'][lot_index]:
                                    # lot is still blocked by same car
                                    timespan = last_round['timespan'][
                                        lot_index]
                                    # print("still blocked by car", car_id)
                                else:
                                    # lot is blocked by new car (id)
                                    now = datetime.now()
                                    now = now.strftime('%H:%M')
                                    timespan = now  # saving blocking_time to current time
                                    # print("now blocked by car", car_id)

                            except:
                                # new car is blocking this lot
                                print("new car - lot_id", lot_id)
                                now = datetime.now()
                                now = now.strftime('%H:%M')
                                timespan = now  # saving blocking_time to current time
                                # print("now blocked by car", car_id)

                            finally:
                                # update array with appropriate timespan
                                taken_lots['timespan'].append(timespan)

                            ### finally ###
                            #taken_lots.append(lot_id)
                            break  # break car loop for this lot, bc it is blocked by a car

                    cv2.drawContours(frame, [cords], -1, lines,
                                     3)  # draw lot in correct color
                    cv2.putText(frame, lot_id, (x1 + 10, y1 + 20),
                                cv2.FONT_HERSHEY_SIMPLEX, .6, black, 1)

                #### update databases ####
                # print last updated timestamp on camerascreen
                now = datetime.now().strftime('%H:%M:%S')
                cv2.putText(frame, now, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, .6,
                            red, 2)
                if FLAGS.update_server:
                    print("Update Server:", now)
                    # update_json(spots[s]["id"], taken_lots, heatmap[s], now)
                    # update_server("daily_usage.json", len(taken_lots['lot_ids']))
                    # update_server("total_usage.json", len(unique_cars))
                    # update_server("heatmap.json", heatmap[s])

                heatmap[s] = [0] * len(lots[s])  # reset heatmap array

                # write round to csv --> if its before 00:00
                if calc_stop > time.time():
                    print("Update CSV:", now)

                    # create list for csv
                    lot_ids = taken_lots['lot_ids']
                    car_ids = taken_lots['car_ids']
                    csv_row = [datetime.now().strftime('%H:%M')]

                    for i in range(len(lots[s])):
                        try:
                            ind = lot_ids.index(str(i))
                            csv_row.append(car_ids[ind])
                            # print("added")

                        except ValueError:
                            csv_row.append(0)
                            # print("not inside")

                    # Append a list as new line to an old csv file
                    append_csv_as_row(filename, csv_row)

                # create new csv and start round for day
                else:
                    unique_cars = []  # reset unique car count on midnight

                    print("reset Heatmap")

                    FMT = '%H:%M:%S'

                    a += 10  #debug: create new csv every x minutes
                    release_time = '00:00:00'.format(a)  # end time
                    #release_time = '19:00:00'.format(a)  # end time
                    release_time = datetime.strptime(release_time, FMT)

                    now = datetime.now().strftime("%H:%M:%S")
                    now = datetime.strptime(now, FMT)

                    tdelta = release_time - now
                    calc_stop = time.time() + tdelta.seconds
                    #print("seconds until calc_stop:", tdelta.seconds)

                    print("create new CSV")
                    filename = datetime.now().strftime('%d_%m_%y')
                    filename = 'data/data_log/csv_log/{}.csv'.format(filename)
                    print(filename)

                    with open(filename, 'a+', newline='') as write_obj:
                        # Create a writer object from csv module
                        list_of_elem = ['timestamp']
                        for i in range(len(lots[s])):
                            list_of_elem.append("lot{}".format(i))
                        csv_writer = csv.writer(write_obj)
                        # Add contents of list as last row in the csv file
                        csv_writer.writerow(list_of_elem)
                        write_obj.close()

                last_round = taken_lots  # save taken_lots of round in "last" round

                # add analysed frame to window of all analysed cameras from data.json
                frames.append(frame)

                if FLAGS.fps:
                    # calculate frames per second of running detections
                    fps = 1.0 / (time.time() - start_time)
                    print("FPS: %.2f" % fps)

                result = np.asarray(frame)
                result = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
                if not FLAGS.dont_show:
                    cv2.imshow("Parking Lot Video", result)

                if FLAGS.save_img:  # save analysed img to local directory for manual evaluation of accuracy
                    save_analysed_img(result)

            except Exception as err:
                print("----- Couldn't process frame ----")
                print(err)

            if cv2.waitKey(1) & 0xFF == ord('q'): break
    cv2.destroyAllWindows()
Exemplo n.º 15
0
def configure_gpu():
    conf = ConfigProto()
    conf.gpu_options.allow_growth = True
    conf.gpu_options.per_process_gpu_memory_fraction = config.PER_PROCESS_GPU_MEMORY_FRACTION
    session = InteractiveSession(config=conf)
Exemplo n.º 16
0
def main(_argv):
    avg = []
    # Definition of the parameters
    max_cosine_distance = 0.4
    nn_budget = None
    nms_max_overlap = 1.0
    #regression model load
    weight_path = './2_input_model_2-3.5%/'
    loaded_model = tf.keras.models.load_model(weight_path)

    # initialize deep sort
    model_filename = 'model_data/mars-small128.pb'
    encoder = gdet.create_box_encoder(model_filename, batch_size=1)
    # calculate cosine distance metric
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    # initialize tracker
    tracker = Tracker(metric)

    # load configuration for object detector
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    video_path = FLAGS.video

    # load tflite model if flag is set
    if FLAGS.framework == 'tflite':
        interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        interpreter.allocate_tensors()
        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()
        print(input_details)
        print(output_details)
    # otherwise load standard tensorflow saved model
    else:
        saved_model_loaded = tf.saved_model.load(FLAGS.weights,
                                                 tags=[tag_constants.SERVING])
        infer = saved_model_loaded.signatures['serving_default']

    # begin video capture
    try:
        vid = cv2.VideoCapture(int(video_path))
    except:
        vid = cv2.VideoCapture(video_path)

    out = None

    # get video ready to save locally if flag is set
    if FLAGS.output:
        # by default VideoCapture returns float instead of int
        width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = int(vid.get(cv2.CAP_PROP_FPS))
        codec = cv2.VideoWriter_fourcc(*FLAGS.output_format)
        out = cv2.VideoWriter(FLAGS.output, codec, fps, (width, height))

    frame_num = 0
    # while video is running
    while True:
        return_value, frame = vid.read()
        if return_value:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            image = Image.fromarray(frame)
        else:
            print('Video has ended or failed, try a different video format!')
            break
        frame_num += 1
        print('Frame #: ', frame_num)
        frame_size = frame.shape[:2]
        image_data = cv2.resize(frame, (input_size, input_size))
        image_data = image_data / 255.
        image_data = image_data[np.newaxis, ...].astype(np.float32)
        start_time = time.time()

        # run detections on tflite if flag is set
        if FLAGS.framework == 'tflite':
            interpreter.set_tensor(input_details[0]['index'], image_data)
            interpreter.invoke()
            pred = [
                interpreter.get_tensor(output_details[i]['index'])
                for i in range(len(output_details))
            ]
            # run detections using yolov3 if flag is set
            if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
                boxes, pred_conf = filter_boxes(pred[1],
                                                pred[0],
                                                score_threshold=0.25,
                                                input_shape=tf.constant(
                                                    [input_size, input_size]))
            else:
                boxes, pred_conf = filter_boxes(pred[0],
                                                pred[1],
                                                score_threshold=0.25,
                                                input_shape=tf.constant(
                                                    [input_size, input_size]))
        else:
            batch_data = tf.constant(image_data)
            pred_bbox = infer(batch_data)
            for key, value in pred_bbox.items():
                boxes = value[:, :, 0:4]
                pred_conf = value[:, :, 4:]

        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf,
                (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=FLAGS.iou,
            score_threshold=FLAGS.score)

        # convert data to numpy arrays and slice out unused elements
        num_objects = valid_detections.numpy()[0]
        bboxes = boxes.numpy()[0]
        bboxes = bboxes[0:int(num_objects)]
        scores = scores.numpy()[0]
        scores = scores[0:int(num_objects)]
        classes = classes.numpy()[0]
        classes = classes[0:int(num_objects)]

        # format bounding boxes from normalized ymin, xmin, ymax, xmax ---> xmin, ymin, width, height
        original_h, original_w, _ = frame.shape
        bboxes = utils.format_boxes(bboxes, original_h, original_w)

        # store all predictions in one parameter for simplicity when calling functions
        pred_bbox = [bboxes, scores, classes, num_objects]
        #print("pred_bbox: ",pred_bbox[0])
        #print("scores: ",pred_bbox[1])
        #print("classes :",pred_bbox[2])
        #print("num :",pred_bbox[3])
        #print("width :",width)
        #print("height :",height)
        # read in all class names from config
        class_names = utils.read_class_names(cfg.YOLO.CLASSES)

        # by default allow all classes in .names file
        allowed_classes = list(class_names.values())

        # custom allowed classes (uncomment line below to customize tracker for only people)
        #allowed_classes = ['person']

        # loop through objects and use class index to get class name, allow only classes in allowed_classes list
        names = []
        deleted_indx = []
        for i in range(num_objects):
            class_indx = int(classes[i])
            class_name = class_names[class_indx]
            if class_name not in allowed_classes:
                deleted_indx.append(i)
            else:
                names.append(class_name)
        names = np.array(names)
        count = len(names)
        if FLAGS.count:
            cv2.putText(frame, "Objects being tracked: {}".format(count),
                        (5, 35), cv2.FONT_HERSHEY_COMPLEX_SMALL, 2,
                        (0, 255, 0), 2)
            print("Objects being tracked: {}".format(count))
        # delete detections that are not in allowed_classes
        bboxes = np.delete(bboxes, deleted_indx, axis=0)
        scores = np.delete(scores, deleted_indx, axis=0)

        # encode yolo detections and feed to tracker
        features = encoder(frame, bboxes)
        detections = [
            Detection(bbox, score, class_name, feature)
            for bbox, score, class_name, feature in zip(
                bboxes, scores, names, features)
        ]

        #initialize color map
        cmap = plt.get_cmap('tab20b')
        colors = [cmap(i)[:3] for i in np.linspace(0, 1, 20)]

        # run non-maxima supression
        boxs = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        classes = np.array([d.class_name for d in detections])
        #print("boxs ",boxs)
        #print("scores ",scores)
        #print("classes ",classes)
        indices = preprocessing.non_max_suppression(boxs, classes,
                                                    nms_max_overlap, scores)
        #print("indices ",indices)
        detections = [detections[i] for i in indices]

        # Call the tracker
        tracker.predict()
        tracker.update(detections)
        cv2.putText(frame, "using regress", (5, 35),
                    cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (255, 0, 255), 2)
        #cv2.putText(frame, "Objects being detected: {}".format(count), (5, 350), cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (0, 0, 255), 2)
        cv2.putText(frame, "frame# {}".format(frame_num), (750, 35),
                    cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (255, 0, 255), 2)

        # update tracks
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlbr()
            class_name = track.get_class()
            if 'entrance' not in classes:
                if len(classes) > 1:
                    if (contains_duplicates(classes) == False):
                        #color = (50, 89, 170)
                        check_rect = 0
                        width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
                        height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
                        ########## set sticker as low priority#############
                        if ((classes[0] == 'mat' or 'sensor')
                                and (classes[1] == 'mat' or 'sensor')):
                            print(
                                "*************NO STK**********************************"
                            )
                            color = (50, 89, 170)
                            x1, y1, x2, y2 = convert2(
                                width, height, int(boxs[0][0]),
                                int(boxs[0][1]), int(boxs[0][0] + boxs[0][2]),
                                int(boxs[0][1] +
                                    boxs[0][3]))  #xywh to xmin ymin xmax ymax
                            x3, y3, x4, y4 = convert2(
                                width, height, int(bboxes[1][0]),
                                int(bboxes[1][1]),
                                int(bboxes[1][0] + bboxes[1][2]),
                                int(bboxes[1][1] + bboxes[1][3]
                                    ))  #xywh to xmin ymin xmax ymax
                            reg_input = np.array([[
                                class_index(classes[0]), x1, y1, x2, y2,
                                class_index(classes[1]), x3, y3, x4, y4
                            ]])
                            predictions = loaded_model.predict(reg_input)
                            a1_pred = predictions[0]
                            b1_pred = predictions[1]
                            c1_pred = predictions[2]
                            d1_pred = predictions[3]
                            xmin, xmax, ymin, ymax = convert(
                                width, height, a1_pred, b1_pred, c1_pred,
                                d1_pred)
                            start_point = (xmin, ymin)
                            end_point = (xmax, ymax)
                            rect1 = xmax - xmin
                            rect2 = ymax - ymin
                            check_rect = rect2 / rect1
                        ################ else condition for sticker ######
                        else:
                            print(
                                "*************USE STK**********************************"
                            )
                            if ((classes[0] == 'famSticker' or 'okmartSticker'
                                 or 'sevenSticker')):
                                color = (60, 120, 40)
                                x1, y1, x2, y2 = convert2(
                                    width, height, int(boxs[0][0]),
                                    int(boxs[0][1]),
                                    int(boxs[0][0] + boxs[0][2]),
                                    int(boxs[0][1] + boxs[0][3])
                                )  #xywh to xmin ymin xmax ymax
                                x3, y3, x4, y4 = convert2(
                                    width, height, int(bboxes[1][0]),
                                    int(bboxes[1][1]),
                                    int(bboxes[1][0] + bboxes[1][2]),
                                    int(bboxes[1][1] + bboxes[1][3])
                                )  #xywh to xmin ymin xmax ymax
                                reg_input = np.array([[
                                    class_index(classes[0]), x1, y1, x2, y2,
                                    class_index(classes[1]), x3, y3, x4, y4
                                ]])

                                #### rario ####
                                C1_x = boxs[0][0] + (boxs[0][2] / 2)
                                C1_y = boxs[0][1] + (boxs[0][3] / 2)
                                C2_x = bboxes[1][0] + (bboxes[1][2] / 2)
                                C2_y = bboxes[1][1] + (bboxes[1][3] / 2)
                                Dx = (C2_x - C1_x)
                                Dy = (C2_y - C1_y)
                                #The two rectangles do not intersect, and there are two rectangles partially overlapping in the X-axis direction. The minimum distance is the distance between the lower line of the upper rectangle and the upper line of the lower rectangle
                                if ((Dx <
                                     ((int(boxs[0][0] + boxs[0][2]) +
                                       int(bboxes[1][0] + bboxes[1][2])) / 2))
                                        and
                                    (Dy >=
                                     ((int(boxs[0][1] + boxs[0][3]) +
                                       rint(bboxes[1][1] + bboxes[1][3])) /
                                      2))):
                                    min_dist = Dy - (
                                        (int(boxs[0][1] + boxs[0][3]) +
                                         int(bboxes[1][1] + bboxes[1][3])) / 2)

                        #The two rectangles do not intersect. There are two partially overlapping rectangles in the Y-axis direction. The minimum distance is the distance between the right line of the left rectangle and the left line of the right rectangle
                                elif (
                                    (Dx >=
                                     ((int(boxs[0][0] + boxs[0][2]) +
                                       int(bboxes[1][0] + bboxes[1][2])) / 2))
                                        and
                                    (Dy < ((int(boxs[0][1] + boxs[0][3]) +
                                            int(bboxes[1][1] + bboxes[1][3])) /
                                           2))):
                                    min_dist = Dx - (
                                        (int(boxs[0][0] + boxs[0][2]) +
                                         int(bboxes[1][0] + bboxes[1][2])) / 2)

                        #Two rectangles do not intersect, two rectangles that do not overlap in the X-axis and Y-axis directions, the minimum distance is the distance between the two closest vertices,
                        # Using the Pythagorean theorem, it is easy to calculate this distance
                                elif (
                                    (Dx >=
                                     ((int(boxs[0][0] + boxs[0][2]) +
                                       int(bboxes[1][0] + bboxes[1][2])) / 2))
                                        and
                                    (Dy >=
                                     ((int(boxs[0][1] + boxs[0][3]) +
                                       int(bboxes[1][1] + bboxes[1][3])) /
                                      2))):
                                    delta_x = Dx - (
                                        (int(boxs[0][0] + boxs[0][2]) +
                                         int(bboxes[1][0] + bboxes[1][2])) / 2)
                                    delta_y = Dy - (
                                        (int(boxs[0][1] + boxs[0][3]) +
                                         int(bboxes[1][1] + bboxes[1][3])) / 2)
                                    min_dist = sqrt(delta_x * delta_x +
                                                    delta_y * delta_y)
                        #The intersection of two rectangles, the minimum distance is negative, return -1
                                else:
                                    min_dist = -1
                                if (classes[1] == 'mat'):
                                    if ((min_dist / Dy) < 3):
                                        predictions = loaded_model.predict(
                                            reg_input)
                                        a1_pred = predictions[0]
                                        b1_pred = predictions[1]
                                        c1_pred = predictions[2]
                                        d1_pred = predictions[3]
                                        xmin, xmax, ymin, ymax = convert(
                                            width, height, a1_pred, b1_pred,
                                            c1_pred, d1_pred)
                                        start_point = (xmin, ymin)
                                        end_point = (xmax, ymax)
                                        rect1 = xmax - xmin
                                        rect2 = ymax - ymin
                                        check_rect = rect2 / rect1
                                    else:
                                        print("not predict")
                                elif (classes[1] == 'sensor'):
                                    if ((min_dist / Dx) < 3):
                                        predictions = loaded_model.predict(
                                            reg_input)
                                        a1_pred = predictions[0]
                                        b1_pred = predictions[1]
                                        c1_pred = predictions[2]
                                        d1_pred = predictions[3]
                                        xmin, xmax, ymin, ymax = convert(
                                            width, height, a1_pred, b1_pred,
                                            c1_pred, d1_pred)
                                        start_point = (xmin, ymin)
                                        end_point = (xmax, ymax)
                                        rect1 = xmax - xmin
                                        rect2 = ymax - ymin
                                        check_rect = rect2 / rect1
                                    else:
                                        print("not predict")
                            elif ((classes[1] == 'famSticker'
                                   or 'okmartSticker' or 'sevenSticker')):
                                color = (60, 120, 40)
                                x1, y1, x2, y2 = convert2(
                                    width, height, int(boxs[0][0]),
                                    int(boxs[0][1]),
                                    int(boxs[0][0] + boxs[0][2]),
                                    int(boxs[0][1] + boxs[0][3])
                                )  #xywh to xmin ymin xmax ymax
                                x3, y3, x4, y4 = convert2(
                                    width, height, int(bboxes[1][0]),
                                    int(bboxes[1][1]),
                                    int(bboxes[1][0] + bboxes[1][2]),
                                    int(bboxes[1][1] + bboxes[1][3])
                                )  #xywh to xmin ymin xmax ymax
                                reg_input = np.array([[
                                    class_index(classes[0]), x1, y1, x2, y2,
                                    class_index(classes[1]), x3, y3, x4, y4
                                ]])

                                #### rario ####
                                C1_x = boxs[0][0] + (boxs[0][2] / 2)
                                C1_y = boxs[0][1] + (boxs[0][3] / 2)
                                C2_x = bboxes[1][0] + (bboxes[1][2] / 2)
                                C2_y = bboxes[1][1] + (bboxes[1][3] / 2)
                                Dx = (C2_x - C1_x)
                                Dy = (C2_y - C1_y)
                                #The two rectangles do not intersect, and there are two rectangles partially overlapping in the X-axis direction. The minimum distance is the distance between the lower line of the upper rectangle and the upper line of the lower rectangle
                                if ((Dx <
                                     ((int(boxs[0][0] + boxs[0][2]) +
                                       int(bboxes[1][0] + bboxes[1][2])) / 2))
                                        and
                                    (Dy >=
                                     ((int(boxs[0][1] + boxs[0][3]) +
                                       rint(bboxes[1][1] + bboxes[1][3])) /
                                      2))):
                                    min_dist = Dy - (
                                        (int(boxs[0][1] + boxs[0][3]) +
                                         int(bboxes[1][1] + bboxes[1][3])) / 2)

                            #The two rectangles do not intersect. There are two partially overlapping rectangles in the Y-axis direction. The minimum distance is the distance between the right line of the left rectangle and the left line of the right rectangle
                                elif (
                                    (Dx >=
                                     ((int(boxs[0][0] + boxs[0][2]) +
                                       int(bboxes[1][0] + bboxes[1][2])) / 2))
                                        and
                                    (Dy < ((int(boxs[0][1] + boxs[0][3]) +
                                            int(bboxes[1][1] + bboxes[1][3])) /
                                           2))):
                                    min_dist = Dx - (
                                        (int(boxs[0][0] + boxs[0][2]) +
                                         int(bboxes[1][0] + bboxes[1][2])) / 2)

                            #Two rectangles do not intersect, two rectangles that do not overlap in the X-axis and Y-axis directions, the minimum distance is the distance between the two closest vertices,
                            # Using the Pythagorean theorem, it is easy to calculate this distance
                                elif (
                                    (Dx >=
                                     ((int(boxs[0][0] + boxs[0][2]) +
                                       int(bboxes[1][0] + bboxes[1][2])) / 2))
                                        and
                                    (Dy >=
                                     ((int(boxs[0][1] + boxs[0][3]) +
                                       int(bboxes[1][1] + bboxes[1][3])) /
                                      2))):
                                    delta_x = Dx - (
                                        (int(boxs[0][0] + boxs[0][2]) +
                                         int(bboxes[1][0] + bboxes[1][2])) / 2)
                                    delta_y = Dy - (
                                        (int(boxs[0][1] + boxs[0][3]) +
                                         int(bboxes[1][1] + bboxes[1][3])) / 2)
                                    min_dist = sqrt(delta_x * delta_x +
                                                    delta_y * delta_y)
                            #The intersection of two rectangles, the minimum distance is negative, return -1
                                else:
                                    min_dist = -1
                                if (classes[0] == 'mat'):
                                    if ((min_dist / Dy) < 3):
                                        predictions = loaded_model.predict(
                                            reg_input)
                                        a1_pred = predictions[0]
                                        b1_pred = predictions[1]
                                        c1_pred = predictions[2]
                                        d1_pred = predictions[3]
                                        xmin, xmax, ymin, ymax = convert(
                                            width, height, a1_pred, b1_pred,
                                            c1_pred, d1_pred)
                                        start_point = (xmin, ymin)
                                        end_point = (xmax, ymax)
                                        rect1 = xmax - xmin
                                        rect2 = ymax - ymin
                                        check_rect = rect2 / rect1
                                    else:
                                        print("not predict")
                                elif (classes[0] == 'sensor'):
                                    if ((min_dist / Dx) < 3):
                                        predictions = loaded_model.predict(
                                            reg_input)
                                        a1_pred = predictions[0]
                                        b1_pred = predictions[1]
                                        c1_pred = predictions[2]
                                        d1_pred = predictions[3]
                                        xmin, xmax, ymin, ymax = convert(
                                            width, height, a1_pred, b1_pred,
                                            c1_pred, d1_pred)
                                        start_point = (xmin, ymin)
                                        end_point = (xmax, ymax)
                                        rect1 = xmax - xmin
                                        rect2 = ymax - ymin
                                        check_rect = rect2 / rect1
                                    else:
                                        print("not predict")

                    ##############

                    ##########################################
                    ######## check door size and display #########if check_rect>1 and frame_num !=104:

                        print("check_rect:{}".format(check_rect))
                        if check_rect > 1:
                            blk = np.zeros(frame.shape, np.uint8)
                            cv2.rectangle(blk, start_point, end_point, color,
                                          cv2.FILLED)
                            frame = cv2.addWeighted(frame, 1.0, blk, 0.5, 1)
                            print(
                                "predict_BBox Coords (xmin, ymin, xmax, ymax): {}"
                                .format((xmin, ymin, xmax, ymax)))
                        else:
                            print("not show predicted bbox")
                        ###############################
            ########
            #      select one entrace
            ########
            #if classes.count('entrance')>1:
            #    entrance_num=[]
            #    iou_list=[]
            #    iou_check=[]
            #    for i in range(len(classes)):
            #        if classes[i]=='entrance'
            #        entrance_num.append(i)
            #        if len(classes)>1:
            #            if(contains_duplicates(classes)==False):
            #                color = (50, 89, 170)
            #                width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
            #                height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
            #                x1,y1,x2,y2=convert2(width,height,int(boxs[0][0]),int(boxs[0][1]),int(boxs[0][0]+boxs[0][2]),int(boxs[0][1]+boxs[0][3]))#xywh to xmin ymin xmax ymax
            #                x3,y3,x4,y4=convert2(width,height,int(bboxes[1][0]),int(bboxes[1][1]),int(bboxes[1][0]+bboxes[1][2]),int(bboxes[1][1]+bboxes[1][3]))#xywh to xmin ymin xmax ymax
            #                reg_input=np.array([[class_index(classes[0]),x1,y1,x2,y2,class_index(classes[1]),x3,y3,x4,y4]])
            #                predictions = loaded_model.predict(reg_input)
            #                a1_pred = predictions[0]
            #                b1_pred = predictions[1]
            #                c1_pred = predictions[2]
            #                d1_pred = predictions[3]
            #                xmin,xmax,ymin,ymax=convert(width,height,a1_pred,b1_pred,c1_pred,d1_pred)
            #                ###IOU###
            #                GT_bbox_area = (xmax -  xmin + 1) * (  ymax -ymin + 1)
            #                ###########
            #                ##check entrace##
            #                Pred_bbox_area =(x_bottomright_p - x_topleft_p + 1 ) * ( y_bottomright_p -y_topleft_p + 1)
            #                x_top_left =np.max([x_topleft_gt, x_topleft_p])
            #                y_top_left = np.max([y_topleft_gt, y_topleft_p])
            #                x_bottom_right = np.min([x_bottomright_gt, x_bottomright_p])
            #                y_bottom_right = np.min([y_bottomright_gt, y_bottomright_p])
            #
            #                intersection_area = (x_bottom_right- x_top_left + 1) * (y_bottom_right-y_top_left  + 1)
            #
            #                union_area = (GT_bbox_area + Pred_bbox_area - intersection_area)
            #
            #                iou_check.append(intersection_area/union_area)
            #
            #        for j in len(iou_check):
            #           if entrance_num[j]<iou_check.max:
            #               track.delete
            #if(int(track.track_id)>=3 or (int(track.track_id)>10 and int(track.track_id)<20 ) ):
            #frame_num
            ###################### draw bbox on screen
            color = colors[int(track.track_id) % len(colors)]
            color = [i * 255 for i in color]

            if (class_name == 'entrance'):
                if (int(track.track_id) == 1 and frame_num > 121):
                    print("skip Tracker ID: {}, Class: {}".format(
                        str(track.track_id), class_name))
                else:
                    print("RED Tracker ID: {}, Class: {}".format(
                        str(track.track_id), class_name))
                    blk = np.zeros(frame.shape, np.uint8)
                    cv2.rectangle(blk,
                                  (int(bbox[0] * 1.05), int(bbox[1] * 1.05)),
                                  (int(bbox[2] * 0.95), int(bbox[3] * 0.95)),
                                  (255, 0, 0), cv2.FILLED)
                    frame = cv2.addWeighted(frame, 1.0, blk, 0.5, 1)
                    cv2.rectangle(frame,
                                  (int(bbox[0] * 1.05), int(bbox[1] * 1.05)),
                                  (int(bbox[2] * 0.95), int(bbox[3] * 0.95)),
                                  color, 2)
                    cv2.rectangle(
                        frame, (int(bbox[0] * 1.05), int(bbox[1] * 1.05 - 30)),
                        (int(bbox[0] * 1.05) +
                         (len(class_name) + len(str(track.track_id))) * 17,
                         int(bbox[1] * 1.05)), color, -1)
                    cv2.putText(
                        frame, class_name + "-" + str(track.track_id),
                        (int(bbox[0] * 1.05), int(bbox[1] * 1.05 - 10)), 0,
                        0.75, (255, 255, 255), 2)

        # if enable info flag then print details about each track
            if FLAGS.info:
                print(
                    "Tracker ID: {}, Class: {},  BBox Coords (xmin, ymin, xmax, ymax): {}"
                    .format(str(track.track_id), class_name, (int(
                        bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]))))

        # calculate frames per second of running detections
        fps = 1.0 / (time.time() - start_time)
        print("FPS: %.2f" % fps)
        avg.append(fps)
        print("avg fps {}".format(statistics.mean(avg)))
        cv2.putText(frame, "FPS: %.2f" % fps, (50, 500),
                    cv2.FONT_HERSHEY_COMPLEX_SMALL, 2, (66, 245, 141), 2)
        result = np.asarray(frame)
        result = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

        if not FLAGS.dont_show:
            cv2.imshow("Output Video", result)

        # if output flag is set, save video file
        if FLAGS.output:
            out.write(result)
        if cv2.waitKey(1) & 0xFF == ord('q'): break
    cv2.destroyAllWindows()
Exemplo n.º 17
0
def pre():
    from tensorflow.compat.v1 import ConfigProto
    from tensorflow.compat.v1 import InteractiveSession
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    InteractiveSession(config=config)
from numpy import save
from numpy import load
import pandas as pd
from sklearn.model_selection import train_test_split
import tensorflow as tf

import pylab
import matplotlib.pyplot as plt
import requests

import os
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = "0,1"  #選擇哪一塊gpu
config = ConfigProto()
config.allow_soft_placement = True  #如果你指定的設備不存在,允許TF自動分配設備
config.gpu_options.per_process_gpu_memory_fraction = 0.9  #分配百分之七十的顯存給程序使用,避免內存溢出,可以自己調整
config.gpu_options.allow_growth = True  #按需分配顯存,這個比較重要
session = InteractiveSession(config=config)

action_name = ["down", "up", "walk", "run", "raise"]
action_name_test = [
    "down_test", "up_test", "walk_test", "run_test", "raise_test"
]
action_mix = ["mix"]

dirname = "json_data_non_seperate"
frame = 50
shift = 3
epochs = 100
Exemplo n.º 19
0
def main(_argv):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    save_trt()
def inference(preprocess_queue, inference_queue):


    
    


    import tensorflow as tf
    import core.utils as utils
    
    from tensorflow.python.saved_model import tag_constants
    from tensorflow.compat.v1 import InteractiveSession
    from tensorflow.compat.v1 import ConfigProto
    from core.functions import count_objects, crop_objects 
    from core.config import cfg
    from core.utils import read_class_names
    import os
    import random
    from core.yolov4 import filter_boxes

    

    tf.keras.backend.clear_session()


    
    input_size = Parameters.input_size


    model = OutsourceContract.model
    framework = Parameters.framework
    tiny = OutsourceContract.tiny
    weights = Parameters.weights
    iou = Parameters.iou
    score = Parameters.score

    physical_devices = tf.config.experimental.list_physical_devices('GPU')

    try:
        if len(physical_devices) > 0:
            tf.config.experimental.set_memory_growth(physical_devices[0], True)
    except:
        pass

        # configure gpu usage
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)

    # load model
    if framework == 'tflite':
        interpreter = tf.lite.Interpreter(model_path=weights)
    else:
        saved_model_loaded = tf.saved_model.load(
            weights, tags=[tag_constants.SERVING])

    # read in all class names from config
    class_names = utils.read_class_names(cfg.YOLO.CLASSES)

    count = Parameters.count
    info = Parameters.info
    crop = Parameters.crop

    while True:
        if not preprocess_queue.empty():
            queueData = preprocess_queue.get()
            while not preprocess_queue.empty():
                queueData = preprocess_queue.get()
            #preprocess_queue.task_done()
            images_data = queueData[0]
            name = queueData[1]
            original_image = queueData[2]

            #preprocess_queue.task_done()

            if framework == 'tflite':
                interpreter.allocate_tensors()
                input_details = interpreter.get_input_details()
                output_details = interpreter.get_output_details()
                interpreter.set_tensor(input_details[0]['index'], images_data)
                interpreter.invoke()
                pred = [interpreter.get_tensor(
                    output_details[i]['index']) for i in range(len(output_details))]
                if model == 'yolov3' and tiny == True:
                    boxes, pred_conf = filter_boxes(
                        pred[1], pred[0], score_threshold=0.25, input_shape=tf.constant([input_size, input_size]))
                else:
                    boxes, pred_conf = filter_boxes(
                        pred[0], pred[1], score_threshold=0.25, input_shape=tf.constant([input_size, input_size]))
            else:
                infer = saved_model_loaded.signatures['serving_default']
                batch_data = tf.constant(images_data)
                pred_bbox = infer(batch_data)
                for key, value in pred_bbox.items():
                    boxes = value[:, :, 0:4]
                    pred_conf = value[:, :, 4:]


            boxes, scores, classes, valid_detections=tf.image.combined_non_max_suppression(
                boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
                scores=tf.reshape(
                    pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
                max_output_size_per_class=50,
                max_total_size=50,
                iou_threshold=iou,
                score_threshold=score
            )  # 1.2ms


            # format bounding boxes from normalized ymin, xmin, ymax, xmax ---> xmin, ymin, xmax, ymax
            
            original_h, original_w, _=original_image.shape

            bboxes=utils.format_boxes(
                boxes.numpy()[0], original_h, original_w)  # 1ms #-> no tf needed

            # hold all detection data in one variable
            pred_bbox=[bboxes, scores.numpy()[0], classes.numpy()[0],
                        valid_detections.numpy()[0]]

            # by default allow all classes in .names file
            allowed_classes=list(class_names.values())

            # custom allowed classes (uncomment line below to allow detections for only people)
            # allowed_classes = ['person']

            # if crop flag is enabled, crop each detection and save it as new image
            if crop:
                crop_path=os.path.join(
                    os.getcwd(), 'detections', 'crop', image_name)
                try:
                    os.mkdir(crop_path)
                except FileExistsError:
                    pass
                crop_objects(cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB),
                            pred_bbox, crop_path, allowed_classes)

            if count:
                # count objects found
                counted_classes=count_objects(
                    pred_bbox, by_class=False, allowed_classes=allowed_classes)
                # loop through dict and print
                for key, value in counted_classes.items():
                    print("Number of {}s: {}".format(key, value))
                boxtext, image=utils.draw_bbox(
                    original_image, pred_bbox, info, counted_classes, allowed_classes=allowed_classes)
            else:
                boxtext, image=utils.draw_bbox(
                    original_image, pred_bbox, info, allowed_classes=allowed_classes)  # 0.5ms

            image=Image.fromarray(image.astype(np.uint8))  # 0.3ms


            inference_queue.put((boxtext, image, name))
Exemplo n.º 21
0
def main(_argv):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size

    if FLAGS.framework == 'tflite':
        interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        interpreter.allocate_tensors()
        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()
        print(input_details)
        print(output_details)
    else:
        saved_model_loaded = tf.saved_model.load(FLAGS.weights,
                                                 tags=[tag_constants.SERVING])
        infer = saved_model_loaded.signatures['serving_default']

    out = None

    # cho camera
    # vid = cv2.VideoCapture('rtsp://*****:*****@192.168.1.180:554/cam/realmonitor?channel=1&subtype=1')

    # cho webcam
    vid = cv2.VideoCapture(0)

    if FLAGS.output:
        # by default VideoCapture returns float instead of int
        width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = int(vid.get(cv2.CAP_PROP_FPS))
        codec = cv2.VideoWriter_fourcc(*FLAGS.output_format)
        out = cv2.VideoWriter(FLAGS.output, codec, fps, (width, height))

    frame_num = 0

    while True:
        return_value, frame = vid.read()

        if return_value:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame_num += 1
            image = Image.fromarray(frame)

        else:
            print('Video has ended or failed, try a different video format!')
            break

        frame_size = frame.shape[:2]
        image_data = cv2.resize(frame, (input_size, input_size))
        image_data = image_data / 255.
        image_data = image_data[np.newaxis, ...].astype(np.float32)

        if FLAGS.framework == 'tflite':
            interpreter.set_tensor(input_details[0]['index'], image_data)
            interpreter.invoke()
            pred = [
                interpreter.get_tensor(output_details[i]['index'])
                for i in range(len(output_details))
            ]
            if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
                boxes, pred_conf = filter_boxes(pred[1],
                                                pred[0],
                                                score_threshold=0.25,
                                                input_shape=tf.constant(
                                                    [input_size, input_size]))
            else:
                boxes, pred_conf = filter_boxes(pred[0],
                                                pred[1],
                                                score_threshold=0.25,
                                                input_shape=tf.constant(
                                                    [input_size, input_size]))
        else:
            batch_data = tf.constant(image_data)
            pred_bbox = infer(batch_data)
            for key, value in pred_bbox.items():
                boxes = value[:, :, 0:4]
                pred_conf = value[:, :, 4:]

        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf,
                (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=FLAGS.iou,
            score_threshold=FLAGS.score)

        # format bounding boxes from normalized ymin, xmin, ymax, xmax ---> xmin, ymin, xmax, ymax
        # định dạng các hộp giới hạn từ chuẩn hóa ymin, xmin, ymax, xmax ---> xmin, ymin, xmax, ymax
        original_h, original_w, _ = frame.shape
        bboxes = utils.format_boxes(boxes.numpy()[0], original_h, original_w)

        pred_bbox = [
            bboxes,
            scores.numpy()[0],
            classes.numpy()[0],
            valid_detections.numpy()[0]
        ]

        # read in all class names from config
        # đọc tất cả các tên lớp từ cấu hình
        class_names = utils.read_class_names(cfg.YOLO.CLASSES)

        # by default allow all classes in .names file
        # theo mặc định cho phép tất cả các lớp trong tệp .names
        allowed_classes = list(class_names.values())

        # custom allowed classes (uncomment line below to allow detections for only people)
        # nếu muốn chọn một đối tượng riêng biệt để phát hiện
        # allowed_classes = ['person']

        # count objects found
        # đếm các đối tượng được tìm thấy
        counted_classes = count_objects(pred_bbox,
                                        by_class=True,
                                        allowed_classes=allowed_classes)

        # loop through dict and print
        # lặp qua và in
        image = utils.draw_bbox(frame,
                                pred_bbox,
                                FLAGS.info,
                                counted_classes,
                                allowed_classes=allowed_classes,
                                read_plate=FLAGS.plate)

        crop_rate = 200  # capture images every so many frames (ex. crop photos every 200 frames)
        crop_path = os.path.join(os.getcwd(), 'detections', 'crop',
                                 'real-time2')
        try:
            os.mkdir(crop_path)
        except FileExistsError:
            pass
        if frame_num % crop_rate == 0:
            crop_objects(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), pred_bbox,
                         crop_path, allowed_classes)
        else:
            pass

        result = np.asarray(image)
        cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
        result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

        cv2.imshow("result", result)

        if FLAGS.output:
            out.write(result)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cv2.destroyAllWindows()
Exemplo n.º 22
0
def main(_argv):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    video_path = FLAGS.video
    # get video name by using split method
    video_name = video_path.split('/')[-1]
    video_name = video_name.split('.')[0]
    if FLAGS.framework == 'tflite':
        interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        interpreter.allocate_tensors()
        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()
        print(input_details)
        print(output_details)
    else:
        saved_model_loaded = tf.saved_model.load(FLAGS.weights,
                                                 tags=[tag_constants.SERVING])
        infer = saved_model_loaded.signatures['serving_default']

    # begin video capture
    try:
        vid = cv2.VideoCapture(int(video_path))
    except:
        vid = cv2.VideoCapture(video_path)

    out = None

    if FLAGS.output:
        # by default VideoCapture returns float instead of int
        width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = int(vid.get(cv2.CAP_PROP_FPS))
        codec = cv2.VideoWriter_fourcc(*FLAGS.output_format)
        out = cv2.VideoWriter(FLAGS.output, codec, fps, (width, height))

    frame_num = 0
    while True:
        return_value, frame_1 = vid.read()
        #For the tokyo picture

        a, b, c, d, e, f, g, h = [209, 1040], [331, 197], [1124, 197], [
            1907, 850
        ], [0, 0], [1920, 0], [1920, 1080], [0, 1080]
        external_poly = [
            np.array([e, b, c, f]),
            np.array([f, c, d, g]),
            np.array([g, d, a, h]),
            np.array([h, a, b, e])
        ]
        frame = cv2.fillPoly(frame_1, external_poly, (0, 0, 0))
        cv2.line(frame, (209, 1040), (331, 197), (255, 0, 0), 2)
        cv2.line(frame, (331, 197), (1124, 197), (255, 0, 0), 2)
        cv2.line(frame, (1124, 197), (1907, 850), (255, 0, 0), 2)
        cv2.line(frame, (209, 1040), (1907, 850), (255, 0, 0), 2)

        if return_value:
            # frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)  #rotate the video for mobile videos
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame_num += 1
            image = Image.fromarray(frame)
        else:
            print('Video has ended or failed, try a different video format!')
            break
        if frame_num % 15 == 0:

            frame_size = frame.shape[:2]
            image_data = cv2.resize(frame, (input_size, input_size))
            image_data = image_data / 255.
            image_data = image_data[np.newaxis, ...].astype(np.float32)
            start_time = time.time()

            if FLAGS.framework == 'tflite':
                interpreter.set_tensor(input_details[0]['index'], image_data)
                interpreter.invoke()
                pred = [
                    interpreter.get_tensor(output_details[i]['index'])
                    for i in range(len(output_details))
                ]
                if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
                    boxes, pred_conf = filter_boxes(
                        pred[1],
                        pred[0],
                        score_threshold=0.25,
                        input_shape=tf.constant([input_size, input_size]))
                else:
                    boxes, pred_conf = filter_boxes(
                        pred[0],
                        pred[1],
                        score_threshold=0.25,
                        input_shape=tf.constant([input_size, input_size]))
            else:
                batch_data = tf.constant(image_data)
                pred_bbox = infer(batch_data)
                for key, value in pred_bbox.items():
                    boxes = value[:, :, 0:4]
                    pred_conf = value[:, :, 4:]

            boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
                boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
                scores=tf.reshape(
                    pred_conf,
                    (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
                max_output_size_per_class=100,
                max_total_size=100,
                iou_threshold=FLAGS.iou,
                score_threshold=FLAGS.score)

            # format bounding boxes from normalized ymin, xmin, ymax, xmax ---> xmin, ymin, xmax, ymax
            original_h, original_w, _ = frame.shape
            bboxes = utils.format_boxes(boxes.numpy()[0], original_h,
                                        original_w)

            pred_bbox = [
                bboxes,
                scores.numpy()[0],
                classes.numpy()[0],
                valid_detections.numpy()[0]
            ]
            # print(pred_bbox[2])
            out_boxes, out_scores, out_classes, num_boxes = pred_bbox

            # read in all class names from config
            class_names = utils.read_class_names(cfg.YOLO.CLASSES)

            # by default allow all classes in .names file
            # allowed_classes = list(class_names.values())

            # custom allowed classes (uncomment line below to allow detections for only SELECTED DETECTION CLASSES)
            allowed_classes = ['person', 'car', 'truck', 'bus', 'motorbike']
            # allowed_classes = ['car']

            # if crop flag is enabled, crop each detection and save it as new image
            if FLAGS.crop:
                crop_rate = 150  # capture images every so many frames (ex. crop photos every 150 frames)
                crop_path = os.path.join(os.getcwd(), 'detections', 'crop',
                                         video_name)
                try:
                    os.mkdir(crop_path)
                except FileExistsError:
                    pass
                if frame_num % crop_rate == 0:
                    final_path = os.path.join(crop_path,
                                              'frame_' + str(frame_num))
                    try:
                        os.mkdir(final_path)
                    except FileExistsError:
                        pass
                    crop_objects(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB),
                                 pred_bbox, final_path, allowed_classes)
                else:
                    pass

            if FLAGS.count:
                # count objects found
                counted_classes = count_objects(
                    pred_bbox, by_class=True, allowed_classes=allowed_classes)
                # loop through dict and print
                for key, value in counted_classes.items():
                    print("Number of {}s: {}".format(key, value))
                image = utils.draw_bbox(frame,
                                        pred_bbox,
                                        FLAGS.info,
                                        counted_classes,
                                        allowed_classes=allowed_classes,
                                        read_plate=FLAGS.plate)
            else:
                image = utils.draw_bbox(frame,
                                        pred_bbox,
                                        FLAGS.info,
                                        allowed_classes=allowed_classes,
                                        read_plate=FLAGS.plate)

            fps = 1.0 / (time.time() - start_time)
            print("FPS: %.2f" % fps)
            result = np.asarray(image)
            cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
            result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

            if not FLAGS.dont_show:
                cv2.imshow("result", result)

            if FLAGS.output:
                out.write(result)
            if cv2.waitKey(1) & 0xFF == ord('q'): break

    cv2.destroyAllWindows()
def test_fw_iter(IteratorClass, args):
    iterator_name = IteratorClass.__module__ + "." + IteratorClass.__name__
    print("Start testing {}".format(iterator_name))
    sess = None
    daliop = None
    dali_train_iter = None
    images = []
    labels = []

    pipes = [
        RN50Pipeline(batch_size=args.batch_size,
                     num_threads=args.workers,
                     device_id=n,
                     num_gpus=args.gpus,
                     data_paths=data_paths,
                     prefetch=PREFETCH,
                     fp16=args.fp16,
                     nhwc=args.nhwc) for n in range(args.gpus)
    ]
    [pipe.build() for pipe in pipes]
    iters = args.iters
    if args.iters < 0:
        iters = pipes[0].epoch_size("Reader")
        assert (all(pipe.epoch_size("Reader") == iters for pipe in pipes))
        iters_tmp = iters
        iters = iters // args.batch_size
        if iters_tmp != iters * args.batch_size:
            iters += 1
        iters_tmp = iters

        iters = iters // args.gpus
        if iters_tmp != iters * args.gpus:
            iters += 1

    if iterator_name == "nvidia.dali.plugin.tf.DALIIterator":
        daliop = IteratorClass()
        for dev in range(args.gpus):
            with tf.device('/gpu:%i' % dev):
                if args.fp16:
                    out_type = tf.float16
                else:
                    out_type = tf.float32
                image, label = daliop(pipeline=pipes[dev],
                                      shapes=[(args.batch_size, 3, 224, 224),
                                              ()],
                                      dtypes=[out_type, tf.int32])
                images.append(image)
                labels.append(label)
        gpu_options = GPUOptions(per_process_gpu_memory_fraction=0.5)
        config = ConfigProto(gpu_options=gpu_options)
        sess = Session(config=config)

    end = time.time()
    for i in range(args.epochs):
        if i == 0:
            print("Warm up")
        else:
            print("Test run " + str(i))
        data_time = AverageMeter()

        if iterator_name == "nvidia.dali.plugin.tf.DALIIterator":
            assert sess != None
            for j in range(iters):
                res = sess.run([images, labels])
                data_time.update(time.time() - end)
                if j % args.print_freq == 0:
                    print(
                        "{} {}/ {}, avg time: {} [s], worst time: {} [s], speed: {} [img/s]"
                        .format(iterator_name, j + 1, iters, data_time.avg,
                                data_time.max_val,
                                args.gpus * args.batch_size / data_time.avg))
                end = time.time()
        else:
            dali_train_iter = IteratorClass(pipes, reader_name="Reader")
            j = 0
            for it in iter(dali_train_iter):
                data_time.update(time.time() - end)
                if j % args.print_freq == 0:
                    print(
                        "{} {}/ {}, avg time: {} [s], worst time: {} [s], speed: {} [img/s]"
                        .format(iterator_name, j + 1, iters, data_time.avg,
                                data_time.max_val,
                                args.gpus * args.batch_size / data_time.avg))
                end = time.time()
                j = j + 1
                if j > iters:
                    break
Exemplo n.º 24
0
import os
import tensorflow as tf
import numpy as np

from . import SlotGatedModel

from . import utils

from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

configp = ConfigProto()
configp.gpu_options.allow_growth = True
session = InteractiveSession(config=configp)

DATASETS_ROOT = "datasets/"
MODELS_ROOT = "models/"

TRAIN_FOLDER_NAME = "train"
TEST_FOLDER_NAME = "test"
VALID_FOLDER_NAME = "valid"

TEXT_VOCAB_FILENAME = 'in_vocab'
SLOTS_VOCAB_FILENAME = 'slot_vocab'
INTENTS_VOCAB_FILENAME = 'intent_vocab'

TEXT_FILENAME = 'seq.in'
SLOTS_FILENAME = 'seq.out'
INTENTS_FILENAME = 'label'

CHECKPOINTS_FOLDER_NAME = "checkpoints"
Exemplo n.º 25
0
def detect(input_image):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)

    # load model
    saved_model_loaded = tf.saved_model.load('./checkpoints/yolov4-416',
                                             tags=[tag_constants.SERVING])

    # loop through images in list and run Yolov4 model on each
    original_image = input_image
    original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

    image_data = cv2.resize(original_image, (input_size, input_size))
    image_data = image_data / 255.

    image_name = 'image_input'

    images_data = []
    for i in range(1):
        images_data.append(image_data)
        images_data = np.asarray(images_data).astype(np.float32)

        infer = saved_model_loaded.signatures['serving_default']
        batch_data = tf.constant(images_data)
        pred_bbox = infer(batch_data)
        for key, value in pred_bbox.items():
            boxes = value[:, :, 0:4]
            pred_conf = value[:, :, 4:]

    # run non max suppression on detections
        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf,
                (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=iou_threshold,
            score_threshold=0.50)

        # format bounding boxes from normalized ymin, xmin, ymax, xmax ---> xmin, ymin, xmax, ymax
        original_h, original_w, _ = original_image.shape
        bboxes = utils.format_boxes(boxes.numpy()[0], original_h, original_w)

        # hold all detection data in one variable
        pred_bbox = [
            bboxes,
            scores.numpy()[0],
            classes.numpy()[0],
            valid_detections.numpy()[0]
        ]

        # read in all class names from config
        class_names = utils.read_class_names(cfg.YOLO.CLASSES)

        # by default allow all classes in .names file
        allowed_classes = list(class_names.values())

        # custom allowed classes (uncomment line below to allow detections for only people)
        #allowed_classes = ['person']

        counted_classes = count_objects(pred_bbox,
                                        by_class=True,
                                        allowed_classes=allowed_classes)
        # loop through dict and print
        string = ""
        for key, value in counted_classes.items():
            string = string + " " + "Number of {}s: {}".format(key, value)

        image = utils.draw_bbox(original_image,
                                pred_bbox,
                                False,
                                counted_classes,
                                allowed_classes=allowed_classes,
                                read_plate=False)

        image = Image.fromarray(image.astype(np.uint8))
        image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)

        cv2.imwrite('./detections/' + 'detection' + '.png', image)
        return image, string
Exemplo n.º 26
0
def main(_argv):
    # Definition of the parameters
    max_cosine_distance = 0.4
    nn_budget = None
    nms_max_overlap = 1.0
    
    # initialize deep sort
    model_filename = 'model_data/mars-small128.pb'
    encoder = gdet.create_box_encoder(model_filename, batch_size=1)
    # calculate cosine distance metric
    metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
    # initialize tracker
    tracker = Tracker(metric)

    # load configuration for object detector
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    video_path = FLAGS.video

    # load tflite model if flag is set
    if FLAGS.framework == 'tflite':
        interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        interpreter.allocate_tensors()
        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()
        print(input_details)
        print(output_details)
    # otherwise load standard tensorflow saved model
    else:
        saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])
        infer = saved_model_loaded.signatures['serving_default']

    # begin video capture
    try:
        vid = cv2.VideoCapture(int(video_path))
    except:
        vid = cv2.VideoCapture(video_path)

    out = None

    # get video ready to save locally if flag is set
    if FLAGS.output:
        # by default VideoCapture returns float instead of int
        width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = int(vid.get(cv2.CAP_PROP_FPS))
        codec = cv2.VideoWriter_fourcc(*FLAGS.output_format)
        out = cv2.VideoWriter(FLAGS.output, codec, fps, (width, height))

    # while video is running
    while True:
        return_value, frame = vid.read()
        if return_value:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            image = Image.fromarray(frame)
        else:
            print('Video has ended or failed, try a different video format!')
            break
    
        frame_size = frame.shape[:2]
        image_data = cv2.resize(frame, (input_size, input_size))
        image_data = image_data / 255.
        image_data = image_data[np.newaxis, ...].astype(np.float32)
        start_time = time.time()

        # run detections on tflite if flag is set
        if FLAGS.framework == 'tflite':
            interpreter.set_tensor(input_details[0]['index'], image_data)
            interpreter.invoke()
            pred = [interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details))]
            # run detections using yolov3 if flag is set
            if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
                boxes, pred_conf = filter_boxes(pred[1], pred[0], score_threshold=0.25,
                                                input_shape=tf.constant([input_size, input_size]))
            else:
                boxes, pred_conf = filter_boxes(pred[0], pred[1], score_threshold=0.25,
                                                input_shape=tf.constant([input_size, input_size]))
        else:
            batch_data = tf.constant(image_data)
            pred_bbox = infer(batch_data)
            for key, value in pred_bbox.items():
                boxes = value[:, :, 0:4]
                pred_conf = value[:, :, 4:]

        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=FLAGS.iou,
            score_threshold=FLAGS.score
        )

        # convert data to numpy arrays and slice out unused elements
        num_objects = valid_detections.numpy()[0]
        bboxes = boxes.numpy()[0]
        bboxes = bboxes[0:int(num_objects)]
        scores = scores.numpy()[0]
        scores = scores[0:int(num_objects)]
        classes = classes.numpy()[0]
        classes = classes[0:int(num_objects)]

        # format bounding boxes from normalized ymin, xmin, ymax, xmax ---> xmin, ymin, width, height
        original_h, original_w, _ = frame.shape
        bboxes = utils.format_boxes(bboxes, original_h, original_w)

        # store all predictions in one parameter for simplicity when calling functions
        pred_bbox = [bboxes, scores, classes, num_objects]

        # read in all class names from config
        class_names = utils.read_class_names(cfg.YOLO.CLASSES)

        # by default allow all classes in .names file
        allowed_classes = list(class_names.values())
        
        # custom allowed classes (uncomment line below to customize tracker for only people)
        #allowed_classes = ['person']

        # loop through objects and use class index to get class name, allow only classes in allowed_classes list
        names = []
        deleted_indx = []
        for i in range(num_objects):
            class_indx = int(classes[i])
            class_name = class_names[class_indx]
            if class_name not in allowed_classes:
                deleted_indx.append(i)
            else:
                names.append(class_name)
        names = np.array(names)

        # delete detections that are not in allowed_classes
        bboxes = np.delete(bboxes, deleted_indx, axis=0)
        scores = np.delete(scores, deleted_indx, axis=0)

        # encode yolo detections and feed to tracker
        features = encoder(frame, bboxes)
        detections = [Detection(bbox, score, class_name, feature) for bbox, score, class_name, feature in zip(bboxes, scores, names, features)]

        #initialize color map
        cmap = plt.get_cmap('tab20b')
        colors = [cmap(i)[:3] for i in np.linspace(0, 1, 20)]

        # run non-maxima supression
        boxs = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        classes = np.array([d.class_name for d in detections])
        indices = preprocessing.non_max_suppression(boxs, classes, nms_max_overlap, scores)
        detections = [detections[i] for i in indices]       

        # Call the tracker
        tracker.predict()
        tracker.update(detections)

        # update tracks
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue 
            bbox = track.to_tlbr()
            class_name = track.get_class()
            
        # draw bbox on screen
            color = colors[int(track.track_id) % len(colors)]
            color = [i * 255 for i in color]
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color, 2)
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1]-30)), (int(bbox[0])+(len(class_name)+len(str(track.track_id)))*17, int(bbox[1])), color, -1)
            cv2.putText(frame, class_name + "-" + str(track.track_id),(int(bbox[0]), int(bbox[1]-10)),0, 0.75, (255,255,255),2)

        # if enable info flag then print details about each track
            if FLAGS.info:
                print("Tracker ID: {}, Class: {},  BBox Coords (xmin, ymin, xmax, ymax): {}".format(str(track.track_id), class_name, (int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]))))

        # calculate frames per second of running detections
        fps = 1.0 / (time.time() - start_time)
        print("FPS: %.2f" % fps)
        result = np.asarray(frame)
        result = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        
        if not FLAGS.dont_show:
            cv2.imshow("Output Video", result)
        
        # if output flag is set, save video file
        if FLAGS.output:
            out.write(result)
        if cv2.waitKey(1) & 0xFF == ord('q'): break
    cv2.destroyAllWindows()
Exemplo n.º 27
0
# -*- coding: utf-8 -*-

import pyrealsense2 as rs
import numpy as np
import cv2
from scipy import ndimage
import matplotlib.pyplot as plt

#import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
h5_path =  './result/msra_clstm_basic_120_P4.hdf5'
model = load_model(h5_path)
model.summary()
#icvl_net = cv2.dnn.readNetFromTensorflow('./new/model.pb')

height = 480
width= 640
cropHeight = 120
cropWidth = 120
fx=628.668
fy=628.668
u0=311.662
v0=231.571
xy_thres = 150
depth_thres = 120
def main(_argv):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    image_path = FLAGS.image
    original_image = cv2.imread(image_path)
    original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

    # image_data = utils.image_preprocess(np.copy(original_image), [input_size, input_size])
    image_data = cv2.resize(original_image, (input_size, input_size))
    image_data = image_data / 255.
    # image_data = image_data[np.newaxis, ...].astype(np.float32)

    images_data = []
    for i in range(1):
        images_data.append(image_data)
    images_data = np.asarray(images_data).astype(np.float32)

    if FLAGS.framework == 'tflite':
        interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        interpreter.allocate_tensors()
        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()
        print(input_details)
        print(output_details)
        interpreter.set_tensor(input_details[0]['index'], images_data)
        interpreter.invoke()
        pred = [interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details))]
        if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
            boxes, pred_conf = filter_boxes(pred[1], pred[0], score_threshold=0.25, input_shape=tf.constant([input_size, input_size]))
        else:
            boxes, pred_conf = filter_boxes(pred[0], pred[1], score_threshold=0.25, input_shape=tf.constant([input_size, input_size]))
    else:
        saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])
        infer = saved_model_loaded.signatures['serving_default']
        batch_data = tf.constant(images_data)
        pred_bbox = infer(batch_data)
        for key, value in pred_bbox.items():
            boxes = value[:, :, 0:4]
            pred_conf = value[:, :, 4:]

    boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
        boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
        scores=tf.reshape(
            pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
        max_output_size_per_class=50,
        max_total_size=50,
        iou_threshold=FLAGS.iou,
        score_threshold=FLAGS.score
    )
    pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]
    f = open("output", "w")
    f.write(str(valid_detections))
    f.close()
    image = utils.draw_bbox(original_image, pred_bbox)
    # image = utils.draw_bbox(image_data*255, pred_bbox)
    image = Image.fromarray(image.astype(np.uint8))
    image.show()
    image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
    cv2.imwrite(FLAGS.output, image)
def main(_argv):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = utils.load_config(FLAGS)
    input_size = FLAGS.size
    video_path = FLAGS.video

    if FLAGS.framework == 'tflite':
        interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        interpreter.allocate_tensors()
        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()
        print(input_details)
        print(output_details)
    else:
        saved_model_loaded = tf.saved_model.load(FLAGS.weights,
                                                 tags=[tag_constants.SERVING])
        infer = saved_model_loaded.signatures['serving_default']

    # begin video capture
    try:
        vid = cv2.VideoCapture(int(video_path))
    except:
        vid = cv2.VideoCapture(video_path)

    out = None

    if FLAGS.output:
        # by default VideoCapture returns float instead of int
        width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = int(vid.get(cv2.CAP_PROP_FPS))
        codec = cv2.VideoWriter_fourcc(*FLAGS.output_format)
        out = cv2.VideoWriter(FLAGS.output, codec, fps, (width, height))

    while True:
        return_value, frame = vid.read()
        if return_value:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            image = Image.fromarray(frame)
        else:
            print('Video has ended or failed, try a different video format!')
            break

        frame_size = frame.shape[:2]
        image_data = cv2.resize(frame, (input_size, input_size))
        image_data = image_data / 255.
        image_data = image_data[np.newaxis, ...].astype(np.float32)
        start_time = time.time()

        if FLAGS.framework == 'tflite':
            interpreter.set_tensor(input_details[0]['index'], image_data)
            interpreter.invoke()
            pred = [
                interpreter.get_tensor(output_details[i]['index'])
                for i in range(len(output_details))
            ]
            if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
                boxes, pred_conf = filter_boxes(pred[1],
                                                pred[0],
                                                score_threshold=0.25,
                                                input_shape=tf.constant(
                                                    [input_size, input_size]))
            else:
                boxes, pred_conf = filter_boxes(pred[0],
                                                pred[1],
                                                score_threshold=0.25,
                                                input_shape=tf.constant(
                                                    [input_size, input_size]))
        else:
            batch_data = tf.constant(image_data)
            pred_bbox = infer(batch_data)
            for key, value in pred_bbox.items():
                boxes = value[:, :, 0:4]
                pred_conf = value[:, :, 4:]

        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf,
                (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=FLAGS.iou,
            score_threshold=FLAGS.score)
        pred_bbox = [
            boxes.numpy(),
            scores.numpy(),
            classes.numpy(),
            valid_detections.numpy()
        ]
        image = utils.draw_bbox(frame, pred_bbox)
        fps = 1.0 / (time.time() - start_time)
        print("FPS: %.2f" % fps)
        result = np.asarray(image)
        cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
        result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

        if not FLAGS.dont_show:
            cv2.imshow("result", result)

        if FLAGS.output:
            out.write(result)
        if cv2.waitKey(1) & 0xFF == ord('q'): break
    cv2.destroyAllWindows()
Exemplo n.º 30
0
from keras.backend.tensorflow_backend import set_session
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession, Session
config = ConfigProto()
config.gpu_options.allow_growth = True
config.log_device_placement = True  # to log device placement (on which device the operation ran)
# session = InteractiveSession(config=config)
import tensorflow as tf
session = tf.Session(config=config)
set_session(session)  # set this TensorFlow session as the default session for Keras
Exemplo n.º 31
0
def detector():
    # Definition of the parameters
    max_cosine_distance = 0.4
    nn_budget = None
    nms_max_overlap = 1.0
    # initialize deep sort
    model_filename = 'model_data/mars-small128.pb'
    encoder = gdet.create_box_encoder(model_filename, batch_size=1)
    # calculate cosine distance metric
    metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
    # initialize tracker
    tracker = Tracker(metric)

    # load configuration for object detector
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    input_size = 416

    # video_path = 'data/video/vertical.mp4'
    video_path = 'uploads/' + session['filename']

    # print(video_path)

    # load tflite model if flag is set

    # TINY
    saved_model_loaded = tf.saved_model.load('./checkpoints/yolov4-tiny-416', tags=[tag_constants.SERVING])
    # NORMAL
    # saved_model_loaded = tf.saved_model.load('./checkpoints/yolov4-416', tags=[tag_constants.SERVING])

    infer = saved_model_loaded.signatures['serving_default']

    # begin video capture
    try:
        vid = cv2.VideoCapture(int(video_path))
    except:
        vid = cv2.VideoCapture(video_path)

    out = None

    # get video ready to save locally if flag is set
    if './outputs/output.avi':
        # by default VideoCapture returns float instead of int
        width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = int(vid.get(cv2.CAP_PROP_FPS))
        codec = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter('./outputs/output.avi', codec, fps, (width, height))

    frame_num = 0
    # while video is running

    data = cv2.VideoCapture(video_path)

    fps = int(data.get(cv2.CAP_PROP_FPS))

    print("fps: " + str(fps))

    bus_counter = []
    car_counter = []
    truck_counter = []
    sum_counter = []

    myDict = {}
    while True:
        cur_minute = math.floor(frame_num / (60 * fps)) + 1

        return_value, frame = vid.read()
        if return_value:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            image = Image.fromarray(frame)
        else:
            print('Video has ended or failed, try a different video format!')
            break

        if fps >= 25:
            if frame_num % 3 != 0:
                frame_num += 1
                continue

        frame_num += 1

        print('Frame #: ', frame_num)
        print('Minute : ', cur_minute)
        frame_size = frame.shape[:2]
        image_data = cv2.resize(frame, (input_size, input_size))
        image_data = image_data / 255.
        image_data = image_data[np.newaxis, ...].astype(np.float32)
        start_time = time.time()

        # run detections
        batch_data = tf.constant(image_data)
        pred_bbox = infer(batch_data)
        for key, value in pred_bbox.items():
            boxes = value[:, :, 0:4]
            pred_conf = value[:, :, 4:]

        boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
            boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
            scores=tf.reshape(
                pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
            max_output_size_per_class=50,
            max_total_size=50,
            iou_threshold=0.45,
            score_threshold=0.50
        )

        # convert data to numpy arrays and slice out unused elements
        num_objects = valid_detections.numpy()[0]
        bboxes = boxes.numpy()[0]
        bboxes = bboxes[0:int(num_objects)]
        scores = scores.numpy()[0]
        scores = scores[0:int(num_objects)]
        classes = classes.numpy()[0]
        classes = classes[0:int(num_objects)]

        # format bounding boxes from normalized ymin, xmin, ymax, xmax ---> xmin, ymin, width, height
        original_h, original_w, _ = frame.shape
        bboxes = utils.format_boxes(bboxes, original_h, original_w)

        # store all predictions in one parameter for simplicity when calling functions
        pred_bbox = [bboxes, scores, classes, num_objects]

        # read in all class names from config
        class_names = utils.read_class_names(cfg.YOLO.CLASSES)

        # by default allow all classes in .names file
        allowed_classes = list(class_names.values())

        # loop through objects and use class index to get class name, allow only classes in allowed_classes list
        names = []
        deleted_indx = []
        for i in range(num_objects):
            class_indx = int(classes[i])
            class_name = class_names[class_indx]
            if class_name not in allowed_classes:
                deleted_indx.append(i)
            else:
                names.append(class_name)
        names = np.array(names)
        count = len(names)

        # encode yolo detections and feed to tracker
        features = encoder(frame, bboxes)
        detections = [Detection(bbox, score, class_name, feature) for bbox, score, class_name, feature in
                      zip(bboxes, scores, names, features)]

        # initialize color map
        cmap = plt.get_cmap('tab20b')
        colors = [cmap(i)[:3] for i in np.linspace(0, 1, 20)]

        # run non-maxima supression
        boxs = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        classes = np.array([d.class_name for d in detections])
        indices = preprocessing.non_max_suppression(boxs, classes, nms_max_overlap, scores)
        detections = [detections[i] for i in indices]

        # Call the tracker
        tracker.predict()
        tracker.update(detections)

        # update tracks
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlbr()
            class_name = track.get_class()

            # draw bbox on screen
            color = colors[int(track.track_id) % len(colors)]
            color = [i * 255 for i in color]

            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color, 2)
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1] - 30)),
                          (int(bbox[0]) + (len(class_name) + len(str(track.track_id))) * 17, int(bbox[1])), color,
                          -1)
            cv2.putText(frame, class_name + "-" + str(track.track_id), (int(bbox[0]), int(bbox[1] - 10)), 0, 0.75,
                        (255, 255, 255), 2)

            # if enable info flag then print details about each track

            height, width, _ = frame.shape

            # Veritical

            cv2.line(frame, (0, int(3 * height / 6)), (width, int(3 * height / 6)), (0, 255, 0), thickness=2)
            center_y = int(((bbox[1]) + (bbox[3])) / 2)
            if center_y <= int(3 * height / 6 + height / 30) and center_y >= int(3 * height / 6 - height / 30):
                if class_name == 'car':
                    car_counter.append(int(track.track_id))
                    sum_counter.append(int(track.track_id))
                    if cur_minute not in myDict:
                        myDict[cur_minute] = []
                    myDict[cur_minute].append(track.track_id)

                elif class_name == 'truck':
                    truck_counter.append(int(track.track_id))
                    sum_counter.append(int(track.track_id))
                    if cur_minute not in myDict:
                        myDict[cur_minute] = []
                    myDict[cur_minute].append(track.track_id)
                elif class_name == 'bus':
                    bus_counter.append(int(track.track_id))
                    sum_counter.append(int(track.track_id))
                    if cur_minute not in myDict:
                        myDict[cur_minute] = []
                    myDict[cur_minute].append(track.track_id)

            # Horizontal

            cv2.line(frame, (int(3 * width / 6), 0), (int(3 * width / 6), height), (0, 255, 0), thickness=2)
            center_x = int(((bbox[0]) + (bbox[2])) / 2)
            if center_x <= int(3 * width / 6 + width / 30) and center_x >= int(3 * width / 6 - width / 30):
                if class_name == 'car':
                    car_counter.append(int(track.track_id))
                    sum_counter.append(int(track.track_id))
                    if cur_minute not in myDict:
                        myDict[cur_minute] = []
                    myDict[cur_minute].append(track.track_id)
                elif class_name == 'truck':
                    truck_counter.append(int(track.track_id))
                    sum_counter.append(int(track.track_id))
                    if cur_minute not in myDict:
                        myDict[cur_minute] = []
                    myDict[cur_minute].append(track.track_id)
                elif class_name == 'bus':
                    bus_counter.append(int(track.track_id))
                    sum_counter.append(int(track.track_id))
                    if cur_minute not in myDict:
                        myDict[cur_minute] = []
                    myDict[cur_minute].append(track.track_id)

        car_count = len(set(car_counter))
        truck_count = len(set(truck_counter))
        bus_count = len(set(bus_counter))

        total_count = len(set(sum_counter))
        print(total_count)

        cv2.putText(frame, "Total Vehicle Count: " + str(total_count), (0, 130), 0, 1, (0, 0, 255), 2)

        # calculate frames per second of running detections
        fps_running = 1.0 / (time.time() - start_time)
        # print("FPS: %.2f" % fps_running)
        result = np.asarray(frame)
        result = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

        if './outputs/tiny.avi':
            out.write(result)
        if cv2.waitKey(1) & 0xFF == ord('q'): break
    cv2.destroyAllWindows()

    # ========================================================================================

    name_db = session['filename']

    # return render_template('result.html', result=number_of_vehicles)

    first_sum = len(set(myDict[1]))
    second_sum = 0
    third_sum = 0
    fourth_sum = 0
    fifth_sum = 0

    if 2 in myDict:
        second_sum = len(set(myDict[2]))
    if 3 in myDict:
        third_sum = len(set(myDict[3]))
    if 4 in myDict:
        fourth_sum = len(set(myDict[4]))
    if 5 in myDict:
        fifth_sum = len(set(myDict[4]))

    conn = sqlite3.connect('database.db')
    cursor = conn.cursor()
    cursor.execute("INSERT INTO vehicles VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                   (name_db, car_count, bus_count, truck_count, first_sum, second_sum, third_sum, fourth_sum, fifth_sum,
                    total_count))
    conn.commit()
    conn.close()

    return render_template('result.html', car=car_count, bus=bus_count, truck=truck_count, total=total_count,
                           first=first_sum, second=second_sum, third=third_sum, fourth=fourth_sum, fifth=fifth_sum,
                           length=cur_minute)