-
Notifications
You must be signed in to change notification settings - Fork 7
Add area and centroid calculation #1173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,4 +25,4 @@ ENV FLASK_APP=object-detection-llm.py | |
|
|
||
| HEALTHCHECK --interval=60s --timeout=10s --start-period=120s --retries=5 CMD curl -f http://localhost:5000/health || exit 1 | ||
|
|
||
| CMD [ "gunicorn", "object-detection-llm:app", "-b", "0.0.0.0:5000", "--capture-output", "--log-level=debug" ] | ||
| CMD [ "gunicorn", "object-detection-llm:app", "-b", "0.0.0.0:5000", "--capture-output", "--log-level=debug", "--timeout", "75"] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Timeout adjustments should really be done in docker-compose, but that isn't implemented until #1077 is complete.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there anything I can do about it now?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't think of anything since #1077 is not implemented. But at least these comments should show up there now. :) |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,33 +68,50 @@ def normalize_bbox(bbox, width, height): | |
| ] | ||
|
|
||
|
|
||
| def filter_objects_by_confidence(objects, threshold): | ||
| def process_objects(objects, threshold): | ||
| """ | ||
| Filter objects based on confidence score threshold | ||
| and replace underscores in labels with spaces. | ||
| Process detected objects by filtering, transforming, and enriching them. | ||
|
|
||
| - Filters objects by confidence threshold | ||
| - Normalizes labels (replaces underscores with spaces) | ||
| - Renumbers IDs sequentially | ||
| - Calculates geometric properties (area, centroid) | ||
|
|
||
| Args: | ||
| objects (list): List of detected objects with confidence scores | ||
| threshold (float): Minimum confidence score (0-1) | ||
|
|
||
| Returns: | ||
| list: Filtered list of objects meeting the confidence threshold | ||
| list: Processed objects with computed properties | ||
| """ | ||
| filtered = [] | ||
| processed = [] | ||
| for obj in objects: | ||
| if obj.get("confidence", 0) >= threshold: | ||
| obj['type'] = obj['type'].replace('_', ' ') | ||
| filtered.append(obj) | ||
| processed.append(obj) | ||
|
|
||
| # Renumber IDs sequentially after filtering | ||
| for idx, obj in enumerate(filtered): | ||
| for idx, obj in enumerate(processed): | ||
| obj['ID'] = idx | ||
|
|
||
| x1, y1, x2, y2 = obj["dimensions"] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't expect to find this in a function "filter_objects" since it has nothing to do with filtering. make function name more generic?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you, renamed. |
||
|
|
||
| # Calculate area (width * height) | ||
| area = (x2 - x1) * (y2 - y1) | ||
|
|
||
| # Calculate centroid | ||
| centroid_x = (x1 + x2) / 2 | ||
| centroid_y = (y1 + y2) / 2 | ||
|
|
||
| # Create object entry according to schema | ||
| obj["area"] = area | ||
| obj["centroid"] = [centroid_x, centroid_y] | ||
|
|
||
| logging.debug( | ||
| f"Filtered {len(objects)} objects to {len(filtered)} " | ||
| f"Processed {len(objects)} objects to {len(processed)} " | ||
| f"objects with confidence >= {threshold}" | ||
| ) | ||
| return filtered | ||
| return processed | ||
|
|
||
|
|
||
| @app.route("/preprocessor", methods=['POST']) | ||
|
|
@@ -148,8 +165,6 @@ def detect_objects(): | |
| parse_json=True | ||
| ) | ||
|
|
||
| logging.pii(f"LLM object detection output: {object_json}") | ||
|
|
||
| if object_json is None or len(object_json.get("objects", [])) == 0: | ||
| logging.error("Failed to extract objects from the graphic.") | ||
| return jsonify({"error": "No objects extracted"}), 204 | ||
|
|
@@ -162,8 +177,9 @@ def detect_objects(): | |
| obj["dimensions"], width, height | ||
| ) | ||
|
|
||
| # Filter objects by confidence threshold | ||
| object_json["objects"] = filter_objects_by_confidence( | ||
| # Filter objects by confidence threshold, add area and centroid, | ||
| # remove underscores from labels, and renumber IDs | ||
| object_json["objects"] = process_objects( | ||
| object_json["objects"], | ||
| CONF_THRESHOLD | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could argue this should be settable in docker-compose, so that it can be tuned by people who have different requirements for number of items vs. accuracy, or who change the model used, and the confidence scale shifts.
As discussed in slack earlier, filtering should also really be a handler issue in the long-run. So confidence threshold should be tuned to "too much" rather than "too little" and individual handlers can decide what tradeoffs they want to make.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is settable in docker-compose, or am I missing something?
Do you want me to change the threshold to 0 and let handers decide what they want to do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, go with a threshold you're comfortable with, since we don't have anyone to implement handler changes at this point. But this needs to be resolved in the future. And, great it is settable in docker-compose. Didn't remember that!