dahyedahye commited on
Commit
6cedaa8
ยท
1 Parent(s): 2d8a39b
Files changed (4) hide show
  1. __pycache__/app.cpython-310.pyc +0 -0
  2. app.py +37 -18
  3. requirements.txt +2 -1
  4. tmp.ipynb +0 -0
__pycache__/app.cpython-310.pyc ADDED
Binary file (1.11 kB). View file
 
app.py CHANGED
@@ -1,8 +1,10 @@
1
- from fastapi import FastAPI, File, UploadFile
2
  import cv2
3
  import numpy as np
4
  from fastapi.responses import StreamingResponse
5
  import io
 
 
6
 
7
  app = FastAPI(
8
  version="0.0.1",
@@ -15,20 +17,37 @@ app = FastAPI(
15
  )
16
 
17
  @app.post("/detect-edges/")
18
- async def detect_edges(file: UploadFile = File(...)):
19
- # ์ด๋ฏธ์ง€๋ฅผ ๋ฐ”์ดํŠธ ๋ฐฐ์—ด๋กœ ์ฝ๊ธฐ
20
- image_bytes = await file.read()
21
- # ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์„ numpy ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
22
- nparr = np.frombuffer(image_bytes, np.uint8)
23
- # numpy ๋ฐฐ์—ด์„ ์ด๋ฏธ์ง€๋กœ ๋””์ฝ”๋”ฉ
24
- img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
25
-
26
- # ์—ฃ์ง€ ๋””ํ…์…˜ ์ˆ˜ํ–‰ (Canny ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉ)
27
- edges = cv2.Canny(img, 100, 200)
28
-
29
- # ์—ฃ์ง€ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์‹œ ์ธ์ฝ”๋”ฉ
30
- _, buffer = cv2.imencode('.png', edges)
31
- io_buf = io.BytesIO(buffer)
32
-
33
- # ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ „์†ก
34
- return StreamingResponse(io_buf, media_type="image/png")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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",
 
17
  )
18
 
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:
37
+ raise HTTPException(status_code=400, detail="Invalid image URL or file not found.")
38
+
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))
requirements.txt CHANGED
@@ -5,4 +5,5 @@ opencv-python-headless # OpenCV, ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, GUI ๊ธฐ๋Šฅ
5
  Pillow # Python Imaging Library (PIL), ์ด๋ฏธ์ง€ ์ž‘์—…์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
6
 
7
  numpy # NumPy, OpenCV ๋ฐ ์ผ๋ฐ˜ ์ˆ˜์น˜ ๊ณ„์‚ฐ์— ํ•„์š”
8
- python-multipart
 
 
5
  Pillow # Python Imaging Library (PIL), ์ด๋ฏธ์ง€ ์ž‘์—…์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
6
 
7
  numpy # NumPy, OpenCV ๋ฐ ์ผ๋ฐ˜ ์ˆ˜์น˜ ๊ณ„์‚ฐ์— ํ•„์š”
8
+ python-multipart
9
+ gdown
tmp.ipynb ADDED
File without changes