Spaces:
Sleeping
Sleeping
Uploaded new version
Browse files- Dockerfile +1 -7
- app-hf.py +53 -0
- app-local.py +61 -0
- requirements.txt +5 -1
- templates/index.html +15 -0
- templates/result.html +10 -0
Dockerfile
CHANGED
|
@@ -17,12 +17,6 @@ USER user
|
|
| 17 |
# Set home to the user's home directory
|
| 18 |
ENV HOME=/home/user \
|
| 19 |
PATH=/home/user/.local/bin:$PATH
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# Define environment variables for GPU, RAM limit, and shared memory
|
| 23 |
-
ENV GPU_ID="0" \
|
| 24 |
-
RAM_LIMIT="4g" \
|
| 25 |
-
SHM_SIZE="2g"
|
| 26 |
|
| 27 |
# Set the working directory to the user's home directory
|
| 28 |
WORKDIR $HOME/app
|
|
@@ -31,4 +25,4 @@ WORKDIR $HOME/app
|
|
| 31 |
COPY --chown=user . $HOME/app
|
| 32 |
|
| 33 |
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
| 34 |
-
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 17 |
# Set home to the user's home directory
|
| 18 |
ENV HOME=/home/user \
|
| 19 |
PATH=/home/user/.local/bin:$PATH
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Set the working directory to the user's home directory
|
| 22 |
WORKDIR $HOME/app
|
|
|
|
| 25 |
COPY --chown=user . $HOME/app
|
| 26 |
|
| 27 |
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
| 28 |
+
CMD ["uvicorn", "app-local:app", "--host", "0.0.0.0", "--port", "7860"]
|
app-hf.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Response
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from transformers import pipeline, YolosForObjectDetection, YolosImageProcessor
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
+
import torch
|
| 6 |
+
import requests
|
| 7 |
+
import io
|
| 8 |
+
import base64
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
app = FastAPI() # Create a new FastAPI app instance
|
| 12 |
+
|
| 13 |
+
# Initialize the Yolos model and image processor
|
| 14 |
+
yolos_model = YolosForObjectDetection.from_pretrained('hustvl/yolos-tiny')
|
| 15 |
+
yolos_image_processor = YolosImageProcessor.from_pretrained("hustvl/yolos-tiny")
|
| 16 |
+
|
| 17 |
+
# Define a route for the root "/"
|
| 18 |
+
@app.get("/")
|
| 19 |
+
def read_root():
|
| 20 |
+
return {"message": "Welcome to the YOLOS Object Detection API!"}
|
| 21 |
+
|
| 22 |
+
# Define a route for detecting objects from an image URL
|
| 23 |
+
@app.get("/", response_class=HTMLResponse)
|
| 24 |
+
def detect_objects(url: str):
|
| 25 |
+
try:
|
| 26 |
+
# Download the image from the specified URL
|
| 27 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 28 |
+
|
| 29 |
+
# Preprocess the image using the Yolos image processor
|
| 30 |
+
inputs = yolos_image_processor(images=image, return_tensors="pt")
|
| 31 |
+
|
| 32 |
+
# Run the Yolos model on the preprocessed image
|
| 33 |
+
outputs = yolos_model(**inputs)
|
| 34 |
+
|
| 35 |
+
# Post-process the object detection results
|
| 36 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 37 |
+
results = yolos_image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]
|
| 38 |
+
|
| 39 |
+
# Draw bounding boxes on the image
|
| 40 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 41 |
+
image_draw = ImageDraw.Draw(image)
|
| 42 |
+
image_draw.rectangle(box.tolist(), outline="red", width=2)
|
| 43 |
+
image_draw.text((box[0], box[1]), f"{yolos_model.config.id2label[label.item()]}: {round(score.item(), 3)}", fill="red")
|
| 44 |
+
|
| 45 |
+
# Save the modified image to a byte stream
|
| 46 |
+
image_byte_array = io.BytesIO()
|
| 47 |
+
image.save(image_byte_array, format="PNG")
|
| 48 |
+
|
| 49 |
+
# Return the image as a Response with content type "image/png"
|
| 50 |
+
return Response(content=image_byte_array.getvalue(), media_type="image/png")
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
app-local.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException, Form
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from transformers import YolosForObjectDetection, YolosImageProcessor
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
+
import torch
|
| 6 |
+
import io
|
| 7 |
+
import base64
|
| 8 |
+
from starlette.requests import Request
|
| 9 |
+
from fastapi.templating import Jinja2Templates
|
| 10 |
+
import httpx
|
| 11 |
+
|
| 12 |
+
app = FastAPI() # Create a FastAPI instance
|
| 13 |
+
templates = Jinja2Templates(directory="templates") # Create a Jinja2Templates instance for handling HTML templates
|
| 14 |
+
|
| 15 |
+
# Initialize YOLOS model and image processor
|
| 16 |
+
yolos_model = YolosForObjectDetection.from_pretrained('hustvl/yolos-tiny')
|
| 17 |
+
yolos_image_processor = YolosImageProcessor.from_pretrained("hustvl/yolos-tiny")
|
| 18 |
+
|
| 19 |
+
# Define a route for the main HTML page
|
| 20 |
+
@app.get("/", response_class=HTMLResponse)
|
| 21 |
+
async def main(request: Request):
|
| 22 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
| 23 |
+
|
| 24 |
+
# Define a route for handling object detection from a submitted form
|
| 25 |
+
@app.post("/", response_class=HTMLResponse)
|
| 26 |
+
async def post_detect_objects(request: Request, url: str = Form(...)):
|
| 27 |
+
try:
|
| 28 |
+
# Download the image from the specified URL
|
| 29 |
+
async with httpx.AsyncClient() as client:
|
| 30 |
+
response = await client.get(url)
|
| 31 |
+
response.raise_for_status() # Raise an exception if there is an error in the request
|
| 32 |
+
content = response.content
|
| 33 |
+
|
| 34 |
+
image = Image.open(io.BytesIO(content))
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# Preprocess the image using the YOLOS image processor
|
| 38 |
+
inputs = yolos_image_processor(images=image, return_tensors="pt")
|
| 39 |
+
|
| 40 |
+
# Run the YOLOS model on the preprocessed image
|
| 41 |
+
outputs = yolos_model(**inputs)
|
| 42 |
+
|
| 43 |
+
# Post-process the object detection results
|
| 44 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 45 |
+
results = yolos_image_processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]
|
| 46 |
+
|
| 47 |
+
# Draw bounding boxes on the image
|
| 48 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 49 |
+
image_draw = ImageDraw.Draw(image)
|
| 50 |
+
image_draw.rectangle(box.tolist(), outline="red", width=2)
|
| 51 |
+
image_draw.text((box[0], box[1]), f"{yolos_model.config.id2label[label.item()]}: {round(score.item(), 3)}", fill="red")
|
| 52 |
+
|
| 53 |
+
# Save the modified image to a byte sequence
|
| 54 |
+
image_byte_array = io.BytesIO()
|
| 55 |
+
image.save(image_byte_array, format="PNG")
|
| 56 |
+
|
| 57 |
+
# Return the image as a response with content type "image/png"
|
| 58 |
+
return templates.TemplateResponse("result.html", {"request": request, "image": base64.b64encode(image_byte_array.getvalue()).decode()})
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
requirements.txt
CHANGED
|
@@ -4,4 +4,8 @@ uvicorn[standard]==0.17.*
|
|
| 4 |
sentencepiece==0.1.*
|
| 5 |
torch==1.11.*
|
| 6 |
transformers==4.*
|
| 7 |
-
Pillow==8.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
sentencepiece==0.1.*
|
| 5 |
torch==1.11.*
|
| 6 |
transformers==4.*
|
| 7 |
+
Pillow==8.2.*
|
| 8 |
+
starlette==0.17.*
|
| 9 |
+
httpx==0.21.*
|
| 10 |
+
jinja2==3.0.*
|
| 11 |
+
python-multipart==0.0.*
|
templates/index.html
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>YOLOS Object Detection</title>
|
| 5 |
+
</head>
|
| 6 |
+
<body>
|
| 7 |
+
<h1>YOLOS Object Detection</h1>
|
| 8 |
+
<form action="/" method="post">
|
| 9 |
+
<label for="url">Image URL:</label>
|
| 10 |
+
<input type="text" id="url" name="url" required>
|
| 11 |
+
<button type="submit">Detect Objects</button>
|
| 12 |
+
</form>
|
| 13 |
+
</body>
|
| 14 |
+
</html>
|
| 15 |
+
|
templates/result.html
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>YOLOS Object Detection Result</title>
|
| 5 |
+
</head>
|
| 6 |
+
<body>
|
| 7 |
+
<h1>YOLOS Object Detection Result</h1>
|
| 8 |
+
<img src="data:image/png;base64,{{ image }}" alt="Detected Objects">
|
| 9 |
+
</body>
|
| 10 |
+
</html>
|