Esempio n. 1
0
def demoVideo(image):

    global count
    count = count + 1

    # print ('count before = {}'.format(count))
    if (count % 10) > 0:
        # count = 1
        im = process_image(image)
        return im

    # count = count+1
    # print ('processing image')
    # print ('count = {}'.format(count))

    im = process_image(image)

    height, width = im.shape[:2]
    mid = width / 2.5
    # print('height = {} and width/2.5 = {}'.format(height, mid))

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(default_net, im)
    timer.toc()
    # print ('Detection took {:.3f}s for '
    #        '{:d} object proposals').format(timer.total_time, boxes.shape[0])

    # Visualize detections for each class
    CONF_THRESH = 0.8
    NMS_THRESH = 0.3
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        font = cv2.FONT_HERSHEY_SIMPLEX

        color = (0, 0, 255)
        inds = np.where(dets[:, -1] >= CONF_THRESH)[0]
        if len(inds) > 0:
            for i in inds:
                bbox = dets[i, :4]
                score = dets[i, -1]
                cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[2], bbox[3]),
                              color, 2)
                if bbox[0] < mid:
                    cv2.putText(im, 'left {:s}'.format(cls), (bbox[0], (int)(
                        (bbox[1] - 2))), font, 0.5, (255, 0, 0), 1)
                else:
                    cv2.putText(im, 'right {:s}'.format(cls, score),
                                (bbox[0], (int)(
                                    (bbox[1] - 2))), font, 0.5, (255, 0, 0), 1)

# cv2.putText(im,'{:s} {:.3f}'.format(cls, score),(bbox[0], (int)((bbox[1]- 2))), font, 0.5, (255,255,255), 1)
    return im
def process_video(image):

    global heat_recent
    heat = np.zeros_like(image[:, :, 0]).astype(np.float)

    box_list = []

    result = find_cars(image, box_list, 400, 500, 1.2, svc, X_scaler, \
        orient, pix_per_cell, cell_per_block, spatial_size, hist_bins)
    result = find_cars(image, box_list, 400, 550, 1.8, svc, X_scaler, \
        orient, pix_per_cell, cell_per_block, spatial_size, hist_bins)
    # result = find_cars(image, box_list, 400, 800, 2.3, svc, X_scaler, \
    #     orient, pix_per_cell, cell_per_block, spatial_size, hist_bins)

    # Add heat to each box in box list
    heat = add_heat(heat, box_list)

    if heat_recent == []:
        heat_recent = heat.reshape(1, heat.shape[0], heat.shape[1])
    else:
        heat = heat.reshape(1, heat.shape[0], heat.shape[1])
        heat_recent = np.concatenate((heat_recent, heat), axis=0)
        if len(heat_recent) > 8:
            heat_recent = np.delete(heat_recent, 0, axis=0)
        heat = np.sum(heat_recent, axis=0)

    # Apply threshold to help remove false positives
    # if visualize is on, do this based on only one frame, so lower thr
    if visualize:
        thr = 1
    else:
        thr = 12
    heat = apply_threshold(heat, thr)

    # Visualize the heatmap when displaying
    heatmap = np.clip(heat, 0, 255)

    # Find final boxes from heatmap using label function
    labels = label(heatmap)

    image_lanes = ld.process_image(image, calibrate=False)
    result = draw_labeled_bboxes(np.copy(image_lanes), labels)

    if visualize:
        fig = plt.figure()
        plt.subplot(121)
        plt.imshow(result)
        plt.title('Car Positions')
        plt.subplot(122)

        plt.imshow(heatmap, cmap='hot')
        plt.title('Heat Map')
        fig.tight_layout()
        plt.savefig('output_images/heatmap.png')
        plt.show()

    return result
Esempio n. 3
0
 def auto_frames():
     # adding path
     
     with picamera.PiCamera() as camera:
         rawCapture = PiRGBArray(camera, size=(640, 480))
         # let camera warm up
         camera.resolution = (640, 480)
         camera.hflip = True
         camera.vflip = True
         time.sleep(2)
         for frame in camera.capture_continuous(rawCapture, 'bgr',
                                              use_video_port=True):
             # return current frame
             #img = cv2.cvtColor(frame.array, cv2.COLOR_BGR2GRAY)
             img, cmd = process_image(frame.array)
               
             status, buf = cv2.imencode('.jpeg', img)
             yield (np.array(buf).tostring(), cmd)
             
             
             rawCapture.truncate()
             rawCapture.seek(0)
def demo(net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    img_name = os.path.basename(image_name)
    # im_file = image_name
    # im = cv2.imread(im_file)
    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
    im = cv2.imread(im_file)

    pimg = process_image(im)
    # cv2.imshow("Processed", pimg)
    # cv2.waitKey(0)
    im = pimg

    height, width = im.shape[:2]
    mid = width / 2.5
    # print('height = {} and width/2.5 = {}'.format(height, mid))

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(net, im)
    timer.toc()
    # print ('Detection took {:.3f}s for '
    #        '{:d} object proposals').format(timer.total_time, boxes.shape[0])

    # Visualize detections for each class
    CONF_THRESH = 0.8
    NMS_THRESH = 0.3
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        # vis_detections(im, cls, dets, thresh=CONF_THRESH)

        font = cv2.FONT_HERSHEY_SIMPLEX
        # print 'class index is {}'.format(cls_ind)

        color = (0, 0, 255)
        inds = np.where(dets[:, -1] >= CONF_THRESH)[0]
        if len(inds) > 0:
            for i in inds:
                bbox = dets[i, :4]
                score = dets[i, -1]
                cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[2], bbox[3]),
                              color, 2)
                if bbox[0] < mid:
                    cv2.putText(im, 'left {:s}'.format(cls), (bbox[0], (int)(
                        (bbox[1] - 2))), font, 0.5, (255, 0, 0), 1)
                else:
                    cv2.putText(im, 'right {:s}'.format(cls, score),
                                (bbox[0], (int)(
                                    (bbox[1] - 2))), font, 0.5, (255, 0, 0), 1)

# cv2.putText(im,'{:s} {:.3f}'.format(cls, score),(bbox[0], (int)((bbox[1]- 2))), font, 0.5, (255,255,255), 1)

# Write the resulting frame
# print 'Final image name is {}'.format(img_name)
    splitName = os.path.splitext(img_name)[0]
    # print (os.path.splitext(img_name)[0])
    # print splitName
    # cv2.imwrite('{:s}_output.jpg'.format(splitName), im)

    ## Display output frame
    # cv2.imshow("output", im)
    # cv2.waitKey(0)

    ## Write output frame
    opDir = '/home/student/cmpe295-masters-project/faster-rcnn-resnet/data/output/'
    cv2.imwrite(os.path.join(opDir, img_name), im)