WalidAlHassan commited on
Commit
bfc9cdd
·
1 Parent(s): 5cab7d5
.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
+ *.jpeg filter=lfs diff=lfs merge=lfs -text
base_width.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "Name": ["Width","hight"],
3
+ "focused_medium_core": [49.6,34.6],
4
+ "modular_small_core": [44.6,17.2],
5
+ "focused_modular_extra_small_core": [25.7,12.7],
6
+ "regular_modular_core": [42.9,25.5],
7
+ "reusable_pack_dispenser_core": [28.15,15.15],
8
+
9
+ "regular_modular_fns": [42.9,25.5],
10
+ "modular_small_fns": [44.8,17.1],
11
+ "focused_medium_fns": [49.6,34.6],
12
+ "reusable_pack_dispenser_fns": [28.15,15.15],
13
+ "focused_modular_extra_small_fns": [25.5,12.7]
14
+ }
distance_calculate.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import requests
3
+ import numpy as np
4
+ import json
5
+ from io import BytesIO
6
+ from fastapi import FastAPI
7
+ from pydantic import BaseModel
8
+ from ultralytics import YOLO
9
+ from PIL import Image
10
+ import traceback
11
+
12
+ app = FastAPI()
13
+
14
+ model = YOLO("posmPJSTRIKE_v1.3.pt")
15
+
16
+ with open("base_width.json", "r") as f:
17
+ base_width = json.load(f)
18
+
19
+ with open("name_conversion.json", "r") as f:
20
+ name_convert = json.load(f)
21
+
22
+ class ImageRequest(BaseModel):
23
+ image_url: str
24
+
25
+ def get_image_from_url(image_url):
26
+ response = requests.get(image_url)
27
+ image = Image.open(BytesIO(response.content)).convert("RGB")
28
+ return np.array(image)
29
+
30
+ def name_conversion(actual_distances,object_positions, name_convert):
31
+ actual_distances_sys = []
32
+ object_positions_sys = {}
33
+ for item in actual_distances:
34
+ actual_distances_sys.append({'object':(name_convert[list(item.values())[0][0]],name_convert[list(item.values())[0][1]]),'distances': str(list(item.values())[1]) + " cm"})
35
+
36
+ for item in object_positions:
37
+ object_positions_sys.update({name_convert[item]:{"top": str(object_positions[item]['top']) + " cm", "bottom": str(object_positions[item]['bottom']) + " cm", "left": str(object_positions[item]['left']) + " cm", "right": str(object_positions[item]['right']) + " cm"}})
38
+ return object_positions_sys, actual_distances_sys
39
+
40
+ def find_position(objects_names_points, par_pix_cm, image):
41
+ object_positions = {}
42
+ for obj in objects_names_points:
43
+ name = list(obj.keys())[0]
44
+ points = list(obj.values())[0]
45
+
46
+ top_distance = round((points[0][1] - 0) * par_pix_cm[name], 2)
47
+ bottom_distance = round((image.size[1] - points[3][1]) * par_pix_cm[name], 2)
48
+ left_distance = round((points[0][0] - 0) * par_pix_cm[name], 2)
49
+ right_distance = round((image.size[0] - points[3][0]) * par_pix_cm[name], 2)
50
+
51
+ object_positions.update({name: {"top": top_distance, "bottom": bottom_distance, "left": left_distance, "right": right_distance}})
52
+ return object_positions
53
+
54
+ def get_actual_distance(closest_points, par_pix_cm):
55
+ actual_results_n_distance = []
56
+ for i in closest_points:
57
+ avg_px_cm = ((par_pix_cm[i[0]] + par_pix_cm[i[1]]) / 2)
58
+ actual_results_n_distance.append({'object': i, 'distances': round(closest_points[i] * avg_px_cm, 2)})
59
+ return actual_results_n_distance
60
+
61
+ def pixel_per_cm(objects_names_width_pix):
62
+ par_pix_cm = {}
63
+ for i in objects_names_width_pix:
64
+ par_pix_cm_width = base_width[i][0] / objects_names_width_pix[i][0]
65
+ par_pix_cm_height = base_width[i][1] / objects_names_width_pix[i][1]
66
+ avg_par_pix_cm = (par_pix_cm_width + par_pix_cm_height) / 2
67
+ par_pix_cm.update({i: avg_par_pix_cm})
68
+ return par_pix_cm
69
+
70
+ def get_points_n_names(results):
71
+ objects_names_points = []
72
+ objects_names_width_pix = {}
73
+ for box, cls in zip(results[0].boxes.xyxy, results[0].boxes.cls):
74
+ x1, y1, x2, y2 = map(int, box)
75
+ class_name = results[0].names[int(cls)]
76
+ width = x2 - x1
77
+ height = y2 - y1
78
+ objects_names_points.append({class_name: [(x1, y1), (x2, y1), (x1, y2), (x2, y2)]})
79
+ objects_names_width_pix.update({class_name: [width, height]})
80
+
81
+ return objects_names_points, objects_names_width_pix
82
+
83
+ def euclidean_distance(point1, point2):
84
+ dist_pixels = math.sqrt((point2[0] - point1[0])**2 + (point2[1] - point1[1])**2)
85
+ return dist_pixels
86
+
87
+ def find_closest_points(lst):
88
+ closest_points = {}
89
+
90
+ for i in range(len(lst)):
91
+ for j in range(i + 1, len(lst)):
92
+ list1 = lst[i]
93
+ list2 = lst[j]
94
+ min_distance = float('inf')
95
+ closest_objects_pair = None
96
+
97
+ for obj1 in list1.values():
98
+ points1 = obj1
99
+ for obj2 in list2.values():
100
+ points2 = obj2
101
+
102
+ for point1 in points1:
103
+ for point2 in points2:
104
+ distance = euclidean_distance(point1, point2)
105
+ if distance < min_distance:
106
+ min_distance = distance
107
+ closest_objects_pair = (list1.keys(), list2.keys())
108
+
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
+ return {
114
+ "object_positions": {
115
+ "Regular Modular FnS": {"top": "107.55 cm", "bottom": "0.0 cm", "left": "103.1 cm", "right": "33.44 cm"},
116
+ "Focused Medium Core": {"top": "101.69 cm", "bottom": "0.0 cm", "left": "47.57 cm", "right": "85.74 cm"},
117
+ "Modular Small FnS": {"top": "83.9 cm", "bottom": "34.75 cm", "left": "49.15 cm", "right": "86.44 cm"}
118
+ },
119
+ "actual_distances": [
120
+ {"object": ["Regular Modular FnS", "Focused Medium Core"], "distances": "8.73 cm"},
121
+ {"object": ["Regular Modular FnS", "Modular Small FnS"], "distances": "12.97 cm"},
122
+ {"object": ["Focused Medium Core", "Modular Small FnS"], "distances": "1.42 cm"}
123
+ ]
124
+ }
125
+
126
+
127
+
128
+ @app.post("/process_image")
129
+ def process_image(request: ImageRequest):
130
+ try:
131
+ image = get_image_from_url(request.image_url)
132
+ image = Image.fromarray(image)
133
+ size = (640, 640)
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)
147
+ par_pix_cm = pixel_per_cm(objects_names_width_pix)
148
+ closest_points = find_closest_points(objects_names_points)
149
+ actual_distances = get_actual_distance(closest_points, par_pix_cm)
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
157
+ for item in actual_distances[:]:
158
+ if item['object'][0] == item['object'][1]:
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,
173
+ "actual_distances": actual_distances_sys
174
+ }
name_conversion.json ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "pvc_base_surface_hanger_core":"PVC Base Surface Hanger Core",
3
+ "paper_base_surface_hanger_core":"Paper Base Surface Hanger Core",
4
+ "pvc_base_surface_hanger_fns":"PVC Base Surface Hanger FnS",
5
+ "paper_base_surface_hanger_fns":"Paper Base Surface Hanger FnS",
6
+ "focused_medium_core":"Focused Medium Core",
7
+ "focused_modular_extra_small_core":"Focused Modular-Extra Small Core",
8
+ "focused_modular_extra_small_fns":"Focused Modular-Extra Small FnS",
9
+ "focused_medium_fns":"Focused Medium FnS",
10
+ "modular_small_core":"Modular Small Core",
11
+ "modular_small_fns":"Modular Small FnS",
12
+ "regular_modular_core":"Regular Modular Core",
13
+ "regular_modular_fns":"Regular Modular FnS",
14
+ "reusable_pack_dispenser_core":"Reusable Pack Dispenser Core",
15
+ "reusable_pack_dispenser_fns":"Reusable Pack Dispenser FnS",
16
+ "street_kiosk_identifier_core":"Street Kiosk Identifier Core",
17
+ "street_kiosk_identifier_fns":"Street Kiosk Identifier FnS"
18
+ }
posmPJSTRIKE_v1.3.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e318caeaf1d380b927894f517d142f4176a4ad20c8706f01fd14d089e7f18980
3
+ size 144064701
test.ipynb ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "for item in actual_distances[:]:\n",
46
+ " if item['object'][0] == item['object'][1]:\n",
47
+ " actual_distances.remove(item)"
48
+ ]
49
+ }
50
+ ],
51
+ "metadata": {
52
+ "kernelspec": {
53
+ "display_name": "bat",
54
+ "language": "python",
55
+ "name": "python3"
56
+ },
57
+ "language_info": {
58
+ "codemirror_mode": {
59
+ "name": "ipython",
60
+ "version": 3
61
+ },
62
+ "file_extension": ".py",
63
+ "mimetype": "text/x-python",
64
+ "name": "python",
65
+ "nbconvert_exporter": "python",
66
+ "pygments_lexer": "ipython3",
67
+ "version": "3.12.8"
68
+ }
69
+ },
70
+ "nbformat": 4,
71
+ "nbformat_minor": 2
72
+ }
test_data/1.jpeg ADDED

Git LFS Details

  • SHA256: 78d307b0bb844becd4e374287b0044a303edc800ec3749e81318bd7e731bfe41
  • Pointer size: 131 Bytes
  • Size of remote file: 136 kB
test_data/2.jpeg ADDED

Git LFS Details

  • SHA256: 14056c929d3916c7f012939b6aee68a964b7cbc0c632b8e2a7a38c19932c5089
  • Pointer size: 131 Bytes
  • Size of remote file: 142 kB
test_data/3.jpeg ADDED

Git LFS Details

  • SHA256: e395a7e3452c5219bd8b9dd773cc1d16e081aa43d16d2f574106d457ca9c98fc
  • Pointer size: 131 Bytes
  • Size of remote file: 230 kB
test_data/4.jpeg ADDED

Git LFS Details

  • SHA256: f003acc9d15f03c366a749d9ececac6b37a686e67390a273669fcb5f845addd0
  • Pointer size: 132 Bytes
  • Size of remote file: 1.09 MB
test_data/5.jpeg ADDED

Git LFS Details

  • SHA256: 6ade32ca4cb6611934983d516b17c20ce7adea8e21a5e4051fac378a1057bb52
  • Pointer size: 131 Bytes
  • Size of remote file: 218 kB
test_data/6.jpeg ADDED

Git LFS Details

  • SHA256: 1d9db0ffd60721e3dc7db116084cacfaee6baacc0565a3373563d10f883b8ef4
  • Pointer size: 132 Bytes
  • Size of remote file: 1.28 MB
test_data/7.jpeg ADDED

Git LFS Details

  • SHA256: f0bc60ad7bb122bb97011cb07b0f788cdafc557f5c5079de87336a684e9027ca
  • Pointer size: 131 Bytes
  • Size of remote file: 338 kB