abdrabo01 commited on
Commit
b03c074
·
verified ·
1 Parent(s): 70f2fcd

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +24 -0
  2. main.py +61 -0
  3. requirements.txt +6 -0
  4. yolo_modeln11_1502.pt +3 -0
Dockerfile ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ ENV PYTHONUNBUFFERED=1 \
4
+ PYTHONDONTWRITEBYTECODE=1 \
5
+ PIP_NO_CACHE_DIR=1 \
6
+ MPLCONFIGDIR=/tmp/matplotlib
7
+
8
+ WORKDIR /app
9
+
10
+ RUN apt-get update && apt-get install -y \
11
+ libgl1-mesa-glx \
12
+ libglib2.0-0 \
13
+ && rm -rf /var/lib/apt/lists/*
14
+
15
+ COPY requirements.txt .
16
+
17
+ RUN pip install --upgrade pip && \
18
+ pip install -r requirements.txt
19
+
20
+ COPY . .
21
+
22
+ EXPOSE 7860
23
+
24
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ import uvicorn
3
+ import base64
4
+ import cv2
5
+ import numpy as np
6
+ from ultralytics import YOLO
7
+ from datetime import datetime
8
+ from pydantic import BaseModel
9
+
10
+ app = FastAPI()
11
+
12
+ model = YOLO("yolo_modeln11_1502.pt")
13
+
14
+ class ImageRequest(BaseModel):
15
+ image: str
16
+
17
+ @app.get("/")
18
+ async def root():
19
+ current_time = datetime.now().isoformat()
20
+ return {"message": "PCB Defects API works", "time": current_time}
21
+
22
+ @app.post("/predict")
23
+ async def predict(request: ImageRequest):
24
+ # Decode Base64
25
+ image_bytes = base64.b64decode(request.image)
26
+ np_arr = np.frombuffer(image_bytes, np.uint8)
27
+ image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
28
+ if image is None:
29
+ return {"error": "Invalid image"}
30
+
31
+ # Inference
32
+ results = model.predict(image)
33
+ result = results[0]
34
+
35
+ # Response
36
+ json_result = {}
37
+ class_counters = {}
38
+
39
+ for box in result.boxes:
40
+ class_id = int(box.cls[0])
41
+ class_name = result.names[class_id]
42
+ x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
43
+
44
+ '''
45
+ Create unique key for each detection
46
+ EX: if multiple "missing_hole", keys will be:
47
+ "missing_hole", "missing_hole_2", ...
48
+ '''
49
+ if class_name in class_counters:
50
+ class_counters[class_name] += 1
51
+ key = f"{class_name}_{class_counters[class_name]}"
52
+ else:
53
+ class_counters[class_name] = 1
54
+ key = class_name
55
+
56
+ json_result[key] = [x1, y1, x2, y2]
57
+
58
+ return json_result
59
+
60
+ if __name__ == "__main__":
61
+ uvicorn.run(app, host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi==0.99.1
2
+ uvicorn==0.22.0
3
+ pydantic==1.10.12
4
+ numpy==1.24.2
5
+ opencv-python==4.7.0.72
6
+ ultralytics
yolo_modeln11_1502.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3f21751b53f8cbd3c7911a54f04b1f15e9de2443785111beef89fd5b54340ebc
3
+ size 10770203