dahyedahye commited on
Commit
00d8519
ยท
1 Parent(s): a7b1429
Files changed (5) hide show
  1. app.py.backup +0 -73
  2. app.py.bacup2 +0 -81
  3. requirements.txt +0 -1
  4. temp_image.png +0 -0
  5. tmp.ipynb +0 -0
app.py.backup DELETED
@@ -1,73 +0,0 @@
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)})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py.bacup2 DELETED
@@ -1,81 +0,0 @@
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",
12
- servers=[
13
- {
14
- "url": "https://leekwoon-edge-api.hf.space",
15
- "description": "image edge detection API",
16
- }
17
- ],
18
- )
19
-
20
- # Pydantic ๋ชจ๋ธ ์ •์˜
21
- class ImageData(BaseModel):
22
- image_base64: str
23
-
24
- @app.post("/process-image/")
25
- 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)})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -5,5 +5,4 @@ opencv-python-headless # OpenCV, ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, GUI ๊ธฐ๋Šฅ
5
  Pillow # Python Imaging Library (PIL), ์ด๋ฏธ์ง€ ์ž‘์—…์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
6
 
7
  numpy # NumPy, OpenCV ๋ฐ ์ผ๋ฐ˜ ์ˆ˜์น˜ ๊ณ„์‚ฐ์— ํ•„์š”
8
- python-multipart
9
  gdown
 
5
  Pillow # Python Imaging Library (PIL), ์ด๋ฏธ์ง€ ์ž‘์—…์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
6
 
7
  numpy # NumPy, OpenCV ๋ฐ ์ผ๋ฐ˜ ์ˆ˜์น˜ ๊ณ„์‚ฐ์— ํ•„์š”
 
8
  gdown
temp_image.png DELETED
Binary file (24 kB)
 
tmp.ipynb DELETED
File without changes