Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -72,13 +72,91 @@ LOGGING_CONFIG = {
|
|
| 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 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
width = int(x_max - x_min)
|
| 84 |
height = int(y_max - y_min)
|
|
@@ -121,15 +199,29 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
| 133 |
# Count the occurrences of each class
|
| 134 |
class_counts = defaultdict(int)
|
| 135 |
for detection in parsed_rows:
|
|
@@ -140,16 +232,19 @@ def infer(image):
|
|
| 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
|
|
|
|
| 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 |
+
def parse_detection(detections, scale_x, scale_y):
|
| 153 |
parsed_rows = []
|
| 154 |
for i in range(len(detections.xyxy)):
|
| 155 |
+
# Rescale the coordinates to match the original image size
|
| 156 |
+
x_min = float(detections.xyxy[i][0]) / scale_x
|
| 157 |
+
y_min = float(detections.xyxy[i][1]) / scale_y
|
| 158 |
+
x_max = float(detections.xyxy[i][2]) / scale_x
|
| 159 |
+
y_max = float(detections.xyxy[i][3]) / scale_y
|
| 160 |
|
| 161 |
width = int(x_max - x_min)
|
| 162 |
height = int(y_max - y_min)
|
|
|
|
| 199 |
start_time = time.time()
|
| 200 |
image_arr = np.frombuffer(image, np.uint8)
|
| 201 |
image = cv2.imdecode(image_arr, cv2.IMREAD_COLOR)
|
| 202 |
+
|
| 203 |
+
# Get original dimensions
|
| 204 |
+
original_height, original_width = image.shape[:2]
|
| 205 |
+
|
| 206 |
+
# Resize image for detection
|
| 207 |
+
target_size = 1920
|
| 208 |
+
image = cv2.resize(image, (target_size, target_size))
|
| 209 |
+
|
| 210 |
+
# Compute scale factors
|
| 211 |
+
scale_x = target_size / original_width
|
| 212 |
+
scale_y = target_size / original_height
|
| 213 |
+
|
| 214 |
+
# Run model
|
| 215 |
results = onnx_model(image)[0]
|
|
|
|
| 216 |
|
| 217 |
+
# Using slicer for detection
|
| 218 |
slicer = sv.InferenceSlicer(callback=callback, slice_wh=(640, 640))
|
| 219 |
detections = slicer(image=image)
|
| 220 |
logging.info("Completed slicing and detection")
|
| 221 |
|
| 222 |
+
# Parse detections and adjust coordinates to original size
|
| 223 |
+
parsed_rows = parse_detection(detections, scale_x, scale_y)
|
| 224 |
+
|
| 225 |
# Count the occurrences of each class
|
| 226 |
class_counts = defaultdict(int)
|
| 227 |
for detection in parsed_rows:
|
|
|
|
| 232 |
)
|
| 233 |
logging.info(f"Summary info: {summary_info}")
|
| 234 |
logging.info(f"Run time: {time.time() - start_time:.2f} seconds")
|
|
|
|
|
|
|
| 235 |
|
| 236 |
+
# Annotate the resized image
|
| 237 |
+
bounding_box_annotator = sv.BoundingBoxAnnotator(thickness=4)
|
| 238 |
annotated_image = image.copy()
|
| 239 |
annotated_image = bounding_box_annotator.annotate(scene=annotated_image, detections=detections)
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
+
# Resize the annotated image back to original dimensions
|
| 242 |
+
annotated_image = cv2.resize(annotated_image, (original_width, original_height))
|
| 243 |
+
|
| 244 |
+
# Return the resized annotated image and parsed detection results
|
| 245 |
+
return annotated_image, parsed_rows
|
| 246 |
|
| 247 |
+
|
| 248 |
@app.post("/process-image/")
|
| 249 |
async def process_image(image: UploadFile = File(...), draw_boxes: bool = False):
|
| 250 |
filename = image.filename
|