rein0421 commited on
Commit
dfb5f93
·
verified ·
1 Parent(s): ff0deb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -88
app.py CHANGED
@@ -854,102 +854,117 @@ async def read_root():
854
  <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
855
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
856
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
857
- <script>
858
- $(function() {
859
- $("#slider").slider({
860
- range: "min",
861
- value: 50,
862
- min: 0,
863
- max: 100,
864
- slide: function(event, ui) {
865
- $("#riskLevelLabel").text(ui.value);
866
- }
867
- });
868
  });
869
-
870
- function previewAndResizeImage() {
871
- const fileInput = document.getElementById('uploadImage');
872
- const uploadedImage = document.getElementById('uploadedImage');
873
- if (fileInput.files && fileInput.files[0]) {
874
- const reader = new FileReader();
875
- reader.onload = function(e) {
876
- const img = new Image();
877
- img.onload = function() {
878
- const maxWidth = 640;
879
- const maxHeight = 640;
880
- let width = img.width;
881
- let height = img.height;
882
- if (width > maxWidth || height > maxHeight) {
883
- const ratio = Math.min(maxWidth / width, maxHeight / height);
884
- width = width * ratio;
885
- height = height * ratio;
886
- }
887
- const canvas = document.createElement('canvas');
888
- canvas.width = width;
889
- canvas.height = height;
890
- const ctx = canvas.getContext('2d');
891
- ctx.drawImage(img, 0, 0, width, height);
892
- uploadedImage.src = canvas.toDataURL('image/jpeg');
893
- uploadedImage.style.display = 'block';
894
- };
895
- img.src = e.target.result;
 
 
 
 
 
 
 
 
 
 
 
 
896
  };
897
- reader.readAsDataURL(fileInput.files[0]);
898
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
899
  }
900
 
901
- function processImage() {
902
- const fileInput = document.getElementById('uploadImage');
903
- const processingType = document.getElementById('processingType').value;
904
- const riskLevel = $("#slider").slider("value");
905
- const resultDiv = document.getElementById('result');
906
- const processedImage = document.getElementById('processedImage');
907
- const downloadLink = document.getElementById('downloadLink');
908
- if (fileInput.files.length === 0) {
909
- alert("画像を選択してください。");
910
- return;
911
- }
912
- const file = fileInput.files[0];
913
- const formData = new FormData();
914
- formData.append('image', file);
915
- formData.append('risk_level', riskLevel);
916
- let apiEndpoint;
917
- if (processingType === "opencv") {
918
- apiEndpoint = "/create-mask-and-inpaint-opencv";
919
- } else if (processingType === "simple_lama") {
920
- apiEndpoint = "/create-mask-and-inpaint-simple-lama";
921
- } else if (processingType === "stamp") {
922
- apiEndpoint = "/create-mask-and-inpaint-stamp";
923
- } else if (processingType == "mosaic") {
924
- apiEndpoint = "/create-mask-and-inpaint-mosaic";
925
- }
926
- const url = "https://rein0421-aidentify.hf.space" + apiEndpoint;
927
- fetch(url, {
928
- method: 'POST',
929
- body: formData
930
- })
931
- .then(response => {
932
- if (!response.ok) {
933
- throw new Error("Network response was not ok");
934
- }
935
- return response.blob();
936
- })
937
- .then(blob => {
938
- const objectURL = URL.createObjectURL(blob);
939
- processedImage.src = objectURL;
940
- downloadLink.href = objectURL;
941
- resultDiv.style.display = "block";
942
- })
943
- .catch(error => {
944
- console.error("画像処理に失敗しました。", error);
945
- alert("画像処理に失敗しました。");
946
- });
947
  }
948
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
949
  </body>
950
  </html>
951
 
952
-
953
  """
954
  return HTMLResponse(content=html_content)
955
  if __name__ == "__main__":
 
854
  <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
855
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
856
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
857
+ <script>
858
+ $(function() {
859
+ $("#slider").slider({
860
+ range: "min",
861
+ value: 50,
862
+ min: 0,
863
+ max: 100,
864
+ slide: function(event, ui) {
865
+ $("#riskLevelLabel").text(ui.value);
866
+ }
 
867
  });
868
+ });
869
+
870
+ let resizedImageBlob = null; // 縮小された画像データを保持
871
+
872
+ function previewAndResizeImage() {
873
+ const fileInput = document.getElementById('uploadImage');
874
+ const uploadedImage = document.getElementById('uploadedImage');
875
+
876
+ if (fileInput.files && fileInput.files[0]) {
877
+ const reader = new FileReader();
878
+ reader.onload = function(e) {
879
+ const img = new Image();
880
+ img.onload = function() {
881
+ const maxWidth = 600;
882
+ const maxHeight = 600;
883
+ let width = img.width;
884
+ let height = img.height;
885
+
886
+ // 縦横比を維持して画像を縮小
887
+ if (width > maxWidth || height > maxHeight) {
888
+ const ratio = Math.min(maxWidth / width, maxHeight / height);
889
+ width = width * ratio;
890
+ height = height * ratio;
891
+ }
892
+
893
+ const canvas = document.createElement('canvas');
894
+ canvas.width = width;
895
+ canvas.height = height;
896
+ const ctx = canvas.getContext('2d');
897
+ ctx.drawImage(img, 0, 0, width, height);
898
+
899
+ // 縮小された画像のプレビュー表示
900
+ uploadedImage.src = canvas.toDataURL('image/jpeg');
901
+ uploadedImage.style.display = 'block';
902
+
903
+ // Blobに変換してサーバーに送信できるように保存
904
+ canvas.toBlob((blob) => {
905
+ resizedImageBlob = blob; // サーバー送信用の縮小画像データを保存
906
+ }, 'image/jpeg');
907
  };
908
+ img.src = e.target.result;
909
+ };
910
+ reader.readAsDataURL(fileInput.files[0]);
911
+ }
912
+ }
913
+
914
+ function processImage() {
915
+ const processingType = document.getElementById('processingType').value;
916
+ const riskLevel = $("#slider").slider("value");
917
+ const resultDiv = document.getElementById('result');
918
+ const processedImage = document.getElementById('processedImage');
919
+ const downloadLink = document.getElementById('downloadLink');
920
+
921
+ if (!resizedImageBlob) {
922
+ alert("画像を選択して縮小してください。");
923
+ return;
924
  }
925
 
926
+ const formData = new FormData();
927
+ formData.append('image', resizedImageBlob, 'resized_image.jpg'); // 縮小画像を送信
928
+ formData.append('risk_level', riskLevel);
929
+
930
+ let apiEndpoint;
931
+ if (processingType === "opencv") {
932
+ apiEndpoint = "/create-mask-and-inpaint-opencv";
933
+ } else if (processingType === "simple_lama") {
934
+ apiEndpoint = "/create-mask-and-inpaint-simple-lama";
935
+ } else if (processingType === "stamp") {
936
+ apiEndpoint = "/create-mask-and-inpaint-stamp";
937
+ } else if (processingType == "mosaic") {
938
+ apiEndpoint = "/create-mask-and-inpaint-mosaic";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
939
  }
940
+
941
+ const url = "https://rein0421-aidentify.hf.space" + apiEndpoint;
942
+
943
+ fetch(url, {
944
+ method: 'POST',
945
+ body: formData
946
+ })
947
+ .then(response => {
948
+ if (!response.ok) {
949
+ throw new Error("Network response was not ok");
950
+ }
951
+ return response.blob();
952
+ })
953
+ .then(blob => {
954
+ const objectURL = URL.createObjectURL(blob);
955
+ processedImage.src = objectURL;
956
+ downloadLink.href = objectURL;
957
+ resultDiv.style.display = "block";
958
+ })
959
+ .catch(error => {
960
+ console.error("画像処理に失敗しました。", error);
961
+ alert("画像処理に失敗しました。");
962
+ });
963
+ }
964
+ </script>
965
  </body>
966
  </html>
967
 
 
968
  """
969
  return HTMLResponse(content=html_content)
970
  if __name__ == "__main__":