Commit
·
fd51dc9
1
Parent(s):
dd440d8
- app.py +29 -21
- app.py.backup +73 -0
app.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
import io
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
-
from fastapi import FastAPI,
|
| 6 |
from fastapi.responses import StreamingResponse, JSONResponse
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI(
|
| 9 |
version="0.0.1",
|
|
@@ -15,59 +17,65 @@ app = FastAPI(
|
|
| 15 |
],
|
| 16 |
)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@app.post("/process-image/")
|
| 19 |
-
def process_image(
|
| 20 |
try:
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
if image is None:
|
| 25 |
-
|
| 26 |
|
| 27 |
-
#
|
| 28 |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
edges = cv2.Canny(gray_image, threshold1=100, threshold2=200)
|
| 32 |
-
|
| 33 |
-
# Convert the edges to a PIL Image for display
|
| 34 |
edges_image = Image.fromarray(edges)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
white_background = Image.new("RGB", edges_image.size, (255, 255, 255))
|
| 38 |
|
| 39 |
-
#
|
| 40 |
edges_on_white = Image.composite(Image.new("RGB", edges_image.size, (0, 0, 0)), white_background, edges_image)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
canny_output_io = io.BytesIO()
|
| 44 |
edges_on_white.save(canny_output_io, format='JPEG')
|
| 45 |
canny_output_io.seek(0)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
sobelx = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=5) #
|
| 49 |
-
sobely = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=5) #
|
| 50 |
|
| 51 |
-
#
|
| 52 |
sobel_combined = cv2.magnitude(sobelx, sobely)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
sobel_combined = cv2.normalize(sobel_combined, None, 0, 255, cv2.NORM_MINMAX)
|
| 56 |
sobel_combined = sobel_combined.astype('uint8')
|
| 57 |
|
| 58 |
-
#
|
| 59 |
sobel_image = Image.fromarray(sobel_combined)
|
| 60 |
|
| 61 |
-
#
|
| 62 |
edges_on_white_sobel = Image.composite(Image.new("RGB", sobel_image.size, (0, 0, 0)), white_background, sobel_image)
|
| 63 |
|
| 64 |
-
#
|
| 65 |
sobel_output_io = io.BytesIO()
|
| 66 |
edges_on_white_sobel.save(sobel_output_io, format='JPEG')
|
| 67 |
sobel_output_io.seek(0)
|
| 68 |
|
| 69 |
# StreamingResponse를 사용하여 파일 반환
|
| 70 |
return StreamingResponse(sobel_output_io, media_type='image/jpeg')
|
| 71 |
-
|
| 72 |
except Exception as e:
|
| 73 |
return JSONResponse(status_code=500, content={"message": str(e)})
|
|
|
|
| 1 |
import io
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
import base64
|
| 5 |
from PIL import Image
|
| 6 |
+
from fastapi import FastAPI, HTTPException
|
| 7 |
from fastapi.responses import StreamingResponse, JSONResponse
|
| 8 |
+
from pydantic import BaseModel
|
| 9 |
|
| 10 |
app = FastAPI(
|
| 11 |
version="0.0.1",
|
|
|
|
| 17 |
],
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Pydantic 모델 정의
|
| 21 |
+
class ImageData(BaseModel):
|
| 22 |
+
image_base64: str
|
| 23 |
+
|
| 24 |
@app.post("/process-image/")
|
| 25 |
+
async def process_image(data: ImageData):
|
| 26 |
try:
|
| 27 |
+
# Base64 문자열을 디코딩하여 이미지 데이터를 생성
|
| 28 |
+
image_data = base64.b64decode(data.image_base64)
|
| 29 |
+
# NumPy 배열로 변환
|
| 30 |
+
nparr = np.frombuffer(image_data, np.uint8)
|
| 31 |
+
# OpenCV를 사용하여 이미지 디코딩
|
| 32 |
+
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 33 |
|
| 34 |
if image is None:
|
| 35 |
+
raise HTTPException(status_code=400, detail="Invalid image data")
|
| 36 |
|
| 37 |
+
# 그레이스케일로 변환
|
| 38 |
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 39 |
|
| 40 |
+
# Canny 에지 검출 적용
|
| 41 |
edges = cv2.Canny(gray_image, threshold1=100, threshold2=200)
|
|
|
|
|
|
|
| 42 |
edges_image = Image.fromarray(edges)
|
| 43 |
|
| 44 |
+
# 흰색 배경 생성
|
| 45 |
white_background = Image.new("RGB", edges_image.size, (255, 255, 255))
|
| 46 |
|
| 47 |
+
# 검은색 에지를 흰색 배경 위에 합성
|
| 48 |
edges_on_white = Image.composite(Image.new("RGB", edges_image.size, (0, 0, 0)), white_background, edges_image)
|
| 49 |
|
| 50 |
+
# 최종 Canny 이미지를 BytesIO 객체에 저장
|
| 51 |
canny_output_io = io.BytesIO()
|
| 52 |
edges_on_white.save(canny_output_io, format='JPEG')
|
| 53 |
canny_output_io.seek(0)
|
| 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 이미지를 BytesIO 객체에 저장
|
| 73 |
sobel_output_io = io.BytesIO()
|
| 74 |
edges_on_white_sobel.save(sobel_output_io, format='JPEG')
|
| 75 |
sobel_output_io.seek(0)
|
| 76 |
|
| 77 |
# StreamingResponse를 사용하여 파일 반환
|
| 78 |
return StreamingResponse(sobel_output_io, media_type='image/jpeg')
|
| 79 |
+
|
| 80 |
except Exception as e:
|
| 81 |
return JSONResponse(status_code=500, content={"message": str(e)})
|
app.py.backup
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from fastapi import FastAPI, File, UploadFile
|
| 6 |
+
from fastapi.responses import StreamingResponse, JSONResponse
|
| 7 |
+
|
| 8 |
+
app = FastAPI(
|
| 9 |
+
version="0.0.1",
|
| 10 |
+
servers=[
|
| 11 |
+
{
|
| 12 |
+
"url": "https://leekwoon-edge-api.hf.space",
|
| 13 |
+
"description": "image edge detection API",
|
| 14 |
+
}
|
| 15 |
+
],
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
@app.post("/process-image/")
|
| 19 |
+
def process_image(file: UploadFile = File(...)):
|
| 20 |
+
try:
|
| 21 |
+
# Load the image using OpenCV
|
| 22 |
+
image = cv2.imdecode(np.frombuffer(file.file.read(), np.uint8), cv2.IMREAD_COLOR)
|
| 23 |
+
|
| 24 |
+
if image is None:
|
| 25 |
+
return JSONResponse(status_code=400, content={"message": "Invalid image file."})
|
| 26 |
+
|
| 27 |
+
# Convert the image to grayscale
|
| 28 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 29 |
+
|
| 30 |
+
# Apply Canny edge detection
|
| 31 |
+
edges = cv2.Canny(gray_image, threshold1=100, threshold2=200)
|
| 32 |
+
|
| 33 |
+
# Convert the edges to a PIL Image for display
|
| 34 |
+
edges_image = Image.fromarray(edges)
|
| 35 |
+
|
| 36 |
+
# Create a white background
|
| 37 |
+
white_background = Image.new("RGB", edges_image.size, (255, 255, 255))
|
| 38 |
+
|
| 39 |
+
# Convert edges to an image with black edges on white background
|
| 40 |
+
edges_on_white = Image.composite(Image.new("RGB", edges_image.size, (0, 0, 0)), white_background, edges_image)
|
| 41 |
+
|
| 42 |
+
# Save the final Canny image to a BytesIO object
|
| 43 |
+
canny_output_io = io.BytesIO()
|
| 44 |
+
edges_on_white.save(canny_output_io, format='JPEG')
|
| 45 |
+
canny_output_io.seek(0)
|
| 46 |
+
|
| 47 |
+
# Apply the Sobel operator for edge detection
|
| 48 |
+
sobelx = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=5) # Sobel operator on the X axis
|
| 49 |
+
sobely = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=5) # Sobel operator on the Y axis
|
| 50 |
+
|
| 51 |
+
# Combine the two gradients
|
| 52 |
+
sobel_combined = cv2.magnitude(sobelx, sobely)
|
| 53 |
+
|
| 54 |
+
# Normalize the result to the range [0, 255]
|
| 55 |
+
sobel_combined = cv2.normalize(sobel_combined, None, 0, 255, cv2.NORM_MINMAX)
|
| 56 |
+
sobel_combined = sobel_combined.astype('uint8')
|
| 57 |
+
|
| 58 |
+
# Convert the result to a PIL Image
|
| 59 |
+
sobel_image = Image.fromarray(sobel_combined)
|
| 60 |
+
|
| 61 |
+
# Composite the Sobel edges on a white background
|
| 62 |
+
edges_on_white_sobel = Image.composite(Image.new("RGB", sobel_image.size, (0, 0, 0)), white_background, sobel_image)
|
| 63 |
+
|
| 64 |
+
# Save the final Sobel image to a BytesIO object
|
| 65 |
+
sobel_output_io = io.BytesIO()
|
| 66 |
+
edges_on_white_sobel.save(sobel_output_io, format='JPEG')
|
| 67 |
+
sobel_output_io.seek(0)
|
| 68 |
+
|
| 69 |
+
# StreamingResponse를 사용하여 파일 반환
|
| 70 |
+
return StreamingResponse(sobel_output_io, media_type='image/jpeg')
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return JSONResponse(status_code=500, content={"message": str(e)})
|