{ "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": [ "
" ], "text/plain": [ "