# fly-ears: a real fruit fly brain watches your videos and hears your music **Project page: ** [![Walkthrough: a real fly brain watches a video (80 s, click to play)](docs/walkthrough.jpg)](docs/walkthrough.mp4) **[Watch the 80-second walkthrough](docs/walkthrough.mp4)**: give it a video, light enters its eyes, its own wiring sees an approaching disc coming, brain, body and emotions, every other sense, the music test, and how every run is checked. ![The fly watching the test video: a dark disc approaches, its looming neurons (LPLC2) fill, the brain map lights up](docs/demo.gif) | the Watch page | the Brain & Body viewer | |---|---| | ![Watch page: paste a link or pick a file, press Watch; the job log on the right](docs/watch.png) | ![Viewer: the video, the brain map, a 3D fly posed by its motor neurons, emotions](docs/viewer.jpg) | | **Inside the brain:** the retina, both eyes' motion detectors at their places in the eye | | | ![Inside the brain: retina, left and right eye T4/T5 maps, ears](docs/inside.jpg) | | A video or a song goes into the eyes and ears of the complete fruit fly connectome (MaleCNS: 166,700 neurons, 25.6 M connections from an electron-microscope map, simulated by `flybrain` from [fly.ai](https://github.com/alextitonis/fly.ai)), frame by frame, in the video's own time. Everything after the receptors is the fly's own wiring: nothing is trained, nothing is drawn in. * **Watch:** paste a link or pick a file. The light reaches 6,006 photoreceptors at their real eye columns, the soundtrack reaches the ears, and you get the video beside the brain: every neuron that fires, both eyes' motion detectors, the escape neuron, a 3D fly posed by its own motor neurons, and readouts of fear, appetite, compass heading and more. * **Listen:** a test, decided before running, of whether the fly brain's response tells human-made music from AI-generated music, beyond what its ears received and beyond a scrambled brain. * **Honest by design:** every watched video is re-simulated and checked step for step (`audit.py`); what is measured, what is modelled and what is only drawn is said on every page; the failures are written down below (the fly's motion detectors are far less direction-selective than a real fly's). ## Setup Needs Python 3.10+, `ffmpeg` on the PATH, and an NVIDIA GPU for real time (a CPU works, slowly). ```bash # 1. the brain: the flybrain code, next to this folder, named firefly git clone https://github.com/arpanguria68-ui/flybrain-nes firefly git clone https://github.com/arpanguria68-ui/fly-ears cd firefly python -m venv .venv .venv/Scripts/pip install -r requirements.txt # Linux/macOS: .venv/bin/pip .venv/Scripts/pip install -r requirements-gpu.txt # NVIDIA GPU (CuPy) .venv/Scripts/python -m flybrain download # the prebuilt brain, ~260 MB, into ~/fly-data cd ../fly-ears # 2. try it: make the test video and let the fly watch it (about 10 s) ../firefly/.venv/Scripts/python make_test_video.py ../firefly/.venv/Scripts/python watch.py out/testvideo/test_events.mp4 --seconds 7 # 3. the browser app: watch, results, brain & body viewer, library, music test ../firefly/.venv/Scripts/python ui.py # http://127.0.0.1:8790/ ``` The first run also fetches the MaleCNS optic-column table (~1 MB) to place every photoreceptor on its eye column. Optional: * **3D body** (NeuroMechFly, needs Python 3.12, ~680 MB): `py -3.12 -m venv .venv-body` then `.venv-body/Scripts/pip install flygym==2.1.0` in this folder; each watched video then gets `body3d.mp4`. * **YouTube links:** `pip install yt-dlp` in the brain's environment. Use videos you have the right to download and use. * **Checks:** `python -m pytest tests -q` (no GPU needed), `python physio.py` (the eye like a real fly's), `python looming.py` (an approaching disc, 5 seeds), `python audit.py out/watch/`. Your videos, songs and results stay on your PC (`out/` and `songs/` are ignored by git). ## Watch a video: the fly sees and hears it, frame by frame ```powershell ..\firefly\.venv\Scripts\python.exe watch.py "https://www.youtube.com/watch?v=..." --seconds 60 ..\firefly\.venv\Scripts\python.exe watch.py my_video.mp4 --start 30 --seconds 60 ..\firefly\.venv\Scripts\python.exe watch.py my_video.mp4 --rewired # scrambled brain, to compare ``` Links need `yt-dlp` (`pip install yt-dlp`); a downloaded file works without it. Use only videos you own or are licensed to use: downloading may be restricted by the site's terms, and other people's videos are copyrighted. Don't publish results made from videos you have no rights to. Every frame (25 a second = two 20 ms brain steps; 50 for 50 fps videos) reaches the brain the way a fly would get it. The frame is spread over both eyes, its left half on the left eye (front of each eye at the frame's centre): | sense | neurons | from the video | |---|---|---| | light | 6,006 photoreceptors (R1-6, R7, R8) | the frame's physical light at each one's eye column, through the eye's optics and its rhodopsin (see Vision) | | motion, looming | T4/T5, LPLC2 and everything after | nothing injected: the fly's own optic lobe computes them from the photoreceptors (fly-own vision, the default) | | sound | Johnston's organ (138) | the soundtrack, as for songs | Out (`out/watch//`): **`fly_watching.mp4`**, the video beside the fly's brain live (every neuron that fires, both eyes' motion detectors, the ear bands, the response of each pathway), with the original sound; `timeline.csv` (every 20 ms: each group's spike rate, looming, ear bands); `summary.json` (each group while watching vs at rest). About 0.9 s of compute per video second (fly-own; 0.4 s assisted) on a GPU. Checked on a test video with known events (bar moving right, then left, then a disc approaching; beeps over the first half): | response (spikes/neuron/s, fly-own vision, seed 1) | bar moving right | bar moving left | approaching disc | still | rest | |---|---|---|---|---|---| | T4/T5 (motion) | 0.83 | 0.73 | 0.80 | 0.64 | 0.48 | | LPLC2 (looming) | 1.36 | 1.88 | **2.92** | 0.53 | 0.97 | | escape (giant fiber) | 1.11 | 1.39 | **2.22** | 0.00 | 0.50 | | ear JO-A/B (beeps in the first two) | 5.53 | 4.47 | 0.33 | 0.33 | 0.32 | (`make_test_video.py` makes this video; `python watch.py out/testvideo/test_events.mp4 --seconds 7`.) ## In the browser ```powershell ..\firefly\.venv\Scripts\python.exe ui.py # opens http://127.0.0.1:8790/ ``` Paste a link or pick a video file, choose where to start and how long, press **Watch**: the log shows the fly at work, then the result plays on the page with the biggest responses. The **Library** keeps everything watched; **Music test** runs the human-vs-AI test on a folder and shows its report. One job at a time (the brain uses the GPU). ## Brain, body and emotions (the viewer) Every watched video gets a viewer (**Brain, body & emotions** on the result, or `/view?name=