Example #1
0
def main():

    saved_pic_num=0

    palm_detector,hand_regressor = initialise_models()

    frame_ct,hasFrame,frame,WINDOW,capture = initialise_opencvstream()
    
    x_coordinates=[]
    y_coordinates=[]
    
    while hasFrame:
        frame_ct +=1
        
        palm_detections,frame=generate_detections(frame,palm_detector)


        flags2,landmarks2,box2 = get_keypoints_roi(palm_detector,palm_detections,hand_regressor,frame)
        
        curr_point_x,curr_point_y = get_and_store_curr_point(x_coordinates,y_coordinates,flags2,landmarks2)
        
        draw_roi(frame, box2)

        draw_detections(frame, palm_detections)

        #initialise blank image to be stored in memory upon clicking x
        blank_image = 255 * np.ones(shape=[512, 512, 3], dtype=np.uint8)
        
        draw_live_line(x_coordinates,y_coordinates,curr_point_x,curr_point_y,frame,blank_image,WINDOW)
        

        #Few functionalities which will be triggered upon pressing specific keys
        
        #1.clear out screen
        if cv2.waitKey(33) == ord('c'):
            x_coordinates=[]
            y_coordinates=[]

        #2.smoothen the curvy line using moving average
        if cv2.waitKey(33) == ord('s'):
            x_coordinates,y_coordinates=smoothening(x_coordinates,y_coordinates,frame,blank_image)

        #save the drawn figure to an image
        if cv2.waitKey(33) == ord('x'):
            cv2.imwrite(str(saved_pic_num)+'.jpg', blank_image)
            df=pd.DataFrame()
            df['x-coordinates']=x_coordinates
            df['y-coordinates']=y_coordinates
            df.to_csv('shape'+str(saved_pic_num)+'.csv')
            saved_pic_num=saved_pic_num+1
        
        
        hasFrame, frame = capture.read()
        key = cv2.waitKey(1)
        if key == 27:
            break

    capture.release()
    cv2.destroyAllWindows()
Example #2
0
    face_detections = denormalize_detections(normalized_face_detections, scale,
                                             pad)

    xc, yc, scale, theta = face_detector.detection2roi(face_detections.cpu())
    img, affine, box = face_regressor.extract_roi(frame, xc, yc, theta, scale)
    flags, normalized_landmarks = face_regressor(img.to(gpu))
    landmarks = face_regressor.denormalize_landmarks(
        normalized_landmarks.cpu(), affine)

    #frame = cv2.rectangle(frame, (0,0), (400,400), (120, 120, 120), 400)

    for i in range(len(flags)):
        landmark, flag = landmarks[i], flags[i]
        if flag > .5:
            draw_landmarks(frame, landmark[:, :2], FACE_CONNECTIONS, size=1)

    draw_roi(frame, box)
    draw_detections(frame, face_detections)

    cv2.imshow(WINDOW, frame[:, :, :])
    # cv2.imwrite('sample/%04d.jpg'%frame_ct, frame[:,:,::-1])

    hasFrame, frame = capture.read()
    key = cv2.waitKey(1)
    if key == 27:
        break

capture.release()
cv2.destroyAllWindows()
Example #3
0
    frame = np.ascontiguousarray(frame[:, ::-1, ::-1])

    img1, img2, scale, pad = resize_pad(frame)

    normalized_pose_detections = pose_detector.predict_on_image(img2)
    pose_detections = denormalize_detections(normalized_pose_detections, scale,
                                             pad)

    xc, yc, scale, theta = pose_detector.detection2roi(pose_detections)
    img, affine, box = pose_regressor.extract_roi(frame, xc, yc, theta, scale)
    flags, normalized_landmarks, mask = pose_regressor(img.to(gpu))
    landmarks = pose_regressor.denormalize_landmarks(normalized_landmarks,
                                                     affine)

    draw_detections(frame, pose_detections)
    draw_roi(frame, box)

    for i in range(len(flags)):
        landmark, flag = landmarks[i], flags[i]
        if flag > .5:
            draw_landmarks(frame, landmark, POSE_CONNECTIONS, size=2)

    cv2.imshow(WINDOW, frame[:, :, ::-1])
    # cv2.imwrite('sample/%04d.jpg'%frame_ct, frame[:,:,::-1])

    hasFrame, frame = capture.read()
    key = cv2.waitKey(1)
    if key == 27:
        break
Example #4
0
def _get_detections(generator,
                    model,
                    score_threshold=0.05,
                    max_detections=100,
                    visualize=False):
    """
    Get the detections from the model using the generator.

    The result is a list of lists such that the size is:
        all_detections[num_images][num_classes] = detections[num_class_detections, 5]

    Args:
        generator: The generator used to run images through the model.
        model: The model to run on the images.
        score_threshold: The score confidence threshold to use.
        max_detections: The maximum number of detections to use per image.
        save_path: The path to save the images with visualized detections to.

    Returns:
        A list of lists containing the detections for each image in the generator.

    """
    all_detections = [[
        None for i in range(generator.num_classes()) if generator.has_label(i)
    ] for j in range(generator.size())]

    for i in progressbar.progressbar(range(generator.size()),
                                     prefix='Running network: '):
        image = generator.load_image(i)
        src_image = image.copy()
        h, w = image.shape[:2]

        anchors = generator.anchors
        image, scale = generator.preprocess_image(image)
        # run network
        # boxes, scores, *_, labels = model.predict_on_batch([np.expand_dims(image, axis=0)])
        boxes, scores, labels, pro_id = model.predict_on_batch([
            np.expand_dims(image, axis=0),  # (1, 896, 896, 3)
            np.expand_dims(anchors, axis=0)
        ])  # (1, 150381, 4)
        boxes /= scale
        boxes[:, :, 0] = np.clip(boxes[:, :, 0], 0, w - 1)
        boxes[:, :, 1] = np.clip(boxes[:, :, 1], 0, h - 1)
        boxes[:, :, 2] = np.clip(boxes[:, :, 2], 0, w - 1)
        boxes[:, :, 3] = np.clip(boxes[:, :, 3], 0, h - 1)

        # select indices which have a score above the threshold
        indices = np.where(scores[0, :] > score_threshold)[0]

        # select those scores
        scores = scores[0][indices]

        # find the order with which to sort the scores
        scores_sort = np.argsort(-scores)[:max_detections]

        # select detections
        # (n, 4)
        image_boxes = boxes[0, indices[scores_sort], :]
        # (n, )
        image_scores = scores[scores_sort]
        # (n, )
        image_labels = labels[0, indices[scores_sort]]
        # (n, )
        image_pro = pro_id[0, indices[scores_sort]]
        # (n, 6)
        detections = np.concatenate([
            image_boxes,
            np.expand_dims(image_scores, axis=1),
            np.expand_dims(image_labels, axis=1)
        ],
                                    axis=1)

        if visualize:
            draw_annotations(src_image,
                             generator.load_annotations(i),
                             label_to_name=generator.label_to_name)
            draw_detections(src_image,
                            detections[:5, :4],
                            detections[:5, 4],
                            detections[:5, 5].astype(np.int32),
                            colors=colors,
                            label_to_name=generator.label_to_name,
                            score_threshold=score_threshold)

            cv2.imwrite(os.path.join('save_img', '{}.png'.format(i)),
                        src_image)
            # cv2.namedWindow('{}'.format(i), cv2.WINDOW_NORMAL)
            # cv2.imshow('{}'.format(i), src_image)
            # cv2.waitKey(0)

        # copy detections to all_detections
        for class_id in range(generator.num_classes()):
            all_detections[i][class_id] = detections[detections[:, -1] ==
                                                     class_id, :-1]

    return all_detections