示例#1
0
def detect_objects(image_np, sess, detection_graph):
    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
    image_np_expanded = np.expand_dims(image_np, axis=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.
    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)
    return image_np
示例#2
0
# In[18]:

while True:

    ret_val, image_np = vidCap.read()
    # 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.
    output_dict = run_inference_for_single_image(image_np_expanded,
                                                 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)
    if cv.waitKey(1) == 27:
        cv.destroyAllWindows()
        vidCap.release()
        break  # esc to quit

    cv.imshow('Reading Cam', cv.resize(image_np, (100, 100)))

# In[19]:

import sys
print(sys.executable)
示例#3
0
def targeted_object_counting(input_video, detection_graph, category_index,
                             is_color_recognition_enabled, targeted_object):

    # input video
    cap = cv2.VideoCapture(input_video)

    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    fps = int(cap.get(cv2.CAP_PROP_FPS))

    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    output_movie = cv2.VideoWriter('the_output.avi', fourcc, fps,
                                   (width, height))

    total_passed_vehicle = 0
    speed = "waiting..."
    direction = "waiting..."
    size = "waiting..."
    color = "waiting..."
    the_result = "..."
    width_heigh_taken = True
    height = 0
    width = 0
    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 all the frames that are extracted from input video
            while (cap.isOpened()):
                ret, frame = cap.read()

                if not ret:
                    print("end of the video file...")
                    break

                input_frame = frame

                # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(input_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})

                # insert information text to video frame
                font = cv2.FONT_HERSHEY_SIMPLEX

                # Visualization of the results of a detection.
                counter, csv_line, the_result = vis_util.visualize_boxes_and_labels_on_image_array(
                    cap.get(1),
                    input_frame,
                    1,
                    is_color_recognition_enabled,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    targeted_objects=targeted_object,
                    use_normalized_coordinates=True,
                    line_thickness=4)
                if (len(the_result) == 0):
                    cv2.putText(input_frame, "...", (10, 35), font, 0.8,
                                (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX)
                else:
                    cv2.putText(input_frame, the_result, (10, 35), font, 0.8,
                                (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX)

                #cv2.imshow('object counting',input_frame)

                output_movie.write(input_frame)
                print("writing frame")

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

            cap.release()
            cv2.destroyAllWindows()
def main():
    Posiciones = []  # Guardo las posiciones iniciales de configuracion
    rospy.init_node("Red_Neuronal")
    reset_cameras()
    close_camera("left")
    close_camera("right")
    close_camera("head")
    #Instrucciones brazo
    open_camera("left", 1280, 800)
    subscribe_to_camera("left")
    endpoint_sub = rospy.Subscriber('/robot/limb/left/endpoint_state',
                                    EndpointState,
                                    callback=endpoint_callback)
    screen_pub = rospy.Publisher('/robot/xdisplay', Image, queue_size=10)
    state_sub = rospy.Subscriber('robot/navigators/left_navigator/state',
                                 NavigatorState,
                                 callback=on_state)
    #Fin instrucciones brazo
    print 'Ahora graba posiciones'
    # Posiciones
    ########3########
    #################
    #2######0#######1
    #################
    ########4########
    #
    while 1:
        if button0:
            Posiciones.append([x_ini, y_ini])
            while button0:
                continue
        if button1 or button2:
            break
    print 'posiciones grabadas'
    Muevee = Mueve(
        'left',
        Posiciones)  # En este punto debo pasar todas las posiciones iniciales
    with detection_graph.as_default():
        with tf.Session() as sess:
            # Get handles to input and output tensors
            ops = tf.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.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, frame.shape[0],
                    frame.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.get_default_graph().get_tensor_by_name(
                'image_tensor:0')
            while (True):
                if img.getImg() is None:
                    continue
                frame = img.getImg()
                #print frame.shape
                c1, c2, c3, c4 = cv2.split(frame)
                frame = cv2.merge([c1, c2, c3])
                # Run inference
                output_dict = sess.run(
                    tensor_dict,
                    feed_dict={image_tensor: np.expand_dims(frame, 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]
                # Visualization of the results of a detection.
                vis_util.visualize_boxes_and_labels_on_image_array(
                    frame,
                    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)

                # Display the resulting frame
                cv2.imshow('frame', frame)
                msgsub = cv_bridge.CvBridge().cv2_to_imgmsg(frame,
                                                            encoding="8UC3")
                screen_pub.publish(msgsub)
                libros = []
                clase_index = 77  # La clase escogida para tomarla con Baxter
                print output_dict['num_detections']
                print output_dict['detection_classes']
                for i in range(output_dict['num_detections']):
                    if output_dict['detection_classes'][i] == clase_index:
                        libros.append(i)
                        print 'celular detectado'
                for i in libros:
                    xmin = int(output_dict['detection_boxes'][i][1] * 1280)
                    xmax = int(output_dict['detection_boxes'][i][3] * 1280)
                    ymin = int(output_dict['detection_boxes'][i][0] * 800)
                    ymax = int(output_dict['detection_boxes'][i][2] * 800)
                    print xmin, xmax, ymin, ymax
                    Muevee.CA(int((xmax + xmin) / 2), int((ymax + ymin) / 2),
                              'Celulares')
                Muevee.randomm()
                Muevee.mover_baxter('base', Muevee.pose[:3], Muevee.pose[3:6])
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

            # When everything done, release the capture
            cv2.destroyAllWindows()
示例#5
0
def run_inference_for_multi_image(graph, TEST_IMAGE_PATHS):
    with graph.as_default():
        with tf.Session() as sess:
            # count FPS, time
            cnt = 0
            FPS10 = 0

            # Get handles to input and output tensors
            ops = tf.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.get_default_graph(
                    ).get_tensor_by_name(tensor_name)
            # for image_path in TEST_IMAGE_PATHS:
            for image_path in glob.iglob(FLAGS.input_path):
                image = Image.open(image_path)
                print image_path
                photo_name = image_path.split('/')[-1]
                image = load_image_into_numpy_array(image)

                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.get_default_graph().get_tensor_by_name(
                    'image_tensor:0')

                # Run inference
                for ii in range(500):
                    print ii
                    if cnt % 100 == 0:
                        start = time.time()
                    output_dict = sess.run(
                        tensor_dict,
                        feed_dict={image_tensor: np.expand_dims(image, 0)})
                    cnt = cnt + 1

                    if cnt % 100 == 99:
                        end = time.time()
                        FPS = (99 * 1 / (end - start))
                        print("FPS(mean), detection: " + str(FPS))

                # 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]
                vis_util.visualize_boxes_and_labels_on_image_array(
                    image,
                    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)
                # plt.figure(figsize=IMAGE_SIZE)
                # plt.imshow(image)
                # image.save(image_path+"1")
                file_name = os.path.split(image_path)[1]
                out_file_name = os.path.join(FLAGS.out_dir, 'out_' + file_name)
                scipy.misc.imsave(out_file_name, image)
def object_counting(input_video, detection_graph, category_index,
                    is_color_recognition_enabled, fps, width, height):
    #initialize .csv
    with open('grocery_report.csv', 'w') as f:
        writer = csv.writer(f)
        csv_line = "Grocery, Qty"
        writer.writerows([csv_line.split(',')])

    # input video
    cap = cv2.VideoCapture(input_video)

    counting_mode = "..."
    height = 0
    width = 0
    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')

            while True:
                ret, image_np = cap.read()

                if not ret:
                    print("end of the video file...")
                    break

                input_frame = image_np

                # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(input_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})

                # insert information text to video frame
                font = cv2.FONT_HERSHEY_SIMPLEX

                # Visualization of the results of a detection.
                counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_image_array(
                    cap.get(0),
                    input_frame,
                    1,
                    is_color_recognition_enabled,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=4)
                # counting_mode = counting_mode + counter
                # print (counting_mode)

                if (len(counting_mode) == 0):
                    cv2.putText(input_frame, "...", (10, 35), font, 0.8,
                                (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX)
                else:
                    cv2.putText(input_frame, str(counting_mode), (10, 35),
                                font, 0.8, (0, 255, 255), 2,
                                cv2.FONT_HERSHEY_SIMPLEX)

                # print ("writing frame")
                cv2.imshow('grocery counting', input_frame)

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

                if (counting_mode != "..."):
                    with open('grocery_r.csv', 'w') as f:
                        writer = csv.writer(f)
                        # size, direction = csv_line.split(',')
                        # writer.writerows([csv_line.split(',')])
                    # with open('grocery_r.csv' ,'r') as reading:
                    #     read = csv.reader(reading)
                    #     for line in read:
                    # line2 =
                    # print(line)

            cap.release()
            cv2.destroyAllWindows()
            feed_dict={image_tensor: frame_expanded})
        lcd.lcd_init()
        lcd.lcd_string(max(scores) + ' ' + max(classes), 1)
        lcd.GPIO.cleanup()
#import lcd, lcd.lcd_init()
# lcd.lcd_string(scores + ' ' + classes, 1)
#try max(scores) and max(classes)
#send scores to lcd plate
#lcd.GPIO.cleanup() when done
        
        # Draw the results of the detection (aka 'visulaize the results')
        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.40)

        cv2.putText(frame,"FPS: {0:.2f}".format(frame_rate_calc),(30,50),font,1,(255,255,0),2,cv2.LINE_AA)

        # All the results have been drawn on the frame, so it's time to display it.
        cv2.imshow('Object detector', frame)

        t2 = cv2.getTickCount()
        time1 = (t2-t1)/freq
        frame_rate_calc = 1/time1

        # Press 'q' to quit
    output_frame = OutputFrame()

    webcam_thread = WebcamThread("Webcam Thread")
    predictor_thread = PredictorThread("Predictor Thread")
    webcam_thread.start()
    predictor_thread.start()

    while True:
        if output_frame.boxes == ():
            to_show = output_frame.frame
        else:
            to_show = output_frame.frame
            vis_util.visualize_boxes_and_labels_on_image_array(
              to_show,
              np.squeeze(output_frame.boxes[0]),
              np.squeeze(output_frame.boxes[2]).astype(np.int32),
              np.squeeze(output_frame.boxes[1]),
              category_index,
              use_normalized_coordinates=True,
              line_thickness=8)

        cv2.imshow('frame', to_show)
        out.write((to_show).astype('u1'))
        if cv2.waitKey(1) & 0xFF == ord('q'):
            done = True
            break

    cap.release()
    out.release()
    cv2.destroyAllWindows()
示例#9
0
                scores = -np.sort(-softmax)[:n_top]

                # 生成heatmap
                cam_A = cam_inception.CAM(feature_maps_1, predictions_1, n_top)
                cam_B = cam_inception.CAM(feature_maps_2, predictions_1, n_top)
                cam = np.maximum(cam_A, cam_B)
                (im_width, im_height) = image.size
                cam_resize = bilinear(cam, im_height, im_width)

                # 保存heatmap
                for j in range(n_top):
                    heatmap = cam_resize[:, :, j]
                    heatmap = grey2rainbow(heatmap * 255)
                    heatmap = Image.fromarray(heatmap.astype('uint8')).convert('RGB')
                    heatmap.save(os.path.join(FLAGS.output_dir, 'test_images/test_{0}_heatmap_{1}.jpg'.format(i, j)))

                # 生成bounding_boxes
                threshold = 0.5
                boxes = cam_inception.bounding_box(cam_resize, threshold)

                vis_util.visualize_boxes_and_labels_on_image_array(
                    image_np,
                    boxes,
                    classes.astype(np.int32),
                    scores,
                    category_index,
                    use_normalized_coordinates=True,
                    min_score_thresh=.3,
                    line_thickness=6)
                plt.imsave(os.path.join(FLAGS.output_dir, 'test_images/test_{0}_output.jpg'.format(i)), image_np)
示例#10
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()
示例#11
0
            image_np_expanded = np.expand_dims(image_np, axis=0)
            image_tensor = GRAPH.get_tensor_by_name("image_tensor:0")

            boxes = GRAPH.get_tensor_by_name("detection_boxes:0")
            scores = GRAPH.get_tensor_by_name("detection_scores:0")
            classes = GRAPH.get_tensor_by_name("detection_classes:0")
            num_detections = 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=box_border_thickness)

            plt.figure()

            image_np = image_np[:, :, ::-1]

            if INSTRUMENT == "aia":
                cv2.imwrite(
                    "%s/resources/aia-detected-images/detected-%03d.jpg" %
                    (MAIN_DIR, ID), image_np)
            elif INSTRUMENT == "hmi":
                cv2.imwrite(
                    "%s/resources/hmi-detected-images/detected-%03d.jpg" %
示例#12
0
def image():

    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_detection = detection_graph.get_tensor_by_name(
                'num_detections:0')

            input_video = filedialog.askopenfilename(
                initialdir="/",
                title="Select file",
                filetypes=(("jpeg files", "*.jpg"), ("All files",
                                                     "*")))  #'traffic'
            image = Image.open(input_video)
            #input_video = input_video.split('/')[-1]
            #video_reader = imageio.get_reader('%s'%input_video)
            print('Image Processing.......')
            video_writer = imageio.get_writer('%s.annotated.jpg' % input_video)
            ##loop through and process each frame
            #t0 = datetime.now()
            #n_frames = 0
            image_np = load_image_into_numpy_array(image)
            #image_np = video_reader
            #for frame in video_reader:
            #    image_np = frame
            #    n_frames+=1

            ##Expand dim since the expects images to have shape[1.None]
            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_detection
             ],
                             feed_dict={image_tensor: image_np_expanded})
            #visualization
            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)
            #Video Writer
            video_writer.append_data(image_np)
            #print(image_np)
            #fps = n_frames/(datetime.now()-t0).total_seconds()

            #print('Frames processed: %s,Speed:%s fps'%(n_frames,fps))
            print('Image Processing Done.....')
            #pygame.mixer.init()
            #pygame.mixer.music.load("welcome1.mp3")
            #pygame.mixer.music.play()
            #while pygame.mixer.music.get_busy() == True:
            #    continue

            #z=z+1
            #cleanup
            #print([category_index.get(i)for i in classes[0]])
            #print(scores[[0]])
            #os.system("mpg321 image.mp3")
            video_writer.close()
示例#13
0
def write_slogan():
    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_detection = detection_graph.get_tensor_by_name(
                'num_detections:0')

            input_video = filedialog.askopenfilename(
                initialdir="/",
                title="Select file",
                filetypes=(("jpeg files", "*.jpg"), ("All files",
                                                     "*")))  #'traffic'
            #input_video = input_video.split('/')[-1]
            video_reader = imageio.get_reader('%s' % input_video)
            print('Video Processing.......')
            video_writer = imageio.get_writer('%s.annotated.mp4' % input_video,
                                              fps=10)
            ##loop through and process each frame
            t0 = datetime.now()
            n_frames = 0
            for frame in video_reader:
                image_np = frame
                n_frames += 1

                ##Expand dim since the expects images to have shape[1.None]
                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_detection
                 ],
                                 feed_dict={image_tensor: image_np_expanded})
                #visualization
                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)
                #os.system("mpg321 video.mp3")
                #Video Writer
                video_writer.append_data(image_np)
                #print(image_np)
            fps = n_frames / (datetime.now() - t0).total_seconds()

            print('Frames processed: %s,Speed:%s fps' % (n_frames, fps))
            '''pygame.mixer.init()
            pygame.mixer.music.load("welcome.mp3")
            pygame.mixer.music.play()
            while pygame.mixer.music.get_busy() == True:
                continue'''
            #plt.show(image_np)
            #language = 'en-uk'
            #with open("Ouput.txt", encoding="utf-8") as file:
            #    file=file.read()
            #    speak = gTTS(text="I think its "+file, lang=language, slow=False)
            #    file=("sold%s.mp3")
            #    speak.save(file)
            #    #cleanup
            print('Video Processing Done.......')
            video_writer.close()
示例#14
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
示例#15
0
    # i.e. a single-column array, where each item in the column has the pixel RGB value
    ret, frame = video.read()
    frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    frame_expanded = np.expand_dims(frame_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: frame_expanded})

    # Draw the results of the detection (aka 'visulaize the results')
    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.60)

    # All the results have been drawn on the frame, so it's time to display it.
    cv2.imshow('Object detector', frame)

    # Press 'q' to quit
    if cv2.waitKey(1) == ord('q'):
        break

# Clean up
video.release()
cv2.destroyAllWindows()
示例#16
0
def classify():

    t = time.time()

    file_dir  = request.args['file_dir']
    file      = request.args['file']
    file_path = file_dir + "/" + file
    camera    = file[0:2]

#   print(time.ctime() + " Classifying image %s" % (file_path),)

    image = Image.open(file_path)

# Note: load_image on 4k images is 10+ seconds!   So have ss limit snapshot filesize, since we resize small for object detection anyway
    image_np = load_image_into_numpy_array(image)
#   image_np = cv2.resize(image_np, (1280,720))

#   print(time.ctime() + " Load time: %0.2f" % ((time.time() - t) * 1000.))

    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})

    objects = []
    for index, value in enumerate(classes[0]):
#       print("i=" + str(index) + " v=" + str(value))
        object_dict = {}
        if scores[0, index] > THRESHOLD:
            cat = (category_index.get(value)).get('name')
            score = str(scores[0, index])
            object_dict = {"name"  : cat, "score" : score}

            # Update a Count-Object-Detected (cod) counter so we can learn to ignore repeated objects.
            # This assumes if the same object (type, box size, and box position) is recognized frequently, then
            # it is probably a fixed, uninteresting object, like a shadow that looks like a person.
            bb1 = boxes[0, index]
            i = camera + " " + cat + " " + str([ '%.1f' % i for i in bb1 ])
            cod[i] += 1
            if(cod[i] < 10):
                objects.append(object_dict)
                print(time.ctime() + " Score: " + score + " Object kepted:  i=" + i + " c=" + str(cod[i]))
            else:
                print(time.ctime() + " Score: " + score + " Object ignored: i=" + i + " c=" + str(cod[i]))
    print(time.ctime() + " Execution time: %0.2f. " % ((time.time() - t) * 1000.) + str(objects))
     
    if(len(objects) > 0):
        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,
            min_score_thresh=THRESHOLD,
            line_thickness=8)
        cat = objects[0]['name']
        file = cat + '_' + file
        file_path_out1 = IMAGES1_DIR + file
        file_path_out2 = IMAGES2_DIR + file
        copyfile(file_path, file_path_out1)
        cv2.imwrite( file_path_out2, cv2.resize(image_np, (1280,720)) );
        
# Note: objects needs strings, not integer, scores to avoid this error:  to avoid TypeError: is not JSON serializable
    return jsonify(objects)
def inference(args):
    device = args.device
    width = args.input_width
    height = args.input_height
    thresh = args.threshold
    label_map_file = args.label_map
    if not label_map_file:
        label_map_file = "utils/mscoco_label_map.pbtxt"
    label_map = create_category_index_dict(label_map_file)
    if device == "x86":
        from detectors.x86_detector import X86Detector
        detector = X86Detector(width=width, height=height, thresh=thresh)
    elif device == "edgetpu":
        from detectors.edgetpu_detector import EdgeTpuDetector
        detector = EdgeTpuDetector(width=width, height=height, thresh=thresh)
    elif device == "jetson":
        from detectors.jetson_detector import JetsonDetector
        detector = JetsonDetector(width=width, height=height, thresh=thresh)
    else:
        raise ValueError(
            "device should be one of 'x86', 'edgetpu' or 'jetson' but you provided {0}"
            .format(device))

    video_uri = args.input_video
    if not os.path.isfile(video_uri):
        raise FileNotFoundError(
            'video file does not exist under: {}'.format(video_uri))
    if not os.path.isdir(args.out_dir):
        logging.info("the provided output directory : {0} is not exist".format(
            args.out_dir))
        logging.info("creating output directory : {0}".format(args.out_dir))
        os.makedirs(args.out_dir, exist_ok=True)

    file_name = ".".join((video_uri.split("/")[-1]).split(".")[:-1])
    input_cap = cv.VideoCapture(video_uri)
    fourcc = cv.VideoWriter_fourcc(*'XVID')
    out_cap = cv.VideoWriter(
        os.path.join(args.out_dir, file_name + "_neuralet_output.avi"), fourcc,
        25, (args.out_width, args.out_height))
    if (input_cap.isOpened()):
        print('opened video ', video_uri)
    else:
        print('failed to load video ', video_uri)
        return

    detector.load_model(args.model_path, label_map)
    running_video = True
    frame_number = 0
    while input_cap.isOpened() and running_video:
        ret, cv_image = input_cap.read()
        if not ret:
            running_video = False
        if np.shape(cv_image) != ():
            out_frame = cv.resize(cv_image, (args.out_width, args.out_height))
            preprocessed_image = detector.preprocess(cv_image)
            result = detector.inference(preprocessed_image)
            output_dict = prepare_visualization(result)
            visualize_boxes_and_labels_on_image_array(
                out_frame,
                output_dict["detection_boxes"],
                output_dict["detection_classes"],
                output_dict["detection_scores"],
                label_map,
                instance_masks=output_dict.get("detection_masks"),
                use_normalized_coordinates=True,
                line_thickness=3)
            out_cap.write(out_frame)
            frame_number += 1
            if frame_number % 100 == 0:
                logging.info("processed {0} frames".format(frame_number))
            print('======= Frame ' + str(frame_count) + ' =========')
            current_frame = tracker.make_frame_info(frame_count,
                                                    squeezed_boxes,
                                                    squeezed_classes,
                                                    squeezed_scores,
                                                    image_np.shape, image_np)
            current_frame = tracker.track(past_frames, current_frame)
            past_frames = tracker.append_current_to_past_frame_info(
                past_frames, current_frame)
            print('(---- End of Frame ' + str(frame_count) + ' ----) \n')

            # Visualization of the results of a detection.
            vis_util.visualize_boxes_and_labels_on_image_array(
                image_np,
                squeezed_boxes,
                squeezed_classes,
                squeezed_scores,
                category_index,
                use_normalized_coordinates=True,
                line_thickness=4)

            # save path to capture raw frames
            FRAME_PATH = f'../datasets/{DATASET_NAME}/frames/eval/frame_{VIDEO_NAME_ALTERED}_{frame_count}.jpg'
            RELATIVE_FRAME_PATH = os.path.abspath(
                os.path.dirname(__file__) + '/' + FRAME_PATH)
            cv2.imwrite(RELATIVE_FRAME_PATH,
                        image_np)  # save frame as JPEG file

            cv2.imshow('object detection', image_np)
            if cv2.waitKey(25) & 0xFF == ord('q'):
                cleanup()
                break
示例#19
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
                
            
            
                
示例#20
0
def detectBOW2():
    import time
    start = time.time()
    time.clock()
    elapsed = 0
    seconds = 25  # 20 S.
    cap = cv2.VideoCapture(1)
    vis_util.f.setPredic("")

    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)
                elapsed = int(time.time() - start)
                cv2.imshow('image', cv2.resize(image_np, (640, 480)))
                st = vis_util.f.getPredic()
                objName = ""
                if st != "":
                    st = st.split("#")
                    objName = st[0]
                    st2 = st[1].split(",")
                    Xmax = st2[3]
                    Xmin = st2[2]
                    K = (int(Xmax) + int(Xmin)) / 2
                    st3 = objName + " " + str(K)
                    #print st3
                if (elapsed >= seconds):
                    with sqlite3.connect("Test_PJ2.db") as con:
                        cur = con.cursor()
                        try:
                            cur.execute(
                                "UPDATE call_Detect SET Name=?,K=? WHERE ID = 1",
                                (objName, K))
                        except:
                            print "I can not see (not update) "
                    break
                    cv2.destroyAllWindows()

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

    cap.release()
    cv2.destroyAllWindows()
示例#21
0
def pet_detector(frame):

    # Use globals for the control variables so they retain their value after function exits
    global detected_inside, detected_outside
    global inside_counter, outside_counter
    global pause, pause_counter

    frame_expanded = np.expand_dims(frame, 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: frame_expanded})

    # Draw the results of the detection (aka 'visulaize the results')
    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.40)

    # Draw boxes defining "outside" and "inside" locations.
    cv2.rectangle(frame, TL_outside, BR_outside, (255, 20, 20), 3)
    cv2.putText(frame, "Outside box", (TL_outside[0] + 10, TL_outside[1] - 10),
                font, 1, (255, 20, 255), 3, cv2.LINE_AA)
    cv2.rectangle(frame, TL_inside, BR_inside, (20, 20, 255), 3)
    cv2.putText(frame, "Inside box", (TL_inside[0] + 10, TL_inside[1] - 10),
                font, 1, (20, 255, 255), 3, cv2.LINE_AA)

    # Check the class of the top detected object by looking at classes[0][0].
    # If the top detected object is a cat (17) or a dog (18) (or a teddy bear (88) for test purposes),
    # find its center coordinates by looking at the boxes[0][0] variable.
    # boxes[0][0] variable holds coordinates of detected objects as (ymin, xmin, ymax, xmax)
    if (((int(classes[0][0]) == 17) or (int(classes[0][0] == 18) or
                                        (int(classes[0][0]) == 88)))
            and (pause == 0)):
        x = int(((boxes[0][0][1] + boxes[0][0][3]) / 2) * IM_WIDTH)
        y = int(((boxes[0][0][0] + boxes[0][0][2]) / 2) * IM_HEIGHT)

        # Draw a circle at center of object
        cv2.circle(frame, (x, y), 5, (75, 13, 180), -1)

        # If object is in inside box, increment inside counter variable
        if ((x > TL_inside[0]) and (x < BR_inside[0]) and (y > TL_inside[1])
                and (y < BR_inside[1])):
            inside_counter = inside_counter + 1

        # If object is in outside box, increment outside counter variable
        if ((x > TL_outside[0]) and (x < BR_outside[0]) and (y > TL_outside[1])
                and (y < BR_outside[1])):
            outside_counter = outside_counter + 1

    # If pet has been detected inside for more than 10 frames, set detected_inside flag
    # and send a text to the phone.
    if inside_counter > 10:
        detected_inside = True
        message = client.messages.create(body='Your pet wants outside!',
                                         from_=twilio_number,
                                         to=my_number)
        inside_counter = 0
        outside_counter = 0
        # Pause pet detection by setting "pause" flag
        pause = 1

    # If pet has been detected outside for more than 10 frames, set detected_outside flag
    # and send a text to the phone.
    if outside_counter > 10:
        detected_outside = True
        message = client.messages.create(body='Your pet wants inside!',
                                         from_=twilio_number,
                                         to=my_number)
        inside_counter = 0
        outside_counter = 0
        # Pause pet detection by setting "pause" flag
        pause = 1

    # If pause flag is set, draw message on screen.
    if pause == 1:
        if detected_inside == True:
            cv2.putText(frame, 'Pet wants outside!',
                        (int(IM_WIDTH * .1), int(IM_HEIGHT * .5)), font, 3,
                        (0, 0, 0), 7, cv2.LINE_AA)
            cv2.putText(frame, 'Pet wants outside!',
                        (int(IM_WIDTH * .1), int(IM_HEIGHT * .5)), font, 3,
                        (95, 176, 23), 5, cv2.LINE_AA)

        if detected_outside == True:
            cv2.putText(frame, 'Pet wants inside!',
                        (int(IM_WIDTH * .1), int(IM_HEIGHT * .5)), font, 3,
                        (0, 0, 0), 7, cv2.LINE_AA)
            cv2.putText(frame, 'Pet wants inside!',
                        (int(IM_WIDTH * .1), int(IM_HEIGHT * .5)), font, 3,
                        (95, 176, 23), 5, cv2.LINE_AA)

        # Increment pause counter until it reaches 30 (for a framerate of 1.5 FPS, this is about 20 seconds),
        # then unpause the application (set pause flag to 0).
        pause_counter = pause_counter + 1
        if pause_counter > 30:
            pause = 0
            pause_counter = 0
            detected_inside = False
            detected_outside = False

    # Draw counter info
    cv2.putText(
        frame,
        'Detection counter: ' + str(max(inside_counter, outside_counter)),
        (10, 100), font, 0.5, (255, 255, 0), 1, cv2.LINE_AA)
    cv2.putText(frame, 'Pause counter: ' + str(pause_counter), (10, 150), font,
                0.5, (255, 255, 0), 1, cv2.LINE_AA)

    return frame
示例#22
0
def targeted_object_counting(input_video, detection_graph, category_index,
                             is_color_recognition_enabled, targeted_object,
                             fps, width, height):
    #initialize .csv
    # input video

    cap = cv2.VideoCapture(input_video)
    width_heigh_taken = True
    height = 0
    width = 0
    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 all the frames that are extracted from input video
            while (cap.isOpened()):
                ret, image_np = cap.read()

                if not ret:
                    print("end of the video file...")
                    break
                random_time = random.randrange(10, 20)
                # time.sleep(random_time)
                # ts = time.time()
                input_frame = image_np

                # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(input_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})

                # insert information text to video frame
                font = cv2.FONT_HERSHEY_SIMPLEX

                # Visualization of the results of a detection.
                counter, csv_line, the_result = vis_util.visualize_boxes_and_labels_on_image_array(
                    cap.get(1),
                    input_frame,
                    1,
                    is_color_recognition_enabled,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    targeted_objects=targeted_object,
                    use_normalized_coordinates=True,
                    line_thickness=4)
                if (len(the_result) == 0):
                    cv2.putText(input_frame, "Does not match target", (10, 35),
                                font, 0.8, (0, 255, 255), 2,
                                cv2.FONT_HERSHEY_SIMPLEX)

                else:
                    # print(the_result)
                    with open('object_counting_report.csv', 'w') as f:
                        writer = csv.writer(f)
                        # res = [str(objects) for obj in the_result]
                        result = the_result
                        csv_line = result
                        writer.writerows([csv_line.split(';')])
                    # with open('object_counting_report.csv', 'r') as f:
                    #         writer = csv.reader(f)
                    #         for line in writer:
                    # line2= [l.split(':') for l in line]

                    # line2[line2.index(":")] = ''
                    # print(line)
                    cv2.putText(input_frame, the_result, (10, 35), font, 0.8,
                                (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX)

                cv2.imshow('object counting', input_frame)

                print("writing frame")
                # os.remove(file) for file in os.listdir('fridge_screenshots') if file.endswith('.png')
                try:
                    os.remove(
                        "static/images/fridge_screenshots/your_fridge.png")
                except:
                    pass
                # cv2.imshow('img1',image_np) #display the captured image
                outfile = 'fridge_screenshots/your_fridge.jpg'

                cv2.imwrite(outfile, image_np)
                # print ("Next picture in: %.2f minutes" % (float(random_time) / 60))

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
                # if(csv_line != "not_available"):
                #         with open('d.csv', 'a') as f:
                #                 writer = csv.writer(f)
                #                 size, direction = csv_line.split(',')
                #                 writer.writerows([csv_line.split(',')])
            #         #initialize .csv
            # with open('object_counting_report.txt', 'w') as f:
            #         writer = csv.writer(f)
            #         csv_line = "Object Type, Object Color, Object Movement Direction, Object Speed (km/h)"
            #         writer.writerows([csv_line.split(',')])
            # with open('object_counting_report.txt', 'r') as f:
            #         writer = csv.reader(f)
            #         for line in writer:
            #             print(line)
            #             print('ghjkjhgfdfghjk')

            cap.release()
            cv2.destroyAllWindows()
def object_counting_webcam(detection_graph, category_index,
                           is_color_recognition_enabled):

    total_passed_vehicle = 0
    speed = "waiting..."
    direction = "waiting..."
    size = "waiting..."
    color = "waiting..."
    counting_mode = "..."
    width_heigh_taken = True
    height = 0
    width = 0
    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')

            #url = 'http://192.168.2.3:8080/video'
            cap = cv2.VideoCapture(0)
            #dims = get_dims(cap, res=my_res)
            #video_type_cv2 = get_video_type(filename)

            #out = cv2.VideoWriter(filename, video_type_cv2, frames_per_seconds, dims)
            (ret, frame) = cap.read()

            # for all the frames that are extracted from input video
            while True:
                # Capture frame-by-frame
                (ret, frame) = cap.read()

                #if not ret:
                #   print("end of the video file...")
                #  break

                input_frame = frame
                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(input_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})

                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

                # insert information text to video frame
                font = cv2.FONT_HERSHEY_SIMPLEX

                # Visualization of the results of a detection.
                counter, csv_line, counting_mode = vis_util.visualize_boxes_and_labels_on_image_array(
                    cap.get(1),
                    input_frame,
                    1,
                    is_color_recognition_enabled,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8)
                if (len(counting_mode) == 0):
                    cv2.putText(input_frame, "...", (10, 35), font, 0.8,
                                (0, 255, 255), 2, cv2.FONT_HERSHEY_SIMPLEX)
                else:
                    cv2.putText(input_frame, counting_mode, (10, 35), font,
                                0.8, (0, 255, 255), 2,
                                cv2.FONT_HERSHEY_SIMPLEX)

                cv2.imshow('object counting', input_frame)
                #out.write(input_frame)

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

            cap.release()
            #out.release()
            cv2.destroyAllWindows()
    if count < 10:
        message = client.messages \
                            .create(
                                body='Garbage detection Count  {} '.format(round(count)),
                                from_='whatsapp:+14155238886',
                                to='whatsapp:+917022514654'
                             )
        print(message.sid)
示例#24
0
    def update(self):
        if not self.running.is_set():
            return

        # Refresh values in listbox
        self.listbox.delete(0, Tk.END)
        for key, value in sorted(self.values.iteritems()):
            self.list_add(key, value)

        # Refresh image if a new one is available
        if self.new_image:
            self.new_image = False

            image = self.image

            canvas_shape = (cfg.DASH_IMAGE_HEIGHT, cfg.DASH_IMAGE_WIDTH,
                     self.IMAGE_DEPTH)
            if not image.shape == canvas_shape:
                # Resize image
                fx = float(cfg.DASH_IMAGE_WIDTH)/image.shape[1]
                fy = float(cfg.DASH_IMAGE_HEIGHT)/image.shape[0]
                image = cv2.resize(image, (0, 0), fx=fx, fy=fy)

            # Convert image from OpenCV format to Tk format
            img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            img_tk = ImageTk.PhotoImage(Image.fromarray(img_rgb))

            self.canvas.itemconfig(self.canvas_image, image=img_tk)
            self.canvas.image = img_tk # reference to keep image alive

        if self.new_result:
            self.new_result = False

            image = self.result[0]
            (boxes, scores, classes, num) = self.result[1]

            vis_util.visualize_boxes_and_labels_on_image_array(
                image,
                np.squeeze(boxes),
                np.squeeze(classes).astype(np.int32),
                np.squeeze(scores),
                self.category_index,
                use_normalized_coordinates=True,
                line_thickness=2)

            canvas_shape = (cfg.DASH_IMAGE_HEIGHT, cfg.DASH_IMAGE_WIDTH,
                     self.IMAGE_DEPTH)
            if not image.shape == canvas_shape:
                # Resize image
                fx = float(cfg.DASH_IMAGE_WIDTH)/image.shape[1]
                fy = float(cfg.DASH_IMAGE_HEIGHT)/image.shape[0]
                image = cv2.resize(image, (0, 0), fx=fx, fy=fy)

            # Convert image from OpenCV format to Tk format
            img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            img_tk = ImageTk.PhotoImage(Image.fromarray(img_rgb))

            self.result_canvas.itemconfig(self.result_image, image=img_tk)
            self.result_canvas.image = img_tk # reference to keep image alive

        self.loop()
        return
                image)  # create the array of image
            image_np_expanded = np.expand_dims(
                image_np, axis=0)  # expand the dimension of image
            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')  # define the scores
            classes = detection_graph.get_tensor_by_name(
                'detection_classes:0')  # define the classes
            num_detections = detection_graph.get_tensor_by_name(
                'num_detections:0')  # detect the number

            (boxes, scores, classes, num_detections) = sess.run(
                [boxes, scores, classes, num_detections],
                feed_dict={image_tensor:
                           image_np_expanded})  # for running the process

            vis_util.visualize_boxes_and_labels_on_image_array(
                image_np,
                np.squeeze(boxes),
                np.squeeze(classes).astype(
                    np.int32),  #  for visulation and work on image
                np.squeeze(scores),
                categories_index,
                use_normalized_coordinates=True,
                line_thickness=8)

            plt.figure(figsize=IMAGE_SIZE)  # for visualization of image
            plt.imshow(image_np)  # show the image
            plt.show()
示例#26
0
def object_detection_function():
    total_passed_vehicle = 0
    speed = 'waiting...'
    direction = 'waiting...'
    size = 'waiting...'
    color = 'waiting...'
    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 all the frames that are extracted from input video
            while cap.isOpened():
                (ret, frame) = cap.read()

                if not ret:
                    print('end of the video file...')
                    break

                input_frame = frame

                # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(input_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})
                # print("counter =", counter)

                # Visualization of the results of a detection.
                (counter, csv_line) = \
                    vis_util.visualize_boxes_and_labels_on_image_array(
                    cap.get(1),
                    input_frame,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=4,
                    )

                # print("counter =", counter)

                total_passed_vehicle = total_passed_vehicle + counter

                # insert information text to video frame
                font = cv2.FONT_HERSHEY_SIMPLEX
                cv2.putText(
                    input_frame,
                    'Detected Vehicles: ' + str(total_passed_vehicle),
                    (10, 35),
                    font,
                    0.8,
                    (0, 0xFF, 0xFF),
                    2,
                    cv2.FONT_HERSHEY_SIMPLEX,
                )

                # when the vehicle passed over line and counted, make the color of ROI line green
                if counter == 1:
                    cv2.line(input_frame, (0, 200), (640, 200), (0, 0xFF, 0),
                             5)
                else:
                    cv2.line(input_frame, (0, 200), (640, 200), (0, 0, 0xFF),
                             5)

                # insert information text to video frame
                cv2.rectangle(input_frame, (10, 275), (230, 337),
                              (180, 132, 109), -1)
                cv2.putText(
                    input_frame,
                    'ROI Line',
                    (545, 190),
                    font,
                    0.6,
                    (0, 0, 0xFF),
                    2,
                    cv2.LINE_AA,
                )
                cv2.putText(
                    input_frame,
                    'LAST PASSED VEHICLE INFO',
                    (11, 290),
                    font,
                    0.5,
                    (0xFF, 0xFF, 0xFF),
                    1,
                    cv2.FONT_HERSHEY_SIMPLEX,
                )
                cv2.putText(
                    input_frame,
                    '-Movement Direction: ' + direction,
                    (14, 302),
                    font,
                    0.4,
                    (0xFF, 0xFF, 0xFF),
                    1,
                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                )
                cv2.putText(
                    input_frame,
                    '-Speed(km/h): ' + speed,
                    (14, 312),
                    font,
                    0.4,
                    (0xFF, 0xFF, 0xFF),
                    1,
                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                )
                cv2.putText(
                    input_frame,
                    '-Color: ' + color,
                    (14, 322),
                    font,
                    0.4,
                    (0xFF, 0xFF, 0xFF),
                    1,
                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                )
                cv2.putText(
                    input_frame,
                    '-Vehicle Size/Type: ' + size,
                    (14, 332),
                    font,
                    0.4,
                    (0xFF, 0xFF, 0xFF),
                    1,
                    cv2.FONT_HERSHEY_COMPLEX_SMALL,
                )

                cv2.imshow('vehicle detection', input_frame)

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

                if csv_line != 'not_available':
                    with open('traffic_measurement.csv', 'a') as f:
                        writer = csv.writer(f)
                        (size, color, direction, speed) = \
                            csv_line.split(',')
                        writer.writerows([csv_line.split(',')])
            cap.release()
            cv2.destroyAllWindows()
示例#27
0
    def frames():
        camera = cv2.VideoCapture(0)
        if not camera.isOpened():
            raise RuntimeError('Could not start camera.')

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

        # ## Object detection imports
        # Here are the imports from the object detection module.

        # In[3]:

        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/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.

        # In[4]:

        # 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

        # In[5]:

        # 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.

        # In[6]:

        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

        # In[7]:

        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)

        # ## Helper code

        # In[8]:

        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)

        # # Detection

        # In[9]:

        # 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 = 'test_images'
        TEST_IMAGE_PATHS = [
            os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i))
            for i in range(1, 3)
        ]

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

        # In[10]:

        with detection_graph.as_default():
            with tf.Session(graph=detection_graph) as sess:
                while True:
                    ret, image_np = camera.read()
                    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                    image_np_expanded = np.expand_dims(image_np, axis=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.
                    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)

                    # cv2.imshow('object detection',
                    #            cv2.resize(image_np, (800, 600)))
                    yield cv2.imencode('.jpg',
                                       cv2.resize(image_np,
                                                  (800, 600)))[1].tobytes()
示例#28
0
def tsdrs_static_images_main():
    print("[INFO] Creating a session...")
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph, config=config) as sess:
            for image_path in TEST_IMAGE_PATHS:
                print("[INFO] Pre-processing image...")
                image = Image.open(image_path)
                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)
                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
                print("[INFO] Detecting image...")
                (boxes, scores, classes, num_detections) = sess.run(
                    [boxes, scores, classes, num_detections],
                    feed_dict={image_tensor: image_np_expanded})

                #   print(classes[0][0], scores[0][0])

                # Visualization of the results of a detection
                print("[INFO] Visualizing detected 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,
                    # min_score_thresh=0.2,
                    line_thickness=8)

                print("[INFO] Showing detected image...")
                image_RGB = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
                cv2.imshow("Image RGB", image_RGB)

                # ## NotificationSystem ## #
                predictions = [
                    category_index.get(value)
                    for index, value in enumerate(classes[0])
                    if scores[0, index] > 0.5
                ]
                if predictions:
                    predicted_class = predictions[0]['name']
                    print("I see a {} sign with confidence {:.1f}%".format(
                        predicted_class, scores[0][0] * 100))

                    class_img = cv2.imread('{}.jpg'.format(predicted_class))
                    cv2.imshow('{}'.format(predicted_class), class_img)

                    engine.say(predicted_class)
                    engine.runAndWait()

                cv2.waitKey(0)
                cv2.destroyAllWindows()

            print("DONE")
            print("HAIL HYDRA!")
def main():
    detection_graph, category_index = load_model()

    start_time = timeit.default_timer()
    total_detection_time = 0

    # setting up known distances
    # KNOWN_DISTANCE = 24.0

    # info about buoy
    # known_height_mm = 189.44
    # known_width_mm = 279.89
    known_height_m = 0.18944
    known_width_m = 0.27989
    # known_height_in = 7.45826772
    # known_width_in = 11.0192913

    KNOWN_HEIGHT = known_height_m
    KNOWN_WIDTH = known_width_m

    # only need to get calibration value if not calibrating
    if (len(sys.argv) == 1):  # sys.argv[1] != 'c'):
        buoyFocalLength = get_calibration_values()

    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')

            # vc = cv2.VideoCapture('/home/pedro/Videos/auv/GOPR0883_small.MP4')
            vc = cv2.VideoCapture(0)

            vc.set(cv2.CAP_PROP_MODE, cv2.CAP_MODE_RGB)
            vc.set(cv2.CAP_PROP_FPS, 60)
            vc.set(cv2.CAP_PROP_CONVERT_RGB, 1)
            vc.set(cv2.CAP_PROP_ISO_SPEED, 400)

            print('Frame Width:', vc.get(cv2.CAP_PROP_FRAME_WIDTH))
            print('Frame Height:', vc.get(cv2.CAP_PROP_FRAME_HEIGHT))
            print('Mode:', vc.get(cv2.CAP_PROP_MODE))
            print('FPS:', vc.get(cv2.CAP_PROP_FPS))
            print('Convert RGB:', vc.get(cv2.CAP_PROP_CONVERT_RGB))
            print('ISO Speed:', vc.get(cv2.CAP_PROP_ISO_SPEED))

            # forucc in MP4V encoding, to be used with MP4 output format ----- doesn't work -----
            fourcc = cv2.VideoWriter_fourcc(*'MP4V')

            # fourcc in XVID encoding, to be used with AVI output format
            # fourcc = cv2.VideoWriter_fourcc(*'XVID')

            # outputting video in AVI format, to be used with XVID fourcc
            # video_path = '/home/pedro/Videos/AUV/output_{0}.AVI'.format(datetime.datetime.now().strftime("%Y-%m-%d"))
            # out = cv2.VideoWriter(video_path, fourcc, vc.get(cv2.CAP_PROP_FPS),
            #                       (int(vc.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vc.get(cv2.CAP_PROP_FRAME_HEIGHT))))

            # outputting video in MP4 format ----- doesn't work -------
            video_path = '/home/pedro/Videos/AUV/output_{0}.MP4'.format(
                datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S"))
            out = cv2.VideoWriter(video_path, fourcc, vc.get(cv2.CAP_PROP_FPS),
                                  (int(vc.get(cv2.CAP_PROP_FRAME_WIDTH)),
                                   int(vc.get(cv2.CAP_PROP_FRAME_HEIGHT))))
            log_path = '/home/pedro/repos/AUV/cv.tensorflow/cv_detection/test_logs/{0}.txt'.format(
                datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S"))
            print(os.path.exists(log_path))
            log = open(log_path, "a")
            # second MP4 without date formatting for name
            # video_path = '/home/pedro/Videos/AUV/output.MP4'
            # out = cv2.VideoWriter(video_path, fourcc, vc.get(cv2.CAP_PROP_FPS),
            #                       (int(vc.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vc.get(cv2.CAP_PROP_FRAME_HEIGHT))))

            # trying to fix output to be able to output in MP4
            # out = cv2.VideoWriter(video_path, 0x00000021, vc.get(cv2.CAP_PROP_FPS),
            #           (int(vc.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vc.get(cv2.CAP_PROP_FRAME_HEIGHT))))

            # Put the code in try-except statements
            # Catch the keyboard exception and
            # release the camera device and
            # continue with the rest of code.
            cv2.namedWindow('Camera Input', cv2.WINDOW_NORMAL)
            try:

                frame_count = 0
                while (vc.isOpened()):
                    # Capture frame-by-frame
                    ret, frame = vc.read()
                    if not ret:
                        # Release the Video Device if ret is false
                        vc.release()
                        # Message to be displayed after releasing the device
                        print("Released Video Resource")
                        break

                    # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

                    # 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_frame_into_numpy_array(frame)

                    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                    image_np_expanded = np.expand_dims(image_np, axis=0)

                    frame_start = timeit.default_timer()

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

                    total_detection_time += timeit.default_timer(
                    ) - frame_start
                    frame_count += 1

                    # 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=4)

                    # prints bounding boxes, class, and scores only if score > 0.1
                    objects = []
                    height = vc.get(cv2.CAP_PROP_FRAME_HEIGHT)
                    width = vc.get(cv2.CAP_PROP_FRAME_WIDTH)
                    for index, value in enumerate(classes[0]):
                        object_dict = {}
                        if scores[0, index] > 0.3:
                            object_dict[(category_index.get(value)).get('name').encode('utf8')] = \
                                                scores[0, index]
                            objects.append(object_dict)
                            ymin = int((boxes[0][0][0] * height))
                            xmin = int((boxes[0][0][1] * width))
                            ymax = int((boxes[0][0][2] * height))
                            xmax = int((boxes[0][0][3] * width))
                            print("Top Left:")  #, file = log)
                            print("xmin: " + str(xmin))
                            print("ymax: " + str(ymax))  #, file = log)
                            print("Bottom Right")  #, file = log)
                            print("xmax: " + str(xmax))
                            print("ymax: " + str(ymax))  #, file = log)
                            print(objects)  #, file = log)
                            # here we can get distance, hwoever we might also want
                            # calibrate
                            perWidth = xmax - xmin
                            if (len(sys.argv) == 1):
                                # call distance_to_camera function
                                # these values come from around line 84, because I
                                # wanted them to be set before the loops
                                print("Distance " + str(
                                    distance_to_camera(KNOWN_WIDTH,
                                                       buoyFocalLength,
                                                       perWidth)))
                            else:
                                # call calibration function
                                print("calibrating")
                                calibrate(perWidth)

                    # out.write(cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR))

                    cv2.imshow('Camera Input', image_np)
                    out.write(image_np)
                    if cv2.waitKey(1) & 0xFF == ord('q'):
                        break

                print('Total Time: ', timeit.default_timer() - start_time)
                print(
                    'Average Time :', total_detection_time /
                    frame_count if frame_count > 0 else 0.0)

            except KeyboardInterrupt:
                # Release the Video Device
                vc.release()
                # Message to be displayed after releasing the device
                print("Released Video Resource")

    # out.release()
    cv2.destroyAllWindows()
示例#30
0
 frame = imutils.resize(frame, width=400)
 image_np_expanded = np.expand_dims(frame, axis=0)
 ch = cv2.waitKey(1)
 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(
     frame,
     np.squeeze(boxes),
     np.squeeze(classes).astype(np.int32),
     np.squeeze(scores),
     category_index,
     use_normalized_coordinates=True)
 if ch == ord('y'):
     objects = vis_util.objects_in_frame(np.squeeze(scores),
                                         np.squeeze(classes))
     for i in objects:
         engine.say("There is" +
                    category_index[int(objects[i])]['name'])
         engine.runAndWait()
 if ch == ord('s'):
     with sr.Microphone() as source:
         audio = r.listen(source)
     try:
         input = r.recognize_google(audio)
示例#31
0
  def streamVideo(self):
    """
    Note: The first frame read in through OpenCV is approximately 30 seconds behind the live video stream
    """
    # Access webcam or file
    cap = cv2.VideoCapture(self.videoFile)
    fps = FPS().start()

    with detection_graph.as_default():
      with tf.Session(graph=detection_graph) as sess:
        while True:
          ret, image_np = cap.read()

          if image_np is not None:
            # Resize captured frame if smallest dimension (excluding color) is greater than 300 px
            if min(image_np.shape[0:1]) > 300:
                reduceBy = max(image_np.shape[0:1])/300
                w_new = int(image_np.shape[0]/reduceBy)
                h_new = int(image_np.shape[1]/reduceBy)
                image_np = cv2.resize(image_np, dsize=(w_new,h_new), interpolation=cv2.INTER_CUBIC)
            
            # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
            image_np_expanded = np.expand_dims(image_np, axis=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.
            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.
            min_threshold = 0.5 # is 0.5 by default in visualize_boxes_and_labels_on_image_array
            
            if self.visualization == True:
                myThread0 = myThread(image_np, boxes, classes, scores, min_threshold, fps)

                # Push data to Power BI dashboard
                t2 = threading.Thread(target = myThread0.push_data)
                t2.daemon = True
                t2.start()

                # Non-threaded visualization
                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,
                    max_boxes_to_draw=999,
                    min_score_thresh=min_threshold,
                    line_thickness=2)
                cv2.imshow(' Project Natick Environmental Sustainability Console - Realtime', cv2.resize(image_np, (800,600)))
                fps.update()

            elif self.visualization == False:
                myThread0 = myThread(img_blank, boxes, classes, scores, min_threshold, fps)
                t2 = threading.Thread(target = myThread0.push_data)
                t2.daemon = True
                t2.start()
                cv2.imshow(' Project Natick Environmental Sustainability Console - Realtime', cv2.resize(img_blank, (800, 600)))
                fps.update()

            # Exit
            if cv2.waitKey(25) & 0xFF == ord('q'):
                fps.stop()
                print('[INFO] elapsed time: {:.2f}'.format(fps.elapsed()))
                print('[INFO] approx. FPS: {:.2f}'.format(fps.fps()))
                cap.release()
                cv2.destroyAllWindows()
                break
示例#32
0
            # Actual detection.
            (boxes, scores, classes,
             num) = sess.run([
                 detection_boxes, detection_scores, detection_classes,
                 num_detections
             ],
                             feed_dict={image_tensor: image_np_expanded})
            #x1 = boxes

            #if(np.allclose(classes)==1):
            # print("It works")
            # Visualization of the results of a detection.
            as1, asd = 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)

            #if(asd=='bottle'):
            #vis_util.secondfunction()
            #break
            cv2.imshow('Object Detection', cv2.resize(image_np, (800, 600)))
            if cv2.waitKey(25) & 0xFF == ord('q'):
                cv2.destroyAllWindows()
                break
        #if(checkingvariable>1):
        # print("it works")
示例#33
0
num_detections = detection_graph.get_tensor_by_name('num_detections:0')

# OpenCV kullanarak görüntü okuyun ve şekle sahip olmak için görüntü boyutlarını genişletin: [1, None, None, 3],
# yani sütundaki her öğenin piksel RGB değerine sahip olduğu tek sütunlu bir dizi.
image = cv2.imread(PATH_TO_IMAGE)
image_expanded = np.expand_dims(image, axis=0) 

# Modeli görüntü olarak girdi olarak çalıştırarak gerçek algılamayı gerçekleştirir.
(boxes, scores, classes, num) = sess.run(
    [detection_boxes, detection_scores, detection_classes, num_detections],
    feed_dict={image_tensor: image_expanded})

# görüntüyü görselleştirir. 

vis_util.visualize_boxes_and_labels_on_image_array(
    image,
    np.squeeze(boxes), #squeeze: Tek boyutlu girişleri kaldırır.
    np.squeeze(classes).astype(np.int32), #classes'ın kopyasını, belirtilen türe dönüştürür.
    np.squeeze(scores),
    category_index,
    use_normalized_coordinates=True, #Kusurları kutucuk içine alır.
    line_thickness=5, 
    min_score_thresh=0.80)

# Tüm sonuçlar resim üzerine çizilmiştir. Şimdi görüntüyü göster.
cv2.imshow('Object detector', image)

# Görüntüyü kapatmak için herhangi bir tuşa basın
cv2.waitKey(0)
cv2.destroyAllWindows()
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()
示例#35
0
    def find_detection(self, input_folder, output_folder):
        """Function to detect images in input folder and save annotated image in an output directory.

            Args:
                input_folder        : Input  file directory.

                output_folder       : Output directory to save the image with object detected.

            Returns:
                None"""
        if not os.path.exists(input_folder):
            print("Input folder not found")
            return 1

        if not os.path.exists(output_folder):
            print("Output folder not present. Creating New folder...")
            os.makedirs(output_folder)

        for root, _, filenames in os.walk(input_folder):
            if (len(filenames) == 0):
                print("Input folder is empty")
                return 1
            time_start = time.time()
            for filename in filenames:
                try:
                    print(
                        "Creating object detection for file : {fn}".format(
                            fn=filename), '\n')

                    file_path = (os.path.join(root, filename))
                    image = cv2.imread(file_path, 1)

                    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) = self.sess.run(
                        [
                            self.detection_boxes, self.detection_scores,
                            self.detection_classes, self.num_detections
                        ],
                        feed_dict={self.image_tensor: image_expanded})
                    # print (self.num_detections,'\n')

                    # 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),
                        self.category_index,
                        use_normalized_coordinates=True,
                        line_thickness=5,
                        min_score_thresh=0.80)
                    # All the results have been drawn on image. Now display the image.
                    # cv2.imshow('Object detector', image)
                    output_path = (os.path.join(output_folder, filename))
                    cv2.imwrite(output_path, image)
                    # Press any key to close the image
                    # cv2.waitKey(1000)
                    # Clean up
                    cv2.destroyAllWindows()
                    # vis_util.save_image_array_as_png(image, output_folder)

                except IOError:
                    print("Existing Object Detection...")
                except:
                    print(
                        'ERROR...object detecion failed for Filename: {fn} , Check file type '
                        .format(fn=filename), '\n')
                else:
                    1
                    # print("Object Detected  successfully !", '\n')

            time_end = time.time()
            print("Object Detection on above images completed successfully !",
                  '\n')
            sec = timedelta(seconds=int(time_end - time_start))
            d = datetime(1, 1, 1) + sec
            print("Time Consumed - hours:{th} - Minutes:{mn} - Second:{sc}".
                  format(th=d.hour, mn=d.minute, sc=d.second))
            print("Annotated images saved in following path :", output_folder)
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()
示例#37
0
文件: Select.py 项目: hyunbool/chuka
    def makeHighlight(self):
        filename = self.f_label.text()
        cap = cv2.VideoCapture(filename)
        video_for_cut = VideoFileClip(filename)
        fps = cap.get(cv2.CAP_PROP_FPS)
        #sys.path.append("..")

        MODEL_NAME = 'soccer_highlight_goal2'

        # 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('training', 'object-detection.pbtxt')

        detection_graph = tf.Graph()
        with detection_graph.as_default():
            od_graph_def = tf.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='')

        category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)

        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)

        count = 1
        cut_count = 0

        hightlight = []
        cut = []
        with detection_graph.as_default():
            with tf.compat.v1.Session(graph=detection_graph) as sess:
                while True:
                    ret, image_np = cap.read()
                    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                    image_np_expanded = np.expand_dims(image_np, axis=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.
                    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.

                    try:
                        (boxes, scores, classes, num_detections) = sess.run(
                            [boxes, scores, classes, num_detections],
                            feed_dict={image_tensor: image_np_expanded})
                    # 동영상 끝나면 highlightui로 넘어가게 된다.
                    except TypeError:
                        self.window = Ui_HighlightWindow()
                        self.window.show()

                        break

                    # 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)

                    if (int(cap.get(1)) % 8 == 0):

                        title = "%d.jpg" % count
                        count += 1
                        # cv2.imshow('object detection', cv2.resize(image_np, (800, 600)))

                        if (float(100 * scores[0][0]) > 99.7):
                            print(title)
                            hightlight.append(count)
                        else:
                            if (count - 1 in hightlight and count + 1 not in hightlight):
                                cut.append(8 * (count - 4))
                                print(count - 1)

                        if (len(cut) > 1):
                            duration1 = cut[0] / fps
                            duration2 = cut[1] / fps
                            length = duration2 - duration1
                            print(length)
                            if (length > 60 or length <= 2):
                                cut[0] = cut[1]
                                del (cut[1])
                            else:
                                start_hour = (duration1 / 3600)
                                start_min = ((duration1 % 3600) / 60)
                                start_sec = duration1 % 60

                                end_hour = (duration2 / 3600)
                                end_min = ((duration2 % 3600) / 60)
                                end_sec = duration2 % 60

                                tmp_video = video_for_cut.subclip(duration1, duration2)
                                tmp_title = "./videos/%d+%d+%d~%d+%d+%d.mp4" % (
                                start_hour, start_min, start_sec, end_hour, end_min, end_sec)
                                cut_count += 1
                                tmp_video.write_videofile(tmp_title, codec='libx264')
                                cut = []
示例#38
0
        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')

        while(cap.isOpened()):
            	vector_image = load_image()
            	vector_image_hd = np.expand_dims(vector_image,axis=0)
            	(boxes,scores,classes,num)=sess.run([detection_boxes,detection_scores,detection_classes,num_detections],feed_dict={image_tensor:vector_image_hd})
            
        	# visualization
        	vis_util.visualize_boxes_and_labels_on_image_array(
            		vector_image,
            		np.squeeze(boxes),
            		np.squeeze(classes).astype(np.int32),
            		np.squeeze(scores),
            		category_index,
            		use_normalized_coordinates=True,
            		line_thickness=5)

        	cv2.imshow('Frame',vector_image)
		
		if cv2.waitKey(10) & 0xFF == ord('q'):
			break

cap.release()
cv2.destroAllWindows()


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')