dahyedahye commited on
Commit
be78bf0
ยท
1 Parent(s): 69e1abf
__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 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",
12
  servers=[
13
  {
14
- "url": "https://leekwoon-edge-api.hf.space",
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
- img = cv2.imread(temp_input_image)
33
 
34
  # ์ด๋ฏธ์ง€๊ฐ€ ์ œ๋Œ€๋กœ ์ฝํ˜”๋Š”์ง€ ํ™•์ธ
35
- if img is None:
36
  raise HTTPException(status_code=400, detail="Invalid image URL or file not found.")
37
 
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
 
 
 
 
 
 
 
 
 
 
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