dahyedahye commited on
Commit
ef7cf2b
ยท
1 Parent(s): 6cedaa8
Files changed (1) hide show
  1. app.py +22 -14
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 StreamingResponse
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
- output = 'temp_image.png'
30
- gdown.download(download_url, output, quiet=False)
31
 
32
  # ์ด๋ฏธ์ง€๋ฅผ ์ฝ์–ด๋“ค์ž„
33
- img = cv2.imread(output)
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
- _, buffer = cv2.imencode('.png', edges)
44
- io_buf = io.BytesIO(buffer)
45
 
46
- # ์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ
47
- os.remove(output)
48
 
49
- # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ „์†ก
50
- return StreamingResponse(io_buf, media_type="image/png")
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")