Spaces:
Running
Running
syurein
commited on
Commit
·
d6a2575
1
Parent(s):
c0dcf37
ダミーの作成
Browse files
app.py
CHANGED
@@ -80,6 +80,20 @@ app.add_middleware(
|
|
80 |
allow_methods=["*"],
|
81 |
allow_headers=["*"],
|
82 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
load_dotenv(dotenv_path='../.env')
|
85 |
HOME = "./"
|
@@ -991,7 +1005,7 @@ async def classify_image_llm(file: UploadFile = File(...)):
|
|
991 |
danger_level = ObjectDetector().detect_danger_level(image_path)
|
992 |
return {"danger":danger_level}
|
993 |
|
994 |
-
|
995 |
@app.post("/analyze")
|
996 |
async def create_mask_sum3(image: UploadFile = File(...),
|
997 |
scene: str = Form(...),
|
@@ -1032,7 +1046,88 @@ async def create_mask_sum3(image: UploadFile = File(...),
|
|
1032 |
|
1033 |
return FileResponse(output_path)
|
1034 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1035 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1036 |
@app.post("/create-mask-and-inpaint-sum-llm-simple")
|
1037 |
async def create_mask_sum2(image: UploadFile = File(...), risk_level: int = Form(...),
|
1038 |
x1: float = Form(...),
|
|
|
80 |
allow_methods=["*"],
|
81 |
allow_headers=["*"],
|
82 |
)
|
83 |
+
# デバッグ用のダミーレスポンス設定
|
84 |
+
USE_DUMMY_RESPONSE = True # ダミーレスポンスを有効化
|
85 |
+
DUMMY_RESPONSE_TYPE = 'random' # 'random', 'fixed', 'error' のいずれかを選択
|
86 |
+
# ダミー画像を生成する関数
|
87 |
+
def create_dummy_image(width=512, height=512, color=(255, 0, 0)):
|
88 |
+
"""単色のダミー画像を生成して一時ファイルに保存"""
|
89 |
+
image = np.full((height, width, 3), color, dtype=np.uint8)
|
90 |
+
temp_path = SAVE_DIR / f"dummy_image_{uuid.uuid4().hex}.jpg"
|
91 |
+
cv2.imwrite(str(temp_path), image)
|
92 |
+
return temp_path
|
93 |
+
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
|
98 |
load_dotenv(dotenv_path='../.env')
|
99 |
HOME = "./"
|
|
|
1005 |
danger_level = ObjectDetector().detect_danger_level(image_path)
|
1006 |
return {"danger":danger_level}
|
1007 |
|
1008 |
+
'''
|
1009 |
@app.post("/analyze")
|
1010 |
async def create_mask_sum3(image: UploadFile = File(...),
|
1011 |
scene: str = Form(...),
|
|
|
1046 |
|
1047 |
return FileResponse(output_path)
|
1048 |
|
1049 |
+
'''
|
1050 |
+
@app.post("/analyze")
|
1051 |
+
async def analyze(
|
1052 |
+
file: UploadFile = File(...), # Next.jsの`file`に対応
|
1053 |
+
scene: str = Form(...),
|
1054 |
+
mode: str = Form(...),
|
1055 |
+
risk_level: str = Form(None), # `value`を`risk_level`に変更
|
1056 |
+
rect: str = Form(None),
|
1057 |
+
):
|
1058 |
+
try:
|
1059 |
+
# 必須フィールドのバリデーション
|
1060 |
+
if not file or not scene or not mode:
|
1061 |
+
raise HTTPException(status_code=400, detail="必須フィールドが不足: file, scene, または mode")
|
1062 |
+
|
1063 |
+
# デバッグモード(ダミーレスポンス)が有効な場合
|
1064 |
+
if USE_DUMMY_RESPONSE:
|
1065 |
+
print(f"デバッグモード: ダミーレスポンスを使用 (タイプ: {DUMMY_RESPONSE_TYPE})")
|
1066 |
+
|
1067 |
+
# テスト用ダミーレスポンスのロジック
|
1068 |
+
match DUMMY_RESPONSE_TYPE:
|
1069 |
+
case 'fixed':
|
1070 |
+
# 固定値のダミーレスポンス(危険度50)
|
1071 |
+
dummy_image_path = create_dummy_image(color=(0, 255, 0)) # 緑色のダミー画像
|
1072 |
+
return FileResponse(
|
1073 |
+
dummy_image_path,
|
1074 |
+
media_type="image/jpeg",
|
1075 |
+
headers={"x-danger": "50"}
|
1076 |
+
)
|
1077 |
+
|
1078 |
+
case 'error':
|
1079 |
+
# エラーシナリオのダミーレスポンス(400エラー)
|
1080 |
+
raise HTTPException(status_code=400, detail="テスト用エラー: 無効なリクエスト")
|
1081 |
+
|
1082 |
+
case 'random' | _:
|
1083 |
+
# ランダムな危険度のダミーレスポンス(デフォルト)
|
1084 |
+
danger = random.randint(0, 100)
|
1085 |
+
dummy_image_path = create_dummy_image(color=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
|
1086 |
+
return FileResponse(
|
1087 |
+
dummy_image_path,
|
1088 |
+
media_type="image/jpeg",
|
1089 |
+
headers={"x-danger": str(danger)}
|
1090 |
+
)
|
1091 |
+
|
1092 |
+
# 通常の処理(ダミー無効の場合)
|
1093 |
+
default_x = 0.001
|
1094 |
+
default_y = 0.001
|
1095 |
+
risk_level = int(risk_level) if risk_level and risk_level.isdigit() else 0 # リスクレベルを整数に変換
|
1096 |
+
|
1097 |
+
# rect JSON文字列をパースして座標を取得
|
1098 |
+
try:
|
1099 |
+
if rect and rect.strip() and rect.lower() != 'null':
|
1100 |
+
rect_data = json.loads(rect)
|
1101 |
+
x1 = float(rect_data.get('x1', default_x))
|
1102 |
+
y1 = float(rect_data.get('y1', default_y))
|
1103 |
+
x2 = float(rect_data.get('x2', default_x))
|
1104 |
+
y2 = float(rect_data.get('y2', default_y))
|
1105 |
+
else:
|
1106 |
+
x1, y1, x2, y2 = default_x, default_y, default_x, default_y
|
1107 |
+
except (json.JSONDecodeError, TypeError, ValueError):
|
1108 |
+
x1, y1, x2, y2 = default_x, default_y, default_x, default_y
|
1109 |
|
1110 |
+
point1 = [x1, y1]
|
1111 |
+
point2 = [x2, y2]
|
1112 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
1113 |
+
unique_id = uuid.uuid4().hex
|
1114 |
+
input_path = save_image(file.file, f"input_{timestamp}_{unique_id}.jpg")
|
1115 |
+
mask_path = llm_to_process_image_simple(risk_level, input_path, point1, point2, thresholds=thresholds)
|
1116 |
+
output_path = f"./output_simple_lama_{timestamp}_{unique_id}.jpg"
|
1117 |
+
print(f'point1,point2: {point1},{point2}') # 消去したくない範囲
|
1118 |
+
|
1119 |
+
# OpenCVでインペイント
|
1120 |
+
inpaint_image_with_mask1(input_path, mask_path, output_path)
|
1121 |
+
|
1122 |
+
return FileResponse(
|
1123 |
+
output_path,
|
1124 |
+
media_type="image/jpeg",
|
1125 |
+
headers={"x-danger": "0"} # 実際の処理では危険度を適切に設定
|
1126 |
+
)
|
1127 |
+
|
1128 |
+
except Exception as e:
|
1129 |
+
print(f"リクエスト処理エラー: {str(e)}")
|
1130 |
+
raise HTTPException(status_code=500, detail="内部サーバーエラー")
|
1131 |
@app.post("/create-mask-and-inpaint-sum-llm-simple")
|
1132 |
async def create_mask_sum2(image: UploadFile = File(...), risk_level: int = Form(...),
|
1133 |
x1: float = Form(...),
|