Commit
ยท
be78bf0
1
Parent(s):
69e1abf
- __pycache__/app.cpython-310.pyc +0 -0
- app.py +55 -13
- temp_image.png +0 -0
- tmp/7bf0a865-7d2b-4439-b234-b6ae50bc48c4.png +0 -0
__pycache__/app.cpython-310.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-310.pyc and b/__pycache__/app.cpython-310.pyc differ
|
|
|
app.py
CHANGED
|
@@ -1,26 +1,30 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
from
|
| 5 |
import io
|
| 6 |
import gdown
|
| 7 |
import os
|
| 8 |
import uuid
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
app = FastAPI(
|
| 11 |
version="0.0.1",
|
| 12 |
servers=[
|
| 13 |
{
|
| 14 |
-
"url":
|
| 15 |
"description": "image edge detection API",
|
| 16 |
}
|
| 17 |
],
|
| 18 |
)
|
| 19 |
|
| 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 |
|
|
@@ -29,25 +33,63 @@ def detect_edges(url: str):
|
|
| 29 |
gdown.download(download_url, temp_input_image, quiet=False)
|
| 30 |
|
| 31 |
# ์ด๋ฏธ์ง๋ฅผ ์ฝ์ด๋ค์
|
| 32 |
-
|
| 33 |
|
| 34 |
# ์ด๋ฏธ์ง๊ฐ ์ ๋๋ก ์ฝํ๋์ง ํ์ธ
|
| 35 |
-
if
|
| 36 |
raise HTTPException(status_code=400, detail="Invalid image URL or file not found.")
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
output_file_path = f'/tmp/{uuid.uuid4()}.png'
|
| 43 |
-
|
| 44 |
|
| 45 |
# ์
๋ ฅ ์ด๋ฏธ์ง ์์ ํ์ผ ์ญ์
|
| 46 |
os.remove(temp_input_image)
|
| 47 |
|
|
|
|
|
|
|
|
|
|
| 48 |
# ํด๋ผ์ด์ธํธ์๊ฒ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ํ์ผ ๊ฒฝ๋ก๋ฅผ ๋ฐํ
|
| 49 |
-
return {"download_url":
|
| 50 |
-
|
| 51 |
except Exception as e:
|
| 52 |
raise HTTPException(status_code=400, detail=str(e))
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
import io
|
| 7 |
import gdown
|
| 8 |
import os
|
| 9 |
import uuid
|
| 10 |
|
| 11 |
+
# URL = "https://leekwoon-edge-api.hf.space"
|
| 12 |
+
URL = "http://localhost:7860"
|
| 13 |
+
|
| 14 |
app = FastAPI(
|
| 15 |
version="0.0.1",
|
| 16 |
servers=[
|
| 17 |
{
|
| 18 |
+
"url": URL,
|
| 19 |
"description": "image edge detection API",
|
| 20 |
}
|
| 21 |
],
|
| 22 |
)
|
| 23 |
|
| 24 |
@app.post("/detect-edges/")
|
| 25 |
+
def detect_edges(url: str, request: Request):
|
|
|
|
| 26 |
try:
|
| 27 |
+
# Google Drive ํ์ผ ID ์ถ์ถ
|
| 28 |
file_id = url.split('/')[-2]
|
| 29 |
download_url = f"https://drive.google.com/uc?id={file_id}"
|
| 30 |
|
|
|
|
| 33 |
gdown.download(download_url, temp_input_image, quiet=False)
|
| 34 |
|
| 35 |
# ์ด๋ฏธ์ง๋ฅผ ์ฝ์ด๋ค์
|
| 36 |
+
image = cv2.imread(temp_input_image)
|
| 37 |
|
| 38 |
# ์ด๋ฏธ์ง๊ฐ ์ ๋๋ก ์ฝํ๋์ง ํ์ธ
|
| 39 |
+
if image is None:
|
| 40 |
raise HTTPException(status_code=400, detail="Invalid image URL or file not found.")
|
| 41 |
|
| 42 |
+
# ๊ทธ๋ ์ด์ค์ผ์ผ๋ก ๋ณํ
|
| 43 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 44 |
+
|
| 45 |
+
# Canny ์์ง ๊ฒ์ถ ์ ์ฉ
|
| 46 |
+
edges = cv2.Canny(gray_image, threshold1=100, threshold2=200)
|
| 47 |
+
edges_image = Image.fromarray(edges)
|
| 48 |
+
|
| 49 |
+
# ํฐ์ ๋ฐฐ๊ฒฝ ์์ฑ
|
| 50 |
+
white_background = Image.new("RGB", edges_image.size, (255, 255, 255))
|
| 51 |
+
|
| 52 |
+
# Canny ์์ง๋ฅผ ํฐ์ ๋ฐฐ๊ฒฝ ์์ ํฉ์ฑ
|
| 53 |
+
edges_on_white = Image.composite(Image.new("RGB", edges_image.size, (0, 0, 0)), white_background, edges_image)
|
| 54 |
+
|
| 55 |
+
# Sobel ์ฐ์ฐ์ ์ ์ฉ
|
| 56 |
+
sobelx = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=5) # X ์ถ์ ๋ํ Sobel ์ฐ์ฐ์
|
| 57 |
+
sobely = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=5) # Y ์ถ์ ๋ํ Sobel ์ฐ์ฐ์
|
| 58 |
|
| 59 |
+
# ๋ ๊ธฐ์ธ๊ธฐ ๊ฒฐํฉ
|
| 60 |
+
sobel_combined = cv2.magnitude(sobelx, sobely)
|
| 61 |
+
|
| 62 |
+
# ๊ฒฐ๊ณผ๋ฅผ [0, 255] ๋ฒ์๋ก ์ ๊ทํ
|
| 63 |
+
sobel_combined = cv2.normalize(sobel_combined, None, 0, 255, cv2.NORM_MINMAX)
|
| 64 |
+
sobel_combined = sobel_combined.astype('uint8')
|
| 65 |
+
|
| 66 |
+
# ๊ฒฐ๊ณผ๋ฅผ PIL ์ด๋ฏธ์ง๋ก ๋ณํ
|
| 67 |
+
sobel_image = Image.fromarray(sobel_combined)
|
| 68 |
+
|
| 69 |
+
# Sobel ์์ง๋ฅผ ํฐ์ ๋ฐฐ๊ฒฝ ์์ ํฉ์ฑ
|
| 70 |
+
edges_on_white_sobel = Image.composite(Image.new("RGB", sobel_image.size, (0, 0, 0)), white_background, sobel_image)
|
| 71 |
+
|
| 72 |
+
# ์ต์ข
Sobel ์ด๋ฏธ์ง๋ฅผ ํ์ผ๋ก ์ ์ฅ (PNG ํ์)
|
| 73 |
output_file_path = f'/tmp/{uuid.uuid4()}.png'
|
| 74 |
+
edges_on_white_sobel.save(output_file_path, format='PNG')
|
| 75 |
|
| 76 |
# ์
๋ ฅ ์ด๋ฏธ์ง ์์ ํ์ผ ์ญ์
|
| 77 |
os.remove(temp_input_image)
|
| 78 |
|
| 79 |
+
# ์๋ฒ์ URL์ ๊ธฐ๋ฐ์ผ๋ก ๋ค์ด๋ก๋ URL ์์ฑ
|
| 80 |
+
download_url = os.path.join(URL, "tmp", os.path.basename(output_file_path))
|
| 81 |
+
|
| 82 |
# ํด๋ผ์ด์ธํธ์๊ฒ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ํ์ผ ๊ฒฝ๋ก๋ฅผ ๋ฐํ
|
| 83 |
+
return {"download_url": download_url}
|
| 84 |
+
|
| 85 |
except Exception as e:
|
| 86 |
raise HTTPException(status_code=400, detail=str(e))
|
| 87 |
|
| 88 |
+
|
| 89 |
+
@app.get("/tmp/{filename}")
|
| 90 |
+
def download_file(filename: str):
|
| 91 |
+
file_path = f"/tmp/{filename}"
|
| 92 |
+
if os.path.exists(file_path):
|
| 93 |
+
return FileResponse(file_path, media_type='image/png', filename=filename)
|
| 94 |
+
else:
|
| 95 |
+
raise HTTPException(status_code=404, detail="File not found")
|
temp_image.png
ADDED
|
tmp/7bf0a865-7d2b-4439-b234-b6ae50bc48c4.png
ADDED
|