Python Forum
fingerprint similarity audio microphone
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fingerprint similarity audio microphone
#1
Hi everyone,
I have the code below. Listen to the audio through the microphone, get the fingerprint of the stream heard, calculate the fingerprint of the saved tracks and compare them. How does this procedure take place? What do you compare? Perform an FFT?
I tried to look at whether the various libraries do FFT but it seems not.

The code file works correctly, but the Dockerfile does not allow booting using Docker.

I also attach Dockerfile even if it seems to not work. Maybe there is some problem in writing it.


# Recueve fingerprint and verify similarity
# Import Library
import acoustid
import sys
import os
import chromaprint
import numpy as np
import matplotlib.pyplot as plt
from fuzzywuzzy import fuzz
 
 
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
 

# Recorder audio with microphone usb and save to file ".wav"
 
 
# Import Library
import pyaudio
import wave
 
 
# Define signal
form_1 = pyaudio.paInt16  # resolution signal in bit (16 bit)
chans = 1  # 1 channel
samp_rate = 44100  # 44.1kHz sampling rate
chunk = 4096  # 2^12 analysis samples
record_secs = 50  # recording duration in seconds
dev_index = 2  # device index
wav_output_filename = '/Users/alessandrorosina/Desktop/rescue_recognition/stream/stream_fingerprint.wav'  # name file “.wav”
 
# Create istance pyaudio
audio = pyaudio.PyAudio()
 
# Create stream pyaudio
stream = audio.open(format=form_1, rate=samp_rate, channels=chans, \
                    input_device_index=dev_index, input=True, \
                    frames_per_buffer=chunk)
 

print("recording")
 
 
frames = []
 
 
for ii in range(0, int((samp_rate / chunk) * (record_secs / 10))):
    data = stream.read(chunk)
    frames.append(data)
 
 
 
DIR_DATABASE = '/Users/alessandrorosina/Desktop/rescue_recognition/track'
DIR_SAMPLES = '/Users/alessandrorosina/Desktop/rescue_recognition/stream'
 
 
def get_fingerprint(filepath):
    """
    Get fingerprint (list of signed integer), version, duration
    """
    duration, fp_encoded = acoustid.fingerprint_file(filepath)
    fp, version = chromaprint.decode_fingerprint(fp_encoded)
 
    # acoustid.fingerprint(samplerate, channels, pcmiter, maxlength=MAX_AUDIO_LENGTH)
 
    return fp, version, duration
 
 
 
def build_fingerprint_database(dirpath, file_ext='.wav'):
    """
    Build database from directory of audio files
    """
    database = {}
    print('\nProcessing {}..'.format(dirpath))
    for f in os.listdir(dirpath):
        path = os.path.join(dirpath, f)
        name, ext = os.path.splitext(f)
        if os.path.isfile(path) and ext == file_ext:
            print('Getting fingerprint from database item: {}..'.format(f))
            database[f], version, duration = get_fingerprint(path)
    return database
 
 
def plot_fingerprints(db):
    """
    Visualize fingerprints in database
    """
    fig = plt.figure()
    numrows = len(db)
    plot_id = 1
 
    for name, fp in db.items():
        # single column grid
        a = fig.add_subplot(numrows, 1, plot_id)
        imgplot = plt.imshow(get_fingerprint_bitmap(fp))
        a.set_title(name)
        plot_id += 1
    plt.show()
 
 
def get_fingerprint_bitmap(fp):
    """
    Plot list of uint32 as (32, len(list)) bitmap
    """
    bitmap = np.transpose(np.array([[b == '1' for b in list('{:32b}'.format(i & 0xffffffff))] for i in fp]))
    return bitmap
 
 
 
 
stream.stop_stream() # stop stream
stream.close() # close channel
audio.terminate() # terminate track audio
 
 
# Save stream in file ".wav"
wavefile = wave.open(wav_output_filename, 'wb') # create or overwrite
wavefile.setnchannels(chans)
wavefile.setsampwidth(audio.get_sample_size(form_1))
wavefile.setframerate(samp_rate)
wavefile.writeframes(b''.join(frames))
wavefile.close()
 
 
if __name__ == '__main__':
 
 
 
    # Directory stream e track
    database = build_fingerprint_database(DIR_DATABASE)
    samples = build_fingerprint_database(DIR_SAMPLES)
 
    print("forwarding path to the function build_fingerprint_database")
    print('\n')
 
    # Find similarity
    for sample, sample_fp in samples.items():
        print('Similarity score of "{}":'.format(sample))
        best_match = None
        for name, fp in database.items():
            similarity = fuzz.ratio(sample_fp, fp)
            if not best_match or best_match['score'] < similarity:
                best_match = {
                    'score': similarity,
                    'name': name
                }
            print('{} {}%'.format(name, similarity))
        print('Best match: {name} ({score}%)\n'.format(**best_match))
 
 
    # plot database
    plot_fingerprints(database)  # Fingerprint database full
    #plot_fingerprints(samples)  # Fingerprint samples
Dockerfile:

FROM ubuntu:16.04

# Download base image ubuntu 16.04
# FROM ubuntu:16.04


# Set proxy server, replace host:port with values for your servers
# ENV http_proxy host:port
# ENV https_proxy host


# Update Ubuntu Software repository
WORKDIR /alessandrorosina/Desktop/test

# RUN apt-get update && apt-get install -y --no-install-recommends apt-utils
RUN apt-get -y update
# RUN apt-get -y upgrade
RUN apt-get -y install debconf

# -y per dare il comando di conferma silente - silent
# RUN apt-get -y install apt-utils
RUN apt-get -y install curl
RUN apt-get -y install python-pip
RUN apt-get -y install python-setuptools
RUN apt-get -y install libasound-dev portaudio19-dev libportaudio2 libportaudiocpp0
RUN apt-get -y install ffmpeg libav-tools

# RUN pip install pyaudio
RUN apt-get -y install python-pyaudio
# RUN apt-get -y install pyaudio
RUN apt-get -y install python3-pyaudio

# RUN apt-get -y install python-acoustid
RUN apt-get -y install git
RUN git clone https://github.com/beetbox/pyacoustid.git
RUN apt-get -y install python-acoustid
# RUN apt-get -y install pyacoustid
RUN apt-get -y install python-numpy
RUN apt-get -y install python-matplotlib
RUN apt-get -y install python-fuzzywuzzy
RUN apt-get -y install python-levenshtein
RUN apt-get -y install python-pil
RUN apt-get -y install python-audioread
RUN apt-get -y install python-certifi
RUN apt-get -y install python-chardet
RUN apt-get -y install python-cycler
RUN apt-get -y install python-idna
RUN apt-get -y install python-pyparsing
RUN apt-get -y install python-serial
RUN apt-get -y install python-dateutil
RUN apt-get -y install python-requests
RUN apt-get -y install python-six
RUN apt-get -y install python-urllib3
RUN apt-get -y install python3-urllib3
COPY . .

# fare copy delle tracce audio

# ENTRYPOINT ["python", "/app/recognition.py"]
ENTRYPOINT ["python", "/alessandrorosina/Desktop/test/recognition.py"]
Reply
#2
From what I found, it includes the chromeprint.h library included in acoustid / chromaprint which in turn with C code FFTW - which is the abbreviation for Fastest Fourier Transform in the West, is a C library for calculating the Fourier Fast Transform under GPL license by the critics of MIT, Matteo Frigo (a computer scientist) and Steven G. Johnson (a physicist).

Is it as correct as I understand it?

In which part of the code is this module called?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Similarity function for couple system sunnydayxo 1 2,092 Apr-16-2021, 07:11 AM
Last Post: MH90000
  How do I improve string similarity in my current code? SUGSKY 3 2,341 May-28-2020, 05:16 AM
Last Post: deanhystad
  Fingerprint audio - chromaprint and get fingerprint alessandro87gatto 5 6,519 Apr-17-2019, 08:55 AM
Last Post: alessandro87gatto
  Similarity network Absolumentpasadrien 3 2,618 Apr-05-2019, 10:31 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020