Spaces:
Sleeping
Sleeping
msmhmorsi
commited on
Commit
·
d24e4b0
1
Parent(s):
57f593d
added all
Browse files- Dockerfile +16 -5
- app.py +155 -5
- requirements.txt +8 -2
Dockerfile
CHANGED
|
@@ -1,16 +1,27 @@
|
|
| 1 |
-
#
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 1 |
+
# Use the official Python image from the Docker Hub
|
|
|
|
|
|
|
| 2 |
FROM python:3.9
|
| 3 |
|
| 4 |
RUN useradd -m -u 1000 user
|
| 5 |
USER user
|
| 6 |
ENV PATH="/home/user/.local/bin:$PATH"
|
| 7 |
|
| 8 |
+
|
| 9 |
+
# Install system dependencies for OpenCV
|
| 10 |
+
RUN apt-get update && apt-get install -y \
|
| 11 |
+
libgl1-mesa-glx \
|
| 12 |
+
&& apt-get clean \
|
| 13 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 14 |
+
|
| 15 |
+
# Set the working directory in the container
|
| 16 |
WORKDIR /app
|
| 17 |
|
| 18 |
+
# Copy the requirements file into the container
|
| 19 |
COPY --chown=user ./requirements.txt requirements.txt
|
|
|
|
| 20 |
|
| 21 |
+
# Install the dependencies
|
| 22 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 23 |
+
|
| 24 |
+
# Copy the rest of the application code
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import fitz
|
| 3 |
+
import numpy as np
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from skimage.color import rgb2gray
|
| 7 |
+
from skimage.measure import label, regionprops
|
| 8 |
+
from fastapi.responses import StreamingResponse
|
| 9 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 11 |
|
| 12 |
+
app = FastAPI(
|
| 13 |
+
title="PDF Processing API",
|
| 14 |
+
description="API for converting PDF to PNG and enhancing images",
|
| 15 |
+
version="1.0.0"
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
# Add CORS middleware
|
| 19 |
+
app.add_middleware(
|
| 20 |
+
CORSMiddleware,
|
| 21 |
+
allow_origins=["*"], # Allows all origins
|
| 22 |
+
allow_credentials=True,
|
| 23 |
+
allow_methods=["*"], # Allows all methods
|
| 24 |
+
allow_headers=["*"], # Allows all headers
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def convert_and_process_pdf(pdf_content: bytes, area_threshold: int = 100) -> BytesIO:
|
| 29 |
+
"""
|
| 30 |
+
Convert the first page of a PDF to a PNG and apply image enhancement.
|
| 31 |
+
Args:
|
| 32 |
+
pdf_content: The PDF file content as bytes.
|
| 33 |
+
area_threshold: Threshold for area filtering (default: 100).
|
| 34 |
+
Returns:
|
| 35 |
+
BytesIO: Enhanced PNG image content.
|
| 36 |
+
"""
|
| 37 |
+
# Open the PDF from bytes
|
| 38 |
+
doc = fitz.open(stream=pdf_content, filetype="pdf")
|
| 39 |
+
|
| 40 |
+
# Load the first page
|
| 41 |
+
page = doc.load_page(0)
|
| 42 |
+
|
| 43 |
+
# Render the page as an image
|
| 44 |
+
pix = page.get_pixmap(dpi=300)
|
| 45 |
+
png_image = pix.tobytes("png")
|
| 46 |
+
|
| 47 |
+
# Load the image with OpenCV
|
| 48 |
+
np_array = np.frombuffer(png_image, dtype=np.uint8)
|
| 49 |
+
img = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
|
| 50 |
+
|
| 51 |
+
# Convert to grayscale
|
| 52 |
+
img_gray = rgb2gray(img)
|
| 53 |
+
|
| 54 |
+
# Convert grayscale to binary using Otsu's threshold
|
| 55 |
+
_, img_binary = cv2.threshold((img_gray * 255).astype(np.uint8), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 56 |
+
|
| 57 |
+
# Invert the binary image
|
| 58 |
+
img_binary = ~img_binary
|
| 59 |
+
|
| 60 |
+
# Label connected components
|
| 61 |
+
label_img = label(img_binary)
|
| 62 |
+
regions = regionprops(label_img)
|
| 63 |
+
|
| 64 |
+
# Filter by area threshold
|
| 65 |
+
valid_labels = [region.label for region in regions if region.area >= area_threshold]
|
| 66 |
+
img_filtered = np.isin(label_img, valid_labels)
|
| 67 |
+
|
| 68 |
+
# Save enhanced image to memory
|
| 69 |
+
output_buffer = BytesIO()
|
| 70 |
+
plt.imsave(output_buffer, ~img_filtered, cmap="gray", format="png")
|
| 71 |
+
output_buffer.seek(0)
|
| 72 |
+
return output_buffer
|
| 73 |
+
|
| 74 |
+
@app.post("/process-pdf/")
|
| 75 |
+
async def process_pdf(
|
| 76 |
+
file: UploadFile = File(...),
|
| 77 |
+
area_threshold: int = 100
|
| 78 |
+
):
|
| 79 |
+
"""
|
| 80 |
+
Process a PDF file and return an enhanced PNG image.
|
| 81 |
+
Args:
|
| 82 |
+
file: The PDF file to process
|
| 83 |
+
area_threshold: Threshold for area filtering (default: 100)
|
| 84 |
+
Returns:
|
| 85 |
+
StreamingResponse: Enhanced PNG image
|
| 86 |
+
"""
|
| 87 |
+
try:
|
| 88 |
+
# Read PDF file content
|
| 89 |
+
pdf_content = await file.read()
|
| 90 |
+
|
| 91 |
+
# Process the PDF and get the enhanced image
|
| 92 |
+
enhanced_image = convert_and_process_pdf(pdf_content, area_threshold)
|
| 93 |
+
|
| 94 |
+
# Return the processed image as a StreamingResponse
|
| 95 |
+
return StreamingResponse(
|
| 96 |
+
enhanced_image,
|
| 97 |
+
media_type="image/png",
|
| 98 |
+
headers={"Content-Disposition": f"attachment; filename={file.filename.rsplit('.', 1)[0]}_enhanced.png"}
|
| 99 |
+
)
|
| 100 |
+
except Exception as e:
|
| 101 |
+
raise HTTPException(status_code=500, detail=f"Error processing PDF: {str(e)}")
|
| 102 |
+
|
| 103 |
+
@app.post("/process-image/")
|
| 104 |
+
async def process_image(
|
| 105 |
+
file: UploadFile = File(...),
|
| 106 |
+
area_threshold: int = 100
|
| 107 |
+
):
|
| 108 |
+
"""
|
| 109 |
+
Process an image file and return an enhanced image.
|
| 110 |
+
Args:
|
| 111 |
+
file: The image file to process
|
| 112 |
+
area_threshold: Threshold for area filtering (default: 100)
|
| 113 |
+
Returns:
|
| 114 |
+
StreamingResponse: Enhanced image
|
| 115 |
+
"""
|
| 116 |
+
try:
|
| 117 |
+
# Read image file content
|
| 118 |
+
image_content = await file.read()
|
| 119 |
+
|
| 120 |
+
# Convert to numpy array
|
| 121 |
+
np_array = np.frombuffer(image_content, dtype=np.uint8)
|
| 122 |
+
img = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
|
| 123 |
+
|
| 124 |
+
# Convert to grayscale
|
| 125 |
+
img_gray = rgb2gray(img)
|
| 126 |
+
|
| 127 |
+
# Convert grayscale to binary using Otsu's threshold
|
| 128 |
+
_, img_binary = cv2.threshold((img_gray * 255).astype(np.uint8), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 129 |
+
|
| 130 |
+
# Invert the binary image
|
| 131 |
+
img_binary = ~img_binary
|
| 132 |
+
|
| 133 |
+
# Label connected components
|
| 134 |
+
label_img = label(img_binary)
|
| 135 |
+
regions = regionprops(label_img)
|
| 136 |
+
|
| 137 |
+
# Filter by area threshold
|
| 138 |
+
valid_labels = [region.label for region in regions if region.area >= area_threshold]
|
| 139 |
+
img_filtered = np.isin(label_img, valid_labels)
|
| 140 |
+
|
| 141 |
+
# Save enhanced image to memory
|
| 142 |
+
output_buffer = BytesIO()
|
| 143 |
+
plt.imsave(output_buffer, ~img_filtered, cmap="gray", format="png")
|
| 144 |
+
output_buffer.seek(0)
|
| 145 |
+
|
| 146 |
+
# Return the processed image as a StreamingResponse
|
| 147 |
+
return StreamingResponse(
|
| 148 |
+
output_buffer,
|
| 149 |
+
media_type="image/png",
|
| 150 |
+
headers={"Content-Disposition": f"attachment; filename={file.filename.rsplit('.', 1)[0]}_enhanced.png"}
|
| 151 |
+
)
|
| 152 |
+
except Exception as e:
|
| 153 |
+
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
| 154 |
+
|
| 155 |
+
if __name__ == "__main__":
|
| 156 |
+
import uvicorn
|
| 157 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,8 @@
|
|
| 1 |
-
fastapi
|
| 2 |
-
uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.104.1
|
| 2 |
+
uvicorn==0.24.0
|
| 3 |
+
python-multipart==0.0.6
|
| 4 |
+
PyMuPDF==1.23.7
|
| 5 |
+
opencv-python==4.8.1.78
|
| 6 |
+
numpy==1.26.2
|
| 7 |
+
scikit-image==0.22.0
|
| 8 |
+
matplotlib==3.8.2
|