File size: 5,983 Bytes
bfbdcb9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
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 (
<div className="space-y-4">
<button
className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition duration-300"
onClick={fetchImages}
disabled={isLoading}
>
{isLoading ? "Loading..." : "Load Images"}
</button>
{groupKeys.length > 0 && (
<div className="space-y-2">
<h3 className="font-semibold">Group {currentGroup}</h3>
<label
htmlFor="new-face"
className="block w-full px-4 py-2 bg-blue-600 text-white rounded cursor-pointer mb-2 transition-colors duration-300 hover:bg-blue-700"
>
Choose New Face
</label>
<input
id="new-face"
className="hidden"
type="file"
onChange={(event) => handleFileChange(event, currentGroup)}
/>
<button
className="ml-2 px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600 transition duration-300"
onClick={() => uploadImage(currentGroup)}
>
Upload Image
</button>
<div className="flex flex-wrap gap-2">
{imageGroups[currentGroup].map((image, idx) => (
<img
key={idx}
src={`http://localhost:8000/images/${image}`}
alt={`Group ${currentGroup} ${idx}`}
className="w-14 h-14 object-cover"
/>
))}
</div>
</div>
)}
<div className="flex justify-between mt-4">
<button
className="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600 transition duration-300"
onClick={previousGroup}
disabled={currentGroupIndex === 0}
>
Previous
</button>
<button
className="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600 transition duration-300"
onClick={nextGroup}
disabled={currentGroupIndex === groupKeys.length - 1}
>
Next
</button>
</div>
{uploadedGroups.size > 0 && (
<button
className="px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600 transition duration-300"
onClick={faceSwap}
>
Face Swap for Group {Array.from(uploadedGroups).join(", ")}
</button>
)}
{isDownload && (
<button
className="px-4 py-2 bg-yellow-500 text-white rounded hover:bg-yellow-600 transition duration-300"
onClick={downloadVideo}
>
Download
</button>
)}
</div>
);
};
export default ImageDisplay;
|