OpenCV face detect examples

This commit is contained in:
Alexander Popov 2023-09-03 12:44:02 +03:00
parent 478b5bb6e6
commit aafe177204
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
7 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,2 @@
*.png
haarcascade_frontalface_default.xml

View File

@ -0,0 +1,21 @@
#!/usr/bin/env python3
import cv2
cam_port = 2
cam = cv2.VideoCapture(cam_port)
while True:
result, image = cam.read()
cv2.imshow('Camera', image)
if cv2.waitKey(1) & 0xFF == ord('s'):
cv2.imwrite('capture.png', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cam.release()
cv2.destroyWindow('Camera')
cv2.destroyAllWindows()

View File

@ -0,0 +1,29 @@
#!/usr/bin/env python3
import cv2
camera = cv2.VideoCapture(2)
while True:
if cv2.waitKey(1) & 0xFF == ord('q'):
break
result, image = camera.read()
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
face_classifier = cv2.CascadeClassifier(
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
)
face = face_classifier.detectMultiScale(
gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(40, 40)
)
for x, y, w, h in face:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('Face Realtime Detector', image)
camera.release()
cv2.destroyAllWindows()

View File

@ -0,0 +1,24 @@
#!/usr/bin/env python3
import cv2
img = cv2.imread('face.png')
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_classifier = cv2.CascadeClassifier(
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
)
face = face_classifier.detectMultiScale(
gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(40, 40)
)
for x, y, w, h in face:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow('Face Detector', img_rgb)
cv2.waitKey(0)
cv2.destroyAllWindows()

View File

@ -0,0 +1,7 @@
import cv2
img = cv2.imread('image.png')
cv2.imshow('Images from Files', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

View File

@ -0,0 +1,4 @@
[tool.black]
skip-string-normalization = true
pycodestyle = true
line-length = 100

View File

@ -0,0 +1,2 @@
numpy==1.25.2
opencv-python==4.8.0.76