Spaces:
Runtime error
Runtime error
Commit
·
e43696a
1
Parent(s):
a227e98
make sync dataset function doesnot delete pkl files
Browse files
deepface/DeepFace.py
CHANGED
|
@@ -565,8 +565,21 @@ def sync_datasets():
|
|
| 565 |
os.makedirs(missing_dir, exist_ok=True)
|
| 566 |
os.makedirs(founded_dir, exist_ok=True)
|
| 567 |
|
| 568 |
-
cloudservice.delete_pkl_files(missing_dir)
|
| 569 |
missing_people = cloudservice.sync_folder('missing_people', missing_dir)
|
| 570 |
-
|
| 571 |
founded_people = cloudservice.sync_folder('founded_people', founded_dir)
|
| 572 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 565 |
os.makedirs(missing_dir, exist_ok=True)
|
| 566 |
os.makedirs(founded_dir, exist_ok=True)
|
| 567 |
|
|
|
|
| 568 |
missing_people = cloudservice.sync_folder('missing_people', missing_dir)
|
| 569 |
+
|
| 570 |
founded_people = cloudservice.sync_folder('founded_people', founded_dir)
|
| 571 |
|
| 572 |
+
def delete_pkls():
|
| 573 |
+
# Set the local directories
|
| 574 |
+
base_dir = os_path.get_main_directory()
|
| 575 |
+
|
| 576 |
+
missing_dir = os.path.join(base_dir, 'mafqoud', 'images', 'missing_people')
|
| 577 |
+
founded_dir = os.path.join(base_dir, 'mafqoud', 'images', 'founded_people')
|
| 578 |
+
|
| 579 |
+
# Ensure the directories exist
|
| 580 |
+
os.makedirs(missing_dir, exist_ok=True)
|
| 581 |
+
os.makedirs(founded_dir, exist_ok=True)
|
| 582 |
+
|
| 583 |
+
cloudservice.delete_pkl_files(missing_dir)
|
| 584 |
+
cloudservice.delete_pkl_files(founded_dir)
|
| 585 |
+
|
deepface/api/src/modules/core/routes.py
CHANGED
|
@@ -142,7 +142,7 @@ def find():
|
|
| 142 |
response.status_code = 400
|
| 143 |
return response
|
| 144 |
|
| 145 |
-
|
| 146 |
if not os.path.exists(img_path) or not os.path.isfile(img_path):
|
| 147 |
# If the image does not exist, return a JSON response with status code 404
|
| 148 |
response = jsonify({'error': 'Image not found'})
|
|
@@ -198,4 +198,10 @@ def find():
|
|
| 198 |
@blueprint.route("/dataset/sync", methods=["GET"])
|
| 199 |
def sync_datasets():
|
| 200 |
result = service.sync_datasets()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
return jsonify(result)
|
|
|
|
| 142 |
response.status_code = 400
|
| 143 |
return response
|
| 144 |
|
| 145 |
+
print(img_path)
|
| 146 |
if not os.path.exists(img_path) or not os.path.isfile(img_path):
|
| 147 |
# If the image does not exist, return a JSON response with status code 404
|
| 148 |
response = jsonify({'error': 'Image not found'})
|
|
|
|
| 198 |
@blueprint.route("/dataset/sync", methods=["GET"])
|
| 199 |
def sync_datasets():
|
| 200 |
result = service.sync_datasets()
|
| 201 |
+
return jsonify(result)
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
@blueprint.route("/delete/pkls", methods=["GET"])
|
| 205 |
+
def delete_pkls():
|
| 206 |
+
result = service.delete_pkls()
|
| 207 |
return jsonify(result)
|
deepface/api/src/modules/core/service.py
CHANGED
|
@@ -73,5 +73,12 @@ def sync_datasets():
|
|
| 73 |
try:
|
| 74 |
DeepFace.sync_datasets()
|
| 75 |
return {'data': 'synced successfully'}, 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
return {'error': str(e)}, 400
|
|
|
|
| 73 |
try:
|
| 74 |
DeepFace.sync_datasets()
|
| 75 |
return {'data': 'synced successfully'}, 200
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return {'error': str(e)}, 400
|
| 78 |
+
|
| 79 |
+
def delete_pkls():
|
| 80 |
+
try:
|
| 81 |
+
DeepFace.delete_pkls()
|
| 82 |
+
return {'data': 'pkl files deleted successfully'}, 200
|
| 83 |
except Exception as e:
|
| 84 |
return {'error': str(e)}, 400
|
deepface/modules/cloudservice.py
CHANGED
|
@@ -1,3 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from flask import Flask, jsonify
|
| 2 |
import cloudinary
|
| 3 |
import cloudinary.api
|
|
@@ -7,6 +74,7 @@ import glob
|
|
| 7 |
import requests
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
from deepface.commons.logger import Logger
|
|
|
|
| 10 |
|
| 11 |
logger = Logger(module="modules/cloudservice.py")
|
| 12 |
|
|
@@ -21,11 +89,32 @@ cloudinary.config(
|
|
| 21 |
|
| 22 |
|
| 23 |
def fetch_cloudinary_images(folder_name):
|
| 24 |
-
|
| 25 |
resources = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
res = cloudinary.api.resources(type='upload', resource_type='image', prefix=f'mafqoud/images/{folder_name}')
|
| 28 |
-
resources.extend(res.get('resources', []))
|
| 29 |
return resources
|
| 30 |
|
| 31 |
def download_image(url, local_path):
|
|
@@ -46,7 +135,7 @@ def sync_folder(folder_name, local_dir):
|
|
| 46 |
public_id = img['public_id']
|
| 47 |
file_name = url.split('/')[-1] # Get the actual file name
|
| 48 |
local_path = os.path.join(local_dir, file_name)
|
| 49 |
-
|
| 50 |
|
| 51 |
if not os.path.exists(local_path):
|
| 52 |
download_image(url, local_path)
|
|
@@ -55,7 +144,7 @@ def sync_folder(folder_name, local_dir):
|
|
| 55 |
# Remove old images
|
| 56 |
local_images = [os.path.join(local_dir, f) for f in os.listdir(local_dir) if os.path.isfile(os.path.join(local_dir, f))]
|
| 57 |
for local_path in local_images:
|
| 58 |
-
if local_path not in downloaded_paths:
|
| 59 |
os.remove(local_path)
|
| 60 |
|
| 61 |
def delete_pkl_files(directory):
|
|
@@ -63,6 +152,3 @@ def delete_pkl_files(directory):
|
|
| 63 |
pkl_files = glob.glob(os.path.join(directory, '*.pkl'))
|
| 64 |
for pkl_file in pkl_files:
|
| 65 |
os.remove(pkl_file)
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 1 |
+
# from flask import Flask, jsonify
|
| 2 |
+
# import cloudinary
|
| 3 |
+
# import cloudinary.api
|
| 4 |
+
# import cloudinary.uploader
|
| 5 |
+
# import os
|
| 6 |
+
# import glob
|
| 7 |
+
# import requests
|
| 8 |
+
# from dotenv import load_dotenv
|
| 9 |
+
# from deepface.commons.logger import Logger
|
| 10 |
+
|
| 11 |
+
# logger = Logger(module="modules/cloudservice.py")
|
| 12 |
+
|
| 13 |
+
# load_dotenv()
|
| 14 |
+
|
| 15 |
+
# # Configure Cloudinary
|
| 16 |
+
# cloudinary.config(
|
| 17 |
+
# cloud_name=os.getenv('CLOUDINARY_CLOUD_NAME'),
|
| 18 |
+
# api_key=os.getenv('CLOUDINARY_API_KEY'),
|
| 19 |
+
# api_secret=os.getenv('CLOUDINARY_API_SECRET')
|
| 20 |
+
# )
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# def fetch_cloudinary_images(folder_name):
|
| 24 |
+
|
| 25 |
+
# resources = []
|
| 26 |
+
|
| 27 |
+
# res = cloudinary.api.resources(type='upload', resource_type='image', prefix=f'mafqoud/images/{folder_name}')
|
| 28 |
+
# resources.extend(res.get('resources', []))
|
| 29 |
+
# return resources
|
| 30 |
+
|
| 31 |
+
# def download_image(url, local_path):
|
| 32 |
+
# response = requests.get(url, stream=True)
|
| 33 |
+
# if response.status_code == 200:
|
| 34 |
+
# with open(local_path, 'wb') as file:
|
| 35 |
+
# for chunk in response:
|
| 36 |
+
# file.write(chunk)
|
| 37 |
+
|
| 38 |
+
# def sync_folder(folder_name, local_dir):
|
| 39 |
+
# cloudinary_images = fetch_cloudinary_images(folder_name)
|
| 40 |
+
# cloudinary_urls = {img['secure_url']: img['public_id'] for img in cloudinary_images}
|
| 41 |
+
|
| 42 |
+
# # Download new images and track downloaded image paths
|
| 43 |
+
# downloaded_paths = []
|
| 44 |
+
# for img in cloudinary_images:
|
| 45 |
+
# url = img['secure_url']
|
| 46 |
+
# public_id = img['public_id']
|
| 47 |
+
# file_name = url.split('/')[-1] # Get the actual file name
|
| 48 |
+
# local_path = os.path.join(local_dir, file_name)
|
| 49 |
+
# print(local_path)
|
| 50 |
+
|
| 51 |
+
# if not os.path.exists(local_path):
|
| 52 |
+
# download_image(url, local_path)
|
| 53 |
+
# downloaded_paths.append(local_path)
|
| 54 |
+
|
| 55 |
+
# # Remove old images
|
| 56 |
+
# local_images = [os.path.join(local_dir, f) for f in os.listdir(local_dir) if os.path.isfile(os.path.join(local_dir, f))]
|
| 57 |
+
# for local_path in local_images:
|
| 58 |
+
# if local_path not in downloaded_paths and (local_path.endswith('.jpg') or local_path.endswith('.jpeg') or local_path.endswith('.png')):
|
| 59 |
+
# os.remove(local_path)
|
| 60 |
+
|
| 61 |
+
# def delete_pkl_files(directory):
|
| 62 |
+
# """Delete all .pkl files in the specified directory."""
|
| 63 |
+
# pkl_files = glob.glob(os.path.join(directory, '*.pkl'))
|
| 64 |
+
# for pkl_file in pkl_files:
|
| 65 |
+
# os.remove(pkl_file)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
from flask import Flask, jsonify
|
| 69 |
import cloudinary
|
| 70 |
import cloudinary.api
|
|
|
|
| 74 |
import requests
|
| 75 |
from dotenv import load_dotenv
|
| 76 |
from deepface.commons.logger import Logger
|
| 77 |
+
from deepface import DeepFace # Assuming this is the correct import for training
|
| 78 |
|
| 79 |
logger = Logger(module="modules/cloudservice.py")
|
| 80 |
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
def fetch_cloudinary_images(folder_name):
|
|
|
|
| 92 |
resources = []
|
| 93 |
+
next_cursor = None
|
| 94 |
+
|
| 95 |
+
while True:
|
| 96 |
+
if next_cursor:
|
| 97 |
+
res = cloudinary.api.resources(
|
| 98 |
+
type='upload',
|
| 99 |
+
resource_type='image',
|
| 100 |
+
prefix=f'mafqoud/images/{folder_name}',
|
| 101 |
+
max_results=500,
|
| 102 |
+
next_cursor=next_cursor
|
| 103 |
+
)
|
| 104 |
+
else:
|
| 105 |
+
res = cloudinary.api.resources(
|
| 106 |
+
type='upload',
|
| 107 |
+
resource_type='image',
|
| 108 |
+
prefix=f'mafqoud/images/{folder_name}',
|
| 109 |
+
max_results=500
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
resources.extend(res.get('resources', []))
|
| 113 |
+
next_cursor = res.get('next_cursor')
|
| 114 |
+
|
| 115 |
+
if not next_cursor:
|
| 116 |
+
break
|
| 117 |
|
|
|
|
|
|
|
| 118 |
return resources
|
| 119 |
|
| 120 |
def download_image(url, local_path):
|
|
|
|
| 135 |
public_id = img['public_id']
|
| 136 |
file_name = url.split('/')[-1] # Get the actual file name
|
| 137 |
local_path = os.path.join(local_dir, file_name)
|
| 138 |
+
print(local_path)
|
| 139 |
|
| 140 |
if not os.path.exists(local_path):
|
| 141 |
download_image(url, local_path)
|
|
|
|
| 144 |
# Remove old images
|
| 145 |
local_images = [os.path.join(local_dir, f) for f in os.listdir(local_dir) if os.path.isfile(os.path.join(local_dir, f))]
|
| 146 |
for local_path in local_images:
|
| 147 |
+
if local_path not in downloaded_paths and (local_path.endswith('.jpg') or local_path.endswith('.jpeg') or local_path.endswith('.png')):
|
| 148 |
os.remove(local_path)
|
| 149 |
|
| 150 |
def delete_pkl_files(directory):
|
|
|
|
| 152 |
pkl_files = glob.glob(os.path.join(directory, '*.pkl'))
|
| 153 |
for pkl_file in pkl_files:
|
| 154 |
os.remove(pkl_file)
|
|
|
|
|
|
|
|
|