# main run loop
while (timeNow < runTime):

    if (picTimer >= picPeriod) or (timeNow == 0.0):
        # capture photo and name it "year_month_day__hour_min_sec"
        img_name.append(str(time.strftime("%Y_%m_%d__%H_%M_%S")) + '.jpg')
        camera.capture(img_name[-1])

        # reset picTimer by updating lastPic time
        lastPic = time.time()

        # write to file. use type 'a' to append or 'w' to overwrite
        with open(file_name, "a") as file:
            file.write((img_name[-1] + '\n'))

        # upload the image that was just taken to dropbox
        db.upload(dbx, img_name[-1], '', '', img_name[-1])

        # upload file to drop box and overwrite previous inventory count since the program is appending to the file
        db.upload(dbx, file_name, '', '', file_name, True)

        # delete image from local directory as to not fill up local hard drive
        os.remove(img_name[-1])

    # calculate time taken to capture image and upload
    picTimer = time.time() - lastPic
    timeLast = timeNow
    timeNow = time.time() - startTime
    dt = timeNow - timeLast
    freq = 1 / dt
        # img4 = cv2.drawMatchesKnn(gray_crop,kp,sample,sample_kp,matches,None,**draw_params)

    ############################################################################################
    # Display Image

    # show the edges only
    cv2.imshow('image1', img2)  # second image with item(s) missing

    for i in range(len(cnts_filt_indx)):
        # cv2.imshow(str(1+3*(i)),crop_img[i])  	# cropped item (indexed)
        # cv2.imshow(str(2+3*(i)),img3[i])		# cropped item with features highlighted (indexed)
        cv2.imshow(
            str(3 + 3 * (i)), img4[i]
        )  # feature matching between item and template from database (indexed)

    # keep image displayed for certain number of milliseconds. If 0 then wait for keystroke
    key = cv2.waitKey(disp_time)

    ############################################################################################
    # Dropbox

    # upload file to drop box and overwrite previous inventory count since the program is appending to the file
    db.upload(dbx, file_name, '', '', file_name, True)

    # calculate loop time and refresh timeNow
    timeLast = timeNow
    timeNow = time.time() - startTime
    dt = timeNow - timeLast
    freq = 1 / dt