Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ import time
|
|
| 18 |
|
| 19 |
# 🔥 Initialize Roboflow
|
| 20 |
API_KEY = st.secrets["roboflow_api_key"]
|
| 21 |
-
rf = roboflow.Roboflow(api_key=API_KEY
|
| 22 |
project = rf.workspace(st.secrets["roboflow_workspace"]).project(st.secrets["roboflow_project"])
|
| 23 |
model = project.version(st.secrets["roboflow_version"]).model
|
| 24 |
model.confidence = 80
|
|
@@ -30,7 +30,7 @@ with st.expander("⚙️ Advanced Settings", expanded=True):
|
|
| 30 |
|
| 31 |
# 📁 Setup Google Drive and Sheets (seção aprimorada)
|
| 32 |
scope = ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/spreadsheets"]
|
| 33 |
-
credentials_dict = json.loads(st.secrets["gcp_service_account"]
|
| 34 |
credentials = service_account.Credentials.from_service_account_info(credentials_dict, scopes=scope)
|
| 35 |
drive_service = build("drive", "v3", credentials=credentials)
|
| 36 |
sheets_client = gspread.authorize(credentials)
|
|
@@ -63,29 +63,48 @@ def safe_predict(image_path):
|
|
| 63 |
def resize_image(image):
|
| 64 |
return image.resize((640, 640))
|
| 65 |
|
|
|
|
| 66 |
def upload_to_drive(image_bytes, filename, folder_id):
|
| 67 |
media = MediaIoBaseUpload(image_bytes, mimetype='image/png')
|
| 68 |
drive_service.files().create(
|
| 69 |
body={"name": filename, "parents": [folder_id]},
|
| 70 |
media_body=media,
|
| 71 |
-
fields='id'
|
|
|
|
| 72 |
).execute()
|
| 73 |
|
|
|
|
| 74 |
def find_or_create_folder(folder_name, parent=None):
|
| 75 |
query = f"name='{folder_name}' and mimeType='application/vnd.google-apps.folder' and trashed=false"
|
| 76 |
if parent:
|
| 77 |
query += f" and '{parent}' in parents"
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
folders = results.get('files', [])
|
| 80 |
if folders:
|
| 81 |
return folders[0]['id']
|
|
|
|
| 82 |
file_metadata = {
|
| 83 |
'name': folder_name,
|
| 84 |
'mimeType': 'application/vnd.google-apps.folder'
|
| 85 |
}
|
| 86 |
if parent:
|
| 87 |
file_metadata['parents'] = [parent]
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
return file.get('id')
|
| 90 |
|
| 91 |
def get_image_bytes(image):
|
|
|
|
| 18 |
|
| 19 |
# 🔥 Initialize Roboflow
|
| 20 |
API_KEY = st.secrets["roboflow_api_key"]
|
| 21 |
+
rf = roboflow.Roboflow(api_key=API_KEY )
|
| 22 |
project = rf.workspace(st.secrets["roboflow_workspace"]).project(st.secrets["roboflow_project"])
|
| 23 |
model = project.version(st.secrets["roboflow_version"]).model
|
| 24 |
model.confidence = 80
|
|
|
|
| 30 |
|
| 31 |
# 📁 Setup Google Drive and Sheets (seção aprimorada)
|
| 32 |
scope = ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/spreadsheets"]
|
| 33 |
+
credentials_dict = json.loads(st.secrets["gcp_service_account"] )
|
| 34 |
credentials = service_account.Credentials.from_service_account_info(credentials_dict, scopes=scope)
|
| 35 |
drive_service = build("drive", "v3", credentials=credentials)
|
| 36 |
sheets_client = gspread.authorize(credentials)
|
|
|
|
| 63 |
def resize_image(image):
|
| 64 |
return image.resize((640, 640))
|
| 65 |
|
| 66 |
+
# --- CÓDIGO CORRIGIDO ---
|
| 67 |
def upload_to_drive(image_bytes, filename, folder_id):
|
| 68 |
media = MediaIoBaseUpload(image_bytes, mimetype='image/png')
|
| 69 |
drive_service.files().create(
|
| 70 |
body={"name": filename, "parents": [folder_id]},
|
| 71 |
media_body=media,
|
| 72 |
+
fields='id',
|
| 73 |
+
supportsAllDrives=True # <-- CORREÇÃO APLICADA
|
| 74 |
).execute()
|
| 75 |
|
| 76 |
+
# --- CÓDIGO CORRIGIDO ---
|
| 77 |
def find_or_create_folder(folder_name, parent=None):
|
| 78 |
query = f"name='{folder_name}' and mimeType='application/vnd.google-apps.folder' and trashed=false"
|
| 79 |
if parent:
|
| 80 |
query += f" and '{parent}' in parents"
|
| 81 |
+
|
| 82 |
+
# Adiciona os parâmetros para buscar em todos os drives/pastas compartilhadas
|
| 83 |
+
results = drive_service.files().list(
|
| 84 |
+
q=query,
|
| 85 |
+
spaces='drive',
|
| 86 |
+
fields='files(id, name)',
|
| 87 |
+
supportsAllDrives=True, # <-- CORREÇÃO APLICADA
|
| 88 |
+
includeItemsFromAllDrives=True # <-- CORREÇÃO APLICADA
|
| 89 |
+
).execute()
|
| 90 |
+
|
| 91 |
folders = results.get('files', [])
|
| 92 |
if folders:
|
| 93 |
return folders[0]['id']
|
| 94 |
+
|
| 95 |
file_metadata = {
|
| 96 |
'name': folder_name,
|
| 97 |
'mimeType': 'application/vnd.google-apps.folder'
|
| 98 |
}
|
| 99 |
if parent:
|
| 100 |
file_metadata['parents'] = [parent]
|
| 101 |
+
|
| 102 |
+
# Adiciona o parâmetro para criar a pasta em um ambiente compartilhado
|
| 103 |
+
file = drive_service.files().create(
|
| 104 |
+
body=file_metadata,
|
| 105 |
+
fields='id',
|
| 106 |
+
supportsAllDrives=True # <-- CORREÇÃO APLICADA
|
| 107 |
+
).execute()
|
| 108 |
return file.get('id')
|
| 109 |
|
| 110 |
def get_image_bytes(image):
|