Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +14 -0
- app.py +47 -0
- requirements.txt +0 -0
- yolov8n.pt +3 -0
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use the official Python 3.10.11 image
|
2 |
+
FROM python:3.10.11
|
3 |
+
|
4 |
+
# Copy the current directory contents into the container at .
|
5 |
+
COPY . .
|
6 |
+
|
7 |
+
# Set the working directory to /
|
8 |
+
WORKDIR /
|
9 |
+
|
10 |
+
# Install requirements.txt
|
11 |
+
RUN pip install --no-cache-dir --upgrade -r /requirements.txt
|
12 |
+
|
13 |
+
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
14 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
from pydantic import BaseModel
|
4 |
+
import numpy as np
|
5 |
+
import cv2
|
6 |
+
from ultralytics import YOLO
|
7 |
+
from PIL import Image
|
8 |
+
import base64
|
9 |
+
from io import BytesIO
|
10 |
+
|
11 |
+
app = FastAPI()
|
12 |
+
model = YOLO("yolov8n.pt")
|
13 |
+
|
14 |
+
origins = ["*"]
|
15 |
+
|
16 |
+
app.add_middleware(
|
17 |
+
CORSMiddleware,
|
18 |
+
allow_origins=origins,
|
19 |
+
allow_credentials=True,
|
20 |
+
allow_methods=["*"],
|
21 |
+
allow_headers=["*"],
|
22 |
+
)
|
23 |
+
|
24 |
+
@app.post("/detect/")
|
25 |
+
async def detect_objects(file: UploadFile):
|
26 |
+
# Process the uploaded image for object detection
|
27 |
+
image_bytes = await file.read()
|
28 |
+
image = np.frombuffer(image_bytes, dtype=np.uint8)
|
29 |
+
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
|
30 |
+
|
31 |
+
# Perform object detection with YOLOv8
|
32 |
+
detections = model(image)
|
33 |
+
|
34 |
+
return detections[0].tojson()
|
35 |
+
|
36 |
+
class ImageData(BaseModel):
|
37 |
+
image: str # Data gambar dalam format base64
|
38 |
+
|
39 |
+
@app.post("/uploadimage")
|
40 |
+
async def upload_image(image_data: ImageData):
|
41 |
+
# Mengonversi base64 ke gambar
|
42 |
+
base64_data = image_data.image.split(',')[1]
|
43 |
+
image = Image.open(BytesIO(base64.b64decode(base64_data)))
|
44 |
+
detections = model(image)
|
45 |
+
|
46 |
+
return detections[0].tojson()
|
47 |
+
|
requirements.txt
ADDED
Binary file (2.56 kB). View file
|
|
yolov8n.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f59b3d833e2ff32e194b5bb8e08d211dc7c5bdf144b90d2c8412c47ccfc83b36
|
3 |
+
size 6549796
|