Inigozr commited on
Commit
c52b1e1
1 Parent(s): 4a05d22
Files changed (4) hide show
  1. Dockerfile +11 -0
  2. README.md +6 -8
  3. app.py +74 -0
  4. requirements.txt +0 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM ultralytics/ultralytics:latest
2
+
3
+ WORKDIR /usr/src/ultralytics/
4
+
5
+ COPY ./requirements.txt /requirements.txt
6
+
7
+ RUN python3 -m pip install -r requirements.txt
8
+
9
+ COPY app.y .
10
+
11
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,10 +1,8 @@
1
  ---
2
- title: Yolo Test
3
- emoji: 📈
4
- colorFrom: green
5
- colorTo: green
6
  sdk: docker
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Basic Docker SDK Space
3
+ emoji: 🐳
4
+ colorFrom: purple
5
+ colorTo: gray
6
  sdk: docker
7
+ app_port: 7860
8
+ ---
 
 
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.responses import HTMLResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ from fastapi.templating import Jinja2Templates
5
+ from ultralytics import YOLO
6
+ import requests
7
+ from PIL import Image
8
+ import numpy as np
9
+ import cv2
10
+ import io
11
+
12
+ app = FastAPI()
13
+
14
+ # Mount a static directory for serving images
15
+ app.mount("/static", StaticFiles(directory="static"), name="static")
16
+
17
+ # Templates for rendering HTML
18
+ templates = Jinja2Templates(directory="templates")
19
+
20
+
21
+ def predict_yolo(image_path):
22
+ # Assuming you have a YOLO API endpoint that accepts an image and returns predictions
23
+ with open(image_path, "rb") as file:
24
+ files = {"file": file}
25
+ # Load a model
26
+ model = YOLO('yolov8n.pt') # pretrained YOLOv8n model
27
+
28
+ # Run batched inference on a list of images
29
+ results = model(image_path) # return a list of Results objects
30
+
31
+ # Process results list
32
+ for result in results:
33
+ boxes = result.boxes # Boxes object for bbox outputs
34
+ # masks = result.masks # Masks object for segmentation masks outputs
35
+ # keypoints = result.keypoints # Keypoints object for pose outputs
36
+ # probs = result.probs # Probs object for classification outputs
37
+ predictions = boxes.json()
38
+ return predictions
39
+
40
+
41
+ def draw_boxes(image, boxes):
42
+ for box in boxes:
43
+ x, y, w, h = box["bbox"]
44
+ cv2.rectangle(image, (int(x), int(y)), (int(x + w), int(y + h)), (0, 255, 0), 2)
45
+ return image
46
+
47
+
48
+ @app.post("/uploadfile/")
49
+ async def create_upload_file(file: UploadFile = File(...)):
50
+ contents = await file.read()
51
+ image = Image.open(io.BytesIO(contents))
52
+
53
+ # Save the image to a static directory
54
+ save_path = f"static/{file.filename}"
55
+ image.save(save_path)
56
+
57
+ # Perform YOLO prediction
58
+ predictions = predict_yolo(save_path)
59
+
60
+ # Draw bounding boxes on the image
61
+ image_np = np.array(image)
62
+ image_with_boxes = draw_boxes(image_np, predictions)
63
+
64
+ # Save the image with bounding boxes
65
+ image_with_boxes_path = f"static/{file.filename.split('.')[0]}_with_boxes.jpg"
66
+ cv2.imwrite(image_with_boxes_path, cv2.cvtColor(image_with_boxes, cv2.COLOR_RGB2BGR))
67
+
68
+ # Render the HTML with the image and bounding boxes
69
+ return templates.TemplateResponse("prediction.html", {"request": file, "image_path": image_with_boxes_path})
70
+
71
+
72
+ @app.get("/")
73
+ async def read_root():
74
+ return {"message": "Hello, this is a YOLO prediction API using FastAPI!"}
requirements.txt ADDED
File without changes