コード例 #1
0
#########################

with GRAPH.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(GRAPH_PATH, "rb") as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name="")

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

PRINTER.info_text("Indexing detection categories")
LABEL_MAP = label_map_util.load_labelmap(LABEL_PATH)
CATEGORIES = label_map_util.convert_label_map_to_categories(
    LABEL_MAP, max_num_classes=NUM_CLASSES, use_display_name=True)
CATEGORY_INDEX = label_map_util.create_category_index(CATEGORIES)

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

PRINTER.info_text("Importing images")
if INSTRUMENT == "aia":
    number_of_images = len(
        next(os.walk("%s/resources/aia-images" % MAIN_DIR))[2]) - 2
    names = os.listdir("%s/resources/aia-images" % MAIN_DIR)
    names.sort()
    cutout_path = [
        os.path.join("%s/resources/aia-images" % MAIN_DIR, "{}".format(name))
        for name in names if name.endswith(".jpg")
    ]
elif INSTRUMENT == "hmi":
    number_of_images = len(
コード例 #2
0
def detect(imgname):
# This is needed since the notebook is stored in the object_detection folder.
    OBJECT_DETECTION_PATH = '/home/wu/Documents/respo/models/research/object_detection'
    sys.path.append("/home/wu/Documents/respo/models/research/object_detection")
    
    
    # Object detection imports
    
    
    
    # Variables
    # What model to download.
    MODEL_NAME = 'ssd_mobilenet_v1_coco_11_06_2017'
    MODEL_FILE = os.path.join(OBJECT_DETECTION_PATH, MODEL_NAME + '.tar.gz')
    DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
    
    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_CKPT = os.path.join(OBJECT_DETECTION_PATH, MODEL_NAME + '/frozen_inference_graph.pb')
    
    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join(OBJECT_DETECTION_PATH, 'data', 'mscoco_label_map.pbtxt')
    
    NUM_CLASSES = 90
    
    
    # Download Model
    opener = urllib.request.URLopener()
    if not os.path.exists(MODEL_FILE):
        print('download model')
        opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
    else:
        print('model existed')
    tar_file = tarfile.open(MODEL_FILE)
    for file in tar_file.getmembers():
        file_name = os.path.basename(file.name)
        if 'frozen_inference_graph.pb' in file_name:
            tar_file.extract(file, OBJECT_DETECTION_PATH)
    
    
    # Load a (frozen) Tensorflow model into memory.
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
    
    
    # Loading label map
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
                                                                use_display_name=True)
    category_index = label_map_util.create_category_index(categories)
    
    
    # Detection
    # For the sake of simplicity we will use only 2 images:
    # image1.jpg
    # image2.jpg
    # If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
    #PATH_TO_TEST_IMAGES_DIR = os.path.join(OBJECT_DETECTION_PATH, 'test_images')
    #TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(5, 6)]
    TEST_IMAGE_PATHS =[imgname]
    
    # Size, in inches, of the output images.
    
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Definite input and output Tensors for detection_graph
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # Each box represents a part of the image where a particular object was detected.
            detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class label.
            detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name('num_detections:0')
            for image_path in TEST_IMAGE_PATHS:
                # the array based representation of the image will be used later in order to prepare the
                # result image with boxes and labels on it.
                image_np = skimage.io.imread(image_path)
                
               
                # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(image_np, axis=0)
                # Actual detection.
                (boxes, scores, classes, num) = sess.run(
                    [detection_boxes, detection_scores, detection_classes, num_detections],
                    feed_dict={image_tensor: image_np_expanded})
                # Visualization of the results of a detection.
                vis_util.visualize_boxes_and_labels_on_image_array(
                    image_np,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8)
                #plt.figure(figsize=IMAGE_SIZE)
                #plt.axis('off')
                #plt.imshow(image_np)
                afterdetect_name='dected_'+imgname.strip('./')
                print (afterdetect_name)
                #plt.savefig(afterdetect_name)
                skimage.io.imsave(afterdetect_name,image_np)
                print ('dectect over')
                return afterdetect_name
                
            
            
                
コード例 #3
0
def main(_):
    PATH_TO_CKPT = FLAGS.path_to_ckpt
    NUM_CLASSES = FLAGS.num_classes
    PATH_TO_LABELS = FLAGS.path_to_labels
    PATH_TO_TEST_IMAGES_DIR = FLAGS.path_to_test_imgs
    RESULT_VIS_PATH = FLAGS.saved_path


    # Step 1: Load a frozen graph
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
    # Step 2: Load label map
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)
    # Step 3: Get the list of image names
    TEST_IMAGE_PATHS = glob.glob(os.path.join(PATH_TO_TEST_IMAGES_DIR, '*.jpg'))

    # Size, in inches, of the output images.
    IMAGE_SIZE = (12, 8)
    if not os.path.exists(RESULT_VIS_PATH):
        os.makedirs(RESULT_VIS_PATH)
    # Record inference time of each frame
    inferece_time = []
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # each box represents a part of the image where a particular object was detected.
            detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class label.
            detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name('num_detections:0')
            for image_path in TEST_IMAGE_PATHS:
                image = Image.open(image_path)
                # the array based representation of the image will be used in order to prepare the
                # result image with boxes and labels on it.
                image_np = load_image_into_numpy_array(image)
                # expand dimensions since the model expects image to have shape [1, None, None, 3]
                image_np_expanded = np.expand_dims(image_np, axis=0)
                # Actual detection.
                start_time = time.time()
                (boxes, scores, classes, num) = sess.run(
                    [detection_boxes, detection_scores, detection_classes, num_detections],
                    feed_dict={image_tensor: image_np_expanded}
                )
                elapse_time = time.time() - start_time
                inferece_time.append(elapse_time)
                vis_util.visualize_boxes_and_labels_on_image_array(
                    image_np,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=3)
                plt.imsave(os.path.join(RESULT_VIS_PATH, os.path.basename(image_path)), image_np)
    print ("Time elapsed for each frame = {}".format(inferece_time))
    print ("Speed of testing is {} seconds/frame".format(np.mean(inferece_time[1:])))
コード例 #4
0
NUM_OCTAVES = 2.5
########################################################################
# Model information
MODEL_NAME = 'ssd_mobilenet_v2_fpn_320'
PATH_TO_SAVED_MODEL = os.path.join(os.getcwd(), 'model_data', MODEL_NAME,
                                   'saved_model')
PATH_TO_LABELS = os.path.join(os.getcwd(), 'model_data', MODEL_NAME,
                              'label_map.pbtxt')
print("Loading saved model ...")
detect_fn = tf.saved_model.load(PATH_TO_SAVED_MODEL)
print("Model Loaded!")

# Load label map and obtain class names and ids
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
category_index = label_map_util.create_category_index(
    label_map_util.convert_label_map_to_categories(label_map,
                                                   max_num_classes=1,
                                                   use_display_name=True))


def visualise_on_image(image, bboxes, labels, scores, score_thresh,
                       vert_thresh):
    (h, w, d) = image.shape
    for bbox, label, score in zip(bboxes, labels, scores):
        if score > score_thresh and bbox[0] > vert_thresh:
            xmin, ymin = int(bbox[1] * w), int(bbox[0] * h)
            xmax, ymax = int(bbox[3] * w), int(bbox[2] * h)

            cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
            cv2.circle(image, ((xmin + xmax) // 2, (ymin + ymax) // 2), 3,
                       (0, 255, 0), -1)
            cv2.putText(image, f"{label}: {int(score*100)} %", (xmin, ymin),
コード例 #5
0
    def loopimg(name="Picture 150.png"):
        IMAGE_NAME = str(name)

        print(name)
        PATH_TO_IMAGE = os.path.join(CWD_PATH, IMAGE_NAME)

        # Number of classes the object detector can identify
        NUM_CLASSES = 4

        # Load the label map.
        #
        # Here we use internal utility functions, but anything that returns a
        # dictionary mapping integers to appropriate string labels would be fine
        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        category_index = label_map_util.create_category_index(categories)

        # Load the Tensorflow model into memory.
        detection_graph = tf.Graph()
        with detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            sess = tf.Session(graph=detection_graph)

# Define input and output tensors (i.e. data) for the object detection classifier

# Input tensor is the image
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

        # Output tensors are the detection boxes, scores, and classes
        # Each box represents a part of the image where a particular object was detected
        detection_boxes = detection_graph.get_tensor_by_name(
            'detection_boxes:0')

        # Each score represents level of confidence for each of the objects.
        # The score is shown on the result image, together with the class label.
        detection_scores = detection_graph.get_tensor_by_name(
            'detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name(
            'detection_classes:0')

        # Number of objects detected
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')

        # Load image using OpenCV and
        # expand image dimensions to have shape: [1, None, None, 3]
        # i.e. a single-column array, where each item in the column has the pixel RGB value
        image = cv2.imread(PATH_TO_IMAGE)
        image_expanded = np.expand_dims(image, axis=0)

        # Perform the actual detection by running the model with the image as input
        (boxes, scores, classes,
         num) = sess.run([
             detection_boxes, detection_scores, detection_classes,
             num_detections
         ],
                         feed_dict={image_tensor: image_expanded})
        printcount = 0
        it = 0
        boxesFiltered = []
        final_score = np.squeeze(scores)
        count = 0
        for i in range(100):
            if scores is None or final_score[i] > 0.5:
                count = count + 1

        for i in classes[0]:
            printcount = printcount + 1
            if (category_index[i]['name'] == "serialreadable"):
                print("cheese")
                checkingSerial = True
                coordinates = vis_util.return_coordinates(
                    image,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8,
                    min_score_thresh=0.90)
                print(category_index[i]['name'])
                print("cheese")
                if (coordinates != None):
                    print(coordinates)
                    print(i)
                    it += 1

                    x = int(coordinates[2])
                    y = int(coordinates[0])
                    w = int(coordinates[3])
                    h = int(coordinates[1])
                    #im2, contours, hierarchy = cv2.findContours(frame,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
                    #x, y, w, h = cv2.boundingRect(contours[i])
                    roi = image[y:y + h, x:x + w]
                    #gaussian_3 = cv2.GaussianBlur(roi, (9,9), 10.0)
                    #unsharp_image = cv2.addWeighted(roi, 1.5, gaussian_3, -0.5, 0, roi)

                    roi = cv2.resize(roi,
                                     None,
                                     fx=4,
                                     fy=4,
                                     interpolation=cv2.INTER_CUBIC)
                    cv2.fastNlMeansDenoisingColored(roi, None, 15, 15, 7, 21)
                    roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
                    kernel = np.zeros((3, 3), np.uint8)
                    #roi = cv2.dilate(roi, kernel, iterations=1)
                    roi = cv2.erode(roi, kernel, iterations=3)
                    #roi = cv2.bilateralFilter(roi,9,75,75)
                    roi = cv2.medianBlur(roi, 3)
                    #edges = cv2.Canny(roi,100,200)
                    #img_dilation = c_m.dilate(edges,N=3,iterations=2)
                    #kernel = np.ones((5,5), np.uint8)
                    #img_dilation = cv2.dilate(roi, kernel, iterations=2)
                    roi = cv2.adaptiveThreshold(roi, 245,
                                                cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                                cv2.THRESH_BINARY_INV, 115, 1)
                    #ret,roi = cv2.threshold(roi,127,255,cv2.THRESH_BINARY_INV)

                    #plt.subplot(121),plt.imshow(roi,cmap = 'gray')
                    #plt.title('Original Image'), plt.xticks([]), plt.yticks([])
                    #plt.subplot(122),plt.imshow(edges,cmap = 'gray')
                    #plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
                    roi = cv2.medianBlur(roi, 3)
                    #roi = cv2.bilateralFilter(roi,9,75,75)

                    cv2.imwrite(str(name) + "zaca.png", roi)
                    print("zaca")
                    #contours=c_m.find_components(edges)
                    #c_m.process_image(str(it)+"roi.jpg",str(it)+"roi.jpg")
                    #API.SetVariable("classify_enable_learning","0");
                    #API.SetVariable("classify_enable_adaptive_matcher","0")
                    #API.
                    r = 0
                    text = readtext.readtext(roi, r)

                    print("{}\n".format(text))
                    if (text == None):
                        text = str('')
                    else:
                        img2 = np.zeros((512, 512, 3), np.uint8)
                        font = cv2.FONT_HERSHEY_SIMPLEX
                        cv2.putText(img2, text, (10, 500), font, 1,
                                    (255, 255, 255), 2, cv2.LINE_AA)
                        cv2.imshow("Results", img2)
                        checkingSerial = False
                else:
                    break
                return name
                if (printcount == count):
                    break
                return name
# Draw the results of the detection (aka 'visulaize the results')
        vis_util.visualize_boxes_and_labels_on_image_array(
            image,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=8,
            min_score_thresh=0.80)
コード例 #6
0
def a(IM_name):
    IMAGE_NAME = 'object_detection/' + IM_name

    # Grab path to current working directory
    CWD_PATH = os.getcwd()

    # Path to frozen detection graph .pb file, which contains the model that is used
    # for object detection.
    PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_NAME,
                                'frozen_inference_graph.pb')

    # Path to label map file
    PATH_TO_LABELS = os.path.join(CWD_PATH, 'training', 'labelmap.pbtxt')

    # Path to image
    PATH_TO_IMAGE = os.path.join(CWD_PATH, IMAGE_NAME)

    # Number of classes the object detector can identify
    NUM_CLASSES = 1

    # Load the label map.
    # Label maps map indices to category names, so that when our convolution
    # network predicts `5`, we know that this corresponds to `king`.
    # Here we use internal utility functions, but anything that returns a
    # dictionary mapping integers to appropriate string labels would be fine
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    # Load the Tensorflow model into memory.
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.compat.v1.GraphDef()
        with tf.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

        sess = tf.compat.v1.Session(graph=detection_graph)

    # Define input and output tensors (i.e. data) for the object detection classifier

    # Input tensor is the image
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    # Output tensors are the detection boxes, scores, and classes
    # Each box represents a part of the image where a particular object was detected
    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

    # Each score represents level of confidence for each of the objects.
    # The score is shown on the result image, together with the class label.
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name(
        'detection_classes:0')

    # Number of objects detected
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')

    # Load image using OpenCV and
    # expand image dimensions to have shape: [1, None, None, 3]
    # i.e. a single-column array, where each item in the column has the pixel RGB value
    image = cv2.imread(PATH_TO_IMAGE)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_expanded = np.expand_dims(image_rgb, axis=0)

    # Perform the actual detection by running the model with the image as input
    (boxes, scores, classes, num) = sess.run(
        [detection_boxes, detection_scores, detection_classes, num_detections],
        feed_dict={image_tensor: image_expanded})

    # Draw the results of the detection (aka 'visulaize the results')

    vis_util.visualize_boxes_and_labels_on_image_array(
        image,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=8,
        min_score_thresh=0.60)

    # new added
    pAth = CWD_PATH + "/output/" + IMAGE_name
    print(pAth)
    cv2.imwrite(pAth, image)
コード例 #7
0
ファイル: object_face.py プロジェクト: eujuu/CaB
import numpy as np
import tensorflow as tf
import sys
from PIL import Image
import cv2
cap = cv2.VideoCapture(0)

from utils import label_map_util
from utils import visualization_utils as vis_util

# ------------------ Face Model Initialization ------------------------------ #
face_label_map = label_map_util.load_labelmap('training_face/labelmap.pbtxt')
face_categories = label_map_util.convert_label_map_to_categories(
    face_label_map, max_num_classes=2, use_display_name=True)
face_category_index = label_map_util.create_category_index(face_categories)

face_detection_graph = tf.Graph()

with face_detection_graph.as_default():
    face_od_graph_def = tf.GraphDef()
    with tf.gfile.GFile('inference_graph_face_ssd/frozen_inference_graph.pb',
                        'rb') as fid:
        face_serialized_graph = fid.read()
        face_od_graph_def.ParseFromString(face_serialized_graph)
        tf.import_graph_def(face_od_graph_def, name='')

    face_session = tf.Session(graph=face_detection_graph)

face_image_tensor = face_detection_graph.get_tensor_by_name('image_tensor:0')
face_detection_boxes = face_detection_graph.get_tensor_by_name(
    'detection_boxes:0')
コード例 #8
0
def detectObject():
    import numpy as np
    import os
    import six.moves.urllib as urllib
    import sys
    import tarfile
    import tensorflow as tf
    import win32
    from PIL import Image

    # This is needed since the notebook is stored in the object_detection folder.
    sys.path.append("..")
    from object_detection.utils import ops as utils_ops

    if tf.__version__ < '1.4.0':
        raise ImportError(
            'Please upgrade your tensorflow installation to v1.4.* or later!')

    import cv2
    cap = cv2.VideoCapture(0)

    # ## Env setup

    from utils import label_map_util

    from utils import visualization_utils as vis_util

    # # Model preparation

    # ## Variables
    #
    # Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_FROZEN_GRAPH` to point to a new .pb file.
    #
    # By default we use an "SSD with Mobilenet" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies.

    # What model to download.
    MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
    MODEL_FILE = MODEL_NAME + '.tar.gz'
    DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'

    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'

    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

    NUM_CLASSES = 90

    # ## Download Model

    # Check the frozen_inference_graph is exists at path.
    # If not ssd_mobilenet_v1_coco_2017_11_17 model is downloaded and frozen_inference_graph.pb moved to path.
    if os.path.exists(PATH_TO_FROZEN_GRAPH):
        print(
            "ssd_mobilenet_v1_coco_2017_11_17/frozen_inference_graph.pb Exists at path. Skipping download... "
        )
    else:
        opener = urllib.request.URLopener()
        opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
        tar_file = tarfile.open(MODEL_FILE)
        for file in tar_file.getmembers():
            file_name = os.path.basename(file.name)
            if 'frozen_inference_graph.pb' in file_name:
                tar_file.extract(file, os.getcwd())

    # ## Load a (frozen) Tensorflow model into memory.

    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.compat.v1.GraphDef()
        with tf.io.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    # ## Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    # # Detection

    def run_inference_for_single_image(image, graph):
        with graph.as_default():
            with tf.compat.v1.Session() as sess:
                # Get handles to input and output tensors
                ops = tf.compat.v1.get_default_graph().get_operations()
                all_tensor_names = {
                    output.name
                    for op in ops for output in op.outputs
                }
                tensor_dict = {}
                for key in [
                        'num_detections', 'detection_boxes',
                        'detection_scores', 'detection_classes',
                        'detection_masks'
                ]:
                    tensor_name = key + ':0'
                    if tensor_name in all_tensor_names:
                        tensor_dict[key] = tf.compat.v1.get_default_graph(
                        ).get_tensor_by_name(tensor_name)
                if 'detection_masks' in tensor_dict:
                    # The following processing is only for single image
                    detection_boxes = tf.squeeze(
                        tensor_dict['detection_boxes'], [0])
                    detection_masks = tf.squeeze(
                        tensor_dict['detection_masks'], [0])
                    # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
                    real_num_detection = tf.cast(
                        tensor_dict['num_detections'][0], tf.int32)
                    detection_boxes = tf.slice(detection_boxes, [0, 0],
                                               [real_num_detection, -1])
                    detection_masks = tf.slice(detection_masks, [0, 0, 0],
                                               [real_num_detection, -1, -1])
                    detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
                        detection_masks, detection_boxes, image.shape[0],
                        image.shape[1])
                    detection_masks_reframed = tf.cast(
                        tf.greater(detection_masks_reframed, 0.5), tf.uint8)
                    # Follow the convention by adding back the batch dimension
                    tensor_dict['detection_masks'] = tf.expand_dims(
                        detection_masks_reframed, 0)
                image_tensor = tf.compat.v1.get_default_graph(
                ).get_tensor_by_name('image_tensor:0')

                # Run inference
                output_dict = sess.run(
                    tensor_dict,
                    feed_dict={image_tensor: np.expand_dims(image, 0)})

                # all outputs are float32 numpy arrays, so convert types as appropriate
                output_dict['num_detections'] = int(
                    output_dict['num_detections'][0])
                output_dict['detection_classes'] = output_dict[
                    'detection_classes'][0].astype(np.uint8)
                output_dict['detection_boxes'] = output_dict[
                    'detection_boxes'][0]
                output_dict['detection_scores'] = output_dict[
                    'detection_scores'][0]
                if 'detection_masks' in output_dict:
                    output_dict['detection_masks'] = output_dict[
                        'detection_masks'][0]
        return output_dict

    while True:
        ret, image_np = cap.read()
        # Actual detection.
        output_dict = run_inference_for_single_image(image_np, detection_graph)
        # Visualization of the results of a detection.
        vis_util.visualize_boxes_and_labels_on_image_array(
            image_np,
            output_dict['detection_boxes'],
            output_dict['detection_classes'],
            output_dict['detection_scores'],
            category_index,
            instance_masks=output_dict.get('detection_masks'),
            use_normalized_coordinates=True,
            line_thickness=8)
        #print(output_dict['detection_scores'])
        lbl = output_dict['detection_classes'][0]
        #print(category_index[lbl]['name'])
        win32.voice(category_index[lbl]['name'])
        cv2.imshow('Image Detction', cv2.resize(image_np, (1280, 720)))
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break
コード例 #9
0
def accident_detection():
    print("car crash detection")
    MODEL_NAME = 'inference_graph'

    CWD_PATH = os.getcwd()

    PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_NAME,
                                'frozen_inference_graph.pb')

    PATH_TO_LABELS = os.path.join(CWD_PATH, 'training', 'labelmap.pbtxt')

    NUM_CLASSES = 2

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

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

        sess = tf.Session(graph=detection_graph)

    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name(
        'detection_classes:0')

    num_detections = detection_graph.get_tensor_by_name('num_detections:0')

    count = 1

    cap = cv2.VideoCapture(
        'C:\\TensorflowModels\\models\\research\\object_detection\\test_video\\acc6.mp4'
    )
    while (cap.isOpened()):
        ret, frame = cap.read()
        image_expanded = np.expand_dims(frame, axis=0)
        (boxes, scores, classes,
         num) = sess.run([
             detection_boxes, detection_scores, detection_classes,
             num_detections
         ],
                         feed_dict={image_tensor: image_expanded})
        # if args.textual:
        print("\n\n" + "=" * 50 + "	Results    " + "=" * 50 + "\n\n")
        print("        Class               Surety")
        print()
        count = 0
        for i in range(scores.shape[1]):
            if scores[0, i] > 0.8:
                print("    " + str(i + 1) + ".  " +
                      str(category_index.get(classes[0, i])['name']) +
                      "    ==>    " + str(scores[0, i] * 100) + ' %')
                print()
                count += 1

                if (str(category_index.get(classes[0,
                                                   i])['name']) == "crashed"):
                    winsound.Beep(440, 500)
        print("\n	Total " + str(count) + " objects classified.\n")

        vis_util.visualize_boxes_and_labels_on_image_array(
            frame,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=8,
            min_score_thresh=0.80)
        cv2.imshow('Object detector', frame)
        count += 1

        cv2.waitKey(1)

    cap.release()

    cv2.destroyAllWindows()
コード例 #10
0
ファイル: app.py プロジェクト: shrutileena/MiniProject
def upload():
    target = os.path.join(APP_ROOT, 'images')
    if not os.path.isdir(target):
        os.mkdir(target)

    for file in request.files.getlist("file"):
        filename = file.filename
        destination = '/'.join([target, filename])
        file.save(destination)
        MODEL_NAME = 'inference_graph'
        IMAGES = os.listdir('C:/Users/MY PC/Desktop/FlaskApp/images')
        IMAGE_NAME = IMAGES[0]
        IMAGE_PATH = 'C:/Users/MY PC/Desktop/FlaskApp/images'

        CWD_PATH = 'E:/miniproject'

        PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_NAME,
                                    'frozen_inference_graph.pb')

        PATH_TO_LABELS = os.path.join(CWD_PATH, 'training', 'labelmap.pbtxt')

        PATH_TO_IMAGE = os.path.join(IMAGE_PATH, IMAGE_NAME)

        NUM_CLASSES = 1

        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        category_index = label_map_util.create_category_index(categories)

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

            sess = tf.Session(graph=detection_graph)

        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

        detection_boxes = detection_graph.get_tensor_by_name(
            'detection_boxes:0')

        detection_scores = detection_graph.get_tensor_by_name(
            'detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name(
            'detection_classes:0')

        num_detections = detection_graph.get_tensor_by_name('num_detections:0')

        image = cv2.imread(PATH_TO_IMAGE)
        image_expanded = np.expand_dims(image, axis=0)

        (boxes, scores, classes,
         num) = sess.run([
             detection_boxes, detection_scores, detection_classes,
             num_detections
         ],
                         feed_dict={image_tensor: image_expanded})

        vis_util.visualize_boxes_and_labels_on_image_array(
            image,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=4,
            min_score_thresh=0.65)

        cv2.imshow('Object detector', image)

        cv2.imwrite('new_img.jpg', image)

        cv2.waitKey(0)

        cv2.destroyAllWindows()
        os.remove(PATH_TO_IMAGE)
    return render_template('predict.html')
コード例 #11
0
 def load_category(self, label_file):
     label_map = label_map_util.load_labelmap(label_file)
     categories = label_map_util.convert_label_map_to_categories(
         label_map, max_num_classes=20, use_display_name=True)
     category_index = label_map_util.create_category_index(categories)
     return category_index
コード例 #12
0
def detect_in_video():

    detection_graph = tf.Graph()
    with detection_graph.as_default():

        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:

            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

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

            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

            detection_boxes = detection_graph.get_tensor_by_name(
                'detection_boxes:0')
            detection_scores = detection_graph.get_tensor_by_name(
                'detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name(
                'detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name(
                'num_detections:0')

            videogen = skvideo.io.vreader('myVideo.mkv')
            frames = list(videogen)

            for i, frame in enumerate(frames[:500]):

                if i % 100 == 0:
                    print(f'{i}/{len(frames)}')

                image_np_expanded = np.expand_dims(frame, axis=0)

                (boxes, scores, classes,
                 num) = sess.run([
                     detection_boxes, detection_scores, detection_classes,
                     num_detections
                 ],
                                 feed_dict={image_tensor: image_np_expanded})

                vis_util.visualize_boxes_and_labels_on_image_array(
                    frame,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=2,
                    min_score_thresh=.20)

                frames2.append(frame)

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

            skvideo.io.vwrite("outputVideoX.mkv", frames2)
            print('Complete!')
コード例 #13
0
def detect_in_video():
    # VideoWriter is the responsible of creating a copy of the video
    # used for the detections but with the detections overlays. Keep in
    # mind the frame size has to be the same as original video.
    #fourcc = cv2.VideoWriter_fourcc(*'XVID')
    #out = cv2.VideoWriter('gun.mp4', cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), 250, (1280, 720))
    out = cv2.VideoWriter('gun7.avi',
                          cv2.VideoWriter_fourcc('M', 'J', 'P',
                                                 'G'), 30, (1280, 720))

    #Load a (frozen) Tensorflow model into memory.
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    #Loading label map
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Definite input and output Tensors for detection_graph
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # Each box represents a part of the image where a particular object
            # was detected.
            detection_boxes = detection_graph.get_tensor_by_name(
                'detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class
            # label.
            detection_scores = detection_graph.get_tensor_by_name(
                'detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name(
                'detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name(
                'num_detections:0')
            #cv2.VideoCapture (): 0 is the default camera of the computer, 1 can change the source.
            #for example: direct access to surveillance cameras
            cap = cv2.VideoCapture(
                'C:\\Users\\28771\\models\\research\\object_detection\\test_images1\\video7.mp4'
            )

            while (cap.isOpened()):
                # Read the frame and capture frame-by-frame
                ret, frame = cap.read()

                # Recolor the frame. By default, OpenCV uses BGR color space.
                detect_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

                image_np_expanded = np.expand_dims(detect_frame, axis=0)

                # Actual detection.
                (boxes, scores, classes,
                 num) = sess.run([
                     detection_boxes, detection_scores, detection_classes,
                     num_detections
                 ],
                                 feed_dict={image_tensor: image_np_expanded})

                # Visualization of the results of a detection.
                # note: perform the detections using a higher threshold
                vis_util.visualize_boxes_and_labels_on_image_array(
                    detect_frame,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8,
                    min_score_thresh=.20)

                #cv2.imshow('detect_output', detect_frame)
                output_rgb = cv2.cvtColor(detect_frame, cv2.COLOR_RGB2BGR)
                cv2.imshow('detect_output', output_rgb)
                out.write(output_rgb)

                #The number in waitKey(1) represents the invalid time before waiting for the key input.
                #The unit is milliseconds. During this time period, the key 'q' will not be recorded.
                #In other words, after the invalid time, it is detected whether the key 'q' is pressed in the time period of the last image display.
                #If not, it will jump out of the if statement and capture and display the next frame of image.
                #The return value of cv2.waitkey (1) is more than 8 bits, but only the last 8 bits are actually valid.
                # To avoid interference, the remaining position is 0 through the '&' operation
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            #when everything done , release the capture
            out.release()
            cap.release()
コード例 #14
0
else:
    print('Coco Model Already Exists!!!')

detection_graph = tf.Graph()  #load the graph
with detection_graph.as_default():
    od_graph_def = tf.compat.v1.GraphDef()  #define the graph
    with tf.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
        serialized_graph = fid.read()  # read the graph
        od_graph_def.ParseFromString(serialized_graph)  #formating of the data
        tf.import_graph_def(od_graph_def, name='')

label_map = label_map_util.load_labelmap(PATH_TO_LABELS)  #load the labels
categories = label_map_util.convert_label_map_to_categories(
    label_map, max_num_classes=NUM_CLASSES,
    use_display_name=True)  #divide labels in different classes
categories_index = label_map_util.create_category_index(
    categories)  #give the index to the category

###################  MODULE 2 ############################


def load_image_into_numpy_array(
        image):  #load the image and convert the image data into numpy array
    (im_width, im_height) = image.size
    return np.array(image.getdata()).reshape(
        (im_height, im_width, 3)).astype(np.uint8)


PATH_TO_TEST_IMAGES_DIR = 'C:/Users/it/PycharmProjects/objectdetection-master/objectdetection-master/object_recognition_detection/object_recognition_detection/test_images/'  #load images for testing
TEST_IMAGE_PATH = [
    os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i))
    for i in range(1, 10)
コード例 #15
0
def camara():

    tamanoMax = 768
    movimiento = False
    count = 0

    #file = tkinter.filedialog.askopenfilename(defaultextension="*.avi", title="Control: diropenbox",initialdir="C:/Users/DANI/Desktop/object_recognition_detection",parent=None)

    cam = cv2.VideoCapture(1)
    if (cam.isOpened() == False):
        print("ERROR: Camara no operativa")
        cam = cv2.VideoCapture(0)
        if (cam.isOpened() == False):
            exit(-1)  # Error acceso a la camara

    # Llamada al método
    fgbg = cv2.createBackgroundSubtractorKNN(history=500,
                                             dist2Threshold=400,
                                             detectShadows=False)

    # Deshabilitamos OpenCL, si no hacemos esto no funciona
    cv2.ocl.setUseOpenCL(False)

    contadorMovimiento = 0

    ret = True

    #cargarTensorflow()

    # # Model preparation
    # Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_CKPT` to point to a new .pb file.
    # By default we use an "SSD with Mobilenet" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies.

    # What model to download.
    MODEL_NAME = 'ssd_mobilenet_v1_coco_11_06_2017'
    MODEL_FILE = MODEL_NAME + '.tar.gz'
    DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'

    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

    NUM_CLASSES = 90

    # ## Download Model

    if not os.path.exists(MODEL_NAME + '/frozen_inference_graph.pb'):
        print('Downloading the model')
        opener = urllib.request.URLopener()
        opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
        tar_file = tarfile.open(MODEL_FILE)
        for file in tar_file.getmembers():
            file_name = os.path.basename(file.name)
            if 'frozen_inference_graph.pb' in file_name:
                tar_file.extract(file, os.getcwd())
        print('Download complete')
    else:
        print('Model already exists')

    # ## Load a (frozen) Tensorflow model into memory.

    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT,
                            'rb') as fid:  # aqui le da el modelo de datos
            serialized_graph = fid.read()  # lee
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    # ## Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES,
        use_display_name=True)  # estaba true
    category_index = label_map_util.create_category_index(categories)

    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(
        "GRABACIONES/" + time.strftime("%d_%m_%y_%H%M%S") + '.avi', fourcc,
        20.0, (640, 480))
    #out1=cv2.VideoWriter("GRABACIONES/"+"contorno_"+time.strftime("%d_%m_%y_%H%M%S")+'.avi', fourcc, 20.0, (640, 480))
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s : %(levelname)s : %(message)s',
        filename=("GRABACIONES/" + "Indicencias_" +
                  time.strftime("%d_%m_%y_%H%M%S") + '.log'),
        filemode='w',
    )
    logging.debug('REGISTRO DE INCIDENCIAS.')
    instanteInicial = time.time()

    instanteFinal = time.time()
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            while True:
                ret, frame = cam.read()
                contornosimg = frame
                if ret == True:
                    movimiento = False
                    frame = cv2.resize(frame, (640, 480))
                    contornosimg = cv2.resize(contornosimg, (640, 480))
                    # IMPORTADO TUTORIAL MOVIMIENTO
                    # Aplicamos el algoritmo
                    fgmask = fgbg.apply(frame)

                    # Copiamos el umbral para detectar los contornos
                    contornosimg = fgmask.copy()

                    # Buscamos contorno en la imagen
                    im, contornos, hierarchy = cv2.findContours(
                        contornosimg, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

                    # Recorremos todos los contornos encontrados
                    for c in contornos:
                        # Eliminamos los contornos más pequeños

                        if cv2.contourArea(c) < 500:
                            continue
                        else:
                            movimiento = True

                        # Obtenemos el bounds del contorno, el rectángulo mayor que engloba al contorno
                        (x, y, w, h) = cv2.boundingRect(c)
                        # Dibujamos el rectángulo del bounds
                        cv2.rectangle(contornosimg, (x, y), (x + w, y + h),
                                      (0, 255, 0), 2)

                    #image_np = cv2.resize(image_np, (640, 480))

                    print(movimiento)

                    if count > 3:
                        if ret == True:
                            # frame = cv2.flip(frame,0)
                            contadorMovimiento = 1
                            timestamp = datetime.datetime.now()
                            ts = timestamp.strftime("%d %B %Y %I:%M:%S%p")
                            cv2.rectangle(frame, (2, 220), (185, 235),
                                          (0, 0, 0), -1)
                            cv2.putText(frame, ts, (5, 230), cv2.FONT_ITALIC,
                                        0.35, (255, 111, 255))
                            detectarObjetos(frame, detection_graph,
                                            category_index, sess)
                            print("grabando")
                            for x in range(20):
                                out.write(frame)
                            #out1.write(contornosimg)
                            #cv2.imshow("ventana", frame)
                    #else:
                    #movimiento=False

                    if movimiento == True:

                        count += 1
                        print("mov")
                        instanteInicial = time.time()

                        if count % 15 == 0:
                            print("movimiento a la hora " +
                                  time.strftime("%y_%m_%d_%H%M%S"))
                            nombre = "namepattern-" + time.strftime(
                                "%y-%m-%d- %H:%M:%S") + ".jpg"

                    else:
                        count = 0

                        instanteFinal = time.time()
                        tiempo = instanteFinal - instanteInicial  # Devuelve un objeto timedelta

                        print(tiempo)
                        if (tiempo > 2):
                            #cuando vuelva a haber movimiento empezara un nuevo video
                            #out = cv2.VideoWriter("GRABACIONES/"+time.strftime("%d_%m_%y_%H%M%S") + '.avi', fourcc, 20.0, (640, 480))
                            instanteInicial = instanteFinal
                            contadorMovimiento = 0
                        else:
                            if (contadorMovimiento == 1):
                                print("grabando")
                                #detectarObjetos(frame, detection_graph, category_index,sess)
                                timestamp = datetime.datetime.now()
                                ts = timestamp.strftime("%d %B %Y %I:%M:%S%p")
                                cv2.rectangle(frame, (2, 220), (185, 235),
                                              (0, 0, 0), -1)
                                cv2.putText(frame, ts, (5, 230),
                                            cv2.FONT_ITALIC, 0.35,
                                            (255, 111, 255))

                                out.write(frame)
                                #out1.write(contornosimg)
                                #cv2.imshow("ventana",frame)

                    movimiento = False

    print("camara")
コード例 #16
0
def main():
    print("starting program . . .")

    if not checkIfNecessaryPathsAndFilesExist():
        return
    # end if

    # this next comment line is necessary to avoid a false PyCharm warning
    # noinspection PyUnresolvedReferences
    if StrictVersion(tf.__version__) < StrictVersion('1.5.0'):
        raise ImportError('Please upgrade your tensorflow installation to v1.5.* or later!')
    # end if

    # load a (frozen) TensorFlow model into memory
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(FROZEN_INFERENCE_GRAPH_LOC, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
        # end with
    # end with

    # Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
    label_map = label_map_util.load_labelmap(LABELS_LOC)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
                                                                use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    imageFilePaths = []
    for imageFileName in os.listdir(TEST_IMAGE_DIR):
        if imageFileName.endswith(".jpg"):
            imageFilePaths.append(TEST_IMAGE_DIR + "/" + imageFileName)
        # end if
    # end for

    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            for image_path in imageFilePaths:

                print(image_path)

                image_np = cv2.imread(image_path)

                if image_np is None:
                    print("error reading file " + image_path)
                    continue
                # end if

                # Definite input and output Tensors for detection_graph
                image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
                # Each box represents a part of the image where a particular object was detected.
                detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
                # Each score represent how level of confidence for each of the objects.
                # Score is shown on the result image, together with the class label.
                detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
                detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
                num_detections = detection_graph.get_tensor_by_name('num_detections:0')

                # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(image_np, axis=0)
                # Actual detection.
                (boxes, scores, classes, num) = sess.run(
                    [detection_boxes, detection_scores, detection_classes, num_detections],
                    feed_dict={image_tensor: image_np_expanded})
                # Visualization of the results of a detection.
                vis_util.visualize_boxes_and_labels_on_image_array(image_np,
                                                                   np.squeeze(boxes),
                                                                   np.squeeze(classes).astype(np.int32),
                                                                   np.squeeze(scores),
                                                                   category_index,
                                                                   use_normalized_coordinates=True,
                                                                   line_thickness=8)
                cv2.imshow("image_np", image_np)
                cv2.waitKey()
コード例 #17
0
def main(image_url):
    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    # PATH_TO_CKPT =  'trainedModels/ssd_mobilenet_RoadDamageDetector.pb'
    PATH_TO_CKPT = 'model/sim.pb'
    # List of the strings that is used to add correct label for each box.
    # PATH_TO_LABELS = 'trainedModels/crack_label_map.pbtxt'
    PATH_TO_LABELS = 'model/sim.pbtxt'
    NUM_CLASSES = 8

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

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    IMAGE_SIZE = (12, 8)

    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Definite input and output Tensors for detection_graph
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # Each box represents a part of the image where a particular object was detected.
            detection_boxes = detection_graph.get_tensor_by_name(
                'detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class label.
            detection_scores = detection_graph.get_tensor_by_name(
                'detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name(
                'detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name(
                'num_detections:0')
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # Each box represents a part of the image where a particular object was detected.
            detection_boxes = detection_graph.get_tensor_by_name(
                'detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class label.
            detection_scores = detection_graph.get_tensor_by_name(
                'detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name(
                'detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name(
                'num_detections:0')
            image = Image.open(image_url)
            # the array based representation of the image will be used later in order to prepare the
            # result image with boxes and labels on it.
            image_np = load_image_into_numpy_array(image)
            # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
            image_np_expanded = np.expand_dims(image_np, axis=0)
            # Actual detection.
            (boxes, scores, classes,
             num) = sess.run([
                 detection_boxes, detection_scores, detection_classes,
                 num_detections
             ],
                             feed_dict={image_tensor: image_np_expanded})
            # Visualization of the results of a detection.
            vis_util.visualize_boxes_and_labels_on_image_array(
                image_np,
                np.squeeze(boxes),
                np.squeeze(classes).astype(np.int32),
                np.squeeze(scores),
                category_index,
                min_score_thresh=0.2,
                use_normalized_coordinates=True,
                line_thickness=8)
            plt.figure(figsize=IMAGE_SIZE)
            plt.imshow(image_np)
            count = 0
            print(scores)
            print(classes)
            for score in range(scores[0].shape[0]):
                if (scores[0][score] > 0.2):
                    print('s:', scores[0][score], 'damage: ',
                          classes[0][score])
                    count += 1
            name = image_url.split('/')[2].split('.')[0]
            name = name + '_predicted.png'
            plt.savefig('media/images/' + name)
            return (name, count)
コード例 #18
0
def dealVideo():
    global size
    q.empty()
    '''
        检测视频中的目标
    '''
    cap = cv2.VideoCapture(
        '/Users/hongyanma/gitspace/python/python/data/1.avi')  # 打开摄像头
    size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    #fourcc = cv2.VideoWriter_fourcc(*'DIVX')
    #fps = cap.get(cv2.CAP_PROP_FPS)
    #videoWriter = cv2.VideoWriter('/Users/hongyanma/gitspace/python/python/data/output.mp4', fourcc, fps, size)

    ##################### Download Model
    # What model to download.
    '''
    MODEL_NAME = 'ssd_inception_v2_coco_11_06_2017'
    MODEL_NAME = 'rfcn_resnet101_coco_11_06_2017'
    MODEL_NAME = 'faster_rcnn_resnet101_coco_11_06_2017'
    MODEL_NAME = 'faster_rcnn_inception_resnet_v2_atrous_coco_11_06_2017'
    '''
    # MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
    # MODEL_NAME = 'ssd_resnet'
    MODEL_NAME = 'ssd_mobilenet_v1_coco_2018_01_28'
    MODEL_FILE = MODEL_NAME + '.tar.gz'
    DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'

    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

    NUM_CLASSES = 90

    # Download model if not already downloaded
    if not os.path.exists(PATH_TO_CKPT):
        print('Downloading model... (This may take over 5 minutes)')
        opener = urllib.request.URLopener()
        opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
        print('Extracting...')
        tar_file = tarfile.open(MODEL_FILE)
        for file in tar_file.getmembers():
            file_name = os.path.basename(file.name)
            if 'frozen_inference_graph.pb' in file_name:
                tar_file.extract(file, os.getcwd())
    else:
        print('Model already downloaded.')

    ##################### Load a (frozen) Tensorflow model into memory.
    print('Loading model...')
    detection_graph = tf.Graph()

    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    ##################### Loading label map
    print('Loading label map...')
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)
    print('Detecting...')
    #test
    # path = '/Users/hongyanma/gitspace/python/python/data/test.mp4'
    # with open(path, 'rb') as fmp3:
    #     data = fmp3.read(1024)
    #     while data:
    #         data = fmp3.read(1024)
    #         q.put(data)
    #test
    image_container = {}
    #cv2.namedWindow('Image')
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            while True:
                ret, image_np = cap.read()  # 从摄像头中获取每一帧图像
                #print('aaa')
                #def generate():
                #data = bytes(image_np)
                #yield data
                #q.put(data)
                #print('sss')
                image_np_expanded = np.expand_dims(image_np, axis=0)
                image_tensor = detection_graph.get_tensor_by_name(
                    'image_tensor:0')
                boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
                scores = detection_graph.get_tensor_by_name(
                    'detection_scores:0')
                classes = detection_graph.get_tensor_by_name(
                    'detection_classes:0')
                num_detections = detection_graph.get_tensor_by_name(
                    'num_detections:0')
                # Actual detection.
                if not ret:
                    print('end of video !! ret flag')
                    break
                if not image_np_expanded.size:
                    print('end of video !!')
                    break
                try:
                    (boxes, scores, classes, num_detections) = sess.run(
                        [boxes, scores, classes, num_detections],
                        feed_dict={image_tensor: image_np_expanded})
                except:
                    print('error!!!')
                    break

                extractData(image_np, np.squeeze(boxes),
                            np.squeeze(classes).astype(np.int32),
                            np.squeeze(scores), category_index,
                            image_container)
                vis_util.visualize_boxes_and_labels_on_image_array(
                    image_np,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8)
                q.put(image_np)
                # data = bytes(image_np)
                # yield data
                #videoWriter.write(image_np)
    global video_end_flag
    video_end_flag = True
    cap.release()
コード例 #19
0
def api_detection_view(request):
    detection = Detection.objects.all()

    if request.method == 'POST':
        serializer = DetectionSerializers

        MODEL_NAME = 'inference_graph'
        IMAGE_NAME = 'P10.jpg'

        CWD_PATH = os.getcwd()

        PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_NAME,
                                    'frozen_inference_graph.pb')
        PATH_TO_LABELS = os.path.join(CWD_PATH, 'training', 'labelmap.pbtxt')
        PATH_TO_IMAGE = os.path.join(CWD_PATH, IMAGE_NAME)

        NUM_CLASSES = 3

        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        category_index = label_map_util.create_category_index(categories)

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

            sess = tf.Session(graph=detection_graph)

        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

        detection_boxes = detection_graph.get_tensor_by_name(
            'detection_boxes:0')
        detection_scores = detection_graph.get_tensor_by_name(
            'detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name(
            'detection_classes:0')

        num_detections = detection_graph.get_tensor_by_name('num_detections:0')

        image = cv2.imread(PATH_TO_IMAGE)
        image_expanded = np.expand_dims(image, axis=0)

        (boxes, scores, classes,
         num) = sess.run([
             detection_boxes, detection_scores, detection_classes,
             num_detections
         ],
                         feed_dict={image_tensor: image_expanded})

        vis_util.visualize_boxes_and_labels_on_image_array(
            image,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=8,
            min_score_thresh=0.60)

        cv2.imshow('Object detector', image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

        serializer.save()

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #20
0
 def _load_label_map(self):
     label_map = label_map_util.load_labelmap(self.PATH_TO_LABELS)
     categories = label_map_util.convert_label_map_to_categories(
         label_map, max_num_classes=self.NUM_CLASSES, use_display_name=True)
     category_index = label_map_util.create_category_index(categories)
     return category_index
コード例 #21
0
def objectDetectionCap():  
    import cv2
    import numpy as np 
    import os
    import six.moves.urllib as urllib
    import sys
    import tarfile
    import tensorflow as tf
    import zipfile
    
    from collections import defaultdict
    from io import StringIO
    from matplotlib import pyplot as plt
    from PIL import Image
    

    # This is needed since the notebook is stored in the object_detection folder.
    #cwd = os.getcwd()
    #os.chdir('../lib')
    #sys.path.append("..")
    os.chdir('../lib')
    from object_detection.utils import ops as utils_ops

    
    if tf.__version__ < '1.4.0':
      raise ImportError('Please upgrade your tensorflow installation to v1.4.* or later!')
    
    
    # ## Env setup
    
    
    # This is needed to display the images.
    get_ipython().magic('matplotlib inline')
    
    
    # ## Object detection imports
    # Here are the imports from the object detection module.
    
    os.chdir('../lib/object_detection')
    from utils import label_map_util
    
    from utils import visualization_utils as vis_util
    
    
    # # Model preparation 
    
    # ## Variables
    # 
    # Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_CKPT` to point to a new .pb file.  
    # 
    # By default we use an "SSD with Mobilenet" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies.
    
    
    # What model to download.
    MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
    MODEL_FILE = MODEL_NAME + '.tar.gz'
    DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
    
    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
    
    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
    
    NUM_CLASSES = 90
    
   
    # ## Download Model
    
#    opener = urllib.request.URLopener()
#    opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
#    tar_file = tarfile.open(MODEL_FILE)
#    for file in tar_file.getmembers():
#      file_name = os.path.basename(file.name)
#      if 'frozen_inference_graph.pb' in file_name:
#        tar_file.extract(file, os.getcwd())
   
    
    # ## Load a (frozen) Tensorflow model into memory.
    
    
    detection_graph = tf.Graph()
    with detection_graph.as_default():
      od_graph_def = tf.GraphDef()
      with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')
    
    
    # ## Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
    
    
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    
    cap = cv2.VideoCapture(0)
    with detection_graph.as_default():
      with tf.Session(graph=detection_graph) as sess:
       ret = True
       while (ret):
          ret,image_np = cap.read()
          image_np_expanded = np.expand_dims(image_np, axis=0)
          image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
          boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
          scores = detection_graph.get_tensor_by_name('detection_scores:0')
          classes = detection_graph.get_tensor_by_name('detection_classes:0')
          num_detections = detection_graph.get_tensor_by_name('num_detections:0')
          (boxes, scores, classes, num_detections) = sess.run(
                  [boxes, scores, classes, num_detections],
                  feed_dict={image_tensor: image_np_expanded})
          vis_util.visualize_boxes_and_labels_on_image_array(
              image_np,
              np.squeeze(boxes),
              np.squeeze(classes).astype(np.int32),
              np.squeeze(scores),
              category_index,
              use_normalized_coordinates=True,
              line_thickness=8)
          cv2.imshow('image', cv2.resize(image_np,(1280,800)))
          if cv2.waitKey(25) & 0xFF == ord('q'):
              cv2.destroyAllWindows()
              cap.release()
              break

    os.chdir('../../doc')
コード例 #22
0
def main():
  # detection result folder
  detection_folder = '/media/yujiang/Data/Seedling/Experiments/counting_results_rgb/Final_model'
  # video folder
  video_folder = '/media/yujiang/Data/Seedling/Datasets/TestVideos'
  # folder for saving counted videos
  saving_folder = '/media/yujiang/Data/Seedling/Experiments/counting_videos_rgb/Final_model'
  # label map
  PATH_TO_LABELS = '/media/yujiang/Data/Seedling/Datasets/TFrecords/pascal_label_map.pbtxt'
  NUM_CLASSES = 2 # DO NOT change this, the number of classess identified by the detection model
  # Create label map
  label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
  categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
  category_index = label_map_util.create_category_index(categories)

  video_prefix_config_dict = {
    'TAMU_2015': {
      'iou_th':0.1, 'tracking_lifetime_th':9, 'is_transpose': False,
      'resize_fx':0.5, 'resize_fy':0.5, 'output_fps':5.0, 'is_display':True},
    'UGA_2015': {
      'iou_th':0.1, 'tracking_lifetime_th':15, 'is_transpose': False,
      'resize_fx':0.5, 'resize_fy':0.5, 'output_fps':5.0, 'is_display':True},
    'UGA_2018': {
      'iou_th':0.1, 'tracking_lifetime_th':9, 'is_transpose': True,
      'resize_fx':0.5, 'resize_fy':0.5, 'output_fps':5.0, 'is_display':True}}

  # Output video codec
  fourcc_codec = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')

  for video_prefix, counting_config in video_prefix_config_dict.items():
    
    # tracking and counting parameters
    is_transpose = counting_config['is_transpose']
    iou_th = counting_config['iou_th']
    tracking_lifetime_th = counting_config['tracking_lifetime_th']
    resize_fx = counting_config['resize_fx']
    resize_fy = counting_config['resize_fy']
    output_fps = counting_config['output_fps']
    is_display = counting_config['is_display']
    # get testing videos with the video prefix
    video_list = os.listdir(video_folder)
    video_list = [f for f in video_list if f.startswith(video_prefix) and f.endswith('.mp4')]

    for video_file in video_list:
      video_name = video_file.split('.')[0]
      detection_file_path = osp.join(detection_folder, '{0}_detection.json'.format(video_name))
      video_file_path = osp.join(video_folder, video_file)
      output_video_filepath = osp.join(saving_folder, '{0}.avi'.format(video_file[:-4]))

      # tracking list
      seedling_id_list = list()
      seedling_lifetime = dict()
      # load detection results
      with open(detection_file_path,'r') as fid:
        jstr = json.load(fid)
        res_list = json.loads(jstr)

        # load video for rendering tracking result
        cap = cv2.VideoCapture(video_file_path)
        frame_num = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

        # generate output video writer
        fr_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
        fr_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
        if is_transpose:
          vd_writer = cv2.VideoWriter(output_video_filepath,
            fourcc_codec, output_fps,
            (int(fr_height*resize_fy), int(fr_width*resize_fx)))
        else:
          vd_writer = cv2.VideoWriter(output_video_filepath,
            fourcc_codec, output_fps,
            (int(fr_width*resize_fx), int(fr_height*resize_fy)))

        # initialize the Kalman filter based tracking algorithm
        mot_tracker = sort.Sort(iou_th=iou_th)
        # process individual frames
        for i in range(frame_num-1):
          ret, frame = cap.read()
          frame = cv2.resize(frame,None,fx=resize_fx, fy=resize_fy, interpolation = cv2.INTER_CUBIC)
          image_np = frame
          if is_transpose:
            image_np = image_np.transpose((1,0,2))
          # load detection result for the current frame
          detection_res = res_list[i]
          frame_bbox = detection_res['bbox']
          frame_bbox_array = np.array(frame_bbox, dtype=np.float32)
          frame_cls = detection_res['box_cls']
          frame_cls = np.array([int(i) for i in frame_cls])
          frame_scores = np.asarray(detection_res['box_scores']).reshape((-1,1))

          if len(frame_bbox_array) != 0:
            dets = np.concatenate((frame_bbox_array, frame_scores), axis=1)
          else:
            dets = np.array([])
          # update trackers
          trackers = mot_tracker.update(dets)
          # update tracker names
          frame_bbox_array_new = trackers[:,0:4]
          frame_cls_new = list()
          category_index_new = dict()
          for d_index, d in enumerate(trackers[:, 4]):
            if d not in seedling_id_list:
              seedling_id_list.append(d)
              # max_cls_id = len(seedling_id_list)
            cur_d = seedling_id_list.index(d)+1

            if cur_d not in list(seedling_lifetime.keys()):
              seedling_lifetime[cur_d] = list()
            seedling_lifetime[cur_d].append(i)
            frame_cls_new.append(cur_d) 
            category_index_new[cur_d] = {'id':d_index, 'name':'Plant'+str(cur_d)}

          # Visualization of the results of a detection.
          vis_util.visualize_boxes_and_labels_on_image_array(
              image_np,
              frame_bbox_array_new,
              frame_cls_new,
              frame_scores,
              category_index_new, # previously use 'category_index'
              use_normalized_coordinates=True,
              line_thickness=2)
          # write to the output video
          vd_writer.write(image_np)
          # 
          if is_display is True:
            cv2.imshow('video_window',image_np)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        cap.release()
        vd_writer.release()
        cv2.destroyAllWindows()
        print('Processed the video {0} and saved as {1}.\n'.format(
          video_file_path, output_video_filepath))
コード例 #23
0
def main():
    print("starting program . . .")

    if not checkIfNecessaryPathsAndFilesExist():
        return
    # end if
    classifications = []
    # for each line in the label file . . .
    for currentLine in tf.gfile.GFile(RETRAINED_LABELS_TXT_FILE_LOC):
        # remove the carriage return
        classification = currentLine.rstrip()
        # and append to the list
        classifications.append(classification)
    # this next comment line is necessary to avoid a false PyCharm warning
    # noinspection PyUnresolvedReferences
    if StrictVersion(tf.__version__) < StrictVersion('1.5.0'):
        raise ImportError(
            'Please upgrade your tensorflow installation to v1.5.* or later!')
    # end if

    # load a (frozen) TensorFlow model into memory
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        with tf.gfile.GFile(FROZEN_INFERENCE_GRAPH_LOC, 'rb') as fid:
            od_graph_def = tf.GraphDef()
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
        # end with
    # end with
    classification_graph = tf.Graph()
    with classification_graph.as_default():
        with tf.gfile.FastGFile(RETRAINED_GRAPH_PB_FILE_LOC,
                                'rb') as retrainedGraphFile:
            # instantiate a GraphDef object
            graphDef = tf.GraphDef()
            # read in retrained graph into the GraphDef object
            graphDef.ParseFromString(retrainedGraphFile.read())
            # import the graph into the current default Graph, note that we don't need to be concerned with the return value
            tf.import_graph_def(graphDef, name='')
    # end with
    # Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
    label_map = label_map_util.load_labelmap(LABELS_LOC)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    with tf.Session(graph=detection_graph) as sessd:
        with tf.Session(graph=classification_graph) as sessc:
            for imageFileName in os.listdir(TEST_IMAGE_DIR):
                if not imageFileName.endswith(".jpg"):
                    continue
                image_path = TEST_IMAGE_DIR + "/" + imageFileName
                print(image_path)
                image_np = cv2.imread(image_path)

                if image_np is None:
                    print("error reading file " + image_path)
                    continue
                # end if

                # Definite input and output Tensors for detection_graph
                image_tensor = detection_graph.get_tensor_by_name(
                    'image_tensor:0')
                # Each box represents a part of the image where a particular object was detected.
                detection_boxes = detection_graph.get_tensor_by_name(
                    'detection_boxes:0')
                # Each score represent how level of confidence for each of the objects.
                # Score is shown on the result image, together with the class label.
                detection_scores = detection_graph.get_tensor_by_name(
                    'detection_scores:0')
                detection_classes = detection_graph.get_tensor_by_name(
                    'detection_classes:0')
                num_detections = detection_graph.get_tensor_by_name(
                    'num_detections:0')

                # Expanded dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(image_np, axis=0)
                # Actual detection.
                (boxes, scores, classes,
                 num) = sessd.run([
                     detection_boxes, detection_scores, detection_classes,
                     num_detections
                 ],
                                  feed_dict={image_tensor: image_np_expanded})
                # Visualization of the results of a detection.
                img_height, img_width, img_channel = image_np.shape
                THRESHOLD = 0.4
                #print(scores[0])
                i = 0
                while scores[0][i] >= THRESHOLD and i <= 99:
                    ymin, xmin, ymax, xmax = boxes[0][i]
                    x_up = int(xmin * img_width)
                    y_up = int(ymin * img_height)
                    x_down = int(xmax * img_width)
                    y_down = int(ymax * img_height)
                    cropped_image = image_np[y_up:y_down, x_up:x_down]
                    detected_ocr = OCR.ocr(cropped_image)
                    # get the final tensor from the graph
                    finalTensor = sessc.graph.get_tensor_by_name(
                        'final_result:0')
                    # convert the OpenCV image (numpy array) to a TensorFlow image
                    tfImage = np.array(cropped_image)[:, :, 0:3]
                    # run the network to get the predictions
                    predictions = sessc.run(finalTensor,
                                            {'DecodeJpeg:0': tfImage})
                    # sort predictions from most confidence to least confidence
                    sortedPredictions = predictions[0].argsort(
                    )[-len(predictions[0]):][::-1]

                    prediction = sortedPredictions[0]
                    strClassification = classifications[prediction]
                    if strClassification.endswith("s"):
                        strClassification = strClassification[:-1]
                    confidence = predictions[0][prediction]
                    scoreAsAPercent = confidence * 100.0
                    label = strClassification + ", " + "{0:.2f}".format(
                        scoreAsAPercent)
                    cv2.putText(image_np, label, (int(x_up), int(y_down)),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 1)
                    #cv2.putText(image_np, detected_ocr, (int((x_down+x_up)/2), int(y_up), cv2.FONT_HERSHEY_SIMPLEX,0.8, (255, 0, 0),1)
                    i = i + 1
                    #cropped_image_path = os.getcwd() + "/detected_number_plates/" + imageFileName[:-4] + str(i) + ".jpg"
                    #cv2.imwrite(cropped_image_path, cropped_image)

                vis_util.visualize_boxes_and_labels_on_image_array(
                    image_np,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=3)

                # cv2.imshow("image_np", image_np)
                # while cv2.waitKey() != 32:
                #     pass
                cv2.imwrite(results_dir + imageFileName, image_np)
コード例 #24
0
def main(_):
  print(time.ctime())
  host, port = FLAGS.server.split(':')
  channel = implementations.insecure_channel(host, int(port))
  stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
  # Send request
  IMAGE_NAME = '/home/rice/tensorflow1/models/research/object_detection/test_image/'

  TEST_IMAGE_PATHS = glob.glob(os.path.join(IMAGE_NAME, '*.*'))


  for image_path in TEST_IMAGE_PATHS:
    print(image_path)

    image = Image.open(image_path)
    # the array based representation of the image will be used later in order to prepare the
    # result image with boxes and labels on it.
    image_np = load_image_into_numpy_array(image)
    image_np_expanded = np.expand_dims(image_np, axis=0)
    print(image_np_expanded.shape)

    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'saved_model'
    request.model_spec.signature_name = 'serving_default'
    # request.inputs['inputs'].CopyFrom(
    #     tf.contrib.util.make_tensor_proto(image_np_expanded,shape=[1]))
    # print(time.ctime())
    # request.inputs['inputs'].CopyFrom(
    #     make_tensor_proto(image_np_expanded)
    # )

    # dims = [tensor_shape_pb2.TensorShapeProto.Dim(size=1)]
    # tensor_shape_proto = tensor_shape_pb2.TensorShapeProto(dim=dims)
    tensor_proto = tensor_pb2.TensorProto(
        dtype=types_pb2.DT_UINT8,
        tensor_shape=tensor_shape.as_shape(image_np_expanded.shape).as_proto(),
        # string_val=[open(image_path, 'rb').read()]
    )
    tensor_proto.tensor_content = image_np_expanded.tostring()

    request.inputs['inputs'].CopyFrom(tensor_proto)

    print(time.ctime())
    start = time.time()
    result = stub.Predict(request, 100.0)  # 10 secs timeout
    #print(result)
    print(time.time() - start)


    boxes = (result.outputs['detection_boxes'].float_val)
    classes = (result.outputs['detection_classes'].float_val)
    scores = (result.outputs['detection_scores'].float_val)

    box_np_arr = array([boxes[x:x + 4] for x in range(0, len(boxes), 4)])
    class_np_arr = array([classes[x:x + 1] for x in range(0, len(classes), 1)])
    score_np_arr = array([scores[x:x + 1] for x in range(0, len(scores), 1)])

    # print(type(box_np_arr),len(box_np_arr))
    # print(type(class_np_arr),len(classes))
    # print(type(class_np_arr),len(scores))

    # print(result)

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=14, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    image_vis,result_box = vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      np.squeeze(box_np_arr),
      np.squeeze(class_np_arr).astype(np.int32),
      np.squeeze(score_np_arr),
      category_index,
      use_normalized_coordinates=True,
      line_thickness=8,
      min_score_thresh=0.80)

    # Save inference to disk
    scipy.misc.imsave('%s.jpg' % (image), image_vis)

    for box,label_score in result_box.items():
        print(label_score,',',box)

    break
コード例 #25
0
# coding: utf-8
from __future__ import print_function
import numpy as np
import os
# import six.moves.urllib as urllib
import sys
# import tarfile
import tensorflow as tf
# import zipfile
# from collections import defaultdict
# from io import StringIO
# import matplotlib
# matplotlib.use('TkAgg')
# from matplotlib import pyplot as plt
# from PIL import Image
from utils import label_map_util
from utils import visualization_utils as vis_util
from utils import ops as utils_ops

# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")

if tf.__version__ < '1.4.0':
    raise ImportError('Please upgrade your tensorflow installation to v1.4.* or later!')


# ## Variables
#
# Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_CKPT` to point to a new .pb file.
コード例 #26
0
def apicall():
    if request.method == 'POST':
        path = './Image'

        # Initialize dlib's face detector (HOG-based) and then create
        # The facial landmark predictor
        detector = dlib.get_frontal_face_detector()
        predictor = dlib.shape_predictor(
            "./models/object_detection/shape_predictor_68_face_landmarks.dat")
        # Image info
        img_file = request.files['pic']
        img_name = img_file.filename
        img_file.save(os.path.join(app.config['UPLOAD_FOLDER'], img_name))
        frame = cv2.imread(os.path.join(app.config['UPLOAD_FOLDER'], img_name))
        frame1 = frame
        image = imutils.resize(frame, width=500)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        rects = detector(gray, 1)

        # Loop over the face detections
        for (i, rect) in enumerate(rects):
            shape = predictor(gray, rect)
            shape = face_utils.shape_to_np(shape)

            for (name, (i, j)) in face_utils.FACIAL_LANDMARKS_IDXS.items():
                clone = image.copy()
                cv2.putText(clone, name, (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
                            0.7, (0, 0, 255), 2)

                for (x, y) in shape[i:j]:
                    cv2.circle(clone, (x, y), 1, (0, 0, 255), -1)

                (x, y, w, h) = cv2.boundingRect(np.array([shape[i:j]]))
                roi = image[y:y + h, x:x + w]
                roi = imutils.resize(roi, width=250, inter=cv2.INTER_CUBIC)
                if (name == "right_eye" or name == "left_eye" or name == "nose"
                        or name == "mouth"):
                    cv2.imwrite(os.path.join(path, name + ".jpg"), roi)

        # Custom model for under eye object detection trained using tensorflow implementation of faster-rcnn based on inception-v2

        PATH_TO_CKPT = "./object_detection/inference_graph/frozen_inference_graph.pb"

        # Path to label map file
        PATH_TO_LABELS = "./object_detection/training/labelmap.pbtxt"

        # Number of classes the object detector can identify
        NUM_CLASSES = 1

        # Load the label map.
        # Label maps map indices to category names, so that when our convolution
        # network predicts `5`, we know that this corresponds to `king`.
        # Here we use internal utility functions, but anything that returns a
        # dictionary mapping integers to appropriate string labels would be fine
        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        category_index = label_map_util.create_category_index(categories)

        # Load the Tensorflow model into memory.
        detection_graph = tf.Graph()
        with detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            sess = tf.Session(graph=detection_graph)

        # Define input and output tensors (i.e. data) for the object detection classifier

        # Input tensor is the image
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

        # Output tensors are the detection boxes, scores, and classes
        # Each box represents a part of the image where a particular object was detected
        detection_boxes = detection_graph.get_tensor_by_name(
            'detection_boxes:0')

        # Each score represents level of confidence for each of the objects.
        # The score is shown on the result image, together with the class label.
        detection_scores = detection_graph.get_tensor_by_name(
            'detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name(
            'detection_classes:0')

        # Number of objects detected
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')

        # Load image using OpenCV and
        # expand image dimensions to have shape: [1, None, None, 3]
        # i.e. a single-column array, where each item in the column has the pixel RGB value
        image = cv2.imread(os.path.join(app.config['UPLOAD_FOLDER'], img_name))
        image_expanded = np.expand_dims(image, axis=0)

        # Perform the actual detection by running the model with the image as input
        (boxes, scores, classes,
         num) = sess.run([
             detection_boxes, detection_scores, detection_classes,
             num_detections
         ],
                         feed_dict={image_tensor: image_expanded})

        # Draw the results of the detection (aka 'visulaize the results')

        vis_util.visualize_boxes_and_labels_on_image_array(
            image,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=4,
            min_score_thresh=0.60)

        # initialize the total number of frames that *consecutively* contain
        # santa along with threshold required to trigger the santa alarm
        TOTAL_CONSEC = 0
        TOTAL_THRESH = 20

        # initialize is the santa alarm has been triggered
        #SANTA = False
        print("[INFO] loading model...")

        frame = cv2.imread(os.path.join(path, "right_eye.jpg"))
        right_eye_img = "righteye" + img_name
        cv2.imwrite(os.path.join("./static", right_eye_img), frame)
        frame = imutils.resize(frame, width=400)
        # prepare the image to be classified by our deep learning network
        image = cv2.resize(frame, (75, 75))
        image = np.float32(image / 255)
        image = img_to_array(image)
        image = np.expand_dims(image, axis=0)
        with graph.as_default():
            right_eye = model_eye.predict(image)[0][0]

        frame = cv2.imread(os.path.join(path, "left_eye.jpg"))
        left_eye_img = "lefteye" + img_name
        cv2.imwrite(os.path.join("./static", left_eye_img), frame)
        frame = imutils.resize(frame, width=400)
        # prepare the image to be classified by our deep learning network
        image = cv2.resize(frame, (75, 75))
        image = np.float32(image / 255)
        image = img_to_array(image)
        image = np.expand_dims(image, axis=0)

        with graph.as_default():
            left_eye = model_eye.predict(image)[0][0]

        frame = cv2.imread(os.path.join(path, "nose.jpg"))
        nose_img = "nose" + img_name
        cv2.imwrite(os.path.join("./static", nose_img), frame)
        frame = imutils.resize(frame, width=400)
        # Prepare the image to be classified by our deep learning network
        image = cv2.resize(frame, (50, 50))
        image = np.float32(image / 255)
        image = img_to_array(image)
        image = np.expand_dims(image, axis=0)

        with graph.as_default():
            nose = model_nose.predict(image)[0][0]

        frame = cv2.imread(os.path.join(path, "mouth.jpg"))
        mouth_img = "mouth" + img_name
        cv2.imwrite(os.path.join("./static", mouth_img), frame)
        frame = imutils.resize(frame, width=400)
        # prepare the image to be classified by our deep learning network
        image = cv2.resize(frame, (50, 50))
        image = np.float32(image / 255)
        image = img_to_array(image)
        image = np.expand_dims(image, axis=0)

        with graph.as_default():
            mouth = model_mouth.predict(image)[0][0]

        frame = cv2.imread(os.path.join(path, "undereye1.jpg"))
        left_undereye_img = "leftundereye" + img_name
        cv2.imwrite(os.path.join("./static", left_undereye_img), frame)
        frame = imutils.resize(frame, width=400)
        # prepare the image to be classified by our deep learning network
        image = cv2.resize(frame, (50, 50))
        image = np.float32(image / 255)
        image = img_to_array(image)
        image = np.expand_dims(image, axis=0)

        with graph.as_default():
            undereye1 = model_undereye.predict(image)[0][0]

        frame = cv2.imread(os.path.join(path, "undereye2.jpg"))
        right_undereye_img = "rightundereye" + img_name
        cv2.imwrite(os.path.join("./static", right_undereye_img), frame)
        frame = imutils.resize(frame, width=400)
        # prepare the image to be classified by our deep learning network
        image = cv2.resize(frame, (50, 50))
        image = np.float32(image / 255)
        image = img_to_array(image)
        image = np.expand_dims(image, axis=0)

        with graph.as_default():
            undereye2 = model_undereye.predict(image)[0][0]

        frame = cv2.imread(os.path.join(path, img_name))
        skin_img = "skin" + img_name
        cv2.imwrite(os.path.join("./static", skin_img), frame)
        frame = imutils.resize(frame, width=400)
        # prepare the image to be classified by our deep learning network
        image = cv2.resize(frame, (100, 100))
        image = np.float32(image / 255)
        image = img_to_array(image)
        image = np.expand_dims(image, axis=0)

        with graph.as_default():
            face = model_face.predict(image)[0][0]

        final_prediction = (((left_eye + right_eye) / 2) * 0.4) + ((
            (undereye1 + undereye2) / 2) * 0.55) + ((
                (nose + mouth + face) / 3) * 0.05)

        label = 'Final Prediction'
        label = "{}: {:.5f}%".format(label, final_prediction * 100)
        frame_final = cv2.putText(frame1, label, (10, 25),
                                  cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0),
                                  2)
        cv2.imwrite(os.path.join("./static", img_name), frame_final)

        os.remove(os.path.join(app.config['UPLOAD_FOLDER'], img_name))

        print("Right Eye: " + str(right_eye) + "\n" + "Left Eye: " +
              str(left_eye) + "\n" + "Nose: " + str(nose) + "\n" + "Mouth: " +
              str(mouth) + "\n" + "Undereye Left: " + str(undereye1) + "\n" +
              "Undereye Right: " + str(undereye2) + "\n" + "Face: " +
              str(face) + "\n" + "Final Prediction: " + str(final_prediction))

    return render_template('./templates/result_final.html',
                           right_eyep=right_eye,
                           left_eyep=left_eye,
                           nosep=nose,
                           mouthp=mouth,
                           undereye1p=undereye1,
                           undereye2p=undereye2,
                           facep=face,
                           final=final_prediction,
                           user_image=img_name,
                           right_eye_imgp=right_eye_img,
                           left_eye_imgp=left_eye_img,
                           nose_imgp=nose_img,
                           mouth_imgp=mouth_img,
                           right_undereye_imgp=right_undereye_img,
                           left_undereye_imgp=left_undereye_img,
                           face_imgp=skin_img)
コード例 #27
0
def execute(path):
    cred = credentials.Certificate(
        "C:/tensorflow1/models/research/object_detection/firebase/schoolai-firebase-adminsdk-78lrk-064b6e6416.json"
    )
    firebase_admin.initialize_app(
        cred, {'databaseURL': 'https://schoolai.firebaseio.com'})
    print(path)
    print(datetime.now())
    MODEL_NAME = 'inference_graph'

    CWD_PATH = os.getcwd()

    PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_NAME,
                                'frozen_inference_graph.pb')

    PATH_TO_LABELS = os.path.join(CWD_PATH, 'training', 'labelmap.pbtxt')

    NUM_CLASSES = 2

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

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

        sess = tf.Session(graph=detection_graph)

    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name(
        'detection_classes:0')

    num_detections = detection_graph.get_tensor_by_name('num_detections:0')
    ref = db.reference('KATHIR/CAM1')
    video = cv2.VideoCapture(path)
    video.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
    video.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)
    x = 0
    while (video.isOpened()):
        x = x + 1
        ret, frame = video.read()
        if (ret == True and x == 1):
            x = 0
            frame_expanded = np.expand_dims(frame, axis=0)

            (boxes, scores, classes,
             num) = sess.run([
                 detection_boxes, detection_scores, detection_classes,
                 num_detections
             ],
                             feed_dict={image_tensor: frame_expanded})
            vis_util.visualize_boxes_and_labels_on_image_array(
                frame,
                np.squeeze(boxes),
                np.squeeze(classes).astype(np.int32),
                np.squeeze(scores),
                category_index,
                use_normalized_coordinates=True,
                line_thickness=8,
                min_score_thresh=0.30)
            now = datetime.now()
            ims = cv2.resize(frame, (960, 540))
            cv2.imshow(path, ims)
            if (scores[0, 0] > 0.65):
                curdate = str(datetime.now()).split(" ")
                hrmin = curdate[1].split(":")
                users_ref = ref.child(curdate[0] + '/' + hrmin[0] + ':' +
                                      hrmin[1])
                users_ref.set({'Flag': 1})
        if cv2.waitKey(1) == ord('q'):
            video.release()
            cv2.destroyAllWindows()
コード例 #28
0
def detect_in_video():

    # VideoWriter is the responsible of creating a copy of the video
    # used for the detections but with the detections overlays. Keep in
    # mind the frame size has to be the same as original video.
    out = cv2.VideoWriter('../testing/test_output.avi', cv2.VideoWriter_fourcc(
        'M', 'J', 'P', 'G'), 24, (412, 224))

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

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Definite input and output Tensors for detection_graph
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # Each box represents a part of the image where a particular object
            # was detected.
            detection_boxes = detection_graph.get_tensor_by_name(
                'detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class
            # label.
            detection_scores = detection_graph.get_tensor_by_name(
                'detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name(
                'detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name(
                'num_detections:0')
            cap = cv2.VideoCapture('../testing/test_input.mp4')

            while(cap.isOpened()):
                # Read the frame
                ret, frame = cap.read()

                if ret == True:
                    # Recolor the frame. By default, OpenCV uses BGR color space.
                    # This short blog post explains this better:
                    # https://www.learnopencv.com/why-does-opencv-use-bgr-color-format/
                    color_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

                    image_np_expanded = np.expand_dims(color_frame, axis=0)

                    # Actual detection.
                    (boxes, scores, classes, num) = sess.run(
                        [detection_boxes, detection_scores,
                            detection_classes, num_detections],
                        feed_dict={image_tensor: image_np_expanded})

                    # Visualization of the results of a detection.
                    # note: perform the detections using a higher threshold
                    vis_util.visualize_boxes_and_labels_on_image_array(
                        color_frame,
                        np.squeeze(boxes),
                        np.squeeze(classes).astype(np.int32),
                        np.squeeze(scores),
                        category_index,
                        use_normalized_coordinates=True,
                        line_thickness=8,
                        min_score_thresh=.20)

                    cv2.imshow('frame', color_frame)
                    output_rgb = cv2.cvtColor(color_frame, cv2.COLOR_RGB2BGR)
                    out.write(output_rgb)

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

            out.release()
            cap.release()
            cv2.destroyAllWindows()
コード例 #29
0
ファイル: objectDetect.py プロジェクト: qwilde1/Snap-N-Snack
def foo(imgin, imgout):
    #%matplotlib inline
    sys.path.append("..")

    MODEL_NAME = 'carrotBeanGraph'

    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('training', 'object-detection.pbtxt')

    # Modify this as you add classes
    NUM_CLASSES = 2

    # Loads the frozen model into memory
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    # Loads the label map
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    # Numpy helper code
    def load_image_into_numpy_array(image):
        (im_width, im_height) = image.size
        return np.array(image.getdata()).reshape(
            (im_height, im_width, 3)).astype(np.uint8)

    # If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
    PATH_TO_TEST_IMAGES_DIR = 'test_images'
    # Only doing one image at a time, so change this.
    # TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(3, 8) ]

    # Size, in inches, of the output images.
    IMAGE_SIZE = (12, 8)

    # Algorithm to create Tensors, detect the objects, and output an image file with mappings.
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Definite input and output Tensors for detection_graph
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # Each box represents a part of the image where a particular object was detected.
            detection_boxes = detection_graph.get_tensor_by_name(
                'detection_boxes:0')
            # Each score represent how level of confidence for each of the objects.
            # Score is shown on the result image, together with the class label.
            detection_scores = detection_graph.get_tensor_by_name(
                'detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name(
                'detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name(
                'num_detections:0')
            # Don't want a for-loop in final version

            #for image_path in TEST_IMAGE_PATHS:
            image = Image.open(
                imgin)  #change later so raw image file, rather than file path
            # the array based representation of the image will be used later in order to prepare the
            # result image with boxes and labels on it.
            image_np = load_image_into_numpy_array(image)
            # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
            image_np_expanded = np.expand_dims(image_np, axis=0)
            # Actual detection.
            (boxes, scores, classes,
             num) = sess.run([
                 detection_boxes, detection_scores, detection_classes,
                 num_detections
             ],
                             feed_dict={image_tensor: image_np_expanded})
            # Visualization of the results of a detection.
            vis_util.visualize_boxes_and_labels_on_image_array(
                image_np,
                np.squeeze(boxes),
                np.squeeze(classes).astype(np.int32),
                np.squeeze(scores),
                category_index,
                use_normalized_coordinates=True,
                line_thickness=8)
            plt.figure(figsize=IMAGE_SIZE)
            #plt.imshow(image_np)
            plt.imsave(imgout, image_np)
コード例 #30
0
# Path to label map file
PATH_TO_LABELS = (
    'F:/tensorflow/models/research/object_detection/training/label_map.pbtxt')

# Number of classes the object detector can identify
NUM_CLASSES = 6

## Load the label map.
# Label maps map indices to category names, so that when our convolution
# network predicts `5`, we know that this corresponds to `king`.
# Here we use internal utility functions, but anything that returns a
# dictionary mapping integers to appropriate string labels would be fine
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(
    label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)

# Load the Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')

    sess = tf.Session(graph=detection_graph)

# Define input and output tensors (i.e. data) for the object detection classifier

# Input tensor is the image
コード例 #31
0
ファイル: probeerselGUI.py プロジェクト: laurasels/racebaan
    def cocomain(input_speed):
        initial_car_acceleration = 800
        if not os.path.exists(MODEL_NAME + '/frozen_inference_graph.pb'):
        	print ('Downloading the model')
        	opener = urllib.request.URLopener()
        	opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
        	tar_file = tarfile.open(MODEL_FILE)
        	for file in tar_file.getmembers():
        	  file_name = os.path.basename(file.name)
        	  if 'frozen_inference_graph.pb' in file_name:
        	    tar_file.extract(file, os.getcwd())
        	print('Download complete')
        else:
        	print('Model already exists')

        # ## Load a (frozen) Tensorflow model into memory.
        detection_graph = tf.Graph()
        with detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

        # Loading label map:
        # Label maps map indices to category names, so that when our convolution
        # network predicts `5`, we know that this corresponds to `airplane`.  Here
        # we use internal utility functions, but anything that returns a dictionary
        # mapping integers to appropriate string labels would be fine

        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        category_index = label_map_util.create_category_index(categories)

        # Intializing the web camera device: 0 for internal and 1 for external camera
        # of the laptop used
        cap = cv2.VideoCapture(0)

        # Running the tensorflow session
        with detection_graph.as_default():
          with tf.Session(graph=detection_graph) as sess:
           ret = True
           while (ret):
              ret,image_np = cap.read()
              #print(image_np)
              # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
              image_np_expanded = np.expand_dims(image_np, axis=0)
              #print(image_np_expanded)
              image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
              # Each box represents a part of the image where a particular object was detected.
              boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
              # Each score represent how level of confidence for each of the objects.
              # Score is shown on the result image, together with the class label.
              scores = detection_graph.get_tensor_by_name('detection_scores:0')
              classes = detection_graph.get_tensor_by_name('detection_classes:0')
              num_detections = detection_graph.get_tensor_by_name('num_detections:0')
              # Actual detection.
              (boxes, scores, classes, num_detections) = sess.run(
                  [boxes, scores, classes, num_detections],
                  feed_dict={image_tensor: image_np_expanded})
              # Visualization of the results of a detection.
              vis_util.visualize_boxes_and_labels_on_image_array(
                  image_np,
                  np.squeeze(boxes),
                  np.squeeze(classes).astype(np.int32),
                  np.squeeze(scores),
                  category_index,
                  use_normalized_coordinates=True,
                  line_thickness=8)
              classes_detected = classes[scores>0.5]
              if 13 in classes_detected:
                  #print('detected')
                  car3.changeSpeed(0, initial_car_acceleration)
                  car2.changeSpeed(0, initial_car_acceleration)
                  #car3.changeLaneRight(250, 250)
                  car3.setLocationChangeCallback(locationChangeCallback)
                  #print(car3.piece)
              elif 10 in classes_detected:
                  print('verkeerslicht detected')
                  #print('detected')
                  car3.changeSpeed(0, initial_car_acceleration)
                  car2.changeSpeed(0, initial_car_acceleration)
                  #car3.changeLaneRight(250, 250)
                  car3.setLocationChangeCallback(locationChangeCallback)
                  """
              elif 1 in classes_detected:
                  print('car detected')
                  car3.changeSpeed(int(initial_car_speed/2), initial_car_acceleration)
                  car2.changeSpeed(int(initial_car_speed/2), initial_car_acceleration)
                  car3.setLocationChangeCallback(locationChangeCallback)
                  """
              else:
                  car3.changeSpeed(input_speed, initial_car_acceleration)
                  car2.changeSpeed(input_speed, initial_car_acceleration)
                  car3.setLocationChangeCallback(locationChangeCallback)
                  #print(car3.piece)
                  #drive(cv2)
              #print(image_np,boxes,classes,scores,category_index)
              #plt.figure(figsize=IMAGE_SIZE)
              #plt.imshow(image_np)
              cv2.imshow('image',cv2.resize(image_np,(1280,960)))
              if cv2.waitKey(25) & 0xFF == ord('q'):
                  cv2.destroyAllWindows()
                  cap.release()
                  break
def main():
    print("starting program . . .")

    if not checkIfNecessaryPathsAndFilesExist():
        return
    # end if

    # now that we've checked for the protoc compile, import the TensorFlow models repo utils content
    from utils import label_map_util
    from utils import visualization_utils as vis_util

    # if TensorFlow version is too old, show error message and bail
    # this next comment line is necessary to avoid a false warning if using the editor PyCharm
    # noinspection PyUnresolvedReferences
    if StrictVersion(tf.__version__) < StrictVersion('1.5.0'):
        print('error: Please upgrade your tensorflow installation to v1.5.* or later!')
        return
    # end if

    # if the frozen inference graph file does not already exist, download the model tar file and unzip it
    try:
        if not os.path.exists(FROZEN_INFERENCE_GRAPH_LOC):
            # if the model tar file has not already been downloaded, download it
            if not os.path.exists(os.path.join(MODEL_SAVE_DIR_LOC, MODEL_FILE_NAME)):
                # download the model
                print("downloading model . . .")
                # instantiate a URLopener object, then download the file
                opener = urllib.request.URLopener()
                opener.retrieve(DOWNLOAD_MODEL_FROM_LOC + MODEL_FILE_NAME, os.path.join(MODEL_SAVE_DIR_LOC, MODEL_FILE_NAME))
            # end if

            # unzip the tar to get the frozen inference graph
            print("unzipping model . . .")
            tar_file = tarfile.open(os.path.join(MODEL_SAVE_DIR_LOC, MODEL_FILE_NAME))
            for file in tar_file.getmembers():
                file_name = os.path.basename(file.name)
                if 'frozen_inference_graph.pb' in file_name:
                    tar_file.extract(file, MODEL_SAVE_DIR_LOC)
                # end if
            # end for
        # end if
    except Exception as e:
        print("error downloading or unzipping model: "   + str(e))
        return
    # end try

    # if the frozen inference graph does not exist after the above, show an error message and bail
    if not os.path.exists(FROZEN_INFERENCE_GRAPH_LOC):
        print("unable to get / create the frozen inference graph")
        return
    # end if

    # load the frozen model into memory
    print("loading frozen model into memory . . .")
    detection_graph = tf.Graph()
    try:
        with detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(FROZEN_INFERENCE_GRAPH_LOC, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')
            # end with
        # end with
    except Exception as e:
        print("error loading the frozen model into memory: " + str(e))
        return
    # end try

    # load the label map
    print("loading label map . . .")
    label_map = label_map_util.load_labelmap(LABEL_MAP_LOC)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    print("starting object detection . . .")
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            for fileName in os.listdir(TEST_IMAGES_DIR):
                if fileName.endswith(".jpg"):
                    image_np = cv2.imread(os.path.join(TEST_IMAGES_DIR, fileName))
                    if image_np is not None:
                        # Definite input and output Tensors for detection_graph
                        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
                        # Each box represents a part of the image where a particular object was detected.
                        detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
                        # Each score represent how level of confidence for each of the objects.
                        # Score is shown on the result image, together with the class label.
                        detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
                        detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
                        num_detections = detection_graph.get_tensor_by_name('num_detections:0')

                        # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                        image_np_expanded = np.expand_dims(image_np, axis=0)
                        # Actual detection.
                        (boxes, scores, classes, num) = sess.run(
                            [detection_boxes, detection_scores, detection_classes, num_detections],
                            feed_dict={image_tensor: image_np_expanded})
                        # Visualization of the results of a detection.
                        vis_util.visualize_boxes_and_labels_on_image_array(image_np,
                                                                           np.squeeze(boxes),
                                                                           np.squeeze(classes).astype(np.int32),
                                                                           np.squeeze(scores),
                                                                           category_index,
                                                                           use_normalized_coordinates=True,
                                                                           line_thickness=8)
                        cv2.imshow("result", image_np)
                        cv2.waitKey()
コード例 #33
0
def load_labels(labels_path, num_classes):
    label_map = label_map_util.load_labelmap(labels_path)
    categories = label_map_util.convert_label_map_to_categories(\
            label_map, max_num_classes=num_classes, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)
    return category_index
コード例 #34
0
from utils import visualization_utils as vis_util

detection_graph=tf.Graph()


with detection_graph.as_default():
    od_graph_def=tf.GraphDef()
    with tf.gfile.GFile(path_to_ckpt,'rb') as fid:
        serialized_graph=fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def,name='')


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


def load_image():
    ret, frame = cap.read()
    array = frame
    array=array.astype(np.uint8)
    return array

test_image_path=[os.path.join('/home/nvidia/data/projects/tensorflow-object-detection-api/models/research/object_detection/test_images','image{}.jpg'.format(i)) for i in range(1,2)]

show_size=(12,8)
i = 0

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
コード例 #35
0
def detection():

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    # Load the Tensorflow model into memory.
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

        sess = tf.Session(graph=detection_graph)

    # Define input and output tensors (i.e. data) for the object detection classifier

    # Input tensor is the image
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    # Output tensors are the detection boxes, scores, and classes
    # Each box represents a part of the image where a particular object was detected
    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

    # Each score represents level of confidence for each of the objects.
    # The score is shown on the result image, together with the class label.
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name(
        'detection_classes:0')

    # Number of objects detected
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')

    # Load image using OpenCV and
    # expand image dimensions to have shape: [1, None, None, 3]
    # i.e. a single-column array, where each item in the column has the pixel RGB value
    image = cv2.imread(PATH_TO_IMAGE)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_expanded = np.expand_dims(image_rgb, axis=0)

    # Perform the actual detection by running the model with the image as input
    (boxes, scores, classes, num) = sess.run(
        [detection_boxes, detection_scores, detection_classes, num_detections],
        feed_dict={image_tensor: image_expanded})

    # Draw the results of the detection (aka 'visulaize the results')

    vis_util.visualize_boxes_and_labels_on_image_array(
        image,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=8,
        min_score_thresh=0.70)

    coordinates = vis_util.return_coordinates(image,
                                              np.squeeze(boxes),
                                              np.squeeze(classes).astype(
                                                  np.int32),
                                              np.squeeze(scores),
                                              category_index,
                                              use_normalized_coordinates=True,
                                              line_thickness=8,
                                              min_score_thresh=0.70)

    for coordinate in coordinates:
        print(coordinate)
        #ymin,ymax,xmin,xmax
        (y1, y2, x1, x2, accuracy, classification) = coordinate

    with open(os.path.join(
            CWD_PATH,
            "json_hand/" + "script" + IMAGE_NAME.split(".")[0] + ".json"),
              "w",
              encoding='utf-8') as f:
        json.dump(coordinates, f, ensure_ascii=False, indent=4)
        f.write('\n')

    output = image.copy()
    cv2.imshow('Object detector', output)

    # Press any key to close the image
    cv2.waitKey(0)

    # Clean up
    cv2.destroyAllWindows()