Skip to content

Commit

Permalink
Merge functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Webreaper committed Mar 7, 2024
1 parent 7afbaa3 commit b9d1133
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
60 changes: 56 additions & 4 deletions Damselfly.Core/Services/ImageRecognitionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Damselfly.Shared.Utils;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SixLabors.ImageSharp.PixelFormats;
using Image = Damselfly.Core.Models.Image;

Expand All @@ -27,7 +28,8 @@ public class ImageRecognitionService(IServiceScopeFactory _scopeFactory,
MetaDataService _metdataService, FaceONNXService _faceOnnxService,
ThumbnailService _thumbService, ConfigService _configService,
ImageClassifier _imageClassifier, ImageCache _imageCache,
WorkService _workService, ExifService _exifService) : IPeopleService, IProcessJobFactory, IRescanProvider
WorkService _workService, ExifService _exifService,
ILogger<ImageRecognitionService> _logger) : IPeopleService, IProcessJobFactory, IRescanProvider
{
// WASM: This should be a MemoryCache
private readonly IDictionary<string, Person> _peopleCache = new ConcurrentDictionary<string, Person>();
Expand Down Expand Up @@ -64,11 +66,50 @@ public async Task<List<string>> GetPeopleNames(string searchText)
return names;
}

public async Task UpdateName(ImageObject faceObject, string name, bool merge)
private async Task MergeWithNamne(ImageObject faceObject, string name)
{
if (!faceObject.IsFace)
throw new ArgumentException("Image object passed to name update.");
using var scope = _scopeFactory.CreateScope();
using var db = scope.ServiceProvider.GetService<ImageContext>();

var transaction = db.Database.BeginTransaction();

try
{
var matchingPeople = await GetAllPeople();

var oldPersonId = faceObject.PersonId;
var newPersonId = matchingPeople.Where( x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
.Select( x => x.PersonId )
.SingleOrDefault(-1);

if( newPersonId != -1 )
{
// Update personID in image objects to the new person ID
await db.ImageObjects.Where( x => x.PersonId == oldPersonId )
.ExecuteUpdateAsync( x => x.SetProperty( p => p.PersonId, newPersonId));

// Update personID in FaceData
await db.FaceData.Where( x => x.PersonId == oldPersonId )
.ExecuteUpdateAsync( x => x.SetProperty( p => p.PersonId, newPersonId));

// Delete old personID
await db.People.Where( x => x.PersonId == oldPersonId )
.ExecuteDeleteAsync();

await transaction.CommitAsync();

await LoadPersonCache( true );
}
}
catch( Exception ex )
{
_logger.LogError( $"Exception while merging person {faceObject.PersonId} => {name}");
await transaction.RollbackAsync();
}
}

private async Task ChangeName(ImageObject faceObject, string name)
{
using var scope = _scopeFactory.CreateScope();
using var db = scope.ServiceProvider.GetService<ImageContext>();

Expand All @@ -89,6 +130,17 @@ public async Task UpdateName(ImageObject faceObject, string name, bool merge)

await db.SaveChangesAsync("SetName");

}
public async Task UpdateName(ImageObject faceObject, string name, bool merge)
{
if (!faceObject.IsFace)
throw new ArgumentException("Image object passed to name update.");

if( merge )
await MergeWithNamne(faceObject, name);
else
await ChangeName( faceObject, name);

if (faceObject.Person.PersonGuid != null)
{
// Add/update the cache
Expand Down
4 changes: 2 additions & 2 deletions Damselfly.Web.Client/Shared/Dialogs/NameDialog.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
Variant="UIConstants.MudVariant" AutoFocus="true"/>
@if( showDupeMessage )
{
<p>
<div class="merge-message">
There is already an entry for '@TypeAheadName'. By selecting that Name, you will
merge/add this face to that person.
</p>
</div>
}
</DialogContent>
<DialogActions>
Expand Down
4 changes: 4 additions & 0 deletions Damselfly.Web.Client/Shared/Dialogs/NameDialog.razor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.merge-message {
margin: 10px;
}
3 changes: 3 additions & 0 deletions Damselfly.Web.Client/Shared/PersonTile.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<img @key="@Person.PersonId" title="@Person.Name" src="@FaceLink">
</NavLink>
</div>
<div class="damselfly-personfield">
@Person.FaceData.Count sets of face data
</div>
<div class="damselfly-personfield">
<MudTextField @key="@Person.PersonId" @bind-Value="@Name" ReadOnly="false" Variant="UIConstants.MudVariant"/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion Damselfly.Web.Client/wwwroot/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const CACHE_VERSION='4.1.0-20240307145827'
const CACHE_VERSION='4.1.0-20240307190510'

0 comments on commit b9d1133

Please sign in to comment.