vumichien commited on
Commit
ca91016
·
verified ·
1 Parent(s): b6dedaf

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ 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/MID_ASSY[[:space:]]SAMPLE[[:space:]]COLOR.jpeg 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 (4.16 kB). View file
 
data/MID_ASSY SAMPLE COLOR.jpeg ADDED

Git LFS Details

  • SHA256: 3b116acbed3f2be7b758d5bd0457913e236fd21ada85f29d709732af9deda4d3
  • Pointer size: 132 Bytes
  • Size of remote file: 3.7 MB
logs/2024-06-14.log ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 2024-06-14 20:09:20,239 - uvicorn.access - INFO - 127.0.0.1:57016 - "GET / HTTP/1.1" 200
2
+ 2024-06-14 20:09:20,379 - uvicorn.access - INFO - 127.0.0.1:57016 - "GET /favicon.ico HTTP/1.1" 404
3
+ 2024-06-14 20:09:25,816 - uvicorn.access - INFO - 127.0.0.1:57024 - "GET /docs HTTP/1.1" 200
4
+ 2024-06-14 20:09:26,030 - uvicorn.access - INFO - 127.0.0.1:57024 - "GET /openapi.json HTTP/1.1" 200
5
+ 2024-06-14 20:09:32,525 - root - INFO - Received process-image request for file: 35f8f42f-450d-4045-93f2-2eac807a4785.jpg
6
+ 2024-06-14 20:09:34,328 - ultralytics - INFO - Loading models\best-data-v5.onnx for ONNX Runtime inference...
7
+ 2024-06-14 20:09:34,712 - ultralytics - INFO -
8
+ 2024-06-14 20:09:35,824 - ultralytics - INFO - 0: 640x640 19 Bolts, 22 Metal-Mounts, 229.2ms
9
+ 2024-06-14 20:09:35,824 - ultralytics - INFO - Speed: 14.2ms preprocess, 229.2ms inference, 21.1ms postprocess per image at shape (1, 3, 640, 640)
10
+ 2024-06-14 20:09:35,824 - ultralytics - INFO -
11
+ 2024-06-14 20:09:36,060 - ultralytics - INFO - 0: 640x640 5 Bolts, 1 Metal-Mount, 222.5ms
12
+ 2024-06-14 20:09:36,060 - ultralytics - INFO - Speed: 6.9ms preprocess, 222.5ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 640)
13
+ 2024-06-14 20:09:36,089 - ultralytics - INFO -
14
+ 2024-06-14 20:09:36,290 - ultralytics - INFO - 0: 640x640 4 Bolts, 2 Velcros, 201.5ms
15
+ 2024-06-14 20:09:36,290 - ultralytics - INFO - Speed: 0.0ms preprocess, 201.5ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 640)
16
+ 2024-06-14 20:09:36,298 - ultralytics - INFO -
17
+ 2024-06-14 20:09:36,541 - ultralytics - INFO - 0: 640x640 1 Bolt, 3 Metal-Mounts, 1 Velcro, 243.1ms
18
+ 2024-06-14 20:09:36,541 - ultralytics - INFO - Speed: 0.0ms preprocess, 243.1ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 640)
19
+ 2024-06-14 20:09:36,541 - ultralytics - INFO -
20
+ 2024-06-14 20:09:36,742 - ultralytics - INFO - 0: 640x640 (no detections), 194.8ms
21
+ 2024-06-14 20:09:36,742 - ultralytics - INFO - Speed: 0.0ms preprocess, 194.8ms inference, 6.5ms postprocess per image at shape (1, 3, 640, 640)
22
+ 2024-06-14 20:09:36,742 - ultralytics - INFO -
23
+ 2024-06-14 20:09:36,978 - ultralytics - INFO - 0: 640x640 7 Bolts, 3 Metal-Mounts, 229.2ms
24
+ 2024-06-14 20:09:36,978 - ultralytics - INFO - Speed: 0.0ms preprocess, 229.2ms inference, 6.5ms postprocess per image at shape (1, 3, 640, 640)
25
+ 2024-06-14 20:09:36,979 - ultralytics - INFO -
26
+ 2024-06-14 20:09:37,187 - ultralytics - INFO - 0: 640x640 10 Bolts, 2 Metal-Mounts, 3 Velcros, 207.7ms
27
+ 2024-06-14 20:09:37,187 - ultralytics - INFO - Speed: 0.0ms preprocess, 207.7ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 640)
28
+ 2024-06-14 20:09:37,187 - ultralytics - INFO -
29
+ 2024-06-14 20:09:37,389 - ultralytics - INFO - 0: 640x640 2 Bolts, 4 Metal-Mounts, 2 Velcros, 194.7ms
30
+ 2024-06-14 20:09:37,389 - ultralytics - INFO - Speed: 7.1ms preprocess, 194.7ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 640)
31
+ 2024-06-14 20:09:37,396 - ultralytics - INFO -
32
+ 2024-06-14 20:09:37,577 - ultralytics - INFO - 0: 640x640 (no detections), 180.8ms
33
+ 2024-06-14 20:09:37,577 - ultralytics - INFO - Speed: 0.0ms preprocess, 180.8ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 640)
34
+ 2024-06-14 20:09:37,583 - ultralytics - INFO -
35
+ 2024-06-14 20:09:37,785 - ultralytics - INFO - 0: 640x640 5 Bolts, 6 Metal-Mounts, 201.9ms
36
+ 2024-06-14 20:09:37,785 - ultralytics - INFO - Speed: 0.0ms preprocess, 201.9ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 640)
37
+ 2024-06-14 20:09:37,785 - ultralytics - INFO -
38
+ 2024-06-14 20:09:37,973 - ultralytics - INFO - 0: 640x640 6 Bolts, 9 Metal-Mounts, 180.9ms
39
+ 2024-06-14 20:09:37,973 - ultralytics - INFO - Speed: 0.7ms preprocess, 180.9ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 640)
40
+ 2024-06-14 20:09:37,973 - ultralytics - INFO -
41
+ 2024-06-14 20:09:38,147 - ultralytics - INFO - 0: 640x640 2 Bolts, 3 Metal-Mounts, 167.1ms
42
+ 2024-06-14 20:09:38,147 - ultralytics - INFO - Speed: 6.6ms preprocess, 167.1ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 640)
43
+ 2024-06-14 20:09:38,153 - ultralytics - INFO -
44
+ 2024-06-14 20:09:38,328 - ultralytics - INFO - 0: 640x640 (no detections), 173.2ms
45
+ 2024-06-14 20:09:38,328 - ultralytics - INFO - Speed: 0.7ms preprocess, 173.2ms inference, 0.5ms postprocess per image at shape (1, 3, 640, 640)
46
+ 2024-06-14 20:09:38,328 - ultralytics - INFO -
47
+ 2024-06-14 20:09:38,522 - ultralytics - INFO - 0: 640x640 1 Bolt, 1 Velcro, 188.2ms
48
+ 2024-06-14 20:09:38,522 - ultralytics - INFO - Speed: 6.2ms preprocess, 188.2ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 640)
49
+ 2024-06-14 20:09:38,522 - ultralytics - INFO -
50
+ 2024-06-14 20:09:38,731 - ultralytics - INFO - 0: 640x640 4 Metal-Mounts, 1 Velcro, 201.7ms
51
+ 2024-06-14 20:09:38,731 - ultralytics - INFO - Speed: 6.9ms preprocess, 201.7ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 640)
52
+ 2024-06-14 20:09:38,731 - ultralytics - INFO -
53
+ 2024-06-14 20:09:38,912 - ultralytics - INFO - 0: 640x640 1 Velcro, 173.7ms
54
+ 2024-06-14 20:09:38,912 - ultralytics - INFO - Speed: 7.1ms preprocess, 173.7ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 640)
55
+ 2024-06-14 20:09:38,918 - ultralytics - INFO -
56
+ 2024-06-14 20:09:39,128 - ultralytics - INFO - 0: 640x640 (no detections), 208.8ms
57
+ 2024-06-14 20:09:39,128 - ultralytics - INFO - Speed: 0.3ms preprocess, 208.8ms inference, 0.0ms postprocess per image at shape (1, 3, 640, 640)
58
+ 2024-06-14 20:09:39,134 - root - INFO - Completed slicing and detection
59
+ 2024-06-14 20:09:39,134 - root - INFO - Summary info: 23 Metal-Mount, 31 Bolt, 8 Velcro
60
+ 2024-06-14 20:09:39,134 - root - INFO - Run time: 6.61 seconds
61
+ 2024-06-14 20:09:39,134 - root - INFO - Returning JSON results
62
+ 2024-06-14 20:09:39,134 - uvicorn.access - INFO - 127.0.0.1:57052 - "POST /process-image/?draw_boxes=false HTTP/1.1" 200
main.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import supervision as sv
2
+ from ultralytics import YOLO
3
+ import cv2
4
+ import numpy as np
5
+ from fastapi import FastAPI, File, UploadFile
6
+ from fastapi.responses import JSONResponse, Response
7
+ import uvicorn
8
+ import logging
9
+ from datetime import datetime
10
+ import os
11
+ import time
12
+ from collections import defaultdict
13
+
14
+ # Ensure the logs directory exists
15
+ if not os.path.exists("logs"):
16
+ os.makedirs("logs")
17
+
18
+ app = FastAPI()
19
+
20
+ # Load the exported ONNX model
21
+ onnx_model = YOLO("models/best-data-v5.onnx", task="detect")
22
+
23
+ # Define the logging configuration
24
+ LOGGING_CONFIG = {
25
+ "version": 1,
26
+ "disable_existing_loggers": False,
27
+ "formatters": {
28
+ "default": {
29
+ "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
30
+ },
31
+ "access": {
32
+ "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
33
+ },
34
+ },
35
+ "handlers": {
36
+ "default": {
37
+ "formatter": "default",
38
+ "class": "logging.StreamHandler",
39
+ "stream": "ext://sys.stdout",
40
+ },
41
+ "file": {
42
+ "formatter": "default",
43
+ "class": "logging.FileHandler",
44
+ "filename": f"logs/{datetime.now().strftime('%Y-%m-%d')}.log",
45
+ "mode": "a",
46
+ },
47
+ "access": {
48
+ "formatter": "access",
49
+ "class": "logging.StreamHandler",
50
+ "stream": "ext://sys.stdout",
51
+ },
52
+ },
53
+ "loggers": {
54
+ "": {
55
+ "handlers": ["default", "file"],
56
+ "level": "INFO",
57
+ },
58
+ "uvicorn.access": {
59
+ "handlers": ["access", "file"],
60
+ "level": "INFO",
61
+ "propagate": False,
62
+ },
63
+ "ultralytics": {
64
+ "handlers": ["default", "file"],
65
+ "level": "INFO",
66
+ "propagate": False,
67
+ },
68
+ }
69
+ }
70
+
71
+ # Apply the logging configuration
72
+ logging.config.dictConfig(LOGGING_CONFIG)
73
+
74
+
75
+ def parse_detection(detections):
76
+ parsed_rows = []
77
+ for i in range(len(detections.xyxy)):
78
+ x_min = float(detections.xyxy[i][0])
79
+ y_min = float(detections.xyxy[i][1])
80
+ x_max = float(detections.xyxy[i][2])
81
+ y_max = float(detections.xyxy[i][3])
82
+
83
+ width = int(x_max - x_min)
84
+ height = int(y_max - y_min)
85
+
86
+ row = {
87
+ "top": int(y_min),
88
+ "left": int(x_min),
89
+ "width": width,
90
+ "height": height,
91
+ "class_id": ""
92
+ if detections.class_id is None
93
+ else int(detections.class_id[i]),
94
+ "confidence": ""
95
+ if detections.confidence is None
96
+ else float(detections.confidence[i]),
97
+ "tracker_id": ""
98
+ if detections.tracker_id is None
99
+ else int(detections.tracker_id[i]),
100
+ }
101
+
102
+ if hasattr(detections, "data"):
103
+ for key, value in detections.data.items():
104
+ row[key] = (
105
+ str(value[i])
106
+ if hasattr(value, "__getitem__") and value.ndim != 0
107
+ else str(value)
108
+ )
109
+ parsed_rows.append(row)
110
+ return parsed_rows
111
+
112
+
113
+ # Run inference
114
+ def callback(image_slice: np.ndarray) -> sv.Detections:
115
+ # logging.info("Running callback for image slice")
116
+ results = onnx_model(image_slice)[0]
117
+ return sv.Detections.from_ultralytics(results)
118
+
119
+
120
+ def infer(image):
121
+ start_time = time.time()
122
+ image_arr = np.frombuffer(image, np.uint8)
123
+ image = cv2.imdecode(image_arr, cv2.IMREAD_COLOR)
124
+ image = cv2.resize(image, (1920, 1920))
125
+ results = onnx_model(image)[0]
126
+ # detections = sv.Detections.from_ultralytics(results)
127
+
128
+ slicer = sv.InferenceSlicer(callback=callback, slice_wh=(640, 640))
129
+ detections = slicer(image=image)
130
+ logging.info("Completed slicing and detection")
131
+
132
+ parsed_rows = parse_detection(detections)
133
+ # Count the occurrences of each class
134
+ class_counts = defaultdict(int)
135
+ for detection in parsed_rows:
136
+ class_name = detection.get("class_name", "Unknown")
137
+ class_counts[class_name] += 1
138
+ summary_info = ", ".join(
139
+ [f"{count} {class_name}" for class_name, count in class_counts.items()]
140
+ )
141
+ logging.info(f"Summary info: {summary_info}")
142
+ logging.info(f"Run time: {time.time() - start_time:.2f} seconds")
143
+ # label_annotator = sv.LabelAnnotator(text_color=sv.Color.BLACK)
144
+ bounding_box_annotator = sv.BoundingBoxAnnotator(thickness=4)
145
+
146
+ annotated_image = image.copy()
147
+ annotated_image = bounding_box_annotator.annotate(scene=annotated_image, detections=detections)
148
+ # annotated_image = label_annotator.annotate(scene=annotated_image, detections=detections)
149
+ # logging.info("Annotated image")
150
+ return annotated_image, parsed_rows
151
+
152
+
153
+ @app.post("/process-image/")
154
+ async def process_image(image: UploadFile = File(...), draw_boxes: bool = False):
155
+ filename = image.filename
156
+ logging.info(f"Received process-image request for file: {filename}")
157
+ image_data = await image.read()
158
+ annotated_image, results = infer(image_data)
159
+ if draw_boxes:
160
+ _, img_encoded = cv2.imencode('.jpg', annotated_image)
161
+ logging.info("Returning annotated image")
162
+ return Response(content=img_encoded.tobytes(), media_type="image/jpeg")
163
+ logging.info("Returning JSON results")
164
+ return JSONResponse(content=results)
165
+
166
+
167
+ @app.get("/")
168
+ def hello_world():
169
+ return 'Hello, World!'
170
+
171
+
172
+ if __name__ == "__main__":
173
+ uvicorn.run("main:app", port=8001, reload=True)
models/__init__.py ADDED
File without changes
models/best-data-v4.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b48e20c75d7c3b103a4843b0dc2be529ab4b66f25a2279f61a936ec0b360e6a4
3
+ size 101965198
models/best-data-v4.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d73597941364c27868db2ffe233cb7b50ad78c953e47398333c3641cb429a6c5
3
+ size 204643878
models/best-data-v5.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:add3500eaa4f5289ed6c5c94206c6b51d3f9b3444294c63d611e4d28279723a6
3
+ size 101965198
models/best-data-v5.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:72ec334351280472b9d820ea888b2b6cf18dbee6912d25f1b7aa8eab09bd201e
3
+ size 204643878
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
+ inference
13
+ supervision
14
+ ultralytics
15
+ onnx