vumichien commited on
Commit
87a77d3
1 Parent(s): dbb1237

Initial commit

Browse files
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ data/IMG_0050.jpg filter=lfs diff=lfs merge=lfs -text
37
+ data/IMG_0051.jpg filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /content
4
+
5
+ RUN mkdir /content/cache/
6
+
7
+ RUN export TRANSFORMERS_CACHE=/content/cache/
8
+
9
+ COPY ./requirements.txt /content/requirements.txt
10
+
11
+ RUN pip install --no-cache-dir --upgrade -r /content/requirements.txt
12
+ RUN apt-get update
13
+ RUN apt-get install -y ffmpeg
14
+
15
+ COPY . .
16
+
17
+ RUN adduser --disabled-password --gecos '' admin
18
+ RUN adduser admin sudo
19
+ RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
20
+
21
+ RUN chown -R admin:admin /content
22
+ RUN chmod -R 777 /content
23
+ USER admin
24
+
25
+ EXPOSE 7860
26
+
27
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
__pycache__/main.cpython-39.pyc ADDED
Binary file (2.39 kB). View file
 
data/IMG_0050.jpg ADDED

Git LFS Details

  • SHA256: 280d23a31eaeff38a4b16c08f5c413b3c1524edc80d5908d0fccd47c79ef0a38
  • Pointer size: 132 Bytes
  • Size of remote file: 2.99 MB
data/IMG_0051.jpg ADDED

Git LFS Details

  • SHA256: da7fa73e9168fe08a9e960395d2ec0a9bcb4826eed29194de3361e2a9210357a
  • Pointer size: 132 Bytes
  • Size of remote file: 2.43 MB
main.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from fastapi import FastAPI, File, UploadFile
4
+ from fastapi.responses import JSONResponse, Response
5
+ import uvicorn
6
+ import logging
7
+ import time
8
+ import supervision as sv
9
+ from ultralytics import YOLO
10
+
11
+ app = FastAPI()
12
+ model = YOLO("models/best_v2.pt", task="detect")
13
+
14
+
15
+ def parse_detection(detections):
16
+ parsed_rows = []
17
+ for i in range(len(detections.xyxy)):
18
+ x_min = float(detections.xyxy[i][0])
19
+ y_min = float(detections.xyxy[i][1])
20
+ x_max = float(detections.xyxy[i][2])
21
+ y_max = float(detections.xyxy[i][3])
22
+
23
+ width = int(x_max - x_min)
24
+ height = int(y_max - y_min)
25
+
26
+ row = {
27
+ "x": int(y_min),
28
+ "y": int(x_min),
29
+ "width": width,
30
+ "height": height,
31
+ "class_id": ""
32
+ if detections.class_id is None
33
+ else int(detections.class_id[i]),
34
+ "confidence": ""
35
+ if detections.confidence is None
36
+ else float(detections.confidence[i]),
37
+ "tracker_id": ""
38
+ if detections.tracker_id is None
39
+ else int(detections.tracker_id[i]),
40
+ }
41
+
42
+ if hasattr(detections, "data"):
43
+ for key, value in detections.data.items():
44
+ if key == "class_name":
45
+ key = "class"
46
+ row[key] = (
47
+ str(value[i])
48
+ if hasattr(value, "__getitem__") and value.ndim != 0
49
+ else str(value)
50
+ )
51
+ parsed_rows.append(row)
52
+ return parsed_rows
53
+
54
+
55
+ def infer(image):
56
+ image_arr = np.frombuffer(image, np.uint8)
57
+ image = cv2.imdecode(image_arr, cv2.IMREAD_COLOR)
58
+ image = cv2.resize(image, (1920, 1920))
59
+ results = model(image)[0]
60
+ width, height = results.orig_shape[1], results.orig_shape[0]
61
+ print(results.speed)
62
+ detections = sv.Detections.from_ultralytics(results)
63
+ parsed_rows = parse_detection(detections)
64
+ parsed_result = {'predictions': parsed_rows, 'image': {'width': width, 'height': height}}
65
+ return parsed_result
66
+
67
+
68
+ @app.post("/process-image/")
69
+ async def process_image(image: UploadFile = File(...)):
70
+ filename = image.filename
71
+ logging.info(f"Received process-image request for file: {filename}")
72
+ image_data = await image.read()
73
+ results = infer(image_data)
74
+ logging.info("Returning JSON results")
75
+ return JSONResponse(content=results)
76
+
77
+
78
+ @app.get("/")
79
+ def hello_world():
80
+ return 'Hello World from Detomo AI!'
81
+
82
+
83
+ if __name__ == "__main__":
84
+ uvicorn.run("main:app", port=8001, reload=True)
models/best_v1.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ccea111609078c2f7b05161233c3050f67166f82c577d310241cd37bf8ecf4bc
3
+ size 36686178
models/best_v2.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:28b05675545530542aa4a3a6a57ce9d48c4af46921b094a034f1d4ac2f7d8101
3
+ size 6259417
requirements.txt ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ flup
4
+ a2wsgi
5
+ python-multipart
6
+ numpy
7
+ imutils
8
+ opencv-python
9
+ pytesseract
10
+ scikit-learn
11
+ roboflow
12
+ supervision
13
+ ultralytics
14
+ onnx
15
+ onnxruntime
test.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ import supervision as sv
3
+
4
+
5
+ def parse_detection(detections):
6
+ parsed_rows = []
7
+ for i in range(len(detections.xyxy)):
8
+ x_min = float(detections.xyxy[i][0])
9
+ y_min = float(detections.xyxy[i][1])
10
+ x_max = float(detections.xyxy[i][2])
11
+ y_max = float(detections.xyxy[i][3])
12
+
13
+ width = int(x_max - x_min)
14
+ height = int(y_max - y_min)
15
+
16
+ row = {
17
+ "x": int(y_min),
18
+ "y": int(x_min),
19
+ "width": width,
20
+ "height": height,
21
+ "class_id": ""
22
+ if detections.class_id is None
23
+ else int(detections.class_id[i]),
24
+ "confidence": ""
25
+ if detections.confidence is None
26
+ else float(detections.confidence[i]),
27
+ "tracker_id": ""
28
+ if detections.tracker_id is None
29
+ else int(detections.tracker_id[i]),
30
+ }
31
+
32
+ if hasattr(detections, "data"):
33
+ for key, value in detections.data.items():
34
+ if key == "class_name":
35
+ key = "class"
36
+ row[key] = (
37
+ str(value[i])
38
+ if hasattr(value, "__getitem__") and value.ndim != 0
39
+ else str(value)
40
+ )
41
+ parsed_rows.append(row)
42
+ return parsed_rows
43
+
44
+ model = YOLO("models/best_v2.pt", task="detect")
45
+ results = model(["data/IMG_0050.jpg"])[0]
46
+ width, height = results.orig_shape[1], results.orig_shape[0]
47
+ print(results.orig_shape)
48
+ print(results.speed)
49
+ output = sv.Detections.from_ultralytics(results)
50
+ output = parse_detection(output)
51
+ parse_result = {'predictions': output, 'image': {'width': width, 'height': height}}
52
+ print(parse_result)