def main(filename):

    # Initialise images
    global img_list, ALL_KP, ALL_BB
    img_list, fps = GetImageList(filename, crop=0.6)
    L = len(img_list)
    ALL_KP = [None] * L
    ALL_BB = [None] * L

    # Create TEMP folder
    if not os.path.isdir(TEMP_FOLDER): os.mkdir(TEMP_FOLDER)

    # Analyse images
    t = time.time()

    img_list_chunks = chunks(list(range(L)), L // N_THREADS)

    threads = []
    for i, chunk in enumerate(img_list_chunks):
        threads.append(Thread(target=Process_Img_List, args=(chunk, i)))
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join()

    print("\n[+] Done!")
    print("Time taken: {}s".format(time.time() - t))
    t = time.time()

    # Saving images
    print("\n[*] Saving images")
    count = 0
    for img, kp_list, box_list in zip(img_list, ALL_KP, ALL_BB):
        plot_hand.plot_img(img,
                           kp_list,
                           box_list,
                           save=TEMP_FOLDER + r"/{}.png".format(count))
        count += 1
        print("[*] Saving image {0}/{1}    \r".format(count, L), end="")

    print("\n[+] Done!")
    print("Time taken: {}s".format(time.time() - t))

    # Creating GIF
    print("[*] Saving to GIF")
    images_save = []
    for i in range(L):
        images_save.append(
            Image.fromarray(imageio.imread(TEMP_FOLDER +
                                           r"/{}.png".format(i))))

    gif_save = images_save[0]
    gif_save.info['duration'] = 1. / fps
    gif_save.save('test2.gif',
                  save_all=True,
                  append_images=images_save[1:],
                  loop=0)

    shutil.rmtree(TEMP_FOLDER)
    print("[+] Done!")
Esempio n. 2
0
File: demo.py Progetto: alchu8/ASL
def process():
    """
    Prepares data for CNN training.
    :return: Outputs csv file of bounding box coordinates for each frame.
    """
    import csv
    import plot_hand
    f = open('hands.csv', 'w', newline='')
    csvwriter = csv.writer(f, delimiter=',')

    img_path = "C:/Users/alchu/Pictures/Camera Roll/"  # CHANGE
    imglist = [x for x in os.listdir(img_path) if x.endswith(('.png', '.jpg'))]
    #imglist = ['x_Albert.jpg','x_Albert2.jpg','x_Albert3.jpg']
    #---------------Don't edit from this point on------------------
    for idx, filename in enumerate(imglist):
        img = Image.open(img_path + filename)
        img = np.array(img)
        dims = np.shape(img)
        # Get predictions
        try:
            kp_list, box_list = detector(img)
        except TypeError:
            print(filename)
            continue
        if kp_list[0] is None:
            print(filename + ", keypoints is None.")
            continue
        # Get bounding box
        x = []
        y = []
        for kp in kp_list[0]:  # first set of keypoints
            x.append(kp[0])
            y.append(kp[1])
        minx = max(min(x) - padding, 0)
        miny = max(min(y) - padding, 0)
        maxx = max(x) + padding
        maxy = max(y) + padding
        width = maxx - minx
        height = maxy - miny
        l = max(width, height)  # square bounding box
        minx = max(minx - ((l - width) / 2), 0)
        maxx = minx + l
        assert maxx < dims[1]
        miny = max(miny - ((l - height) / 2), 0)
        maxy = miny + l
        assert maxy < dims[0]
        bbox = []
        bbox.append([minx, miny])
        bbox.append([maxx, miny])
        bbox.append([maxx, maxy])
        bbox.append([minx, maxy])
        bbox = [np.array(bbox), None]
        #print(kp_list)
        #print(bbox)
        #print(np.shape(bbox[0]))
        csvwriter.writerow([filename, bbox[0]])
        # Plot predictions
        try:
            plot_hand.plot_img(img, kp_list, bbox, save=None)
        except TypeError:  # multiple sets of keypoints bug out the plot function
            print(filename + " has multiple sets of keypoints.")
            continue
    f.close()