Skip to content

Commit

Permalink
Fix dimensionality bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenglongMa committed Jun 7, 2023
1 parent fa08618 commit 28b2536
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ optional arguments:
-o DIRECTORY, --output DIRECTORY
The path of output file, defaults to the directory of this script.
--n_colors N CONFIG: the number of dominant colors to be extracted, defaults to 2.
--new_width WIDTH CONFIG: resize the images with the specified width, defaults to 200.
--new_width WIDTH CONFIG: resize the images with the specified width. Negative value will be ignored, defaults to 250.
--scale SCALE CONFIG: how much the image size is reduced at each image scale, defaults to 1.1
--min_nbrs NEIGHBORS CONFIG: how many neighbors each candidate rectangle should have to retain it.
Higher value results in less detections but with higher quality.
Higher value results in less detections but with higher quality, defaults to 5.
--min_size WIDTH [HEIGHT ...]
CONFIG: minimum possible face size. Faces smaller than that are ignored, defaults to "30 30".
```
Expand Down Expand Up @@ -242,7 +242,7 @@ If more than one faces are detected, there will be multiple rows for that image.
stone -d (or --debug)
```

This option will store the processed image (like the Lenna example above) in `./path/to/output/debug` folder.
This option will store the processed image (like the Lenna example above) in `./path/to/output/debug/faces_<n>` folder, where `<n>` is the number of faces detected in the image.

By default, to save space, the app does not store processed images.

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup
from setuptools import find_packages

VERSION = '0.1.11'
VERSION = '0.1.12'

with open('README.md') as f:
LONG_DESCRIPTION = f.read()
Expand Down
78 changes: 42 additions & 36 deletions src/stone/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def classify(image, n_dominant_colors, categories, cate_labels, report_image, de
color_w = 100
total_height = 0
for index, color in enumerate(colors):
color_h = round(report_image.shape[0] * props[index])
color_h = int(np.floor(report_image.shape[0] * props[index]))
total_height += color_h
bar = create_bar(color_h, color_w, color)
color_bars.append(bar)
Expand Down Expand Up @@ -222,7 +222,7 @@ def main():
parser.add_argument('--n_colors', type=int, metavar='N',
help='CONFIG: the number of dominant colors to be extracted, defaults to 2.', default=2)
parser.add_argument('--new_width', type=int, metavar='WIDTH',
help='CONFIG: resize the images with the specified width. Negative value will be ignored, defaults to -1.', default=-1)
help='CONFIG: resize the images with the specified width. Negative value will be ignored, defaults to 250.', default=250)

# Refer to https://stackoverflow.com/a/20805153/8860079
parser.add_argument('--scale', type=float, help='CONFIG: how much the image size is reduced at each image scale, defaults to 1.1', default=1.1)
Expand Down Expand Up @@ -274,42 +274,48 @@ def main():
basename, extension = filename.stem, filename.suffix

LOG.info(f'\n----- Processing {basename} -----')
ori_image = cv2.imread(str(filename.resolve()), cv2.IMREAD_UNCHANGED)
if ori_image is None:
LOG.warning(f'{filename}.{extension} is not found or is not a valid image.')
try:
ori_image = cv2.imread(str(filename.resolve()), cv2.IMREAD_UNCHANGED)
if ori_image is None:
LOG.warning(f'{filename}.{extension} is not found or is not a valid image.')
continue

resized_image = imutils.resize(ori_image, width=args.new_width) if args.new_width > 0 else ori_image
final_image = resized_image.copy()
faces = detect_faces(resized_image, args.scale, args.min_nbrs, min_size)

debug_imgs = []
if len(faces) > 0:
LOG.info(f'Found {len(faces)} face(s)')
for idx, (x1, y1, x2, y2) in enumerate(faces):
sub_filename = f'{basename}-{idx + 1}'
LOG.info(f'Face {idx + 1} location: {x1}:{x2}')
face = resized_image[y1:y2, x1:x2]
if debug:
final_image = draw_rects(resized_image, [x1, y1, x2, y2])
res, _debug_img = classify(face, n_dominant_colors, categories, cate_labels, final_image, debug)
writerow(f, [sub_filename, f'{x1}:{x2}'] + res)
debug_imgs.append(_debug_img)
else:
LOG.info(f'Found 0 face, will detect global skin area instead')
res, _debug_img = classify(resized_image, n_dominant_colors, categories, cate_labels, final_image, debug)
writerow(f, [basename, 'NA'] + res)
debug_imgs.append(_debug_img)

if debug:
debug_dir = os.path.join(output_dir, f'./debug/faces_{len(faces)}')
os.makedirs(debug_dir, exist_ok=True)
for idx, img in enumerate(debug_imgs):
sub_filename = f'{basename}-{idx + 1}'
debug_filename = os.path.join(debug_dir, f'{sub_filename}{extension}')
cv2.imwrite(debug_filename, img)
if is_single_file:
cv2.imshow(f'Skin Tone Classifier - {sub_filename}', img)
except Exception as e:
LOG.error(f'Error occurred while processing {filename}: {e}')
writerow(f, [basename, f'Error: {e}'])
continue

resized_image = imutils.resize(ori_image, width=args.new_width) if args.new_width > 0 else ori_image
final_image = resized_image.copy()
faces = detect_faces(resized_image, args.scale, args.min_nbrs, min_size)

debug_imgs = []
if len(faces) > 0:
LOG.info(f'Found {len(faces)} face(s)')
for idx, (x1, y1, x2, y2) in enumerate(faces):
sub_filename = f'{basename}-{idx + 1}'
LOG.info(f'Face {idx + 1} location: {x1}:{x2}')
face = resized_image[y1:y2, x1:x2]
if debug:
final_image = draw_rects(resized_image, [x1, y1, x2, y2])
res, _debug_img = classify(face, n_dominant_colors, categories, cate_labels, final_image, debug)
writerow(f, [sub_filename, f'{x1}:{x2}'] + res)
debug_imgs.append(_debug_img)
else:
LOG.info(f'Found 0 face, will detect global skin area instead')
res, _debug_img = classify(resized_image, n_dominant_colors, categories, cate_labels, final_image, debug)
writerow(f, [basename, 'NA'] + res)
debug_imgs.append(_debug_img)

if debug:
debug_dir = os.path.join(output_dir, './debug')
os.makedirs(debug_dir, exist_ok=True)
for idx, img in enumerate(debug_imgs):
sub_filename = f'{basename}-{idx + 1}'
debug_filename = os.path.join(debug_dir, f'{sub_filename}{extension}')
cv2.imwrite(debug_filename, img)
if is_single_file:
cv2.imshow(f'Skin Tone Classifier - {sub_filename}', img)
f.close()

cv2.waitKey(0)
Expand Down

0 comments on commit 28b2536

Please sign in to comment.