-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Based on CVZone, ripped out what we needed Reduces dependencies.
- Loading branch information
Showing
5 changed files
with
179 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
Selfisegmentation from CVZone | ||
By: Computer Vision Zone | ||
Website: https://www.computervision.zone/ | ||
With slight modifications for our project | ||
""" | ||
import cv2 # type: ignore | ||
import mediapipe as mp # type: ignore | ||
import numpy as np | ||
|
||
|
||
class SelfieSegmentation: | ||
def __init__(self, model=1): | ||
""" | ||
:param model: model type 0 or 1. 0 is general 1 is landscape(faster) | ||
""" | ||
self.model = model | ||
self.mpDraw = mp.solutions.drawing_utils | ||
self.mpSelfieSegmentation = mp.solutions.selfie_segmentation | ||
self.selfieSegmentation = self.mpSelfieSegmentation.SelfieSegmentation( | ||
self.model | ||
) | ||
|
||
def removeBG(self, img, imgBg=(255, 255, 255), threshold=0.1): | ||
""" | ||
:param img: image to remove background from | ||
:param imgBg: BackGround Image | ||
:param threshold: higher = more cut, lower = less cut | ||
:return: | ||
""" | ||
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | ||
results = self.selfieSegmentation.process(imgRGB) | ||
condition = np.stack((results.segmentation_mask,) * 3, axis=-1) > threshold | ||
if isinstance(imgBg, tuple): | ||
_imgBg = np.zeros(img.shape, dtype=np.uint8) | ||
_imgBg[:] = imgBg | ||
imgOut = np.where(condition, img, _imgBg) | ||
else: | ||
imgOut = np.where(condition, img, imgBg) | ||
return imgOut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
FPS Module | ||
By: Computer Vision Zone | ||
Website: https://www.computervision.zone/ | ||
Slightly modified for our project | ||
""" | ||
|
||
import time | ||
|
||
import cv2 # type: ignore | ||
|
||
|
||
class FPS: | ||
""" | ||
Helps in finding Frames Per Second and display on an OpenCV Image | ||
""" | ||
|
||
def __init__(self): | ||
self.pTime = time.time() | ||
|
||
def update(self, img=None, pos=(20, 50), color=(255, 0, 0), scale=3, thickness=3): | ||
""" | ||
Update the frame rate | ||
:param img: Image to display on, can be left blank if only fps value required | ||
:param pos: Position on the FPS on the image | ||
:param color: Color of the FPS Value displayed | ||
:param scale: Scale of the FPS Value displayed | ||
:param thickness: Thickness of the FPS Value displayed | ||
:return: | ||
""" | ||
cTime = time.time() | ||
try: | ||
fps = 1 / (cTime - self.pTime) | ||
self.pTime = cTime | ||
if img is None: | ||
return fps | ||
else: | ||
cv2.putText( | ||
img, | ||
f"FPS: {int(fps)}", | ||
pos, | ||
cv2.FONT_HERSHEY_PLAIN, | ||
scale, | ||
color, | ||
thickness, | ||
) | ||
return fps, img | ||
except ZeroDivisionError: | ||
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
""" | ||
General collection of utility functions | ||
""" | ||
import cv2 # type: ignore | ||
|
||
|
||
def whiteness_offset(img) -> float: | ||
"""Uses the amount of white vs other noise in the image to decide a threshold for background removal | ||
Args: | ||
img (np.nparray): The image to calculate the threshold for | ||
Returns: | ||
float: The threshold to use for background removal | ||
""" | ||
# Convert the image to grayscale | ||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | ||
# Calculate the average pixel value | ||
avg = gray.mean() | ||
# Calculate the threshold | ||
thresh = avg / 255 | ||
return thresh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters