Skip to content

Commit 8e2de64

Browse files
authored
Merge pull request #21 from superannotateai/folder-limitations
Folder limitations
2 parents e464097 + df493d0 commit 8e2de64

File tree

2 files changed

+48
-49
lines changed

2 files changed

+48
-49
lines changed

superannotate/db/project_images.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
from .project_api import get_project_and_folder_metadata
1818
from .projects import (
1919
__create_image, get_image_array_to_upload,
20-
get_project_default_image_quality_in_editor, upload_image_array_to_s3
20+
get_project_default_image_quality_in_editor, upload_image_array_to_s3,
21+
_get_available_image_counts
2122
)
2223
from .utils import _get_upload_auth_token
2324

@@ -99,12 +100,9 @@ def upload_image_to_project(
99100
folder_id = get_project_root_folder_id(project)
100101

101102
team_id, project_id = project["team_id"], project["id"]
102-
params = {
103-
'team_id': team_id,
104-
'folder_id' : folder_id
105-
}
106-
res = _get_upload_auth_token(params=params,project_id=project_id)
107-
prefix = res['filePath']
103+
params = {'team_id': team_id, 'folder_id': folder_id}
104+
res = _get_upload_auth_token(params=params, project_id=project_id)
105+
prefix = res['filePath']
108106
s3_session = boto3.Session(
109107
aws_access_key_id=res['accessKeyId'],
110108
aws_secret_access_key=res['secretAccessKey'],
@@ -224,18 +222,23 @@ def copy_images(
224222
)
225223
if image_names is None:
226224
image_names = search_images((source_project, source_project_folder))
225+
226+
limit = _get_available_image_counts(
227+
destination_project, destination_project_folder
228+
)
229+
image_names = image_names[:limit]
227230
res = _copy_images(
228231
(source_project, source_project_folder),
229232
(destination_project, destination_project_folder), image_names,
230233
include_annotations, copy_annotation_status, copy_pin
231234
)
232235
logger.info(
233236
"Copied images %s from %s to %s. Number of skipped images %s",
234-
image_names,
235-
source_project["name"] + "" if source_project_folder is None else "/" +
236-
source_project_folder["name"], destination_project["name"] +
237-
"" if destination_project_folder is None else destination_project["name"] + "/" +
238-
destination_project_folder["name"], len(res["skipped"])
237+
image_names, source_project["name"] +
238+
"" if source_project_folder is None else source_project["name"] + "/" +
239+
source_project_folder["name"], destination_project["name"] + ""
240+
if destination_project_folder is None else destination_project["name"] +
241+
"/" + destination_project_folder["name"], len(res["skipped"])
239242
)
240243
return res["skipped"]
241244

superannotate/db/projects.py

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,14 @@ def _get_video_frames_count(video_path):
210210
Get video frames count
211211
"""
212212
video = cv2.VideoCapture(str(video_path), cv2.CAP_FFMPEG)
213-
total_num_of_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
214-
if total_num_of_frames < 0:
215-
total_num_of_frames = 0
216-
flag = True
217-
while flag:
218-
flag, _ = video.read()
219-
if flag:
220-
total_num_of_frames += 1
221-
else:
222-
break
213+
total_num_of_frames = 0
214+
flag = True
215+
while flag:
216+
flag, _ = video.read()
217+
if flag:
218+
total_num_of_frames += 1
219+
else:
220+
break
223221
return total_num_of_frames
224222

225223

@@ -281,16 +279,25 @@ def _get_video_rotate_code(video_path):
281279

282280

283281
def _extract_frames_from_video(
284-
start_time, end_time, ratio, video, video_path, tempdir, limit, rotate_code,
285-
total_num_of_frames
282+
start_time, end_time, video_path, tempdir, limit, target_fps
286283
):
284+
video = cv2.VideoCapture(str(video_path), cv2.CAP_FFMPEG)
285+
if not video.isOpened():
286+
raise SABaseException(0, "Couldn't open video file " + str(video_path))
287+
total_num_of_frames = _get_video_frames_count(video_path)
288+
logger.info("Video frame count is %s.", total_num_of_frames)
289+
ratio = 1.0
290+
if target_fps:
291+
ratio = _get_video_fps_ration(target_fps, video, ratio)
292+
rotate_code = _get_video_rotate_code(video_path)
287293
video_name = Path(video_path).stem
288294
frame_no = 0
289295
frame_no_with_change = 1.0
290296
extracted_frame_no = 1
291297
logger.info("Extracting frames from video to %s.", tempdir.name)
292298
zero_fill_count = len(str(total_num_of_frames))
293-
while extracted_frame_no < (limit + 1):
299+
extracted_frames_paths = []
300+
while len(extracted_frames_paths) < limit:
294301
success, frame = video.read()
295302
if not success:
296303
break
@@ -305,16 +312,16 @@ def _extract_frames_from_video(
305312
continue
306313
if rotate_code:
307314
frame = cv2.rotate(frame, rotate_code)
308-
cv2.imwrite(
309-
str(
310-
Path(tempdir.name) / (
311-
video_name + "_" +
312-
str(extracted_frame_no).zfill(zero_fill_count) + ".jpg"
313-
)
314-
), frame
315+
path = str(
316+
Path(tempdir.name) / (
317+
video_name + "_" +
318+
str(extracted_frame_no).zfill(zero_fill_count) + ".jpg"
319+
)
315320
)
321+
cv2.imwrite(path, frame)
322+
extracted_frames_paths.append(path)
316323
extracted_frame_no += 1
317-
return extracted_frame_no - 1
324+
return extracted_frames_paths
318325

319326

320327
def upload_video_to_project(
@@ -361,24 +368,13 @@ def upload_video_to_project(
361368
"The function does not support projects containing images attached with URLs"
362369
)
363370
logger.info("Uploading from video %s.", str(video_path))
364-
rotate_code = _get_video_rotate_code(video_path)
365-
video = cv2.VideoCapture(str(video_path), cv2.CAP_FFMPEG)
366-
if not video.isOpened():
367-
raise SABaseException(0, "Couldn't open video file " + str(video_path))
368-
369-
total_num_of_frames = _get_video_frames_count(video_path)
370-
logger.info("Video frame count is %s.", total_num_of_frames)
371-
ratio = 1.0
372-
if target_fps:
373-
ratio = _get_video_fps_ration(target_fps, video, ratio)
374371
tempdir = tempfile.TemporaryDirectory()
375-
extracted_frame_no = _extract_frames_from_video(
376-
start_time, end_time, ratio, video, video_path, tempdir, limit,
377-
rotate_code, total_num_of_frames
372+
extracted_frames = _extract_frames_from_video(
373+
start_time, end_time, video_path, tempdir, limit, target_fps
378374
)
379375
logger.info(
380376
"Extracted %s frames from video. Now uploading to platform.",
381-
extracted_frame_no
377+
len(extracted_frames)
382378
)
383379
filenames = upload_images_from_folder_to_project(
384380
(project, folder),
@@ -917,7 +913,7 @@ def upload_images_to_project(
917913
prefix = res['filePath']
918914
limit = res['availableImageCount']
919915
images_to_upload = img_paths[:limit]
920-
images_to_skip = img_paths[limit:]
916+
images_to_skip = [str(path) for path in img_paths[limit:]]
921917
chunksize = int(math.ceil(len(images_to_upload) / _NUM_THREADS))
922918

923919
tqdm_thread = threading.Thread(
@@ -2353,4 +2349,4 @@ def clone_project(
23532349
if project_description is not None:
23542350
metadata["description"] = project_description
23552351

2356-
return create_project_from_metadata(metadata)
2352+
return create_project_from_metadata(metadata)

0 commit comments

Comments
 (0)