import React, { useState } from "react"; const ImageDisplay = ({ uid }) => { const [imageGroups, setImageGroups] = useState({}); const [uploadedImages, setUploadedImages] = useState({}); const [uploadedGroups, setUploadedGroups] = useState(new Set()); const [isLoading, setIsLoading] = useState(false); const [isDownload, setIsDownload] = useState(false); const [currentGroupIndex, setCurrentGroupIndex] = useState(0); const groupKeys = Object.keys(imageGroups); const currentGroup = groupKeys[currentGroupIndex]; const nextGroup = () => { if (currentGroupIndex < groupKeys.length - 1) { setCurrentGroupIndex(currentGroupIndex + 1); } }; const previousGroup = () => { if (currentGroupIndex > 0) { setCurrentGroupIndex(currentGroupIndex - 1); } }; const fetchImages = async () => { setIsLoading(true); try { const response = await fetch(`http://localhost:8000/get_images/${uid}`); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const data = await response.json(); setImageGroups(data); } catch (error) { console.error("Could not fetch images:", error); } finally { setIsLoading(false); } }; const handleFileChange = (event, group) => { setUploadedImages({ ...uploadedImages, [group]: event.target.files[0], }); }; const uploadImage = async (group) => { const formData = new FormData(); formData.append("file", uploadedImages[group]); try { const response = await fetch( `http://localhost:8000/uploadnewfaces/${uid}/${group}`, { method: "POST", body: formData, } ); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } setUploadedGroups(new Set(uploadedGroups.add(group))); // fetchImages(); } catch (error) { console.error("Could not upload image:", error); } }; const downloadVideo = async () => { const response = await fetch( `http://localhost:8000/download_result_video/${uid}` ); const blob = await response.blob(); const downloadUrl = window.URL.createObjectURL(blob); const link = document.createElement("a"); link.href = downloadUrl; link.download = uid + ".mp4"; document.body.appendChild(link); link.click(); link.remove(); window.URL.revokeObjectURL(downloadUrl); }; const faceSwap = async () => { try { const groupIdsArray = Array.from(uploadedGroups).map(Number); console.log(groupIdsArray); const response = await fetch(`http://localhost:8000/faceswap/${uid}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ group_ids: groupIdsArray }) }); if (!response.ok) { const errorResponse = await response.json(); console.error('Error Response:', errorResponse); throw new Error(`HTTP error! Status: ${response.status}`); } const data = await response.json(); console.log('Face swap response:', data); setIsDownload(true); // fetchImages(); } catch (error) { console.error("Could not perform face swap:", error); } }; return (
{groupKeys.length > 0 && (

Group {currentGroup}

handleFileChange(event, currentGroup)} />
{imageGroups[currentGroup].map((image, idx) => ( {`Group ))}
)}
{uploadedGroups.size > 0 && ( )} {isDownload && ( )}
); }; export default ImageDisplay;