-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
78 lines (71 loc) · 2.5 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import cv2
import logging
import datetime
from typing_extensions import Literal
logLevelType = Literal["info", "warn", "error"]
def logger(message: str, logLevel: logLevelType = "info", noConsolePrint: bool = False):
"""
Generates logs for the system in both command line and logger file
Parameters
----------
message: str
A message to be shown in both logger file and command line
example: "My Sample Message"
logLevel: Literal, optional (default to "info)
The level of logs. Possible values are "info", "warn", and "error"
example: "warn"
noConsolePrint: bool, optional (default to False)
If True, the log will only be printed in the logger file
example: True
"""
# Create a console log by default
if (not noConsolePrint):
print(message)
# Create a log in the log file
currentMoment = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
printMessage = f'[{currentMoment}] {message}'
if (logLevel is 'warn'):
logging.warn(printMessage)
elif (logLevel is 'error'):
logging.error(printMessage)
else:
logging.info(printMessage)
def frameResizer(image, size):
"""
Resizes the input frame into a desired width, while keeping the aspect ratio
Parameters
----------
message: CV image file
The input image file
example: "My Sample Message"
logLevel: Literal, optional (default to "info)
The level of logs. Possible values are "info", "warn", and "error"
example: "warn"
noConsolePrint: bool, optional (default to False)
If True, the log will only be printed in the logger file
example: True
"""
# Calculating the dimensions
imageHeight, imageWidth = image.shape[:2]
aspectRatio = imageWidth / imageHeight
# Resizing frame's width, while keeping its aspect ratio
generatedImageW = size
generatedImageH = int(generatedImageW / aspectRatio)
# Scale the frame
generatedImage = cv2.resize(
image, (generatedImageW, generatedImageH), interpolation=cv2.INTER_AREA)
return generatedImage
def emptyFolderRemover(mainDir: str):
"""
Removes empty folders (like when no frames extracted from videos)
Parameters
----------
mainDir: string
The parent directory containing empty and filled folders
example: "C:/Some/Path"
"""
folders = list(os.walk(mainDir))[1:]
for folder in folders:
if not folder[2]:
os.rmdir(folder[0])