Skip to content

Instantly share code, notes, and snippets.

@sloev
Created March 1, 2024 10:32
Show Gist options
  • Save sloev/8554e028ef8f7ccc942222b047b2525d to your computer and use it in GitHub Desktop.
Save sloev/8554e028ef8f7ccc942222b047b2525d to your computer and use it in GitHub Desktop.
realtime microphone to spectrogram to imagehash
import ffmpeg
import numpy as np
import cv2
import sys
# pip install ffmpeg-python
# pip install opencv-python
# pip install opencv-contrib-python
process1 = (
ffmpeg
.input("default", f='pulse', fragment_size=16, channels=1, rtbufsize="1024B", fflags="nobuffer")
.filter('showspectrum', s='32x32', mode="combined",slide="scroll", scale="log")
.output('pipe:', format='rawvideo', pix_fmt='gray')
.run_async(pipe_stdout=True)
)
while True:
in_bytes = process1.stdout.read(32 * 32)
if not in_bytes:
break
in_frame = (
np
.frombuffer(in_bytes, np.uint8)
.reshape([32, 32, 1])
)
h=cv2.img_hash.pHash(in_frame) # 8-byte hash
pH=int.from_bytes(h.tobytes(), byteorder='big', signed=False)
sys.stdout.write(f"got_hash {pH}\n")
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment