Exemplo n.º 1
0
def show_bboxes(folder):
    with open(lfw_pickle, 'rb') as file:
        data = pickle.load(file)

    samples = data['samples']
    for sample in tqdm(samples):
        full_path = sample['full_path']
        bounding_boxes = sample['bounding_boxes']
        landmarks = sample['landmarks']
        img = cv.imread(full_path)
        img = draw_bboxes(img, bounding_boxes, landmarks)
        filename = os.path.basename(full_path)
        filename = os.path.join(folder, filename)
        cv.imwrite(filename, img)
Exemplo n.º 2
0
def face_detect():
    start = time.time()
    ensure_folder(STATIC_DIR)
    ensure_folder(UPLOAD_DIR)
    file = request.files['file']
    fn = secure_filename(file.filename)
    full_path = os.path.join(UPLOAD_DIR, fn)
    file.save(full_path)
    # resize(full_path)
    print('full_path: ' + full_path)

    img = Image.open(full_path).convert('RGB')
    bboxes, landmarks = detect_faces(img)
    num_faces = len(bboxes)

    if num_faces > 0:
        img = cv.imread(full_path)
        draw_bboxes(img, bboxes, landmarks)
        cv.imwrite(full_path, img)

    elapsed = time.time() - start

    return num_faces, float(elapsed), str(fn), bboxes, landmarks
Exemplo n.º 3
0
 def callback_button1(self):
     """Running button"""
     if not self.fa.is_loaded:
         self.set_text('Network is not loaded')
         return
     name = tk.filedialog.askopenfilename()  # open dialog
     if path.exists(name):
         self.img_file_path = name
         self.load_img()  # load image
         img = cv2.imread(name)
         img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
         bboxes = self.fa.FD.detect_faces(img, conf_th=0.9,
                                          scales=[1])  # face detection
         print("{} faces are detected".format(len(bboxes)))
         img_result = draw_bboxes(img, bboxes)  # draw face boxes
         pts = self.fa.FA.face_alignment(img, bboxes)  # face alignment
         print("Face Alignment is done")
         img_result = draw_pts(img_result, pts)  # draw landmarks
         plt.figure(1)  # show result
         plt.axis('off')
         plt.imshow(img_result)
         plt.show()
Exemplo n.º 4
0
    imgs = torch.zeros([10, 3, im_size, im_size], dtype=torch.float)
    ensure_folder('images')

    for i in range(10):
        sample = samples[i]
        fullpath = sample['fullpath']
        raw = cv.imread(fullpath)
        raw = cv.resize(raw, (im_size, im_size))
        img = raw[..., ::-1]  # RGB
        img = transforms.ToPILImage()(img)
        img = transformer(img)
        imgs[i] = img

        cv.imwrite('images/{}_img.jpg'.format(i), raw)
        raw = draw_bboxes(raw, sample['pts'])
        cv.imwrite('images/{}_true.jpg'.format(i), raw)

    with torch.no_grad():
        outputs = model(imgs)

    for i in range(10):
        output = outputs[i].cpu().numpy()
        output = np.reshape(output, (4, 1, 2))
        output = output * im_size
        print('output: ' + str(output))
        print('output.shape: ' + str(output.shape))

        img = cv.imread('images/{}_img.jpg'.format(i))
        img = draw_bboxes(img, output)
        cv.imwrite('images/{}_out.jpg'.format(i), img)
Exemplo n.º 5
0
    ]

    imgs = torch.zeros([4, 3, im_size, im_size], dtype=torch.float)
    ensure_folder('images')

    for i in range(len(file_list)):
        file = file_list[i]
        fullpath = os.path.join('images', file)
        print(fullpath)
        raw = cv.imread(fullpath)
        raw = cv.resize(raw, (im_size, im_size))
        img = raw[..., ::-1]  # RGB
        img = transforms.ToPILImage()(img)
        img = transformer(img)
        imgs[i] = img
        cv.imwrite('images/img_bld_{}.jpg'.format(i), raw)

    with torch.no_grad():
        outputs = model(imgs)

    for i in range(len(file_list)):
        output = outputs[i].cpu().numpy()
        output = np.reshape(output, (4, 1, 2))
        output = output * im_size
        print('output: ' + str(output))
        print('output.shape: ' + str(output.shape))

        img = cv.imread('images/img_bld_{}.jpg'.format(i))
        img = draw_bboxes(img, output)
        cv.imwrite('images/out_bld_{}.jpg'.format(i), img)
Exemplo n.º 6
0
dc2.start()
dc3.start()
dc4.start()

# Wait to process every frame:
while queue_results.qsize() < NUM_OF_FRAMES:
    time.sleep(1)

print(time.time() - st, queue_results.qsize())

# Save results
dict_results = {}
while (not queue_results.empty()):
    idx, img_orig, list_res = queue_results.get()
    dict_results[idx] = list_res
    newImg = draw_bboxes(img_orig, list_res)
    cv2.imwrite(f'/home/dobreff/videos/outputs/test/resize{idx}.jpg', newImg)
    print(f'Item {idx} saved!')

with open('/home/dobreff/videos/outputs/test/test_resize.pickle',
          'wb') as handle:
    pickle.dump(dict_results, handle, protocol=pickle.HIGHEST_PROTOCOL)

# # Mivel maxra fel van töltve a Queue, addig pörgök a ciklusban amíg van benne elem:
# while (not queue_frames.empty()) or (not queue_results.empty()):
# 	# Grab result image
# 	idx, img_orig, list_res = queue_results.get()
# 	newImg = draw_bboxes(img_orig, list_res)
# 	cv2.imwrite(f'/home/dobreff/videos/outputs/{idx}.jpg', newImg)
# 	print(f'Item {idx} saved!')
Exemplo n.º 7
0
def output_check_result(save_path, xml_path, img_path, recog_path,
                        recognition_xml):
    '''
    将含有问题算式的图片输出出来
    1.标注错误
    2.版面分析错误
    :param save_path: 保存输出结果的地址
    :return:
    '''

    all_img = set_xml_data(xml_path, img_path, recog_path, recognition_xml)

    for i, img_result in tqdm(enumerate(all_img)):
        img_result.row_connect()

        column_pairs = img_result.column_connect()
        img_result.graph_to_forest()

        if img_result.problem_label:

            save_path1 = os.path.join(save_path, 'problem')

            if not os.path.exists(save_path1):
                os.mkdir(save_path1)

            img = img_result.img
            img, x_pro, y_pro = image_size_normal(img)

            img2 = img.copy()

            draw_bboxes(img, img_result.problem_label, x_pro, y_pro)
            img = draw_result(img, img_result.problem_label, x_pro, y_pro)
            img.save(
                os.path.join(save_path1,
                             img_result.img_path.split('/')[-1]))

            draw_bboxes(img2, img_result.print_word + img_result.hand_word,
                        x_pro, y_pro)
            draw_pair(img_result.print_hand_pair, img_result.print_word,
                      img_result.hand_word, img2, (255, 0, 0), x_pro, y_pro)

            draw_pair(img_result.bracket_pair, img_result.merge,
                      img_result.print_not_pair, img2, (0, 0, 255), x_pro,
                      y_pro)

            # draw_column_pair(column_pairs,img_result.all_after_row_connect,img2,x_pro, y_pro)
            img2 = draw_result(img2, [], x_pro, y_pro)
            img2.save(
                os.path.join(
                    save_path1,
                    img_result.img_path.split('/')[-1].replace('.', '_.')))

        if (img_result.problem_label + img_result.error_label):

            save_path2 = os.path.join(save_path, 'problem_error')
            if not os.path.exists(save_path2):
                os.mkdir(save_path2)

            img = img_result.img
            img, x_pro, y_pro = image_size_normal(img)

            img2 = img.copy()

            draw_bboxes(img, img_result.problem_label + img_result.error_label,
                        x_pro, y_pro)
            img = draw_result(
                img, img_result.problem_label + img_result.error_label, x_pro,
                y_pro)
            img.save(
                os.path.join(save_path2,
                             img_result.img_path.split('/')[-1]))

            draw_bboxes(img2, img_result.print_word + img_result.hand_word,
                        x_pro, y_pro)
            draw_pair(img_result.print_hand_pair, img_result.print_word,
                      img_result.hand_word, img2, (255, 0, 0), x_pro, y_pro)

            draw_pair(img_result.bracket_pair, img_result.merge,
                      img_result.print_not_pair, img2, (0, 0, 255), x_pro,
                      y_pro)

            # draw_column_pair(column_pairs,img_result.all_after_row_connect,img2,x_pro, y_pro)
            img2 = draw_result(img2, [], x_pro, y_pro)
            img2.save(
                os.path.join(
                    save_path2,
                    img_result.img_path.split('/')[-1].replace('.', '_.')))
def process_img(vd, img_file, out_dir='output_images', process_pool=None):

    img = cv2.imread(img_file)

    t1 = time.time()
    bounding_boxes, heatmap, windows = vd.detect_vehicles(
        img, process_pool=process_pool)
    t2 = time.time()

    plt.figure(figsize=(20, 15))

    rows = np.ceil((len(windows) + 2) / 2)

    all_bboxes = []

    i = 1
    for scale, cells_per_step, bboxes in windows:

        i += 1
        plt.subplot(rows, 2, i)
        w_tot = len(bboxes)
        w_pos = len(
            list(filter(lambda bbox: bbox[1] >= vd.min_confidence, bboxes)))
        w_rej = len(
            list(
                filter(
                    lambda bbox: bbox[1] > 0 and bbox[1] < vd.min_confidence,
                    bboxes)))
        box_text = 'Scale: {}, Cells per Step: {}, Windows (Total/Positive/Rejected): {}/{}/{}'.format(
            scale, cells_per_step, w_tot, w_pos, w_rej)
        plt.title(box_text)
        box_img = draw_windows(np.copy(img),
                               bboxes,
                               min_confidence=vd.min_confidence,
                               lines_thick=(2, 3, 2))
        plt.imshow(cv2.cvtColor(box_img, cv2.COLOR_BGR2RGB))
        all_bboxes.extend(bboxes)

    plt.subplot(rows, 2, 1)
    plt.title('Original Image')
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

    box_img = draw_windows(np.copy(img),
                           all_bboxes,
                           min_confidence=vd.min_confidence,
                           lines_thick=(2, 3, 2))
    plt.subplot(rows, 2, i + 1)
    w_tot = len(all_bboxes)
    w_pos = len(
        list(filter(lambda bbox: bbox[1] >= vd.min_confidence, all_bboxes)))
    w_rej = len(
        list(
            filter(lambda bbox: bbox[1] > 0 and bbox[1] < vd.min_confidence,
                   all_bboxes)))
    box_text = 'Combined - Min Confidence: {}, Windows (Total/Positive/Rejected): {}/{}/{}'.format(
        vd.min_confidence, w_tot, w_pos, w_rej)
    plt.title(box_text)
    plt.imshow(cv2.cvtColor(box_img, cv2.COLOR_BGR2RGB))

    plt.tight_layout()
    img_prefix = os.path.split(img_file)[-1].split('.')[0]
    plt.savefig(os.path.join(out_dir, img_prefix + '_window_search.jpg'))

    plt.figure(figsize=(20, 10))

    plt.subplot(221)
    plt.title('Original Image')
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

    plt.subplot(222)
    plt.title(box_text)
    plt.imshow(cv2.cvtColor(box_img, cv2.COLOR_BGR2RGB))
    plt.subplot(223)
    plt.title('Heatmap')
    heatmap_o = vd._heatmap(img, windows, 0)
    plt.imshow(heatmap_o, cmap='hot')

    plt.subplot(224)
    heatmap_text = 'Heatmap - Min confidence: {}, Threshold: {}'.format(
        vd.min_confidence, vd.heat_threshold)
    plt.title(heatmap_text)
    plt.imshow(heatmap, cmap='hot')

    plt.tight_layout()

    plt.savefig(os.path.join(out_dir, img_prefix + '_heatmap.jpg'))

    plt.figure(figsize=(20, 10))

    plt.subplot(221)
    plt.title('Original Image')
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

    plt.subplot(222)
    plt.title(box_text)
    plt.imshow(cv2.cvtColor(box_img, cv2.COLOR_BGR2RGB))

    plt.subplot(223)
    plt.title(heatmap_text)
    plt.imshow(heatmap, cmap='hot')

    labeled_img = draw_bboxes(np.copy(img),
                              bounding_boxes, (250, 150, 55),
                              2,
                              fill=True)

    plt.subplot(224)
    plt.title('Labeled Image')
    plt.imshow(cv2.cvtColor(labeled_img, cv2.COLOR_BGR2RGB))

    plt.tight_layout()
    plt.savefig(os.path.join(out_dir, img_prefix + '_labeled.jpg'))

    return t2 - t1
Exemplo n.º 9
0
    transformer = data_transforms['valid']

    test_image = 'images/ski.jpg'
    bgr_img = cv.imread(test_image)  # B,G,R order
    bgr_img = cv.resize(bgr_img, (400, 300))
    rgb_img = cv.cvtColor(bgr_img, cv.COLOR_BGR2RGB)
    h, w = rgb_img.shape[:2]

    x_test = torch.zeros((3, h, w), dtype=torch.float)
    img = transforms.ToPILImage()(rgb_img)
    img = transformer(img)
    x_test[:, :, :] = img

    with torch.no_grad():
        pred = model([x_test])[0]

    boxes = pred['boxes'].cpu().numpy().tolist()
    labels = pred['labels'].cpu().numpy().tolist()
    scores = pred['scores'].cpu().numpy().tolist()
    keypoints = pred['keypoints'].cpu().numpy().tolist()

    # print('boxes.size(): ' + str(boxes.size()))
    # print('labels.size(): ' + str(labels.size()))
    # print('scores.size(): ' + str(scores.size()))
    # print('keypoints.size(): ' + str(keypoints.size()))

    img = draw_bboxes(bgr_img, boxes, scores, keypoints)
    cv.imshow('image', img)
    cv.waitKey(0)
Exemplo n.º 10
0
# print("\nOutput size here: ", torch.nn.Sequential(*list(model.children())[:6]).to(device)(x).shape)

# yolo = YOLOv3().to(device).eval()
# inp = torch.randn(1, 3, 480, 640).to(device)
# pred = yolo(inp)
# print(pred.shape)
# with torch.no_grad():
#     for _ in trange(100):
#         pred = yolo(inp)

# normalize = tv.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

# root = "data/COCO/val2017/"
# ann_file = "data/COCO/annotations/instances_val2017.json"
# ds = tv.datasets.CocoDetection(root, ann_file)
# img, lbl = ds[101]
# pprint(len(ds))
# pprint(img.size)
# pprint(type(lbl))
# pprint(lbl)

root = "data/COCO/cleaned/val2017/"
coco = COCODataset(root)
for i in np.random.randint(0, len(coco), 25):
    img, (bboxes, cats) = coco[i]
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    img = draw_bboxes(img, bboxes, cats)
    cv2.imshow("Boxes", img)
    cv2.waitKey(0)
cv2.destroyAllWindows()
Exemplo n.º 11
0
def face_attributes():
    start = time.time()
    ensure_folder(STATIC_DIR)
    ensure_folder(UPLOAD_DIR)
    file = request.files['file']
    fn = secure_filename(file.filename)
    full_path = os.path.join(UPLOAD_DIR, fn)
    file.save(full_path)
    # resize(full_path)
    print('full_path: ' + full_path)

    img = Image.open(full_path).convert('RGB')
    bboxes, landmarks = detect_faces(img)

    result = None

    if len(bboxes) > 0:
        i = select_central_face((im_size, im_size), bboxes)
        bbox = bboxes[i]
        img = cv.imread(full_path)
        boxed = draw_bboxes(img, [bbox], [landmarks[i]])
        cv.imwrite(full_path, boxed)
        img = crop_image(img, bbox)
        img = cv.resize(img, (im_size, im_size))
        img = transforms.ToPILImage()(img)
        img = transformer(img)
        img = img.to(device)

        inputs = torch.zeros([1, 3, im_size, im_size], dtype=torch.float)
        inputs[0] = img

        with torch.no_grad():
            reg_out, expression_out, gender_out, glasses_out, race_out = model(
                inputs)

        reg_out = reg_out.cpu().numpy()
        age_out = reg_out[0, 0]
        pitch_out = reg_out[0, 1]
        roll_out = reg_out[0, 2]
        yaw_out = reg_out[0, 3]
        beauty_out = reg_out[0, 4]

        age = int(age_out * 100)
        pitch = float('{0:.2f}'.format(pitch_out * 360 - 180))
        roll = float('{0:.2f}'.format(roll_out * 360 - 180))
        yaw = float('{0:.2f}'.format(yaw_out * 360 - 180))
        beauty = float('{0:.2f}'.format(beauty_out * 100))
        beauty_prob = float('{0:.4f}'.format(get_prob(beauty)))

        _, expression_out = expression_out.topk(1, 1, True, True)
        _, gender_out = gender_out.topk(1, 1, True, True)
        _, glasses_out = glasses_out.topk(1, 1, True, True)
        _, race_out = race_out.topk(1, 1, True, True)
        expression_out = expression_out.cpu().numpy()
        gender_out = gender_out.cpu().numpy()
        glasses_out = glasses_out.cpu().numpy()
        race_out = race_out.cpu().numpy()

        expression = idx2name(int(expression_out[0, 0]), 'expression')
        gender = idx2name(int(gender_out[0, 0]), 'gender')
        glasses = idx2name(int(glasses_out[0, 0]), 'glasses')
        race = idx2name(int(race_out[0, 0]), 'race')

        result = {
            'age': age,
            'pitch': pitch,
            'roll': roll,
            'yaw': yaw,
            'beauty': beauty,
            'beauty_prob': beauty_prob,
            'expression': expression,
            'gender': gender,
            'glasses': glasses,
            'race': race
        }

    elapsed = time.time() - start

    return result, float(elapsed), str(fn)
Exemplo n.º 12
0
def main():
    ''' input '''
    # choose the input stream
    #captureSource = "video/video_116.mp4"
    #captureSource = "video/video_205.mp4"
    captureSource = "video/video_white.mp4"
    #captureSource = 0  # webcam
    cap = cv2.VideoCapture(captureSource)
    ''' trackers typology '''
    # choose the tracker
    trackerName = "CSRT"  # "MOSSE" | "KCF" | "CSRT"
    tm = TrackerManager(trackerName, maxFailures=20)
    ''' parameters '''
    # try to change these parameters
    period = 1  # length of the period: only on the first frame of the period we detect objects (instead, we track them in every frame)
    maintainDetected = True  # True if in transition frames, in case of overlapping bboxes,  we want to keep those of the detector (False if we want to keep those of the tracker)
    frameWidth = 512
    ''' some background subtractor with default params '''
    # bgSubtractor = cv2.bgsegm.createBackgroundSubtractorMOG(history=200, nmixtures=5, backgroundRatio=0.7, noiseSigma=0)
    # bgSubtractor = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=16,	detectShadows=True)
    # bgSubtractor = cv2.bgsegm.createBackgroundSubtractorGMG(initializationFrames=120, decisionThreshold=0.8)
    # bgSubtractor = cv2.createBackgroundSubtractorKNN(history=500, dist2Threshold=400.0, detectShadows=True)
    # bgSubtractor = CompositeBackgroundSubtractor(bgSubtractor1, bgSubtractor2, ...)
    ''' background subtractor '''
    # define a background subtractor to be used for object detection
    #bgSubtractor = cv2.createBackgroundSubtractorMOG2(history=200, varThreshold=25, detectShadows=True)     # good for video/video_116.mp4
    bgSubtractor = cv2.createBackgroundSubtractorKNN(
        history=500, dist2Threshold=500.0,
        detectShadows=True)  # good for video/video_205.mp4
    # bgSubtractor = CompositeBackgroundSubtractor(
    #     cv2.bgsegm.createBackgroundSubtractorMOG(history=600, nmixtures=3, backgroundRatio=0.2, noiseSigma=2.3),
    #     cv2.createBackgroundSubtractorMOG2(history=200, varThreshold=14, detectShadows=True)) # good for video/video_white.mp4
    ''' pipeline '''
    # define the pipeline of functions to be executed on the b/w image, after the background subtraction and before getting bounding rects of contours
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7))
    pipeline = ProcessPipeline()
    pipeline \
        .add(cv2.medianBlur, ksize=5) \
        .add(cv2.dilate, kernel=kernel) \
        .add(cv2.dilate, kernel=kernel) \
        .add(cv2.dilate, kernel=kernel) \
        .add(cv2.dilate, kernel=kernel) \
        .add(cv2.dilate, kernel=kernel) \
        .add(fillHoles) \
        .add(cv2.erode, kernel=kernel) \
        .add(cv2.erode, kernel=kernel) \
    ''' create object detector and face detector '''
    od = ObjectDetector(bgSubtractor, pipeline)
    fd = FaceDetector()
    ''' auto-definition of output folder '''
    outputDir = "output"
    if captureSource == 0:
        outputDir = os.path.join(outputDir, "webcam")
    else:
        outputDir = os.path.join(outputDir,
                                 captureSource[:captureSource.find(".")])
    outputDir = os.path.join(outputDir, trackerName)
    print("Tracking video '%s' with tracker %s" % (captureSource, trackerName))
    ''' cycle begins '''
    frameNumber = 0
    frames = 0
    seconds = 0
    eta = 0.05
    totalTime = 0
    show = True
    oneSkipOnly = False
    while True:
        ''' handle input: esc to quit; space to pause/start; "n" to go one frame at a time '''
        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break
        elif k == ord(' ') or oneSkipOnly:
            show = not show
            oneSkipOnly = False
        elif k == ord('n'):
            show = True
            oneSkipOnly = True
        if not show:
            if oneSkipOnly:
                show = False
            continue

        start = timer()
        ''' reading next frame '''
        ret, frameOrig = cap.read()
        if not ret:
            break
        frameOrig = cv2.flip(frameOrig, 1)
        frame = imutils.resize(frameOrig, width=frameWidth)
        scale = frameOrig.shape[1] / frameWidth

        detectedObjects = []
        if frameNumber % period == 0:
            ''' detection by background subtraction '''
            detectedObjects = od.detect(frame)
            ''' objects tracking, faces detection'''
        ''' tracking '''
        success, objects = tm.update(frame,
                                     detectedObjects,
                                     maintainDetected=maintainDetected)
        objIDs = tm.getIDs()
        tm.removeDeadTrackers()

        failed_objects = [obj for suc, obj in zip(success, objects) if not suc]
        failed_objIDs = [
            objID for suc, objID in zip(success, objIDs) if not suc
        ]
        succ_objIDs = [objID for suc, objID in zip(success, objIDs) if suc]
        objects = [obj for suc, obj in zip(success, objects) if suc]
        ''' detection of faces '''
        faces_bboxes = fd.detectFaces(frameOrig, objects, objIDs, scale=scale)
        ''' images merging and show '''
        frameOrig = draw_bboxes(frameOrig,
                                objects, (255, 0, 0),
                                succ_objIDs,
                                scale=scale)
        frameOrig = draw_bboxes(frameOrig,
                                failed_objects, (0, 0, 255),
                                failed_objIDs,
                                scale=scale)
        frameOrig = draw_bboxes(frameOrig, faces_bboxes, (0, 255, 0))
        frameOrig = cv2.resize(frameOrig, (640, 640))
        cv2.imshow('frame', frameOrig)
        ''' some stats '''
        frameNumber += 1
        end = timer()
        frames = eta + (1 - eta) * frames
        seconds = eta * (end - start) + (1 - eta) * seconds
        print(
            "\rFrame: %04d    FPS: %03d   Active trackers: %02d    Failed trackers: %02d           "
            % (frameNumber, int(
                frames // seconds), len(objects), len(failed_objects)),
            end="")
        totalTime += end - start

    cap.release()
    cv2.destroyAllWindows()
    ''' save on disk '''
    fd.dump(outputDir)

    avgFPS = str(round(frameNumber / totalTime, 2))
    print("\rAverage FPS: " + avgFPS)
    with open(os.path.join(outputDir, "info.txt"), "w") as file:
        bgSubClsName = str(bgSubtractor.__class__)
        bgSubClsName = bgSubClsName[bgSubClsName.index("'") +
                                    1:bgSubClsName.rindex("'")]
        file.write("tracker: " + trackerName + "\n")
        file.write("background subtractor: " + bgSubClsName + "\n")
        file.write("average FPS: " + avgFPS + "\n")
Exemplo n.º 13
0
img = cv2.imread(IMAGE_PATH)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# load detectors.
DET1 = MTCNN(device='cuda')
DET2 = FaceBoxes(device='cuda')
DET3 = TinyFace(device='cuda')
DET4 = PyramidBox(device='cuda')
DET5 = S3FD(device='cuda')
DET6 = DSFD(device='cuda')

# MTCNN returns bboxes and landmarks.
t = time.time()
bboxes, _ = DET1.detect_faces(img, conf_th=0.9, scales=[1])
print('MTCNN : %d faces in %.4f seconds.' % (len(bboxes), time.time() - t))
img1 = draw_bboxes(img, bboxes)
sizes = []
for box in bboxes:
    sizes.append((box[2] - box[0]) * (box[3] - box[1]))
print(min(sizes))
print(max(sizes))

# FaceBoxes returns bboxes.
t = time.time()
bboxes = DET2.detect_faces(img, conf_th=0.9, scales=[1])
print('FaceBoxes : %d faces in %.4f seconds.' % (len(bboxes), time.time() - t))
img2 = draw_bboxes(img, bboxes)
sizes = []
for box in bboxes:
    sizes.append((box[2] - box[0]) * (box[3] - box[1]))
print(min(sizes))
Exemplo n.º 14
0
pngList = glob.glob(imagesPath + '/*.png')
savedList = glob.glob(imagesWithBox + '/*.png')
savedListSet = set()
needHandleList = []

for savedListPath in savedList:
    path = savedListPath.replace(imagesWithBox, '')
    savedListSet.add(path)
for pngListPath in pngList:
    path = pngListPath.replace(imagesPath, '')
    if not savedListSet.__contains__(path):
        needHandleList.append(pngListPath)

print("png num: ", len(pngList), 'handled num: ', len(savedListSet), 'needHandle num: ', len(needHandleList))

for pngPath in needHandleList:
    print("handle", pngPath)
    img = cv2.imread(pngPath)
    img = img[regionY:regionY + regionH, regionX:regionX + regionW]
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    bboxes = tf.detect_faces(img, conf_th=0.9, scales=[1])
    print(bboxes)
    img = draw_bboxes(img, bboxes, thickness=1)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    savePath = pngPath.replace(imagesPath, imagesWithBox)
    print(savePath)
    cv2.imwrite(savePath, img)

print("png num: ", len(pngList), 'handled num: ', len(savedListSet), 'needHandle num: ', len(needHandleList))
 def draw_bboxes(self, query_path, bboxes, output_path):
     query_img = utils.read_image(query_path)
     boxes_img = utils.draw_bboxes(query_img, bboxes)
     cv2.imwrite(output_path, boxes_img)
def main():
    ''' input '''
    # choose the input stream
    #captureSource = 0  # webcam
    #captureSource = 'video/video_116.mp4'
    #captureSource = 'video/video_205.mp4'
    captureSource = 'video/video_white.mp4'
    cap = cv2.VideoCapture(captureSource)
    ''' some background subtractor with default params '''
    # bgSubtractor = cv2.bgsegm.createBackgroundSubtractorMOG(history=200, nmixtures=5, backgroundRatio=0.7, noiseSigma=0)
    # bgSubtractor = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=16,	detectShadows=True)
    # bgSubtractor = cv2.bgsegm.createBackgroundSubtractorGMG(initializationFrames=120, decisionThreshold=0.8)
    # bgSubtractor = cv2.createBackgroundSubtractorKNN(history=500, dist2Threshold=400.0, detectShadows=True)
    # bgSubtractor = CompositeBackgroundSubtractor(bgSubtractor1, bgSubtractor2, ...)
    ''' list of background subtractors '''
    backgroundSubtractors = [
        cv2.createBackgroundSubtractorMOG2(
            history=200, varThreshold=25,
            detectShadows=True),  # good for video/video_116.mp4
        cv2.createBackgroundSubtractorKNN(
            history=500, dist2Threshold=500.0,
            detectShadows=True),  # good for video/video_205.mp4
        CompositeBackgroundSubtractor(
            cv2.bgsegm.createBackgroundSubtractorMOG(history=600,
                                                     nmixtures=3,
                                                     backgroundRatio=0.2,
                                                     noiseSigma=2.3),
            cv2.createBackgroundSubtractorMOG2(
                history=200, varThreshold=14,
                detectShadows=True))  # good for video/video_white.mp4
    ]
    ''' pipeline steps applied after background subtraction '''
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7))
    pipeline = ProcessPipeline()
    pipeline \
        .add(cv2.medianBlur, ksize=5) \
        .add(cv2.dilate, kernel=kernel) \
        .add(cv2.dilate, kernel=kernel) \
        .add(cv2.dilate, kernel=kernel) \
        .add(cv2.dilate, kernel=kernel) \
        .add(cv2.dilate, kernel=kernel) \
        .add(fillHoles) \
        .add(cv2.erode, kernel=kernel) \
        .add(cv2.erode, kernel=kernel)
    ''' detectors creation and beginning of video analysis '''
    detectors = [
        ObjectDetector(bgSub, pipeline) for bgSub in backgroundSubtractors
    ]
    show = True
    oneSkipOnly = False
    while True:
        ''' handle input: esc to quit; space to pause/start; "n" to go one frame at a time '''
        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break
        elif k == ord(' ') or oneSkipOnly:
            show = not show
            oneSkipOnly = False
        elif k == ord('n'):
            show = True
            oneSkipOnly = True
        if not show:
            if oneSkipOnly:
                show = False
            continue
        ''' reading next frame '''
        ret, frame = cap.read()
        if not ret:
            break
        frame = imutils.resize(frame, width=512)
        frame = cv2.flip(frame, 1)
        ''' detection '''
        color = (255, 0, 0)  # blue
        outputFrames = [
            draw_bboxes(frame, detector.detect(frame), color)
            for detector in detectors
        ]
        ''' show frames '''
        imgOutput = mergeImgs(
            [[*detector.pipeline.intermediateOutputsBGR, outputFrame]
             for detector, outputFrame in zip(detectors, outputFrames)])
        imgOutput = cv2.resize(imgOutput, (1920, 1000))
        cv2.imshow('frame', imgOutput)

    cap.release()
    cv2.destroyAllWindows()
Exemplo n.º 17
0

if __name__ == '__main__':
    # construct the argument parse and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image", help="path to the image file")
    ap.add_argument("-c", "--coords",
                    help="comma seperated list of source points")
    args = vars(ap.parse_args())

    # load the image and grab the source coordinates (i.e. the list of
    # of (x, y) points)
    # NOTE: using the 'eval' function is bad form, but for this example
    # let's just roll with it -- in future posts I'll show you how to
    # automatically determine the coordinates without pre-supplying them
    image = cv2.imread(args["image"])
    pts = np.array(eval(args["coords"]), dtype="float32")

    # apply the four point tranform to obtain a "birds eye view" of
    # the image
    warped = four_point_transform(image, pts)

    pts = np.array(pts)
    pts = np.reshape(pts, (4, 1, 2))
    image = draw_bboxes(image, pts)

    # show the original and warped images
    cv2.imshow("Original", image)
    cv2.imshow("Warped", warped)
    cv2.waitKey(0)
Exemplo n.º 18
0
    def process_frame(self, frame, process_pool):

        img = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

        if self.detect_lanes:
            # Uses the undistored image
            img, _, warped_img = self.image_processor.process_image(img)

        bboxes, heatmap, windows = self.vehicle_detector.detect_vehicles(
            img, process_pool=process_pool)

        frame_out = np.copy(img) if self.debug else img
        # Labelled image
        frame_out = draw_bboxes(frame_out,
                                bboxes, (250, 150, 55),
                                1,
                                fill=True)

        frame_out_text = 'Frame Smoothing: {}, Min Confidence: {}, Threshold: {}'.format(
            self.vehicle_detector.smooth_frames,
            self.vehicle_detector.min_confidence,
            self.vehicle_detector.heat_threshold)

        self.write_text(frame_out, frame_out_text)
        self.write_text(frame_out,
                        'Detected Cars: {}'.format(len(bboxes)),
                        pos=(30, frame_out.shape[0] - 30),
                        font_color=(0, 250, 150))

        if self.detect_lanes:
            _, polyfit, curvature, deviation, fail_code = self.lane_detector.detect_lanes(
                warped_img)

            fill_color = (0, 255, 0) if fail_code == 0 else (0, 255, 255)

            lane_img = self.lane_detector.draw_lanes(frame_out,
                                                     polyfit,
                                                     fill_color=fill_color)
            lane_img = self.image_processor.unwarp_image(lane_img)

            frame_out = cv2.addWeighted(frame_out, 1.0, lane_img, 1.0, 0)

            curvature_text = 'Left Curvature: {:.1f}, Right Curvature: {:.1f}'.format(
                curvature[0], curvature[1])
            offset_text = 'Center Offset: {:.2f} m'.format(deviation)

            self.write_text(frame_out, curvature_text, pos=(30, 60))
            self.write_text(frame_out, offset_text, pos=(30, 90))

        frame_out = cv2.cvtColor(frame_out, cv2.COLOR_BGR2RGB)

        if self.debug:

            result = []

            self.write_frame_count(frame_out)
            result.append(frame_out)

            # Unthresholded heatmap image
            heatmap_o = self.vehicle_detector._heatmap(img, windows, 0)
            heatmap_o = self.normalize_heatmap(heatmap_o)
            heatmap_o = np.dstack((heatmap_o, np.zeros_like(heatmap_o),
                                   np.zeros_like(heatmap_o)))
            self.write_frame_count(heatmap_o)

            result.append(heatmap_o)

            # Heatmap image
            heatmap = self.normalize_heatmap(heatmap)
            heatmap = np.dstack(
                (np.zeros_like(heatmap), np.zeros_like(heatmap), heatmap))
            self.write_frame_count(heatmap)

            result.append(cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB))

            heatmap_img = cv2.addWeighted(img, 1, heatmap, 0.8, 0)

            result.append(cv2.cvtColor(heatmap_img, cv2.COLOR_BGR2RGB))

            all_windows = []

            # Windows search image
            for scale, cells_per_step, layer_windows in windows:

                all_windows.extend(layer_windows)

                layer_img = draw_windows(
                    np.copy(img),
                    layer_windows,
                    min_confidence=self.vehicle_detector.min_confidence)

                w_tot = len(layer_windows)
                w_pos = len(
                    list(
                        filter(
                            lambda bbox: bbox[1] >= self.vehicle_detector.
                            min_confidence, layer_windows)))
                w_rej = len(
                    list(
                        filter(
                            lambda bbox: bbox[1] > 0 and bbox[1] < self.
                            vehicle_detector.min_confidence, layer_windows)))

                self.write_text(
                    layer_img,
                    'Scale: {}, Cells per Steps: {}, Min Confidence: {}'.
                    format(scale, cells_per_step,
                           self.vehicle_detector.min_confidence))
                layer_text = 'Windows (Total/Positive/Rejected): {}/{}/{}'.format(
                    w_tot, w_pos, w_rej)
                self.write_text(layer_img,
                                layer_text,
                                pos=(30, layer_img.shape[0] - 30))
                self.write_frame_count(layer_img)

                result.append(cv2.cvtColor(layer_img, cv2.COLOR_BGR2RGB))

            # Combined scales image
            box_img = draw_windows(
                np.copy(img),
                all_windows,
                min_confidence=self.vehicle_detector.min_confidence)

            w_tot = len(all_windows)
            w_pos = len(
                list(
                    filter(
                        lambda bbox: bbox[1] >= self.vehicle_detector.
                        min_confidence, all_windows)))
            w_rej = len(
                list(
                    filter(
                        lambda bbox: bbox[1] > 0 and bbox[1] < self.
                        vehicle_detector.min_confidence, all_windows)))

            self.write_text(
                box_img, 'Min Confidence: {}'.format(
                    self.vehicle_detector.min_confidence))
            box_text = 'Windows (Total/Positive/Rejected): {}/{}/{}'.format(
                w_tot, w_pos, w_rej)
            self.write_text(box_img,
                            box_text,
                            pos=(30, layer_img.shape[0] - 30))
            self.write_frame_count(box_img)

            result.append(cv2.cvtColor(box_img, cv2.COLOR_BGR2RGB))

        else:
            result = frame_out

        return result