Skip to content

Commit

Permalink
fix: thershold option and fix hide person
Browse files Browse the repository at this point in the history
  • Loading branch information
sai80082 committed Dec 1, 2024
1 parent 7639de9 commit e0f96ce
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
16 changes: 7 additions & 9 deletions src/components/people/PersonItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ArrowUpRight, Info, Search } from "lucide-react";
import { useConfig } from "@/contexts/ConfigContext";
import { useToast } from "../ui/use-toast";
import { Badge } from "../ui/badge";

import { Button } from "@/components/ui/button";
interface IProps {
person: IPerson;
onRemove: (person: IPerson) => void;
Expand Down Expand Up @@ -78,18 +78,11 @@ export default function PersonItem({ person, onRemove }: IProps) {
)}
>
<div className="relative w-full h-auto group">
<div
onClick={() => {
handleHide(!formData.isHidden);
}}
>
<Avatar
className="w-full min-h-full h-auto rounded-lg"
src={person.thumbnailPath}
alt={person.name}
/>
</div>

<div className="absolute bottom-2 w-full flex justify-center items-center">
<Badge variant={"secondary"} className="text-xs !font-medium font-mono">{person.assetCount} Assets</Badge>
</div>
Expand All @@ -108,8 +101,13 @@ export default function PersonItem({ person, onRemove }: IProps) {
<Info size={16} />
</Link>
</div>
<div className="absolute top-2 right-2 ">
<div className="absolute top-2 right-2 flex flex-col gap-2">
<PersonMergeDropdown person={person} onRemove={onRemove}/>
<Button variant="outline" className="!py-0.5 !px-2 text-xs h-7" onClick={() => {
handleHide(!formData.isHidden);
}}>
Hide
</Button>
</div>
</div>
{!editMode ? (
Expand Down
25 changes: 24 additions & 1 deletion src/components/people/PersonMergeDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function PersonMergeDropdown({
const [selectedPeople, setSelectedPeople] = useState<IPerson[]>([]);
const [primaryPerson, setPrimaryPerson] = useState<IPerson>(person);
const [merging, setMerging] = useState(false);
const [threshold, setThreshold] = useState(0.5);
const { toast } = useToast();

const selectedIds = useMemo(
Expand Down Expand Up @@ -89,7 +90,7 @@ export function PersonMergeDropdown({
};

const fetchSuggestions = () => {
return listSimilarFaces(person.id)
return listSimilarFaces(person.id, threshold)
.then(setSimilarPeople)
.catch(() => {
toast({
Expand Down Expand Up @@ -161,6 +162,13 @@ export function PersonMergeDropdown({
setSelectedPeople(newSelected);
};

useEffect(() => {
if (open) {
setSimilarLoading(true);
fetchSuggestions();
}
}, [threshold, open]);

useEffect(() => {
if (open && !similarPeople.length) fetchSuggestions();
}, [open, person.id, similarPeople]);
Expand Down Expand Up @@ -279,6 +287,21 @@ export function PersonMergeDropdown({
handleSearch(e.target.value);
}}
/>
<div>
<label htmlFor="threshold" className="block text-sm font-medium text-gray-300">
Threshold
</label>
<Input
type="number"
step="0.01"
min="0"
max="1"
value={threshold}
onChange={(e) => setThreshold(parseFloat(e.target.value))}
placeholder="Threshold (0 to 1)"
className="mt-2"
/>
</div>

{selectedPeople.length > 0 ? (
<div className="flex flex-nowrap overflow-x-auto gap-2 py-2">
Expand Down
5 changes: 3 additions & 2 deletions src/handlers/api/people.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const mergePerson = (id: string, targetIds: string[]) => {
return API.post(MERGE_PERSON_PATH(id), { ids: targetIds })
}

export const listSimilarFaces = (id: string) => {
return API.get(SIMILAR_FACES_PATH(id)).then((response) => response.map((person: any) => cleanUpPerson(person, true)));
export const listSimilarFaces = (id: string, threshold: number) => {
return API.get(SIMILAR_FACES_PATH(id), { threshold: threshold })
.then((response) => response.map((person: any) => cleanUpPerson(person, true)));
}
6 changes: 4 additions & 2 deletions src/pages/api/people/[id]/similar-faces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface IQuery {
minimumAssetCount: number;
sort: ISortField;
sortOrder: "asc" | "desc";
threshold?: number;
}
export default async function handler(
req: NextApiRequest,
Expand All @@ -26,9 +27,10 @@ export default async function handler(
try {
const {
id,
threshold = 0.5,
} = req.query as any as IQuery;

const currentUser = await getCurrentUser(req);
const currentUser = await getCurrentUser(req);
const personRecords = await db
.select()
.from(person)
Expand Down Expand Up @@ -92,7 +94,7 @@ export default async function handler(
and(
ne(person.id, id),
eq(person.ownerId, currentUser.id),
gt(similarity, 0.5)
gt(similarity, threshold)
)
)
.limit(12);
Expand Down

0 comments on commit e0f96ce

Please sign in to comment.