OpenCV face detect examples
This commit is contained in:
parent
478b5bb6e6
commit
aafe177204
2
snipplets/code/Python/opencv/.gitignore
vendored
Normal file
2
snipplets/code/Python/opencv/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*.png
|
||||
haarcascade_frontalface_default.xml
|
21
snipplets/code/Python/opencv/capture-image.py
Executable file
21
snipplets/code/Python/opencv/capture-image.py
Executable 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()
|
29
snipplets/code/Python/opencv/face-detect-realtime.py
Executable file
29
snipplets/code/Python/opencv/face-detect-realtime.py
Executable 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()
|
24
snipplets/code/Python/opencv/face-detect.py
Executable file
24
snipplets/code/Python/opencv/face-detect.py
Executable 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()
|
7
snipplets/code/Python/opencv/open-image.py
Normal file
7
snipplets/code/Python/opencv/open-image.py
Normal file
@ -0,0 +1,7 @@
|
||||
import cv2
|
||||
|
||||
img = cv2.imread('image.png')
|
||||
|
||||
cv2.imshow('Images from Files', img)
|
||||
cv2.waitKey(0)
|
||||
cv2.destroyAllWindows()
|
4
snipplets/code/Python/opencv/pyproject.toml
Normal file
4
snipplets/code/Python/opencv/pyproject.toml
Normal file
@ -0,0 +1,4 @@
|
||||
[tool.black]
|
||||
skip-string-normalization = true
|
||||
pycodestyle = true
|
||||
line-length = 100
|
2
snipplets/code/Python/opencv/requirements.txt
Normal file
2
snipplets/code/Python/opencv/requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
numpy==1.25.2
|
||||
opencv-python==4.8.0.76
|
Loading…
Reference in New Issue
Block a user