rein0421 commited on
Commit
d5eaf1b
·
verified ·
1 Parent(s): 8d6ea56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -11
app.py CHANGED
@@ -11,7 +11,7 @@ from PIL import Image, ImageFilter
11
  import numpy as np
12
  import cv2
13
  import pycocotools.mask as mask_util
14
-
15
  from fastapi import FastAPI, File, UploadFile, Form
16
  from fastapi.middleware.cors import CORSMiddleware
17
  from fastapi.responses import FileResponse, HTMLResponse
@@ -47,7 +47,10 @@ import matplotlib.pyplot as plt
47
  #この下のコードは特定の領域をマスクしないタイプのコード
48
  import uuid
49
  from datetime import datetime
50
-
 
 
 
51
 
52
 
53
  import cv2
@@ -648,10 +651,7 @@ def classify_new_image(new_image_vector, sums_data, loaded_vectors, loaded_objec
648
  new_image_label = knn.predict([new_image_vector])
649
  return new_image_label[0]
650
 
651
- import torch
652
- import cv2
653
- import numpy as np
654
- from ultralytics import YOLO # YOLOv8ライブラリ
655
 
656
  def process_image_vec(image_path):
657
  # GPUを使用できるか確認
@@ -764,7 +764,44 @@ def resize_mask_to_match(image_path, mask_path):
764
 
765
  # マスク画像を上書き保存
766
  resized_mask.save(mask_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
767
 
 
 
768
  @app.post("/create-mask-and-inpaint-sum")
769
  async def create_mask_sum(image: UploadFile = File(...), risk_level: int = Form(...),
770
  x1: float = Form(...),
@@ -778,14 +815,11 @@ async def create_mask_sum(image: UploadFile = File(...), risk_level: int = Form(
778
  point1 = [default_x if math.isnan(x1) else x1, default_y if math.isnan(y1) else y1]
779
 
780
  point2 = [default_x if math.isnan(x2) else x2, default_y if math.isnan(y2) else y2]
781
-
782
-
783
- input_path = save_image(image.file, "input.jpg")
784
- mask_path = special_process_image_yolo(risk_level, input_path, point1, point2,thresholds=thresholds)
785
- # 現在のタイムスタンプを生成
786
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
787
  # 一意な識別子を生成
788
  unique_id = uuid.uuid4().hex
 
 
789
  output_path = f"./output_simple_lama_{timestamp}_{unique_id}.jpg"
790
 
791
  # OpenCVでインペイント
 
11
  import numpy as np
12
  import cv2
13
  import pycocotools.mask as mask_util
14
+ import face_recognition
15
  from fastapi import FastAPI, File, UploadFile, Form
16
  from fastapi.middleware.cors import CORSMiddleware
17
  from fastapi.responses import FileResponse, HTMLResponse
 
47
  #この下のコードは特定の領域をマスクしないタイプのコード
48
  import uuid
49
  from datetime import datetime
50
+ import torch
51
+ import cv2
52
+ import numpy as np
53
+ from ultralytics import YOLO # YOLOv8ライブラリ
54
 
55
 
56
  import cv2
 
651
  new_image_label = knn.predict([new_image_vector])
652
  return new_image_label[0]
653
 
654
+
 
 
 
655
 
656
  def process_image_vec(image_path):
657
  # GPUを使用できるか確認
 
764
 
765
  # マスク画像を上書き保存
766
  resized_mask.save(mask_path)
767
+ @app.post("/inpaint_with_face")
768
+ async def face_recognize(
769
+ image: UploadFile = File(...),
770
+ face_image: UploadFile = File(...),
771
+ option: int = Form(...),
772
+ stamp_image: UploadFile = File(...)
773
+ ):
774
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
775
+ unique_id = uuid.uuid4().hex
776
+ input_path = save_image(image.file, f"./input_{timestamp}_{unique_id}.jpg")
777
+ stamp_path = save_image(stamp_image.file, f"./stamp_{timestamp}_{unique_id}.jpg")
778
+ face_path = save_image(face_image.file, f"./face_{timestamp}_{unique_id}.jpg")
779
+
780
+ # 画像の読み込み
781
+ target_image = face_recognition.load_image_file(input_path)
782
+ rgb_image = cv2.cvtColor(target_image, cv2.COLOR_RGB2BGR)
783
+ stamp_img = cv2.imread(stamp_path)
784
+
785
+ # 顔の位置とエンコーディングを取得
786
+ face_locations = face_recognition.face_locations(target_image)
787
+ face_encodings = face_recognition.face_encodings(target_image, face_locations)
788
+
789
+ # 顔認識してスタンプまたはモザイクで隠す処理
790
+ for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
791
+ if option == 1: # スタンプで隠す
792
+ stamp_resized = cv2.resize(stamp_img, (right - left, bottom - top))
793
+ rgb_image[top:bottom, left:right] = stamp_resized
794
+ elif option == 2: # モザイクで隠す
795
+ face_region = rgb_image[top:bottom, left:right]
796
+ blurred_face = cv2.GaussianBlur(face_region, (99, 99), 30)
797
+ rgb_image[top:bottom, left:right] = blurred_face
798
+
799
+ # 結果を保存または返却 (ここでは仮に保存)
800
+ output_path = f"./output_{timestamp}_{unique_id}.jpg"
801
+ cv2.imwrite(output_path, rgb_image)
802
 
803
+ return FileResponse(output_path)
804
+
805
  @app.post("/create-mask-and-inpaint-sum")
806
  async def create_mask_sum(image: UploadFile = File(...), risk_level: int = Form(...),
807
  x1: float = Form(...),
 
815
  point1 = [default_x if math.isnan(x1) else x1, default_y if math.isnan(y1) else y1]
816
 
817
  point2 = [default_x if math.isnan(x2) else x2, default_y if math.isnan(y2) else y2]
 
 
 
 
 
818
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
819
  # 一意な識別子を生成
820
  unique_id = uuid.uuid4().hex
821
+ input_path = save_image(image.file, f"./input_{timestamp}_{unique_id}.jpg")
822
+ mask_path = special_process_image_yolo(risk_level, input_path, point1, point2,thresholds=thresholds)
823
  output_path = f"./output_simple_lama_{timestamp}_{unique_id}.jpg"
824
 
825
  # OpenCVでインペイント