Commit ·
406ff7d
1
Parent(s): bfc9cdd
update logger
Browse files- __pycache__/distance_calculate.cpython-312.pyc +0 -0
- distance_calculate.py +42 -18
- hots-pot.log +4 -0
- test.ipynb +20 -39
__pycache__/distance_calculate.cpython-312.pyc
ADDED
|
Binary file (9.91 kB). View file
|
|
|
distance_calculate.py
CHANGED
|
@@ -8,6 +8,23 @@ from pydantic import BaseModel
|
|
| 8 |
from ultralytics import YOLO
|
| 9 |
from PIL import Image
|
| 10 |
import traceback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
|
@@ -109,19 +126,19 @@ def find_closest_points(lst):
|
|
| 109 |
closest_points.update({(list(closest_objects_pair[0])[0], list(closest_objects_pair[1])[0]): round(min_distance, 2)})
|
| 110 |
return closest_points
|
| 111 |
|
| 112 |
-
def defult_response():
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
|
| 126 |
|
| 127 |
|
|
@@ -134,13 +151,13 @@ def process_image(request: ImageRequest):
|
|
| 134 |
image.thumbnail(size)
|
| 135 |
except Exception:
|
| 136 |
print(traceback.format_exc())
|
| 137 |
-
return defult_response()
|
| 138 |
|
| 139 |
try:
|
| 140 |
res = model(image)
|
| 141 |
except Exception:
|
| 142 |
print(traceback.format_exc())
|
| 143 |
-
return defult_response()
|
| 144 |
|
| 145 |
try:
|
| 146 |
objects_names_points, objects_names_width_pix = get_points_n_names(res)
|
|
@@ -150,7 +167,7 @@ def process_image(request: ImageRequest):
|
|
| 150 |
object_positions = find_position(objects_names_points, par_pix_cm, image)
|
| 151 |
except Exception:
|
| 152 |
print(traceback.format_exc())
|
| 153 |
-
return defult_response()
|
| 154 |
|
| 155 |
try:
|
| 156 |
# Remove the distance between the same object
|
|
@@ -159,14 +176,21 @@ def process_image(request: ImageRequest):
|
|
| 159 |
actual_distances.remove(item)
|
| 160 |
except Exception:
|
| 161 |
print(traceback.format_exc())
|
| 162 |
-
return defult_response()
|
| 163 |
|
| 164 |
try:
|
| 165 |
# Convert the object names to the system names
|
| 166 |
object_positions_sys, actual_distances_sys = name_conversion(actual_distances,object_positions, name_convert)
|
|
|
|
| 167 |
except Exception:
|
| 168 |
print(traceback.format_exc())
|
| 169 |
-
return defult_response()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
return {
|
| 172 |
"object_positions": object_positions_sys,
|
|
|
|
| 8 |
from ultralytics import YOLO
|
| 9 |
from PIL import Image
|
| 10 |
import traceback
|
| 11 |
+
import logging
|
| 12 |
+
|
| 13 |
+
# Create a logger
|
| 14 |
+
logger = logging.getLogger("hots-pot")
|
| 15 |
+
logger.setLevel(logging.DEBUG)
|
| 16 |
+
|
| 17 |
+
# Create a file handler
|
| 18 |
+
file_handler = logging.FileHandler("hots-pot.log")
|
| 19 |
+
file_handler.setLevel(logging.DEBUG)
|
| 20 |
+
|
| 21 |
+
# Create a formatter and add it to the handler
|
| 22 |
+
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
| 23 |
+
formatter = logging.Formatter('%(asctime)s %(levelname)s - %(message)s')
|
| 24 |
+
file_handler.setFormatter(formatter)
|
| 25 |
+
|
| 26 |
+
# Add the handler to the logger
|
| 27 |
+
logger.addHandler(file_handler)
|
| 28 |
|
| 29 |
app = FastAPI()
|
| 30 |
|
|
|
|
| 126 |
closest_points.update({(list(closest_objects_pair[0])[0], list(closest_objects_pair[1])[0]): round(min_distance, 2)})
|
| 127 |
return closest_points
|
| 128 |
|
| 129 |
+
# def defult_response():
|
| 130 |
+
# return {
|
| 131 |
+
# "object_positions": {
|
| 132 |
+
# "Regular Modular FnS": {"top": "107.55 cm", "bottom": "0.0 cm", "left": "103.1 cm", "right": "33.44 cm"},
|
| 133 |
+
# "Focused Medium Core": {"top": "101.69 cm", "bottom": "0.0 cm", "left": "47.57 cm", "right": "85.74 cm"},
|
| 134 |
+
# "Modular Small FnS": {"top": "83.9 cm", "bottom": "34.75 cm", "left": "49.15 cm", "right": "86.44 cm"}
|
| 135 |
+
# },
|
| 136 |
+
# "actual_distances": [
|
| 137 |
+
# {"object": ["Regular Modular FnS", "Focused Medium Core"], "distances": "8.73 cm"},
|
| 138 |
+
# {"object": ["Regular Modular FnS", "Modular Small FnS"], "distances": "12.97 cm"},
|
| 139 |
+
# {"object": ["Focused Medium Core", "Modular Small FnS"], "distances": "1.42 cm"}
|
| 140 |
+
# ]
|
| 141 |
+
# }
|
| 142 |
|
| 143 |
|
| 144 |
|
|
|
|
| 151 |
image.thumbnail(size)
|
| 152 |
except Exception:
|
| 153 |
print(traceback.format_exc())
|
| 154 |
+
# return defult_response()
|
| 155 |
|
| 156 |
try:
|
| 157 |
res = model(image)
|
| 158 |
except Exception:
|
| 159 |
print(traceback.format_exc())
|
| 160 |
+
# return defult_response()
|
| 161 |
|
| 162 |
try:
|
| 163 |
objects_names_points, objects_names_width_pix = get_points_n_names(res)
|
|
|
|
| 167 |
object_positions = find_position(objects_names_points, par_pix_cm, image)
|
| 168 |
except Exception:
|
| 169 |
print(traceback.format_exc())
|
| 170 |
+
# return defult_response()
|
| 171 |
|
| 172 |
try:
|
| 173 |
# Remove the distance between the same object
|
|
|
|
| 176 |
actual_distances.remove(item)
|
| 177 |
except Exception:
|
| 178 |
print(traceback.format_exc())
|
| 179 |
+
# return defult_response()
|
| 180 |
|
| 181 |
try:
|
| 182 |
# Convert the object names to the system names
|
| 183 |
object_positions_sys, actual_distances_sys = name_conversion(actual_distances,object_positions, name_convert)
|
| 184 |
+
logger.info(json.dumps({'Position':object_positions_sys, 'Distance':actual_distances_sys}))
|
| 185 |
except Exception:
|
| 186 |
print(traceback.format_exc())
|
| 187 |
+
# return defult_response()
|
| 188 |
+
|
| 189 |
+
print("#" * 50)
|
| 190 |
+
print("object_positions_sys", object_positions_sys)
|
| 191 |
+
print("-" * 50)
|
| 192 |
+
print("actual_distances_sys", actual_distances_sys)
|
| 193 |
+
print("#" * 50)
|
| 194 |
|
| 195 |
return {
|
| 196 |
"object_positions": object_positions_sys,
|
hots-pot.log
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
hots-pot - INFO - {"Position": {"Modular Small Core": {"top": "68.12 cm", "bottom": "66.92 cm", "left": "23.03 cm", "right": "50.37 cm"}, "Modular Small FnS": {"top": "70.21 cm", "bottom": "75.08 cm", "left": "70.73 cm", "right": "11.02 cm"}}, "Distance": [{"object": ["Modular Small Core", "Modular Small FnS"], "distances": "2.89 cm"}]}
|
| 2 |
+
2025-04-13 13:03:53,222 INFO - {"Position": {"Modular Small Core": {"top": "68.12 cm", "bottom": "66.92 cm", "left": "23.03 cm", "right": "50.37 cm"}, "Modular Small FnS": {"top": "70.21 cm", "bottom": "75.08 cm", "left": "70.73 cm", "right": "11.02 cm"}}, "Distance": [{"object": ["Modular Small Core", "Modular Small FnS"], "distances": "2.89 cm"}]}
|
| 3 |
+
2025-04-13 13:04:00,698 INFO - {"Position": {"Modular Small Core": {"top": "68.12 cm", "bottom": "66.92 cm", "left": "23.03 cm", "right": "50.37 cm"}, "Modular Small FnS": {"top": "70.21 cm", "bottom": "75.08 cm", "left": "70.73 cm", "right": "11.02 cm"}}, "Distance": [{"object": ["Modular Small Core", "Modular Small FnS"], "distances": "2.89 cm"}]}
|
| 4 |
+
2025-04-13 13:04:04,891 INFO - {"Position": {"Modular Small Core": {"top": "68.12 cm", "bottom": "66.92 cm", "left": "23.03 cm", "right": "50.37 cm"}, "Modular Small FnS": {"top": "70.21 cm", "bottom": "75.08 cm", "left": "70.73 cm", "right": "11.02 cm"}}, "Distance": [{"object": ["Modular Small Core", "Modular Small FnS"], "distances": "2.89 cm"}]}
|
test.ipynb
CHANGED
|
@@ -1,50 +1,31 @@
|
|
| 1 |
{
|
| 2 |
"cells": [
|
| 3 |
-
{
|
| 4 |
-
"cell_type": "code",
|
| 5 |
-
"execution_count": 26,
|
| 6 |
-
"metadata": {},
|
| 7 |
-
"outputs": [],
|
| 8 |
-
"source": [
|
| 9 |
-
"actual_distances = [{'object': ('focused_medium_core', 'regular_modular_fns'), 'distances': 14.7}, {'object': ('focused_medium_core', 'focused_medium_fns'), 'distances': 9.04}, {'object': ('focused_medium_core', 'modular_small_fns'), 'distances': 8.12}, {'object': ('regular_modular_fns', 'regular_modular_fns'), 'distances': 0.39}, {'object': ('regular_modular_fns', 'focused_medium_fns'), 'distances': 1.71}, {'object': ('regular_modular_fns', 'modular_small_fns'), 'distances': 10.6}, {'object': ('focused_medium_fns', 'modular_small_fns'), 'distances': 9.73}, {'object': ('modular_small_fns', 'modular_small_fns'), 'distances': 0.0}]"
|
| 10 |
-
]
|
| 11 |
-
},
|
| 12 |
-
{
|
| 13 |
-
"cell_type": "code",
|
| 14 |
-
"execution_count": 27,
|
| 15 |
-
"metadata": {},
|
| 16 |
-
"outputs": [
|
| 17 |
-
{
|
| 18 |
-
"data": {
|
| 19 |
-
"text/plain": [
|
| 20 |
-
"[{'object': ('focused_medium_core', 'regular_modular_fns'), 'distances': 14.7},\n",
|
| 21 |
-
" {'object': ('focused_medium_core', 'focused_medium_fns'), 'distances': 9.04},\n",
|
| 22 |
-
" {'object': ('focused_medium_core', 'modular_small_fns'), 'distances': 8.12},\n",
|
| 23 |
-
" {'object': ('regular_modular_fns', 'regular_modular_fns'), 'distances': 0.39},\n",
|
| 24 |
-
" {'object': ('regular_modular_fns', 'focused_medium_fns'), 'distances': 1.71},\n",
|
| 25 |
-
" {'object': ('regular_modular_fns', 'modular_small_fns'), 'distances': 10.6},\n",
|
| 26 |
-
" {'object': ('focused_medium_fns', 'modular_small_fns'), 'distances': 9.73},\n",
|
| 27 |
-
" {'object': ('modular_small_fns', 'modular_small_fns'), 'distances': 0.0}]"
|
| 28 |
-
]
|
| 29 |
-
},
|
| 30 |
-
"execution_count": 27,
|
| 31 |
-
"metadata": {},
|
| 32 |
-
"output_type": "execute_result"
|
| 33 |
-
}
|
| 34 |
-
],
|
| 35 |
-
"source": [
|
| 36 |
-
"actual_distances"
|
| 37 |
-
]
|
| 38 |
-
},
|
| 39 |
{
|
| 40 |
"cell_type": "code",
|
| 41 |
"execution_count": null,
|
| 42 |
"metadata": {},
|
| 43 |
"outputs": [],
|
| 44 |
"source": [
|
| 45 |
-
"
|
| 46 |
-
"
|
| 47 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
]
|
| 49 |
}
|
| 50 |
],
|
|
|
|
| 1 |
{
|
| 2 |
"cells": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
"execution_count": null,
|
| 6 |
"metadata": {},
|
| 7 |
"outputs": [],
|
| 8 |
"source": [
|
| 9 |
+
"import logging\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"# Create a logger\n",
|
| 12 |
+
"logger = logging.getLogger(\"hots-pot\")\n",
|
| 13 |
+
"logger.setLevel(logging.DEBUG)\n",
|
| 14 |
+
"\n",
|
| 15 |
+
"# Create a file handler\n",
|
| 16 |
+
"file_handler = logging.FileHandler(\"hots-pot.log\")\n",
|
| 17 |
+
"file_handler.setLevel(logging.DEBUG)\n",
|
| 18 |
+
"\n",
|
| 19 |
+
"# Create a formatter and add it to the handler\n",
|
| 20 |
+
"formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n",
|
| 21 |
+
"file_handler.setFormatter(formatter)\n",
|
| 22 |
+
"\n",
|
| 23 |
+
"# Add the handler to the logger\n",
|
| 24 |
+
"logger.addHandler(file_handler)\n",
|
| 25 |
+
"\n",
|
| 26 |
+
"# Example usage\n",
|
| 27 |
+
"logger.info(\"This is an info message\")\n",
|
| 28 |
+
"logger.error(\"This is an error\")\n"
|
| 29 |
]
|
| 30 |
}
|
| 31 |
],
|