def read_from_mp3_folder(path): """ Reads folder of mp3s into database Parameters ---------- path : String global path to folder Returns ------- Database database of songs Dependencies ------------ Song to fingerprint append_database """ database = Database() folder = Path(path) for file in folder.iterdir(): if not file.is_dir() and file.suffix == ".mp3": print(file.stem) name = file.stem samples = get_mp3_data(file) peaks = sample_to_peaks(samples) fingerprint = get_fingerprint(peaks, 15, len(database.id_to_name)) append_database(database, fingerprint, name) return database
def get_post_upload(): """ Function is called when a user wants to upload sound and meta-data to the database. """ file_key = "sound_file" upload_folder = "sounds" if request.method == "POST" and file_key in request.files: sound_file = request.files[file_key] file_path = os.path.join( upload_folder, request.form["sound_name"] + ".wav" ) sound_file.save(file_path) db.insert_reference( request.form["sound_name"], fingerprint.get_fingerprint(file_path), float(request.form["r_ref"]), float(request.form["l_ref"]), request.form["class"] ) return render_template("upload.html")
def get_post_upload(): """ Function is called when a user wants to upload sound and meta-data to the database. """ file_key = "sound_file" upload_folder = "sounds" if request.method == "POST" and file_key in request.files: sound_file = request.files[file_key] file_path = os.path.join(upload_folder, request.form["sound_name"] + ".wav") sound_file.save(file_path) db.insert_reference(request.form["sound_name"], fingerprint.get_fingerprint(file_path), float(request.form["r_ref"]), float(request.form["l_ref"]), request.form["class"]) return render_template("upload.html")
def init(): """ Initializes the RethinkDB database @return Whether the database was reinitialized or not """ conn = r.connect() if not DB in r.db_list().run(conn): create(conn) f_print = fingerprint.get_fingerprint(SAMPLE_PATH) insert_reference( SAMPLE_NAME, f_print, SAMPLE_R_REF, SAMPLE_L_REF, SAMPLE_CLASS ) return True else: return False
def get_fingerprint(file_name): f_new = converter.convert(file_name) fp = fingerprint.get_fingerprint(f_new) os.remove(os.path.join(cf.FOLDER_TEMP, f_new)) return fp
def check_matches(audio_data, database): """ Checks if audio data from a song matches a song in the database, and if so, which song it is Parameters ---------- audio_data : np.array([]) A 1D array of audio samples from recorded audio or audio file. database : dictionary An dictionary containing fingerprint (peak to list of songs) mappings for all songs Returns ------- int() The song id of the recognized song; -1 if no song recognized Dependencies ------------ sample_to_spectrogram() spectrogram_to_peaks() <peaks to keys> (to be named) """ req = 20 #Determine through experimentation no_match = -1 kvpairs = fp.get_fingerprint(spk.sample_to_peaks(audio_data), 15) #audio_data, times --> spectrogram --> array of peaks: peak = (t, f) --> list of peaks in the song #COUNT NUMBER OF MATCHED PEAKS FOR EACH SONG IN THE DATABASE, RECORD OFFSETS match_cnt = Counter() #print(database) #print(kvpairs) for k, v in kvpairs.items(): #print(k) if k in database: #print(k) #print(database[k]) for value in database[k]: #print("value: {}".format(value)) #print(v) match_cnt.update(((value[0], value[1] - v[0][1]), )) #ELIMINATE SONGS WITH INSUFFICIENT MATCHES bad_matches = [] for match in match_cnt: #print(match) if match_cnt[match] < req: #print(match_cnt[match]) bad_matches.append(match) for bad_match in bad_matches: del match_cnt[bad_match] #print(match_cnt) if len(match_cnt) == 0: return no_match #RETURN THE SONG ID WITH THE MOST MATCHES return match_cnt.most_common(1)[0][0][0] #OLD CODE--WILL PROBABLY NOT NEED (IGNORE) ''' sample_time = np.array(v) song_time = np.array(database[k]) sample_time[:,1] = song_time[:,1] - sample_time[:,1] #maybe will have to reverse, but this works with array broadcasting match_cnt.update(sample_time) ''' ''' for pair in v: if pair[0] not in match_cnt: match_cnt[pair[0]] = 1 else: match_cnt[pair[0]] += 1 ''' ''' times = list() #Times at which peaks were found in given song posmatches = list() #Values (lists of (song1, time), (song2, time)...) for possible matches ''' ''' for p, t in kvpairs: if p in database: times.append(t) #time at which matched peak was found (in given song) is appended to times posmatches.append(database[p]) #actual corresponding time and song values from database are recorded ''' '''
img_collector = collector.ImageCollector() for filename in imgs: camera = extract_camera_name(filename) img_collector[camera].append(filename) cameras = list(img_collector.imgs.keys()) print('Images loaded!') # Get fingerprint print('Generating fingerprints...') fps = [] labels = [] count = 0 for camera in cameras: imgs = img_collector.imgs[camera] for group in chunks(imgs, properties['group']): fp = fingerprint.get_fingerprint(imgs, camera) fp = eliminate_nan_inf(fp) fps.append(fp) labels.append(camera) count += 1 print(f"==> {count/len(cameras)}%") print('Finished!') # Train NN print("Training NN...") shuffled_inds = list(range(len(fps))) print(np.array(fps).shape) fps = torch.Tensor(np.transpose(np.array(fps), (0, 3, 1, 2))).to( device) # Convert to arrays labels = np.array(labels) # Convert to arrays random.shuffle(shuffled_inds)