working example

This commit is contained in:
Nickiel12 2023-09-22 19:58:34 -07:00
parent b265d2abd5
commit b1eba5ea2d
3 changed files with 1531 additions and 47 deletions

View file

@ -1,31 +1,24 @@
import cv2
import numpy as np
def check_within(imgSize, baseMultiplier, currentX, currentY, currentW, currentH):
baseYbottom = imgSize[0] * baseMultiplier
baseXRight = imgSize[1] * baseMultiplier
upperMultiplier = 1 - baseMultiplier
baseYTop = imgSize[0] * upperMultiplier
baseXLeft = imgSize[1] * upperMultiplier
currentBottomY = currentY + currentH
currentRightX = currentX + currentW
multiplication_factor = 0.05
print(str(baseYbottom) + " " + str(currentY))
def get_adjustment_amount(imgSize, currentX, currentY, currentW, currentH):
if baseYbottom > currentY:
return (255, 255, 255)
elif baseYTop < currentBottomY:
return (0, 0, 0)
elif baseXRight > currentX:
return (255, 0, 0)
elif baseXLeft < currentRightX:
return (0, 0, 255)
else:
return(0, 255, 0)
current_top_left = [currentX, currentY]
current_bottom_right = [currentX + currentW, currentY + currentH]
current_top_right = [currentX + currentW, currentY]
# find the difference between the left gap and the right gap, divide it by two, and multiply it by the speed scale
horizontal_adjustment = multiplication_factor * (currentX - (imgSize[0] - current_top_right[0])) / 2
vertical_adjustment = multiplication_factor * (currentY - (imgSize[0] - current_bottom_right[1])) / 2
return [horizontal_adjustment, vertical_adjustment]
cap = cv2.VideoCapture(0, cv2.IMREAD_GRAYSCALE) # instead of grayscale you can also use -1, 0, or 1.
faceCascade = cv2.CascadeClassifier(r"C:\Users\Owner\Desktop\Face Detection\haar_frontface.xml") # CHECK THIS FIRST TROUBLE SHOOTING
faceCascade = cv2.CascadeClassifier(r"./lbpcascade_frontalface.xml") # CHECK THIS FIRST TROUBLE SHOOTING
tmp, frm = cap.read()
height, width, channels = frm.shape
@ -45,34 +38,16 @@ while(True):
minSize=(30, 30)
)
print("Found {0} faces!".format(len(faces)))
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
color = check_within([height, width], .1, x, y, w, h)
cv2.rectangle(frame, (int(width*.1), int(height*.1)), (int(width*.9), int(height*.9)), (150, 0 , 150))
cv2.rectangle(frame, (x, y), (x+w, y+h), color)
adjustment_required = get_adjustment_amount([width, height], x, y, w, h)
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 255, 255))
print(f"Adjust right: {adjustment_required[0]}")
print(f"Adjust up : {adjustment_required[1]}")
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cap.destroyAllWindows()
# you can save the image with
# cv2.imwrite('watchgray.png',img)
'''
instead of the above code you can replace everything below
img = cv2....
with
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.plot([200,300,400],[100,200,300],'c', linewidth=5)
plt.show()
for a matplotlib chart
'''

1505
lbpcascade_frontalface.xml Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,13 @@
{ pkgs ? import <nixpkgs> {} }:
let
my-python-packages = ps: with ps; [
opencv4
numpy
# other python packages
];
my-python = pkgs.python3.withPackages my-python-packages;
in my-python.env
in
pkgs.mkShell {
buildInputs = with pkgs.python311Packages; [
numpy
(opencv4.override { enableGtk2 = true; })
];
}