File size: 5,323 Bytes
ebc51c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO: Created TensorFlow Lite XNNPACK delegate for CPU.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from f_segment_img import *\n",
    "from f_measurents import *\n",
    "import gradio as gr\n",
    "import dotenv\n",
    "import ast\n",
    "dotenv.load_dotenv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_sam():\n",
    "    sam_checkpoint = \"sam_vit_h_4b8939.pth\"\n",
    "    model_type = \"vit_h\"; device = \"cuda\"\n",
    "    sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)\n",
    "    return sam\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plt2arr(fig, draw=True):\n",
    "    if draw: fig.canvas.draw()\n",
    "    rgba_buf = fig.canvas.buffer_rgba()\n",
    "    (w,h) = fig.canvas.get_width_height()\n",
    "    rgba_arr = np.frombuffer(rgba_buf, dtype=np.uint8).reshape((h,w,4))\n",
    "    return rgba_arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def frame_size_width_mm(dropdown_label):\n",
    "    if dropdown_label == 'Small (142 mm)': frame_width_px = 142\n",
    "    elif dropdown_label == 'Medium (xx mm)': frame_width_px = 150\n",
    "    elif dropdown_label == 'Large (xx mm)': frame_width_px = 155\n",
    "    return frame_width_px"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ipd_app(image,dropdown_label):\n",
    "    # \n",
    "    landmarks = ast.literal_eval(os.environ['landmarks'])\n",
    "    frame_processed, measurements = measure_landmarks_img(image, landmarks, plot_landmarks_on_img = True, plot_data_on_img = True)\n",
    "    # \n",
    "    image, img_cropped, masks_selection, objects_segmented = segment_frame_from_img(image, landmarks, create_sam())\n",
    "    # \n",
    "    frame_width_px = get_frame_width(masks_selection)\n",
    "    frame_width_mm = frame_size_width_mm(dropdown_label)\n",
    "    ipd_mm = ipd_calibration(measurements['ipd_px'], frame_width_px, frame_width_mm)\n",
    "    text_ipd = 'IPD: ' + str(round(ipd_mm,2)) + ' mm'\n",
    "    # \n",
    "    sam_check = plot_sam_check_segmentation_frame(image, img_cropped, objects_segmented)\n",
    "    sam_check_numpy = plt2arr(sam_check, draw = True)\n",
    "    # \n",
    "    return text_ipd, frame_processed, str(measurements), sam_check_numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_test = '/Users/danielfiuzadosil/Documents/GitHub_Repo/Bryant_Medical/eCommerce/App_IPD [Master]/ipd_app/data/raw/segmentation/sample_w_frames.jpeg'\n",
    "image = cv2.cvtColor(cv2.imread(image_test),cv2.COLOR_BGR2RGB)\n",
    "dropdown_label = \"Small (xx mm)\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/homebrew/lib/python3.9/site-packages/gradio/deprecation.py:43: UserWarning: You have unused kwarg parameters in Dropdown, please remove them: {'info': 'For calibration'}\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IMPORTANT: You are using gradio version 3.7, however version 3.14.0 is available, please upgrade.\n",
      "--------\n",
      "Running on local URL:  http://127.0.0.1:7860\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "dropdown = gr.Dropdown([\"Small (142 mm)\", \"Medium (xx mm)\", \"Large (xx mm)\"], label=\"Refractives Frame Size\", info=\"For calibration\")\n",
    "demo = gr.Interface(fn=ipd_app, inputs=[\"image\",dropdown], outputs=[\"text\", \"image\", \"text\", \"image\"])\n",
    "demo.launch(debug=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.15"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}