public SongRetrieval retrieveSongFromSongId() { SongRetrieval sr = null; DBConnect connection = new DBConnect(); string query = "SELECT s.songTitle, ar.artistName, al.albumName FROM song s INNER JOIN artist ar ON s.artistId = ar.artistId INNER JOIN album al ON s.albumId = al.albumId WHERE s.songId = @songId;"; try { MySqlCommand cmd = new MySqlCommand(query, connection.OpenConnection()); cmd.CommandText = query; cmd.Prepare(); cmd.Parameters.AddWithValue("@songId", this.songId); //string songTitle = (string)cmd.ExecuteScalar(); MySqlDataReader dataReader = cmd.ExecuteReader(); while (dataReader.Read()) { sr = new SongRetrieval((string)dataReader[0], (string)dataReader[1], (string)dataReader[2]); } connection.CloseConnection(); return(sr); } catch (Exception ex) { return(null); } }
public SongIdentification(SongRetrieval identifiedSong, int accuracy, int maxConsecutiveHashes, string misc) { this.identifiedSong = identifiedSong; this.accuracy = accuracy; this.maxConsecutiveHashes = maxConsecutiveHashes; this.misc = misc; }
protected void scanAudio_Click(object sender, EventArgs e) { addSongResult.Visible = false; waiting.Visible = false; ingestFingerprint.Visible = false; videoAuthor.Visible = false; videoTitle.Visible = false; if (uploadedfile.HasFile) { string displayedResult = ""; ArrayList listOfResults = new ArrayList(); // Upload the file to server String fileName = uploadedfile.FileName; savePath += fileName; uploadedfile.SaveAs(savePath); string fileExtension = Path.GetExtension(savePath); if (!fileExtension.Equals(".mp3")) { savePath = convertVideoToAudio(fileName); savePath = savePath.Trim(); } SongRetrieval sr = new SongRetrieval(); sr.retrieveAllHash(); ArrayList hashValue = sr.getSongHashes(); double songLength = Math.Round((getMediaDuration(savePath) / 4) / 60, 2); //for (int b = 0; b < (getMediaDuration(savePath) / 4 - 2); b += 30) for(int b = 0; b < 60; b+=30) { // Analyze the files (input file) string filename = savePath; IntPtr fingerprint = getFingerPrint(filename, b, 30); string analysisResults = Marshal.PtrToStringAnsi(fingerprint); SongInfo song = JsonHelper.JsonDeserializer<SongInfo>(analysisResults); double[,] clientCode = analyzeCodegen(song.code); // Analyze input file with hashes obtained from database double[,] masterCode = null; ArrayList matchingEvents = new ArrayList(); //List of events that gives the greatest match (value stored is the index that gives the most number of matches) int maxConsecutiveCount = 9; int previousCount = 0; int currentCount = 0; // Identify events that matches for (int i = 0; i < hashValue.Count; i++) { masterCode = analyzeCodegen(hashValue[i].ToString()); int[] comparisonResult = compareClientMasterWithoutTimeOffset(clientCode, masterCode); currentCount = comparisonResult[1]; // Compares first criteria - Consecutive hashes if (comparisonResult[0] == maxConsecutiveCount) { matchingEvents.Add(i); } else if (comparisonResult[0] > maxConsecutiveCount) { matchingEvents.Clear(); matchingEvents.Add(i); maxConsecutiveCount = comparisonResult[0]; } // Compares second criteria - Total count if (matchingEvents.Count > 1) { if (currentCount > previousCount) { matchingEvents.Clear(); matchingEvents.Add(i); } else matchingEvents.RemoveAt(1); } if (comparisonResult[0] >= maxConsecutiveCount) previousCount = currentCount; } ArrayList songIdList = (ArrayList)sr.getSongIdList(); int[] songId = new int[matchingEvents.Count]; for (int i = 0; i < matchingEvents.Count; i++) { songId[i] = (int)songIdList[(int)matchingEvents[i]]; } // Identify the song string title = "Unidentified"; SongRetrieval identifiedSong = null; for (int i = 0; i < songId.Length; i++) { SongRetrieval songHash = new SongRetrieval((int)songId[i], true); if (i == 0) { identifiedSong = songHash.retrieveSongFromSongId(); title = identifiedSong.getSongTitle(); } else { if (!title.Equals(songHash.retrieveSongFromSongId().getSongTitle())) title = "Unidentified"; } } // Identifying the accuracy of the results int accuracy = 0; if (maxConsecutiveCount > 700) accuracy = 100; else if (maxConsecutiveCount > 500) accuracy = 80; else if (maxConsecutiveCount > 300) accuracy = 50; else if (maxConsecutiveCount > 100) accuracy = 30; else if (maxConsecutiveCount > 50) accuracy = 20; else if (title.Equals("Unidentified")) accuracy = 0; else accuracy = 10; // Identifying whether the identified song matches the meta data string misc = ""; if (song.metadata.title.Equals("")) { misc = "This song has no file properties attached."; } else if (title.Equals(song.metadata.title)) { if (accuracy < 90) accuracy += 10; misc = "Song identified matches file properties of the file sent for analysis."; } else if (title.Equals("Unidentified")) { misc = "Song could not be identified. This song might not exist in our database."; } else { if (accuracy <= 10) accuracy -= 10; if (accuracy > 50) misc = "The file properties might not be labelled correctly as it does not match the song identifed by us!"; else { misc = "Song identified might not be accurate as the desired song might not be in our database or large changes has been done to the file"; } } displayedResult = "File Analyzed: " + fileName + "<br/><br/>File Properties<br/>============<br/>" + "Title: " + song.metadata.title + "<br/>" + "Album Artist: " + song.metadata.artist + "<br/>" + "Album: " + song.metadata.release + "<br/>" + "Length: " + songLength + " minutes<br/>" + "Genre: " + song.metadata.genre + "<br/>" + "Bitrate: " + song.metadata.bitrate + " kbps<br/><br/>" + "Identification Results" + "<br/>=====================<br/>"; // If the song can be identified and the accuracy threshold is set to 20%, it will be added to be displayed as results if (identifiedSong != null && accuracy > 20) { SongIdentification si = new SongIdentification(identifiedSong, accuracy, maxConsecutiveCount, misc); listOfResults.Add(si); } } string sumTitle = ""; for (int j = 0; j < listOfResults.Count; j++) { SongIdentification mainObj = (SongIdentification)listOfResults[j]; SongRetrieval song = mainObj.getIdentifiedSong(); if (!sumTitle.Contains(song.getSongTitle())) { if (!sumTitle.Equals("") && mainObj.getAccuracy() > 10) { sumTitle = sumTitle + ", " + song.getSongTitle(); } else { sumTitle = song.getSongTitle(); } displayedResult = displayedResult + "Title: " + song.getSongTitle() + "<br/>" + "Album Artist: " + song.getArtistName() + "<br/>" + "Album: " + song.getAlbumName() + "<br/>" + "Total Matching Consecutive Hashes: " + mainObj.getMaxConsecutiveHashes() + "<br/>" + "Accuracy Level: " + mainObj.getAccuracy() + " %<br/>" + "Matching of File Properties: " + mainObj.getMisc() + "<br/><br/>"; } } if (!sumTitle.Equals("")) { if (fileExtension.Equals(".mp3")) { result.Text = "<b>File Analyzed: " + fileName + "<br/>Song Identified: " + sumTitle + "</b>"; displayedResult = displayedResult + "Conclusion<br/>=============<br/>" + "Song Identified: " + sumTitle; } else { result.Text = "<b>File Analyzed: " + fileName + "<br/>The video consist of the following identified song: " + sumTitle + "</b>"; displayedResult = displayedResult + "Conclusion<br/>=============<br/>" + "The video consist of the following identified song: " + sumTitle; } ingestFingerprint.Visible = false; } else { if (fileExtension.Equals(".mp3")) { result.Text = "<b>File Analyzed: " + fileName + "<br/>Song could not be identified</b><br/><br/>To contribute, you can choose to add this song into our database."; displayedResult = displayedResult + "Title: Unidentified" + "<br/>" + "Album Artist: Unidentified" + "<br/>" + "Album: Unidentified" + "<br/><br/>" + "Conclusion<br/>=========<br/>" + "Song could not be identified"; ingestFingerprint.Visible = true; ingestFingerprint.Enabled = true; ingestFingerprint.Text = "Add Song"; pathName.Text = savePath; } else { // Video format - no song identified // Extract key frames from the videos to do comparison int totalFrames = convertVideoToKeyFrames(fileName); string outputFile = @"C:\temp\uploads\output"; // Retrieve all fingerprint for comparison ImageFingerprint image = new ImageFingerprint(); ArrayList fingerprint = image.retrieveAllHashes(); int pcc = 0; ImageFingerprint matchingPictures = null; int highestPcc = 35; bool identified = false; for (int i = 1; i < totalFrames; i++) { string fileCount = i.ToString(); while (fileCount.Count() < 3) { fileCount = "0" + fileCount; } outputFile = outputFile + fileCount + ".jpg"; // Retrieve digest of input image for comparison IdentifyImage1.DigestRaw raw = new IdentifyImage1.DigestRaw(); IdentifyImage1.ph_image_digest(outputFile, 1, 1, ref raw, 180); IdentifyImage1.Digest d = IdentifyImage1.Digest.fromRaw(raw); for (int j = 0; j < fingerprint.Count; j++) { // Generate a struct for comparison from database IdentifyImage1.Digest d2 = new IdentifyImage1.Digest(); d2.id = ((ImageFingerprint)fingerprint[j]).getDigestId(); d2.coeffs = ((ImageFingerprint)fingerprint[j]).getDigestCoeffs(); d2.size = 40; // Compare the results pcc = IdentifyImage1.computeSimilarities(d.coeffs, d2.coeffs); if (pcc > highestPcc) { matchingPictures = (ImageFingerprint)fingerprint[j]; highestPcc = pcc; } } if (matchingPictures == null) { ulong hasha = 0; IdentifyImage1.ph_dct_imagehash(outputFile, ref hasha); int minHammingDistance = 20; for (int a = 0; a < fingerprint.Count; a++) { // Compute Hamming Distance int hammingDistance = IdentifyImage1.computeHammingDistance(hasha, ((ImageFingerprint)fingerprint[a]).getHash()); if (hammingDistance < minHammingDistance) { matchingPictures = (ImageFingerprint)fingerprint[a]; minHammingDistance = hammingDistance; } } if (minHammingDistance < 10) { identified = true; } } else identified = true; if (identified) break; else { highestPcc = 35; outputFile = @"C:\temp\uploads\output"; matchingPictures = null; } } if (identified) { result.Text = "<b>File Analyzed: " + fileName + "<br/>Song in the video could not be identified but identical key frames from videos has been identified to be identical to copyrighted videos</b><br/>"; displayedResult = displayedResult + "Title: Unidentified" + "<br/>" + "Album Artist: Unidentified" + "<br/>" + "Album: Unidentified" + "<br/>" + "Key Frames: Identified to be identical to copyrighted videos" + "<br/><br/>" + "Conclusion<br/>=========<br/>" + "Song from the video could not be identified but key frames are identified to be identical to copyrighted videos."; } else { result.Text = "<b>File Analyzed: " + fileName + "<br/>Song and key frames from video could not be identified</b>"; displayedResult = displayedResult + "Title: Unidentified" + "<br/>" + "Album Artist: Unidentified" + "<br/>" + "Album: Unidentified" + "<br/>" + "Key Frames: Unidentified" + "<br/><br/>" + "Conclusion<br/>=========<br/>" + "Song and key frames from the video could not be identified."; ingestFingerprint.Visible = true; ingestFingerprint.Enabled = true; ingestFingerprint.Text = "Add Video"; pathName.Text = savePath + ";" + totalFrames; videoAuthor.Visible = true; videoTitle.Visible = true; videoTitle.Text = "Enter Title..."; videoAuthor.Text = "Enter Author..."; videoTitle.Attributes.Add("onfocus", "if(this.value == 'Enter Title...') this.value='';"); videoTitle.Attributes.Add("onblur", "if(this.value == '' || this.value == ' ') this.value='Enter Title...'"); videoAuthor.Attributes.Add("onfocus", "if(this.value == 'Enter Author...') this.value='';"); videoAuthor.Attributes.Add("onblur", "if(this.value == '' || this.value == ' ') this.value='Enter Author...'"); } } } exportedResults.Text = displayedResult; waiting.Text = "Analysis Completed!"; result.Visible = true; exportToPdf.Enabled = true; exportToPdf.Visible = true; } else { waiting.Visible = true; waiting.Text = "No file has been selected for analysis"; } }
/* Add new fingerprint into the database according to the specified file name * */ public bool addNewFingerprint(string fileName) { int songDuration = (int)(getMediaDuration(fileName) / 4) - 2; // In seconds double[] comparisonResult = new double[2]; Artist artist = null; Album album = null; SongRetrieval sr = null; int songId = -1; for (int i = 0; i < songDuration; i = i + 30) { IntPtr fingerprint2 = getFingerPrint(fileName, i, 60); string analysisResults2 = Marshal.PtrToStringAnsi(fingerprint2); SongInfo song2 = JsonHelper.JsonDeserializer<SongInfo>(analysisResults2); if (song2.metadata.title.Equals("") || song2.metadata.artist.Equals("") || song2.metadata.release.Equals("")) { song2.metadata.title = videoTitle.Text; song2.metadata.artist = videoAuthor.Text; song2.metadata.release = videoTitle.Text; } if (i == 0) { artist = new Artist(song2.metadata.artist); artist.InsertArtist(); album = new Album(song2.metadata.release); album.InsertAlbum(); sr = new SongRetrieval(artist.retrieveArtistId(), album.retrieveAlbumId(), song2.metadata.title, song2.metadata.bitrate); sr.InsertSong(); songId = sr.retrieveLatestSongId(); sr = null; sr = new SongRetrieval(songId, song2.code, i); sr.InsertSongHash(); } else { sr.setSongHash(song2.code); sr.setSongOffset(i); sr.InsertSongHash(); } } return true; }
public void setIdentifiedSong(SongRetrieval identifiedSong) { this.identifiedSong = identifiedSong; }
public SongRetrieval retrieveSongFromSongId() { SongRetrieval sr = null; DBConnect connection = new DBConnect(); string query = "SELECT s.songTitle, ar.artistName, al.albumName FROM song s INNER JOIN artist ar ON s.artistId = ar.artistId INNER JOIN album al ON s.albumId = al.albumId WHERE s.songId = @songId;"; try { MySqlCommand cmd = new MySqlCommand(query, connection.OpenConnection()); cmd.CommandText = query; cmd.Prepare(); cmd.Parameters.AddWithValue("@songId", this.songId); //string songTitle = (string)cmd.ExecuteScalar(); MySqlDataReader dataReader = cmd.ExecuteReader(); while(dataReader.Read()) sr = new SongRetrieval((string)dataReader[0], (string)dataReader[1], (string)dataReader[2]); connection.CloseConnection(); return sr; } catch (Exception ex) { return null; } }