Example #1
0
def predict_time(img, THRESHOLD=0.2):
    boxes, probs = yolo_detector.predict(img, THRESHOLD)

    image, scaled_boxes = draw_scaled_boxes(
        img, boxes, probs,
        ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":"])

    detected = []
    for prob in probs:
        detected.append(classes[np.argmax(prob)])

    final = []
    for i in range(len(scaled_boxes)):
        dict = {}
        dict['x'] = scaled_boxes[i][0]
        dict['w'] = scaled_boxes[i][1]
        dict['y'] = scaled_boxes[i][2]
        dict['h'] = scaled_boxes[i][3]
        dict['class'] = detected[i]

        final.append(dict)

    final = sorted(final, key=lambda x: x['x'])
    detected = [sub['class'] for sub in final]
    detected = ''.join(detected)
    #print('Detected: ', detected)

    return detected
def get_digits_boxes_img(image):
    im_size = np.shape(image)
    boxes, probs = yolo.predict(image, float(threshold))
    labels = np.argmax(probs, axis=1) if len(probs) > 0 else []
    probs = np.max(probs, axis=1) if len(probs) > 0 else []

    # 4. save detection result
    image = draw_scaled_boxes(image, boxes, probs, config['model']['labels'])

    centers = []

    for count in range(len(boxes)):

        box = boxes[count]

        x = (((box[0] + box[2] / 2) / im_size[0]))
        y = (((box[1] + box[3] / 2) / im_size[1]))
        centers.append([x, y])
    centers = np.array(centers)
    jersey_numbers = []

    if len(centers) > 1:
        cluster.fit_predict(centers)
        cluster_labels = cluster.labels_
        clusters = np.unique(cluster_labels)

        conf_score = []
        for c_id in clusters:
            g_centers = centers[list(cluster_labels == c_id)]
            g_labels = labels[list(cluster_labels == c_id)]
            g_probs = probs[list(cluster_labels == c_id)]
            center_x = [center[0] for center in g_centers]

            g_labels = g_labels[np.argsort(center_x)]
            number = int((''.join(str(label) for label in g_labels)))
            jersey_numbers.append(number)
            conf_score.append(np.mean(g_probs))
    else:
        jersey_numbers = list(labels)
        conf_score = [np.mean(probs)]


#     print("{}-boxes are detected".format(len(boxes)))
#     print("jersey numbers detected are {}".format(jersey_numbers))

    return jersey_numbers, conf_score
Example #3
0
        config['model']['labels'],
        is_only_detect=config['train']['is_only_detect'])

    n_true_positives = 0
    n_truth = 0
    n_pred = 0
    for i in range(len(annotations)):
        img_path = annotations.fname(i)
        img_fname = os.path.basename(img_path)
        image = cv2.imread(img_path)
        true_boxes = annotations.boxes(i)
        true_labels = annotations.code_labels(i)

        boxes, probs = yolo.predict(image, float(args.threshold))
        labels = np.argmax(probs, axis=1) if len(probs) > 0 else []

        # 4. save detection result
        image = draw_scaled_boxes(image, boxes, probs,
                                  config['model']['labels'])
        output_path = os.path.join(write_dname, os.path.split(img_fname)[-1])

        cv2.imwrite(output_path, image)
        print("{}-boxes are detected. {} saved.".format(
            len(boxes), output_path))

        n_true_positives += count_true_positives(boxes, true_boxes, labels,
                                                 true_labels)
        n_truth += len(true_boxes)
        n_pred += len(boxes)
    print(calc_score(n_true_positives, n_truth, n_pred))
Example #4
0
    # match boxes coordinate to original image
    if crop_imgs.index(best_crop_img) == 0:
        pass
    elif crop_imgs.index(best_crop_img) == 1:
        best_boxes[:, 0] = best_boxes[:, 0] + int(img.shape[1] * 0.2)
        best_boxes[:, 2] = best_boxes[:, 2] + int(img.shape[1] * 0.2)
    elif crop_imgs.index(best_crop_img) == 2:
        best_boxes[:, 0] = best_boxes[:, 0] + int(img.shape[1] * 0.2)
        best_boxes[:, 1] = best_boxes[:, 1] + int(img.shape[0] * 0.15)
        best_boxes[:, 2] = best_boxes[:, 2] + int(img.shape[1] * 0.2)
        best_boxes[:, 3] = best_boxes[:, 3] + int(img.shape[0] * 0.15)

    # detection result
    image = draw_scaled_boxes(
        img, best_boxes, best_probs,
        ["10", "1", "2", "3", "4", "5", "6", "7", "8", "9"])
    print("{}-boxes are detected.".format(len(best_boxes)))
    # plt.imshow(image)
    # plt.show()

    # append to result list
    result_dict = {"bbox": [], "label": [], "score": []}
    try:
        result_dict["bbox"] = bbox_format(best_boxes).tolist()
        result_dict["label"] = np.argmax(np.array(best_probs), axis=1).tolist()
        result_dict["score"] = np.amax(np.array(best_probs), axis=1).tolist()
    except:
        pass
    result_list.append(result_dict)
    print(result_dict)
Example #5
0
                print("pause playback")
                time.sleep(0.5)
        postion = []


cap = cv2.VideoCapture(0)
Connect_to_token()
creat_list()

while True:
    _, frame = cap.read()
    #startar detektering
    boxes, probs = yolo.predict(frame, config["model"]["DEFAULT_THRESHOLD"])
    labels = np.argmax(probs, axis=1) if len(probs) > 0 else []
    #sparar detekterings resultat
    frame = draw_scaled_boxes(frame, boxes, probs, config['model']['labels'])
    label_list = config['model']['labels']
    cv2.imshow("imageo", frame)
    cv2.waitKey(1)

    if 0 in labels:  #om en tummeup hittas
        try:
            Add_to_list()
        except:
            input(
                " No active device found ... press ENTER when you are back on track"
            )

    if 1 in labels:  #om en hand hittas
        try:
            Change_song_puse_play()
def get_digits_boxes(img, show_img=False, save_img=False):
    img_path = img_path.replace('\\', '/')
    #    img_path = img_path.replace('\','/')
    image = cv2.imread(img_path)
    im_size = np.shape(image)
    boxes, probs = yolo.predict(image, float(threshold))
    labels = np.argmax(probs, axis=1) if len(probs) > 0 else []
    probs = np.max(probs, axis=1) if len(probs) > 0 else []

    # 4. save detection result
    image = draw_scaled_boxes(image, boxes, probs, config['model']['labels'])

    centers = []

    for count in range(len(boxes)):

        box = boxes[count]

        x = (((box[0] + box[2] / 2) / im_size[0]))
        y = (((box[1] + box[3] / 2) / im_size[1]))
        centers.append([x, y])
    centers = np.array(centers)
    jersey_numbers = []

    if len(centers) > 1:
        cluster.fit_predict(centers)
        cluster_labels = cluster.labels_
        clusters = np.unique(cluster_labels)

        conf_score = []
        for c_id in clusters:
            g_centers = centers[list(cluster_labels == c_id)]
            g_labels = labels[list(cluster_labels == c_id)]
            g_probs = probs[list(cluster_labels == c_id)]
            center_x = [center[0] for center in g_centers]

            g_labels = g_labels[np.argsort(center_x)]
            number = int((''.join(str(label) for label in g_labels)))
            jersey_numbers.append(number)
            conf_score.append(np.mean(g_probs))
    else:
        jersey_numbers = labels
        conf_score = np.mean(probs)
    numbers_string = ''
    for number in jersey_numbers:
        numbers_string = numbers_string + str(number) + ' '

    cv2.putText(image, 'Jersey Number: %s' % numbers_string, (25, 20),
                cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)

    #    labels=labels[np.argsort(center_x)]
    #    number = int((''.join(str(label) for label in labels)))
    if save_img:

        output_path = os.path.join(
            ntpath.dirname(img_path) + '/' +
            (img_path.split("/")[-1]).split('.')[0] + '_out.jpg')
        cv2.imwrite(output_path, image)
    if show_img:
        cv2.imshow("jersey number detected image", image)
        cv2.waitKey(5000)
        cv2.destroyAllWindows()
    print("{}-boxes are detected".format(len(boxes)))
    print("jersey numbers detected are {}".format(jersey_numbers))

    return jersey_numbers, conf_score, labels, boxes
Example #7
0
    annotations = parse_annotation(config['train']['valid_annot_folder'],
                                   config['train']['valid_image_folder'],
                                   config['model']['labels'],
                                   is_only_detect=config['train']['is_only_detect'])

    n_true_positives = 0
    n_truth = 0
    n_pred = 0
    for i in range(len(annotations)):
        img_path = annotations.fname(i)
        img_fname = os.path.basename(img_path)
        image = cv2.imread(img_path)
        true_boxes = annotations.boxes(i)
        true_labels = annotations.code_labels(i)
        
        boxes, probs = yolo.predict(image, float(args.threshold))
        labels = np.argmax(probs, axis=1) if len(probs) > 0 else [] 
      
        # 4. save detection result
        image = draw_scaled_boxes(image, boxes, probs, config['model']['labels'])
        output_path = os.path.join(write_dname, os.path.split(img_fname)[-1])
        
        cv2.imwrite(output_path, image)
        print("{}-boxes are detected. {} saved.".format(len(boxes), output_path))

        n_true_positives += count_true_positives(boxes, true_boxes, labels, true_labels)
        n_truth += len(true_boxes)
        n_pred += len(boxes)
    print(calc_score(n_true_positives, n_truth, n_pred))

Example #8
0
    #n_truth = 0
    #n_pred = 0
    #for i in range(len(annotations)):
    for filename in os.listdir(args.path):
        #img_path = annotations.fname(i)
        img_path = os.path.join(args.path, filename)
        #img_fname = os.path.basename(img_path)
        img_fname = filename
        image = cv2.imread(img_path)
        #true_boxes = annotations.boxes(i)
        #true_labels = annotations.code_labels(i)

        boxes, probs = yolo.predict(image, float(args.threshold))

        # 4. save detection result
        image = draw_scaled_boxes(image, boxes, probs, labels)
        output_path = os.path.join(write_dname, os.path.split(img_fname)[-1])
        label_list = config['model']['labels']
        right_label = np.argmax(probs, axis=1) if len(probs) > 0 else []
        #cv2.imwrite(output_path, image)
        print("{}-boxes are detected. {} saved.".format(
            len(boxes), output_path))
        if len(probs) > 0:
            #create_ann(filename,image,boxes,right_label,label_list)
            cv2.imwrite(output_path, image)

        #n_true_positives += count_true_positives(boxes, true_boxes, labels, true_labels)
        #n_truth += len(true_boxes)
        #n_pred += len(boxes)
    #print(calc_score(n_true_positives, n_truth, n_pred))
lst.sort()
json_dic = []
for i in range(13068):
    
    file_name = str(i+1) + ".png"
    print(file_name)
    image_path = os.path.join(images_dir, file_name)
    img = cv2.imread(image_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)    

    boxes, probs, confidences  = yolo_detector.predict(img, THRESHOLD)

    #     4. save detection result
    image, dic = draw_scaled_boxes(img,
                              boxes,
                              probs,
                              ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
                             confidences)

    print("{}-boxes are detected.".format(len(boxes)))
    print(dic)
    json_dic.append(dic)
#     plt.imshow(image)
#     plt.show()
    
# data = json.dumps(json_dic)
with open('data.json', 'w') as fp:
    json.dump(json_dic, fp)


# %%