Exemple #1
0
def _write_examples(writer,
                    coder,
                    filenames,
                    indices,
                    bboxes=None,
                    labels=None):
    for i in indices:
        filename = filenames[i]

        image_buffer, height, width = utils.process_image(filename, coder)

        if FLAGS.ssd_graph_path is not None:
            box, score, clazz = utils.detect_objects(image_buffer, coder)

            if False:
                # Browse the results of object detection
                from PIL import Image, ImageDraw
                im = Image.open(filename)

                im_width, im_height = im.size
                ymin, xmin, ymax, xmax = box
                (left, right, top,
                 bottom) = (xmin * im_width, xmax * im_width, ymin * im_height,
                            ymax * im_height)

                draw = ImageDraw.Draw(im)
                draw.line([(left, top), (left, bottom), (right, bottom),
                           (right, top), (left, top)],
                          width=4,
                          fill='red')
                del draw

                im.save(
                    os.path.join('/Users/SHYBookPro/Desktop/bbxes',
                                 os.path.basename(filename)), "PNG")
            example = utils.convert_to_example(filename,
                                               image_buffer,
                                               height,
                                               width,
                                               box,
                                               int(clazz),
                                               bbox_normalize=True)
        elif bboxes is not None and labels is not None:
            bbox = bboxes[i]
            label = labels[i]
            example = utils.convert_to_example(filename, image_buffer, height,
                                               width, bbox, label)
        else:
            example = utils.convert_to_example(filename, image_buffer, height,
                                               width)
        writer.write(example.SerializeToString())
Exemple #2
0
def image_detect(i_path):
    """Starting detect object"""
    # Load the image

    print(i_path)

    img = cv2.imread(i_path)
    # Convert the image to RGB
    original_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # We resize the image to the input width and height of the first layer of the network.
    resized_image = cv2.resize(original_image, (m.width, m.height))

    # Set the NMS threshold
    nms_thresh = 0.6
    # Set the IOU thresholdectec
    iou_thresh = 0.4

    # Detect objects in the image
    boxes = utils.detect_objects(m, resized_image, iou_thresh, nms_thresh)

    img = original_image.copy()
    width = img.shape[1]
    height = img.shape[0]

    for i in range(len(boxes)):
        box = boxes[i]
        # Get the (x,y) pixel coordinates of the lower-left and lower-right corners
        # of the bounding box relative to the size of the image.
        x1 = int(np.around((box[0] - box[2] / 2.0) * width))
        y1 = int(np.around((box[1] - box[3] / 2.0) * height))
        x2 = int(np.around((box[0] + box[2] / 2.0) * width))
        y2 = int(np.around((box[1] + box[3] / 2.0) * height))

        if len(box) >= 7 and class_names:
            cls_conf = box[5]
            cls_id = box[6]
            print('%i. %s: %f' % (i + 1, class_names[cls_id], cls_conf))
            print("left top right bottom :", x1, y1, x2, y2)

    # Print the objects found and the confidence level
    utils.print_objects(boxes, class_names)
    #Plot the image with bounding boxes and corresponding object class labels
    # Set the default figure size
    plt.rcParams['figure.figsize'] = [24.0, 14.0]
    utils.plot_boxes(original_image, boxes, class_names, plot_labels=True)
Exemple #3
0
def count_person(image, frame_no, maxpeople):
    maxpeople = maxpeople
    # Set the default figure 
    frame_no = frame_no
    plt.rcParams['figure.figsize'] = [24.0, 14.0] #wXh
    
    og_img = image
    # Load the image
#    img = cv2.imread(og_img)
    
    # Convert the image to RGB
    original_image = cv2.cvtColor(og_img, cv2.COLOR_BGR2RGB)
    
    # We resize the image to the input width and height of the first layer of the network.    
    resized_image = cv2.resize(original_image, (m.width, m.height))
    
    # Display the images
    #plt.subplot(121)
    #plt.title('Original Image')
    #plt.imshow(original_image)
    #plt.subplot(122)
    #plt.title('Resized Image')
    #plt.imshow(resized_image)
    #plt.show()
    
    # Set the NMS threshold
    nms_thresh = 0.5
    
    # Set the IOU threshold
    iou_thresh = 0.5
    
#    print("till here")
    # Detect objects in the image
    boxes = detect_objects(m, resized_image, iou_thresh, nms_thresh)
#    print("detect_object")
    # Print the objects found and the confidence level
    print_objects(maxpeople, boxes, class_names)
    
    #Plot the image with bounding boxes and corresponding object class labels
    plot_boxes(frame_no, original_image, boxes, class_names)

    
#https://www.pyimagesearch.com/2018/11/12/yolo-object-detection-with-opencv/
Exemple #4
0
def get_pos(cap):
    """
    function to get coordinates of hand in the frame

    :param: cap: videocapture object

    :return: median of y coordinate of detected hand

    """

    # get width and heigth of frame
    width, height = (cap.get(3), cap.get(4))

    # read frame
    ret, frame = cap.read()

    # convert frame from BGR to RGM
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    # get coordinate and score of detected hand
    boxes, scores = utils.detect_objects(frame, graph, sess)

    # draw box on the detected coordinates in the frame
    center = utils.draw(scores, boxes, width, height, frame)

    # show processed frame in the window
    cv2.imshow('Detection', cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))

    # wait for 'q' key press
    # if pressed, window is closed and negative number returned
    if cv2.waitKey(1) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        return -1

    # if hand detected, return center of y coordinates
    if center:
        return center
Exemple #5
0
def detect(model,
           original_image,
           min_score,
           max_overlap,
           top_k,
           suppress=None):
    """
    Detect objects in an image with a trained SSD300, and visualize the results.
    :param original_image: image, a PIL Image
    :param min_score: minimum threshold for a detected box to be considered a match for a certain class
    :param max_overlap: maximum overlap two boxes can have so that the one with the lower score is not suppressed via Non-Maximum Suppression (NMS)
    :param top_k: if there are a lot of resulting detection across all classes, keep only the top 'k'
    :param suppress: classes that you know for sure cannot be in the image or you do not want in the image, a list
    :return: annotated image, a PIL Image
    """

    # Transforms
    resize = transforms.Resize((300, 300))
    to_tensor = transforms.ToTensor()
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    image = normalize(to_tensor(resize(original_image)))

    # Move to default device
    image = image.to(device)

    # Forward prop.
    predicted_locs, predicted_scores = model(image.unsqueeze(0))

    # Detect objects in SSD output
    det_boxes, det_labels, det_scores = detect_objects(model,
                                                       priors_cxcy,
                                                       predicted_locs,
                                                       predicted_scores,
                                                       min_score=min_score,
                                                       max_overlap=max_overlap,
                                                       top_k=top_k,
                                                       n_classes=n_classes)

    # Move detections to the CPU
    det_boxes = det_boxes[0].to('cpu')

    # Transform to original image dimensions
    original_dims = torch.FloatTensor([
        original_image.width, original_image.height, original_image.width,
        original_image.height
    ]).unsqueeze(0)
    det_boxes = det_boxes * original_dims

    # Decode class integer labels
    det_labels = [rev_label_map[l] for l in det_labels[0].to('cpu').tolist()]

    # If no objects found, the detected labels will be set to ['0.'], i.e. ['background'] in SSD300.detect_objects() in model.py
    if det_labels == ['background']:
        # Just return original image
        return original_image

    # Annotate
    annotated_image = original_image
    draw = ImageDraw.Draw(annotated_image)
    #font = ImageFont.truetype("./calibril.ttf", 15)
    font = ImageFont.truetype("arial.ttf", 15)

    # Suppress specific classes, if needed
    for i in range(det_boxes.size(0)):
        if suppress is not None:
            if det_labels[i] in suppress:
                continue

        # Boxes
        box_location = det_boxes[i].tolist()
        draw.rectangle(xy=box_location, outline=label_color_map[det_labels[i]])
        draw.rectangle(
            xy=[l + 1. for l in box_location],
            outline=label_color_map[det_labels[i]]
        )  # a second rectangle at an offset of 1 pixel to increase line thickness
        # draw.rectangle(xy=[l + 2. for l in box_location], outline=label_color_map[
        #     det_labels[i]])  # a third rectangle at an offset of 1 pixel to increase line thickness
        # draw.rectangle(xy=[l + 3. for l in box_location], outline=label_color_map[
        #     det_labels[i]])  # a fourth rectangle at an offset of 1 pixel to increase line thickness

        # Text
        text_size = font.getsize(det_labels[i].upper())
        text_location = [box_location[0] + 2., box_location[1] - text_size[1]]
        textbox_location = [
            box_location[0], box_location[1] - text_size[1],
            box_location[0] + text_size[0] + 4., box_location[1]
        ]
        draw.rectangle(xy=textbox_location,
                       fill=label_color_map[det_labels[i]])
        draw.text(xy=text_location,
                  text=det_labels[i].upper(),
                  fill='white',
                  font=font)
    del draw

    return annotated_image
Exemple #6
0
nms_thresh = 0.6

# Set the IOU threshold
iou_thresh = 0.4

# Set the default figure size
plt.rcParams['figure.figsize'] = [24.0, 14.0]

# Load the image
img = cv2.imread('./images/1.jpg')

# Convert the image to RGB
original_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# We resize the image to the input width and height of the first layer of the network.
resized_image = cv2.resize(original_image, (m.width, m.height))

# Set the IOU threshold. Default value is 0.4
iou_thresh = 0.4

# Set the NMS threshold. Default value is 0.6
nms_thresh = 0.6

# Detect objects in the image
boxes = ut.detect_objects(m, resized_image, iou_thresh, nms_thresh)

# Print the objects found and the confidence level
# print_objects(boxes, class_names)

# Plot the image with bounding boxes and corresponding object class labels
ut.plot_boxes(original_image, boxes, class_names, plot_labels=True)
Exemple #7
0
def detect(img_path):
    original_image = Image.open(img_path, mode='r').convert('RGB')
    model = SSD(class_num=n_classes, backbone='VGG', device=device)
    model = load_pretrained(model, 'ssd300_params_vgg_seaship_best.pth')
    model.to(device)

    resize = transforms.Resize((300, 300))
    to_tensor = transforms.ToTensor()
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    image = normalize(to_tensor(resize(original_image)))
    image = image.to(device)

    predicted_locs, predicted_scores = model(image.unsqueeze(0))

    det_boxes, det_labels, det_scores = detect_objects(model.priors,
                                                       predicted_locs,
                                                       predicted_scores, 0.40,
                                                       0.45, 200, n_classes)
    #print(len(det_labels[0]))

    det_boxes = det_boxes[0].to('cpu')

    original_dims = torch.FloatTensor([
        original_image.width, original_image.height, original_image.width,
        original_image.height
    ]).unsqueeze(0)
    det_boxes = det_boxes * original_dims

    det_labels = [rev_label_map[l] for l in det_labels[0].to('cpu').tolist()]

    if det_labels == ['background']:
        return original_image

    annotated_image = original_image
    draw = ImageDraw.Draw(annotated_image)
    font = ImageFont.truetype('Arial.ttf', 15)

    for i in range(det_boxes.size(0)):
        box_location = det_boxes[i].tolist()
        draw.rectangle(xy=box_location, outline=label_color_map[det_labels[i]])
        draw.rectangle(xy=[l + 1. for l in box_location],
                       outline=label_color_map[det_labels[i]])

        text_size = font.getsize(det_labels[i].upper())
        text_location = [box_location[0] + 2., box_location[1] - text_size[1]]
        textbox_location = [
            box_location[0], box_location[1] - text_size[1],
            box_location[0] + text_size[0] + 4., box_location[1]
        ]
        draw.rectangle(xy=textbox_location,
                       fill=label_color_map[det_labels[i]])
        draw.text(xy=text_location,
                  text=det_labels[i].upper(),
                  fill='white',
                  font=font)

    del draw

    annotated_image.save('temp/res005.jpg')
    return annotated_image