示例#1
0
def get_document_bounds(videoIO, feature, folderPath, youtube_id):

    client = MongoClient(
        'mongodb+srv://qhacks:[email protected]/test?retryWrites=true'
    )
    db = client['qhacks']

    global notes_screenshots
    global glob_data
    global length
    global prev_file
    global prev_time
    global secs
    global temp
    """Returns document bounds given an image."""

    # setting up frame by frame per 5 secs
    myclip = VideoFileClip(videoIO)
    frames = []

    for frame in myclip.iter_frames(fps=0.2):
        frames.append(frame)
        # print("hi")

    for count, single_frame in enumerate(frames, start=1):
        # print("stephen")
        #print(i)
        img = Image.fromarray(single_frame, 'RGB')

        dir_path = os.path.dirname(os.path.realpath(__file__))

        #print(dir_path)

        file = "/file%d.png" % count

        #print(file)

        #print(folderPath)

        filename = dir_path + "/" + folderPath + "/" + file

        #print(filename)

        img.save(filename)
        #img.show()

        # if length is 5:
        #     break
        # img_process = img.tobytes()

        #runnin the image processor
        first = True
        build_word = ""
        words = []
        # words.append("a")

        client = vision.ImageAnnotatorClient()

        bounds = []

        temp = filename

        #CHANGED HERE
        # content = img_process

        # content = Image.open(filename)
        with io.open(filename, 'rb') as image_file:
            content = image_file.read()

        # print(content)

        image = types.Image(content=content)

        response = client.document_text_detection(image=image)
        document = response.full_text_annotation

        # Collect specified feature bounds by enumerating all document features
        for page in document.pages:
            for block in page.blocks:
                for paragraph in block.paragraphs:

                    for word in paragraph.words:

                        # for symbol in word.symbols:
                        #if (feature == FeatureType.SYMBOL):
                        #bounds.append(symbol.bounding_box)

                        #if (feature == FeatureType.WORD):

                        if first and feature == FeatureType.WORD:
                            bounds.append(word.bounding_box)
                            first = False

                        for i in word.symbols:
                            if hasattr(i, "text"):
                                if i.property.detected_break.type is 0:
                                    build_word += i.text
                                else:
                                    build_word += i.text
                                    # print(build_word)
                                    words.append(build_word)
                                    build_word = ""

                    #if (feature == FeatureType.PARA):
                    #bounds.append(paragraph.bounding_box)

                #if (feature == FeatureType.BLOCK):
                #bounds.append(block.bounding_box)

            #if (feature == FeatureType.PAGE):
            #bounds.append(block.bounding_box)

        # The list `bounds` contains the coordinates of the bounding boxes.

        # temp = {
        #     "bound": bounds
        # }

        # bound_data = {
        #     "v0x": temp['bound'][0].vertices[0].x,
        #     "v0y": temp['bound'][0].vertices[0].y,
        #     "v1x": temp['bound'][0].vertices[1].x,
        #     "v1y": temp['bound'][0].vertices[1].y,
        #     "v2x": temp['bound'][0].vertices[2].x,
        #     "v2y": temp['bound'][0].vertices[2].y,
        #     "v3x": temp['bound'][0].vertices[3].x,
        #     "v3y": temp['bound'][0].vertices[3].y,
        # }

        collection_texts = db['timestamps']

        for i in words:
            db_data = {"secs": secs, "keyword": i, "youtube_id": "tasty"}
            collection_texts.insert_one(db_data)

        # db_data = {
        #     "secs": secs,
        #     "keyword": words,
        #     "youtube_id": "hello"
        # }

        data = {secs: words}

        if (len(data[secs]) == 0):
            data = {secs: "a"}

        #print(data)

        # print(data['bound'])

        # print(data['bound'])
        # print(type(data['bound'][0]))
        # print(type(data['bound'][0].vertices))
        # print(type(data['bound'][0].vertices[0].x))

        glob_data.append(data)
        length += 1

        if length > 1:
            if glob_data[length - 1][secs][0] and glob_data[length -
                                                            2][prev_time][0]:
                if glob_data[length -
                             1][secs][0] == glob_data[length -
                                                      2][prev_time][0]:
                    prev_file = temp
                    prev_time = secs
                else:
                    screenshot_data = {
                        "secs": secs,
                        "file": prev_file,
                        "youtube_id": "tasty"
                    }
                    notes_screenshots.append(screenshot_data)
                    prev_file = temp
                    prev_time = secs  #HERE BABY
                    # imagerino = Image.open(prev_file)
                    # imagerino.show()
        secs += 5

    # print(glob_data)
    # print("STEPHENNNNNN")
    # print(screenshot_data)

    collection_screenshots = db['screenshots']
    collection_screenshots.insert_many(notes_screenshots)