Commit
ยท
ef7cf2b
1
Parent(s):
6cedaa8
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
from fastapi.responses import
|
| 5 |
import io
|
| 6 |
import gdown
|
| 7 |
import os
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI(
|
| 10 |
version="0.0.1",
|
|
@@ -19,18 +20,16 @@ app = FastAPI(
|
|
| 19 |
@app.post("/detect-edges/")
|
| 20 |
def detect_edges(url: str):
|
| 21 |
# Google Drive ํ์ผ ID ์ถ์ถ
|
| 22 |
-
print('got ', str)
|
| 23 |
-
print('###############')
|
| 24 |
try:
|
| 25 |
file_id = url.split('/')[-2]
|
| 26 |
download_url = f"https://drive.google.com/uc?id={file_id}"
|
| 27 |
|
| 28 |
# ์ด๋ฏธ์ง๋ฅผ Google Drive์์ ๋ค์ด๋ก๋
|
| 29 |
-
|
| 30 |
-
gdown.download(download_url,
|
| 31 |
|
| 32 |
# ์ด๋ฏธ์ง๋ฅผ ์ฝ์ด๋ค์
|
| 33 |
-
img = cv2.imread(
|
| 34 |
|
| 35 |
# ์ด๋ฏธ์ง๊ฐ ์ ๋๋ก ์ฝํ๋์ง ํ์ธ
|
| 36 |
if img is None:
|
|
@@ -39,15 +38,24 @@ def detect_edges(url: str):
|
|
| 39 |
# ์ฃ์ง ๋ํ
์
์ํ (Canny ์๊ณ ๋ฆฌ์ฆ ์ฌ์ฉ)
|
| 40 |
edges = cv2.Canny(img, 100, 200)
|
| 41 |
|
| 42 |
-
# ์ฃ์ง ์ด๋ฏธ์ง๋ฅผ
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
# ์์ ํ์ผ ์ญ์
|
| 47 |
-
os.remove(
|
| 48 |
|
| 49 |
-
# ๊ฒฐ๊ณผ
|
| 50 |
-
return
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
-
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
from fastapi.responses import FileResponse
|
| 5 |
import io
|
| 6 |
import gdown
|
| 7 |
import os
|
| 8 |
+
import uuid
|
| 9 |
|
| 10 |
app = FastAPI(
|
| 11 |
version="0.0.1",
|
|
|
|
| 20 |
@app.post("/detect-edges/")
|
| 21 |
def detect_edges(url: str):
|
| 22 |
# Google Drive ํ์ผ ID ์ถ์ถ
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
file_id = url.split('/')[-2]
|
| 25 |
download_url = f"https://drive.google.com/uc?id={file_id}"
|
| 26 |
|
| 27 |
# ์ด๋ฏธ์ง๋ฅผ Google Drive์์ ๋ค์ด๋ก๋
|
| 28 |
+
temp_input_image = f'/tmp/{uuid.uuid4()}.png'
|
| 29 |
+
gdown.download(download_url, temp_input_image, quiet=False)
|
| 30 |
|
| 31 |
# ์ด๋ฏธ์ง๋ฅผ ์ฝ์ด๋ค์
|
| 32 |
+
img = cv2.imread(temp_input_image)
|
| 33 |
|
| 34 |
# ์ด๋ฏธ์ง๊ฐ ์ ๋๋ก ์ฝํ๋์ง ํ์ธ
|
| 35 |
if img is None:
|
|
|
|
| 38 |
# ์ฃ์ง ๋ํ
์
์ํ (Canny ์๊ณ ๋ฆฌ์ฆ ์ฌ์ฉ)
|
| 39 |
edges = cv2.Canny(img, 100, 200)
|
| 40 |
|
| 41 |
+
# ์ฃ์ง ์ด๋ฏธ์ง๋ฅผ ํ์ผ๋ก ์ ์ฅ
|
| 42 |
+
output_file_path = f'/tmp/{uuid.uuid4()}.png'
|
| 43 |
+
cv2.imwrite(output_file_path, edges)
|
| 44 |
|
| 45 |
+
# ์
๋ ฅ ์ด๋ฏธ์ง ์์ ํ์ผ ์ญ์
|
| 46 |
+
os.remove(temp_input_image)
|
| 47 |
|
| 48 |
+
# ํด๋ผ์ด์ธํธ์๊ฒ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ํ์ผ ๊ฒฝ๋ก๋ฅผ ๋ฐํ
|
| 49 |
+
return {"download_url": f"/download/{os.path.basename(output_file_path)}"}
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@app.get("/download/{filename}")
|
| 56 |
+
def download_file(filename: str):
|
| 57 |
+
file_path = f"/tmp/{filename}"
|
| 58 |
+
if os.path.exists(file_path):
|
| 59 |
+
return FileResponse(file_path, media_type='image/png', filename=filename)
|
| 60 |
+
else:
|
| 61 |
+
raise HTTPException(status_code=404, detail="File not found")
|