dahyedahye commited on
Commit
ac5ec09
Β·
1 Parent(s): 4ae7707
Files changed (3) hide show
  1. Dockerfile +18 -0
  2. app.py +88 -0
  3. requirements.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ RUN useradd -m -u 1000 user
7
+ USER user
8
+ ENV PATH="/home/user/.local/bin:$PATH"
9
+
10
+ WORKDIR /app
11
+
12
+ COPY --chown=user ./requirements.txt requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
+
15
+ COPY --chown=user . /app
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
17
+
18
+
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import shutil
4
+ from PIL import Image
5
+ from fastapi import FastAPI, File, UploadFile
6
+ from fastapi.responses import FileResponse, JSONResponse
7
+ import numpy as np
8
+
9
+ app = FastAPI()
10
+
11
+ @app.post("/process-image/")
12
+ async def process_image(file: UploadFile = File(...)):
13
+ """
14
+ Upload an image file and get the processed image with Canny and Sobel edge detection applied.
15
+ """
16
+ try:
17
+ # Create a temporary directory
18
+ temp_dir = "temp"
19
+ os.makedirs(temp_dir, exist_ok=True)
20
+
21
+ # Save the uploaded file temporarily
22
+ input_file_path = os.path.join(temp_dir, file.filename)
23
+ with open(input_file_path, "wb") as buffer:
24
+ shutil.copyfileobj(file.file, buffer)
25
+
26
+ # Load the image using OpenCV
27
+ image = cv2.imread(input_file_path)
28
+
29
+ if image is None:
30
+ return JSONResponse(status_code=400, content={"message": "Invalid image file."})
31
+
32
+ # Convert the image to grayscale
33
+ gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
34
+
35
+ # Apply Canny edge detection
36
+ edges = cv2.Canny(gray_image, threshold1=100, threshold2=200)
37
+
38
+ # Convert the edges to a PIL Image for display
39
+ edges_image = Image.fromarray(edges)
40
+
41
+ # Create a white background
42
+ white_background = Image.new("RGB", edges_image.size, (255, 255, 255))
43
+
44
+ # Convert edges to an image with black edges on white background
45
+ edges_on_white = Image.composite(Image.new("RGB", edges_image.size, (0, 0, 0)), white_background, edges_image)
46
+
47
+ # Save the final Canny image
48
+ output_canny_path = os.path.join(temp_dir, "canny_edges_white_bg_image.jpg")
49
+ edges_on_white.save(output_canny_path)
50
+
51
+ # Apply the Sobel operator for edge detection
52
+ sobelx = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=5) # Sobel operator on the X axis
53
+ sobely = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=5) # Sobel operator on the Y axis
54
+
55
+ # Combine the two gradients
56
+ sobel_combined = cv2.magnitude(sobelx, sobely)
57
+
58
+ # Normalize the result to the range [0, 255]
59
+ sobel_combined = cv2.normalize(sobel_combined, None, 0, 255, cv2.NORM_MINMAX)
60
+ sobel_combined = sobel_combined.astype('uint8')
61
+
62
+ # Convert the result to a PIL Image
63
+ sobel_image = Image.fromarray(sobel_combined)
64
+
65
+ # Composite the Sobel edges on a white background
66
+ edges_on_white_sobel = Image.composite(Image.new("RGB", sobel_image.size, (0, 0, 0)), white_background, sobel_image)
67
+
68
+ # Save the final Sobel image
69
+ output_sobel_path = os.path.join(temp_dir, "sobel_edges_white_bg_image.jpg")
70
+ edges_on_white_sobel.save(output_sobel_path)
71
+
72
+ # Choose which image to return, for example, return the Canny edge detection image
73
+ return FileResponse(path=output_canny_path, filename="canny_edges_white_bg_image.jpg", media_type='image/jpeg')
74
+
75
+ # If you want to return Sobel edge detection image, uncomment the next line and comment the previous return line
76
+ # return FileResponse(path=output_sobel_path, filename="sobel_edges_white_bg_image.jpg", media_type='image/jpeg')
77
+
78
+ except Exception as e:
79
+ return JSONResponse(status_code=500, content={"message": str(e)})
80
+
81
+ finally:
82
+ # Clean up temporary files
83
+ if os.path.exists(input_file_path):
84
+ os.remove(input_file_path)
85
+ if os.path.exists(output_canny_path):
86
+ os.remove(output_canny_path)
87
+ if os.path.exists(output_sobel_path):
88
+ os.remove(output_sobel_path)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi # FastAPI μ›Ή ν”„λ ˆμž„μ›Œν¬
2
+ uvicorn # FastAPI μ„œλ²„ 싀행을 μœ„ν•œ Uvicorn ASGI μ„œλ²„
3
+
4
+ opencv-python-headless # OpenCV, 이미지 처리 라이브러리, GUI κΈ°λŠ₯이 ν•„μš” μ—†λŠ” ν™˜κ²½μ—μ„œλŠ” headless 버전을 μ‚¬μš©
5
+ Pillow # Python Imaging Library (PIL), 이미지 μž‘μ—…μ„ μœ„ν•œ 라이브러리
6
+
7
+ numpy # NumPy, OpenCV 및 일반 수치 계산에 ν•„μš”