Spaces:
Build error
Build error
Commit
·
dce95b8
1
Parent(s):
f90c66a
post
Browse files- distance_calculate3.ipynb +0 -205
- post.ipynb +46 -0
distance_calculate3.ipynb
DELETED
|
@@ -1,205 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"cells": [
|
| 3 |
-
{
|
| 4 |
-
"cell_type": "code",
|
| 5 |
-
"execution_count": 11,
|
| 6 |
-
"metadata": {},
|
| 7 |
-
"outputs": [
|
| 8 |
-
{
|
| 9 |
-
"name": "stdout",
|
| 10 |
-
"output_type": "stream",
|
| 11 |
-
"text": [
|
| 12 |
-
"\n",
|
| 13 |
-
"0: 384x512 1 focused_medium_core, 1 modular_small_core, 341.3ms\n",
|
| 14 |
-
"Speed: 1.5ms preprocess, 341.3ms inference, 1.5ms postprocess per image at shape (1, 3, 384, 512)\n"
|
| 15 |
-
]
|
| 16 |
-
}
|
| 17 |
-
],
|
| 18 |
-
"source": [
|
| 19 |
-
"import math\n",
|
| 20 |
-
"import cv2\n",
|
| 21 |
-
"import numpy as np\n",
|
| 22 |
-
"from ultralytics import YOLO\n",
|
| 23 |
-
"import json\n",
|
| 24 |
-
"\n",
|
| 25 |
-
"model = YOLO(\"posmPJSTRIKE_v1.3.pt\")\n",
|
| 26 |
-
"image = cv2.imread(\"1.jpeg\")\n",
|
| 27 |
-
"res = model(image)\n",
|
| 28 |
-
"\n",
|
| 29 |
-
"with open(\"base_width.json\", \"r\") as f:\n",
|
| 30 |
-
" base_width = json.load(f)"
|
| 31 |
-
]
|
| 32 |
-
},
|
| 33 |
-
{
|
| 34 |
-
"cell_type": "code",
|
| 35 |
-
"execution_count": 12,
|
| 36 |
-
"metadata": {},
|
| 37 |
-
"outputs": [],
|
| 38 |
-
"source": [
|
| 39 |
-
"def find_position(objects_names_points, par_pix_cm, image):\n",
|
| 40 |
-
" object_positions = {}\n",
|
| 41 |
-
" for obj in objects_names_points:\n",
|
| 42 |
-
" name = list(obj.keys())[0]\n",
|
| 43 |
-
" points = list(obj.values())[0]\n",
|
| 44 |
-
"\n",
|
| 45 |
-
" top_distance = round((points[0][1] - 0) * par_pix_cm[name],2)\n",
|
| 46 |
-
" bottom_distance = round((image.shape[0] - points[3][1]) * par_pix_cm[name],2)\n",
|
| 47 |
-
" left_distance = round((points[0][0] - 0) * par_pix_cm[name],2)\n",
|
| 48 |
-
" right_distance = round((image.shape[1] - points[3][0]) * par_pix_cm[name],2)\n",
|
| 49 |
-
" \n",
|
| 50 |
-
" object_positions.update({name: {\"top\": top_distance, \"bottom\": bottom_distance, \"left\": left_distance, \"right\": right_distance}})\n",
|
| 51 |
-
" return object_positions\n",
|
| 52 |
-
"\n",
|
| 53 |
-
"def get_actual_distance(closest_points, par_pix_cm):\n",
|
| 54 |
-
" actual_results_n_distance = {}\n",
|
| 55 |
-
" for i in closest_points:\n",
|
| 56 |
-
" avg_px_cm = ((par_pix_cm[i[0]] + par_pix_cm[i[1]])/2)\n",
|
| 57 |
-
" actual_results_n_distance.update({i:round(closest_points[i]*avg_px_cm,2)})\n",
|
| 58 |
-
" return actual_results_n_distance\n",
|
| 59 |
-
"\n",
|
| 60 |
-
"def pixel_per_cm(objects_names_width_pix):\n",
|
| 61 |
-
" par_pix_cm = {}\n",
|
| 62 |
-
" for i in objects_names_width_pix:\n",
|
| 63 |
-
" par_pix_cm_width = base_width[i][0]/objects_names_width_pix[i][0]\n",
|
| 64 |
-
" par_pix_cm_hight = base_width[i][1]/objects_names_width_pix[i][1]\n",
|
| 65 |
-
" avg_par_pix_cm = (par_pix_cm_width + par_pix_cm_hight)/2\n",
|
| 66 |
-
" par_pix_cm.update({i:avg_par_pix_cm})\n",
|
| 67 |
-
" return par_pix_cm\n",
|
| 68 |
-
"\n",
|
| 69 |
-
"def get_points_n_names(results):\n",
|
| 70 |
-
" objects_names_points = []\n",
|
| 71 |
-
" objects_names_width_pix = {}\n",
|
| 72 |
-
" for box, cls in zip(results[0].boxes.xyxy, results[0].boxes.cls):\n",
|
| 73 |
-
" x1, y1, x2, y2 = map(int, box)\n",
|
| 74 |
-
" class_name = results[0].names[int(cls)]\n",
|
| 75 |
-
" width = x2 - x1\n",
|
| 76 |
-
" hight = y2 - y1\n",
|
| 77 |
-
" objects_names_points.append({class_name: [(x1, y1), (x2, y1), (x1, y2), (x2, y2)]})\n",
|
| 78 |
-
" objects_names_width_pix.update({class_name: [width, hight]})\n",
|
| 79 |
-
" \n",
|
| 80 |
-
" return objects_names_points, objects_names_width_pix\n",
|
| 81 |
-
"\n",
|
| 82 |
-
"def euclidean_distance(point1, point2):\n",
|
| 83 |
-
" dist_pixels = math.sqrt((point2[0] - point1[0])**2 + (point2[1] - point1[1])**2)\n",
|
| 84 |
-
" return dist_pixels\n",
|
| 85 |
-
"\n",
|
| 86 |
-
"def find_closest_points(lst):\n",
|
| 87 |
-
" closest_points = {}\n",
|
| 88 |
-
" \n",
|
| 89 |
-
" for i in range(len(lst)):\n",
|
| 90 |
-
" for j in range(i + 1, len(lst)):\n",
|
| 91 |
-
" list1 = lst[i]\n",
|
| 92 |
-
" list2 = lst[j]\n",
|
| 93 |
-
" min_distance = float('inf')\n",
|
| 94 |
-
" closest_objects_pair = None\n",
|
| 95 |
-
" \n",
|
| 96 |
-
" for obj1 in list1.values():\n",
|
| 97 |
-
" points1 = obj1\n",
|
| 98 |
-
" for obj2 in list2.values():\n",
|
| 99 |
-
" points2 = obj2\n",
|
| 100 |
-
" \n",
|
| 101 |
-
" for point1 in points1:\n",
|
| 102 |
-
" for point2 in points2:\n",
|
| 103 |
-
" distance = euclidean_distance(point1, point2)\n",
|
| 104 |
-
" if distance < min_distance:\n",
|
| 105 |
-
" min_distance = distance\n",
|
| 106 |
-
" closest_objects_pair = (list1.keys(), list2.keys())\n",
|
| 107 |
-
" \n",
|
| 108 |
-
" closest_points.update({(list(closest_objects_pair[0])[0],list(closest_objects_pair[1])[0]):round(min_distance, 2)})\n",
|
| 109 |
-
" return closest_points\n",
|
| 110 |
-
"\n",
|
| 111 |
-
"objects_names_points, objects_names_width_pix = get_points_n_names(res)\n",
|
| 112 |
-
"par_pix_cm = pixel_per_cm(objects_names_width_pix)\n",
|
| 113 |
-
"closest_points = find_closest_points(objects_names_points)\n",
|
| 114 |
-
"actual_distances = get_actual_distance(closest_points, par_pix_cm)\n",
|
| 115 |
-
"object_position = find_position(objects_names_points, par_pix_cm, image)"
|
| 116 |
-
]
|
| 117 |
-
},
|
| 118 |
-
{
|
| 119 |
-
"cell_type": "code",
|
| 120 |
-
"execution_count": null,
|
| 121 |
-
"metadata": {},
|
| 122 |
-
"outputs": [],
|
| 123 |
-
"source": []
|
| 124 |
-
},
|
| 125 |
-
{
|
| 126 |
-
"cell_type": "code",
|
| 127 |
-
"execution_count": null,
|
| 128 |
-
"metadata": {},
|
| 129 |
-
"outputs": [],
|
| 130 |
-
"source": []
|
| 131 |
-
},
|
| 132 |
-
{
|
| 133 |
-
"cell_type": "code",
|
| 134 |
-
"execution_count": null,
|
| 135 |
-
"metadata": {},
|
| 136 |
-
"outputs": [],
|
| 137 |
-
"source": []
|
| 138 |
-
},
|
| 139 |
-
{
|
| 140 |
-
"cell_type": "code",
|
| 141 |
-
"execution_count": null,
|
| 142 |
-
"metadata": {},
|
| 143 |
-
"outputs": [],
|
| 144 |
-
"source": []
|
| 145 |
-
},
|
| 146 |
-
{
|
| 147 |
-
"cell_type": "code",
|
| 148 |
-
"execution_count": 12,
|
| 149 |
-
"metadata": {},
|
| 150 |
-
"outputs": [],
|
| 151 |
-
"source": [
|
| 152 |
-
"import requests\n",
|
| 153 |
-
"\n",
|
| 154 |
-
"url = \"https://WalidAlHassan-Distance.hf.space/process_image\"\n",
|
| 155 |
-
"data = {\"image_url\": \"https://i.postimg.cc/BQ0gHXjQ/1.jpg\"}\n",
|
| 156 |
-
"\n",
|
| 157 |
-
"response = requests.post(url, json=data)\n",
|
| 158 |
-
"\n",
|
| 159 |
-
"# print(response.status_code)\n",
|
| 160 |
-
"# print(response.json()) # Prints the response data\n"
|
| 161 |
-
]
|
| 162 |
-
},
|
| 163 |
-
{
|
| 164 |
-
"cell_type": "code",
|
| 165 |
-
"execution_count": 10,
|
| 166 |
-
"metadata": {},
|
| 167 |
-
"outputs": [
|
| 168 |
-
{
|
| 169 |
-
"data": {
|
| 170 |
-
"text/plain": [
|
| 171 |
-
"<Response [500]>"
|
| 172 |
-
]
|
| 173 |
-
},
|
| 174 |
-
"execution_count": 10,
|
| 175 |
-
"metadata": {},
|
| 176 |
-
"output_type": "execute_result"
|
| 177 |
-
}
|
| 178 |
-
],
|
| 179 |
-
"source": [
|
| 180 |
-
"response"
|
| 181 |
-
]
|
| 182 |
-
}
|
| 183 |
-
],
|
| 184 |
-
"metadata": {
|
| 185 |
-
"kernelspec": {
|
| 186 |
-
"display_name": "icr",
|
| 187 |
-
"language": "python",
|
| 188 |
-
"name": "python3"
|
| 189 |
-
},
|
| 190 |
-
"language_info": {
|
| 191 |
-
"codemirror_mode": {
|
| 192 |
-
"name": "ipython",
|
| 193 |
-
"version": 3
|
| 194 |
-
},
|
| 195 |
-
"file_extension": ".py",
|
| 196 |
-
"mimetype": "text/x-python",
|
| 197 |
-
"name": "python",
|
| 198 |
-
"nbconvert_exporter": "python",
|
| 199 |
-
"pygments_lexer": "ipython3",
|
| 200 |
-
"version": "3.12.9"
|
| 201 |
-
}
|
| 202 |
-
},
|
| 203 |
-
"nbformat": 4,
|
| 204 |
-
"nbformat_minor": 2
|
| 205 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [
|
| 8 |
+
{
|
| 9 |
+
"name": "stdout",
|
| 10 |
+
"output_type": "stream",
|
| 11 |
+
"text": [
|
| 12 |
+
"{'object_positions': {'Regular Modular FnS': {'top': '107.55 cm', 'bottom': '0.0 cm', 'left': '103.1 cm', 'right': '33.44 cm'}, 'Focused Medium Core': {'top': '101.69 cm', 'bottom': '0.0 cm', 'left': '47.57 cm', 'right': '85.74 cm'}, 'Modular Small FnS': {'top': '83.9 cm', 'bottom': '34.75 cm', 'left': '49.15 cm', 'right': '86.44 cm'}}, 'actual_distances': [{'object': ['Regular Modular FnS', 'Focused Medium Core'], 'distances': '8.73 cm'}, {'object': ['Regular Modular FnS', 'Modular Small FnS'], 'distances': '12.97 cm'}, {'object': ['Focused Medium Core', 'Modular Small FnS'], 'distances': '1.42 cm'}]}\n"
|
| 13 |
+
]
|
| 14 |
+
}
|
| 15 |
+
],
|
| 16 |
+
"source": [
|
| 17 |
+
"import requests\n",
|
| 18 |
+
"url = \"https://WalidAlHassan-Distance.hf.space/process_image\"\n",
|
| 19 |
+
"data = {\"image_url\": \"https://i.postimg.cc/BQ0gHXjQ/1.jpg\"}\n",
|
| 20 |
+
"response = requests.post(url, json=data)\n",
|
| 21 |
+
"print(response.json())"
|
| 22 |
+
]
|
| 23 |
+
}
|
| 24 |
+
],
|
| 25 |
+
"metadata": {
|
| 26 |
+
"kernelspec": {
|
| 27 |
+
"display_name": "icr",
|
| 28 |
+
"language": "python",
|
| 29 |
+
"name": "python3"
|
| 30 |
+
},
|
| 31 |
+
"language_info": {
|
| 32 |
+
"codemirror_mode": {
|
| 33 |
+
"name": "ipython",
|
| 34 |
+
"version": 3
|
| 35 |
+
},
|
| 36 |
+
"file_extension": ".py",
|
| 37 |
+
"mimetype": "text/x-python",
|
| 38 |
+
"name": "python",
|
| 39 |
+
"nbconvert_exporter": "python",
|
| 40 |
+
"pygments_lexer": "ipython3",
|
| 41 |
+
"version": "3.12.9"
|
| 42 |
+
}
|
| 43 |
+
},
|
| 44 |
+
"nbformat": 4,
|
| 45 |
+
"nbformat_minor": 2
|
| 46 |
+
}
|