Greatly cleaned up the python script and added a dockerfile

This commit is contained in:
Gregory Campbell
2026-06-19 11:17:14 -04:00
parent 0c09377e1b
commit a8a50fc89b
2 changed files with 130 additions and 96 deletions
+6
View File
@@ -0,0 +1,6 @@
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
ENTRYPOINT ["python", "SkinDetection.py"]
+124 -96
View File
@@ -1,114 +1,142 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#SkinDetection.py """SkinDetection.py
# This program runs all 4 skin detection algorithms one after the other Runs several skin-detection algorithms on a single image and saves results.
# Run `python3 SkinDetection.py Path/To/Image` to specify """
# inital image, removing this command arguements will result in
# the script attempting hard coded locations so errors may arise
# The
from PIL import Image from __future__ import annotations
import math
import argparse
import colorsys import colorsys
import sys import sys
from pathlib import Path
from typing import Callable
#Explicitly Defined Skin Region Model from PIL import Image
def EDSRModel(image_path):
im = Image.open(image_path)
pixels = im.load()
rgb_im = im.convert('RGB')
for i in range(im.size[0]):
for j in range(im.size[1]):
r,g,b = rgb_im.getpixel((i,j))
if r > 95 and g > 40 and b > 20 and (max(r, g, b) - min(r, g, b)) > 15 and abs(r - g) > 15 and r > g and r > b:
x = j
y = i
else:
pixels[i,j] = (0,0,0)
im.save("Results/Explicitly-Defined-Skin-Region.png")
#Colour Segmentation in Normalization rg Color Model def load_image(path: Path) -> Image.Image:
def NormalizationrgModel(image_path): image = Image.open(path)
im = Image.open(image_path) return image.convert("RGB")
pixels = im.load()
rgb_im = im.convert('RGB')
for i in range(im.size[0]):
for j in range(im.size[1]):
r,g,b = rgb_im.getpixel((i,j))
R = 0.0
G = 0.0
B = 0.0
if r > 0 or g > 0 or b > 0:
R = r/float(r+g+b)
G = g/float(r+g+b)
B = b/float(r+g+b)
if 0.465 >= R >= 0.36 and 0.363 >= G >= 0.28:
x = j
y = i
else:
pixels[i,j] = (0,0,0)
im.save("Results/Normalizationrg.png")
#Color Segmentation in HSV Color Model def save_result(image: Image.Image, path: Path) -> None:
def HSVModel(image_path): path.parent.mkdir(parents=True, exist_ok=True)
im = Image.open(image_path) image.save(path)
pixels = im.load()
rgb_im = im.convert('RGB')
for i in range(im.size[0]):
for j in range(im.size[1]):
r,g,b = rgb_im.getpixel((i,j))
r = r/255.0
g = g/255.0
b = b/255.0
h,s,v = colorsys.rgb_to_hsv(r,g,b)
h = h*255
if 50 >= h >= 0 and 0.68 >= s >= 0.2 and 1 >= v >= 0.35:
x = j
y = i
else:
pixels[i,j] = (0,0,0)
im.save("Results/HSV.png")
#Color Segmentation in YCBCR Color Model def mask_image(
def YCBCRModel(image_path): image: Image.Image,
im = Image.open(image_path) predicate: Callable[[int, int, int], bool],
pixels = im.load() invert: bool = False,
ycbcr_im = im.convert('YCbCr') ) -> Image.Image:
for i in range(im.size[0]): result = image.copy()
for j in range(im.size[1]): pixels = result.load()
y,Cb,Cr = ycbcr_im.getpixel((i,j))
if 142.5 >= Cb >= 97.5 and 134 >= Cr >= 17:
pixels[i,j] = (0,0,0)
else:
x = i
y = j
im.save("Results/YCBCR.png") for x in range(result.width):
for y in range(result.height):
r, g, b = image.getpixel((x, y))
if predicate(r, g, b) ^ invert:
continue
pixels[x, y] = (0, 0, 0)
def main(): return result
args = sys.argv[1:]
if len(sys.argv) == 1:
image_path = "Images/skin.jpg"
else:
image_path = args[0]
EDSRModel(image_path) def explicitly_defined_skin_region(r: int, g: int, b: int) -> bool:
NormalizationrgModel(image_path) return (
HSVModel(image_path) r > 95
YCBCRModel(image_path) and g > 40
return and b > 20
and (max(r, g, b) - min(r, g, b)) > 15
and abs(r - g) > 15
and r > g
and r > b
)
def normalized_rg_skin_region(r: int, g: int, b: int) -> bool:
total = r + g + b
if total == 0:
return False
R = r / float(total)
G = g / float(total)
return 0.36 <= R <= 0.465 and 0.28 <= G <= 0.363
def hsv_skin_region(r: int, g: int, b: int) -> bool:
r_norm = r / 255.0
g_norm = g / 255.0
b_norm = b / 255.0
h, s, v = colorsys.rgb_to_hsv(r_norm, g_norm, b_norm)
h *= 255
return 0 <= h <= 50 and 0.2 <= s <= 0.68 and 0.35 <= v <= 1
def ycbcr_skin_region(r: int, g: int, b: int) -> bool:
y = 0.299 * r + 0.587 * g + 0.114 * b
cb = 128 - 0.168736 * r - 0.331264 * g + 0.5 * b
cr = 128 + 0.5 * r - 0.418688 * g - 0.081312 * b
return 97.5 <= cb <= 142.5 and 17 <= cr <= 134
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Run multiple skin detection algorithms on an input image."
)
parser.add_argument(
"image_path",
nargs="?",
default="Images/skin.jpg",
help="Path to the input image.",
)
parser.add_argument(
"--output-dir",
default="Results",
help="Directory to write output images.",
)
parser.add_argument(
"--show",
action="store_true",
help="Open each generated result image after processing.",
)
return parser.parse_args()
def main() -> int:
args = parse_args()
image_path = Path(args.image_path)
output_dir = Path(args.output_dir)
show_results = args.show
if not image_path.exists():
print(f"Error: image path not found: {image_path}", file=sys.stderr)
return 1
try:
image = load_image(image_path)
except OSError as error:
print(f"Error opening image: {error}", file=sys.stderr)
return 1
algorithms = [
("Explicitly-Defined-Skin-Region.png", explicitly_defined_skin_region, False),
("Normalizationrg.png", normalized_rg_skin_region, False),
("HSV.png", hsv_skin_region, False),
("YCBCR.png", ycbcr_skin_region, True),
]
for filename, predicate, invert in algorithms:
output_path = output_dir / filename
result = mask_image(image, predicate, invert=invert)
save_result(result, output_path)
if show_results:
result.show(title=filename)
print(f"Saved {len(algorithms)} results to {output_dir}")
return 0
if __name__ == "__main__": if __name__ == "__main__":
main() raise SystemExit(main())