Skip to content

call photo_recognition after audio_recognition raise _path_isfile error #13

Description

@Sgiostra-BitBang

Error

_path_isfile: path should be string, bytes, os.PathLike or integer, not bytearray

Example

Using the code below, if call from the main photo_recognition, all goes fine.
If I call photo_recognition after audio_recognition (as in the main), i got this error:
_path_isfile: path should be string, bytes, os.PathLike or integer, not bytearray

Code

from MyLibs.logger import get_logger

logger = get_logger("stefano")

import asyncio
from pathlib import Path

from frame_sdk.frame import Frame
from frame_sdk.camera import Quality, AutofocusType
from frame_sdk.display import PaletteColors, Alignment
from BFLibs.brilliant_frame_wrapper import AUDIO_FILE_PATH, IMAGE_FILE_PATH, run_transcription
# from agent_flow.flow import create_agent_flow
# from agent_flow.nodes import Keys
# run_transcription use speech_recognition - recognize_google
AUDIO_FILE_PATH = './generated_files/recorded_audio.wav'
IMAGE_FILE_PATH = './generated_files/captured_photo.jpg'

async def audio_recognition(f):
    transcribed_text = None
    try:
        await f.bluetooth.send_break_signal()
        await f.display.show_text(f"Fai tap per registrare -->", align=Alignment.MIDDLE_CENTER)
        await f.motion.wait_for_tap()
        await f.display.show_text("In ascolto...", align=Alignment.MIDDLE_CENTER)
        await f.microphone.save_audio_file(AUDIO_FILE_PATH, silence_cutoff_length_in_seconds=3)
        await f.display.show_text("Riconoscimento testo...", align=Alignment.MIDDLE_CENTER)
        transcribed_text = run_transcription(AUDIO_FILE_PATH)
    except Exception as e:
        print(f"{str(e)}")
        await f.bluetooth.send_break_signal()
        await f.bluetooth.send_reset_signal()

    return transcribed_text


async def photo_recognition(f, file_name=None):
    recognized_image = None
    try:
        await f.bluetooth.send_break_signal()
        # await f.display.show_text(f"Fai tap per scattare -->", align=Alignment.MIDDLE_CENTER)
        # await f.motion.wait_for_tap()
        await f.display.show_text("Acquisizione immagine, non muoverti...",
                                  align=Alignment.MIDDLE_CENTER,
                                  color=PaletteColors.YELLOW)
        await asyncio.sleep(1)
        photo = await f.camera.take_photo(quality=Quality.VERY_HIGH)
        if file_name:
            image_file_path = Path(IMAGE_FILE_PATH).parent / file_name
        else:
            image_file_path = Path(IMAGE_FILE_PATH)
        with open(image_file_path, "wb") as fw:
            fw.write(photo)
        # await f.camera.save_photo(IMAGE_FILE_PATH,
        #                           autofocus_seconds=3,
        #                           quality=Quality.VERY_HIGH,
        #                           autofocus_type=AutofocusType.CENTER_WEIGHTED)
        # await f.display.show_text("Ci siamo quasi...", align=Alignment.MIDDLE_CENTER, color=PaletteColors.ORANGE)
        # recognized_image = yolo_inference(IMG_MODEL_PATH, IMAGE_FILE_PATH)
        # await asyncio.sleep(2)
        # print(f'Riconoscimento immagine: {recognized_image}')
        await f.display.show_text(f"Acquisizione immagine completata",
                                  align=Alignment.MIDDLE_CENTER,
                                  color=PaletteColors.GREEN)
        print(image_file_path)
        await asyncio.sleep(2)
        await f.display.show()
    except Exception as e:
        print(f"{str(e)}")
        await f.bluetooth.send_break_signal()
        await f.bluetooth.send_reset_signal()
    return recognized_image


async def main():
    # my_flow = create_agent_flow()
    # shared = {}
    async with Frame() as f:
        for i in range(60):
            if f.bluetooth.is_connected():
               break
            print("Attendo un secondo ...")
            await asyncio.sleep(1)
        if not f.bluetooth.is_connected():
            print(f"Frame NOT Connected")
            return

        try:
            message = "Frame Connected"
            print(message)
            await f.display.show_text(message, align=Alignment.MIDDLE_CENTER)
            await asyncio.sleep(2)
            # let's get the current battery level
            message = f"Frame battery: {await f.get_battery_level()}%"
            print(message)
            await f.display.show_text(message, align=Alignment.MIDDLE_CENTER, color=PaletteColors.ORANGE)
            await asyncio.sleep(2)

            transcribed_text = await audio_recognition(f)
            print(transcribed_text)
            await f.display.show_text("Completato", align=Alignment.MIDDLE_CENTER, color=PaletteColors.ORANGE)
            await f.bluetooth.send_break_signal()
            await asyncio.sleep(2)

            # await f.display.show()
            if True:
            # if message.lower() in ("scatta una foto", "fai una foto"):
                ret = await photo_recognition(f)
                print(ret)

        except Exception as e:
            print(f"{str(e)}")
            return
        finally:
            if f:
                if f.bluetooth.is_connected():
                    await f.bluetooth.disconnect()


async def reset():
    async with (Frame() as f):
        if f.bluetooth.is_connected():
            await f.bluetooth.send_break_signal()
            await f.bluetooth.send_reset_signal()
            await f.bluetooth.disconnect()


async def take_a_photo(image_name):
    async with Frame() as f:
        ret = await photo_recognition(f, image_name)
        print(ret)

        # await f.display.show_text("Wait....", align=Alignment.MIDDLE_CENTER, color=PaletteColors.YELLOW)
        # photo = await f.camera.take_photo(quality=Quality.VERY_HIGH)
        # with open(IMAGE_FILE_PATH, "wb") as fw:
        #     fw.write(photo)
        # await f.display.show_text("Done!")
        if f.bluetooth.is_connected():
            await f.bluetooth.send_reset_signal()
            await f.bluetooth.disconnect()


def yolo():
    from BFLibs.image_inference_with_custom_model import yolo_inference
    from BFLibs.brilliant_frame_wrapper import IMG_MODEL_PATH

    for image in ("./generated_files/robot rosso.jpg", "./generated_files/robot verde.jpg"):
        recognized_image = yolo_inference(IMG_MODEL_PATH, "./generated_files/robot rosso.jpg")
        print(image, recognized_image)


if __name__ == "__main__":
    # asyncio.run(take_a_photo("stefano2.jpg"))
    asyncio.run(main())
    # asyncio.run(reset())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions