Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,23 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
import os
|
| 4 |
import pandas as pd
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
-
from PIL import Image
|
| 7 |
-
from shapely.geometry import Polygon
|
| 8 |
-
from shapely.ops import unary_union
|
| 9 |
import zipfile
|
| 10 |
-
import
|
| 11 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
# === THEME SETUP ===
|
| 14 |
st.markdown("""
|
| 15 |
<style>
|
| 16 |
.main {
|
|
@@ -31,62 +38,64 @@ st.markdown("""
|
|
| 31 |
</style>
|
| 32 |
""", unsafe_allow_html=True)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
if uploaded_files:
|
| 43 |
-
st.subheader("Uploaded Images")
|
| 44 |
-
for uploaded_file in uploaded_files:
|
| 45 |
-
image = Image.open(uploaded_file)
|
| 46 |
-
st.image(image, caption=uploaded_file.name, use_column_width=True)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
model_name = st.text_input("pre-eclampsia-vhaot")
|
| 51 |
-
project_version = st.text_input("20")
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
for uploaded_file in uploaded_files:
|
| 63 |
-
image = Image.open(uploaded_file)
|
| 64 |
-
image_path = f"temp_{uuid.uuid4().hex}.png"
|
| 65 |
-
image.save(image_path)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
if mask_url:
|
| 75 |
-
st.image(mask_url, caption="Segmented Image", use_column_width=True)
|
| 76 |
-
else:
|
| 77 |
-
st.warning(f"No segmentation detected for {uploaded_file.name}.")
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import roboflow
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
| 5 |
import zipfile
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
+
import json
|
| 9 |
+
from shapely.geometry import Polygon
|
| 10 |
+
from PIL import Image
|
| 11 |
+
from io import BytesIO
|
| 12 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 13 |
+
from google.oauth2 import service_account
|
| 14 |
+
from googleapiclient.discovery import build
|
| 15 |
+
from googleapiclient.http import MediaIoBaseUpload
|
| 16 |
+
import gspread
|
| 17 |
+
import time
|
| 18 |
+
|
| 19 |
+
st.set_page_config(page_title="Image Segmentation - Roboflow", layout="wide")
|
| 20 |
|
|
|
|
| 21 |
st.markdown("""
|
| 22 |
<style>
|
| 23 |
.main {
|
|
|
|
| 38 |
</style>
|
| 39 |
""", unsafe_allow_html=True)
|
| 40 |
|
| 41 |
+
# 🔥 Initialize Roboflow
|
| 42 |
+
API_KEY = "mGkz7QhkhD90YfeiaOxV"
|
| 43 |
+
rf = roboflow.Roboflow(api_key=API_KEY)
|
| 44 |
+
project = rf.workspace().project("pre-eclampsia-vhaot")
|
| 45 |
+
model = project.version("20").model
|
| 46 |
+
model.confidence = 80
|
| 47 |
+
model.overlap = 25
|
| 48 |
+
dpi_value = 300
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
with st.expander("⚙️ Advanced Settings", expanded=True):
|
| 51 |
+
model.confidence = st.slider("Model Confidence (%)", 20, 100, 80)
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# 📁 Setup Google Drive and Sheets
|
| 54 |
+
scope = ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/spreadsheets"]
|
| 55 |
+
credentials_dict = json.loads(st.secrets["gcp_service_account"])
|
| 56 |
+
credentials = service_account.Credentials.from_service_account_info(credentials_dict, scopes=scope)
|
| 57 |
+
drive_service = build("drive", "v3", credentials=credentials)
|
| 58 |
+
sheets_client = gspread.authorize(credentials)
|
| 59 |
+
sheet = sheets_client.open_by_url(st.secrets["feedback_sheet_url"]).sheet1
|
| 60 |
|
| 61 |
+
# 📌 Helper Functions
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
def calculate_polygon_area(points):
|
| 64 |
+
polygon = Polygon([(p['x'], p['y']) for p in points])
|
| 65 |
+
return polygon.area
|
| 66 |
|
| 67 |
+
def safe_predict(image_path):
|
| 68 |
+
for attempt in range(3):
|
| 69 |
+
try:
|
| 70 |
+
return model.predict(image_path)
|
| 71 |
+
except:
|
| 72 |
+
time.sleep(1)
|
| 73 |
+
return None
|
| 74 |
|
| 75 |
+
def resize_image(image):
|
| 76 |
+
return image.resize((640, 640))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
def upload_to_drive(image_bytes, filename, folder_id):
|
| 79 |
+
media = MediaIoBaseUpload(image_bytes, mimetype='image/png')
|
| 80 |
+
drive_service.files().create(
|
| 81 |
+
body={"name": filename, "parents": [folder_id]},
|
| 82 |
+
media_body=media,
|
| 83 |
+
fields='id'
|
| 84 |
+
).execute()
|
| 85 |
|
| 86 |
+
def find_or_create_folder(folder_name, parent=None):
|
| 87 |
+
query = f"name='{folder_name}' and mimeType='application/vnd.google-apps.folder' and trashed=false"
|
| 88 |
+
if parent:
|
| 89 |
+
query += f" and '{parent}' in parents"
|
| 90 |
+
results = drive_service.files().list(q=query, spaces='drive', fields='files(id, name)').execute()
|
| 91 |
+
folders = results.get('files', [])
|
| 92 |
+
if folders:
|
| 93 |
+
return folders[0]['id']
|
| 94 |
+
file_metadata = {
|
| 95 |
+
'name': folder_name,
|
| 96 |
+
'mimeType': 'application/vnd.google-apps.folder'
|
| 97 |
+
}
|
| 98 |
+
if parent:
|
| 99 |
+
file_metadata['parents'] = [parent]
|
| 100 |
+
file = drive_service.files().create(body=file_metadata, fields='id').execute()
|
| 101 |
+
return file.get('id')
|