Created
October 30, 2023 13:40
-
-
Save jrialland/3b37a374ac0d8a7e8da68b7d9b50721b to your computer and use it in GitHub Desktop.
python opencv example
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
from cvzone.FaceMeshModule import FaceMeshDetector | |
import cv2 | |
cap = cv2.VideoCapture(0) | |
detector = FaceMeshDetector(maxFaces=2) | |
while True: | |
success, img = cap.read() | |
img, faces = detector.findFaceMesh(img) | |
if faces: | |
print(faces[0]) | |
cv2.imshow("Image", img) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
cap.release() | |
cv2.destroyAllWindows() |
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
import numpy as np | |
import cv2 | |
import logging | |
from cvzone import FPS | |
from cvzone.SelfiSegmentationModule import SelfiSegmentation | |
from scipy.ndimage import gaussian_filter # for blurring the background image | |
import pyvirtualcam # exports as a live video stream | |
# will be used to add fps counter in the generated video | |
fpsReader = FPS() | |
# NOTE : pyvirtualcam uses v4l2loopback virtual cameras on Linux. | |
# To create a v4l2loopback virtual camera on Ubuntu, run the following: | |
# sudo apt install v4l2loopback-dkms | |
# sudo modprobe v4l2loopback devices=1 | |
# NOTE : On my particular setup I had to do the following : | |
# sudo modprobe -r v4l2loopback | |
# sudo modprobe v4l2loopback video_nr=5 | |
virtualdevice = "/dev/video5" | |
windowname=virtualdevice # the name of the window | |
segmentor = SelfiSegmentation() | |
# initialize video from the webcam | |
video = cv2.VideoCapture(0) # under linux it opens /dev/video0 | |
ret, frame = video.read() | |
W,H = frame.shape[1], frame.shape[0] | |
logging.debug(f'size={W}x{H}') | |
with pyvirtualcam.Camera( | |
width=W, height=H, fps=30, device=virtualdevice | |
) as vcam: | |
while True: | |
# load next frame | |
ret, frame = video.read() | |
if not ret: | |
raise Exception("video.read() failed") | |
# Compute a blurred version of the video frame | |
blurred = gaussian_filter(frame, sigma=12) | |
# Detect persons & replace the background by the blurred image | |
result = segmentor.removeBG(frame, blurred, threshold=0.8) | |
# Add FPS count | |
fps, result = fpsReader.update( | |
result, pos=(5, 15), color=(0, 255, 0), scale=1, thickness=1 | |
) | |
# Send the frame to the virtual camera | |
vcam.send(result) | |
vcam.sleep_until_next_frame() | |
cv2.imshow(windowname, result) | |
# 'q' exits the loop | |
if cv2.waitKey(1) & 0xFF == ord("q"): | |
break | |
# closing the window exits the loop | |
if cv2.getWindowProperty(windowname, cv2.WND_PROP_VISIBLE) == 0: | |
break | |
# cleanup | |
cv2.destroyAllWindows() | |
video.release() |
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
numpy==1.24.1 | |
opencv-python==4.7.0.68 | |
pyvirtualcam==0.10.2 | |
mediapipe==0.9.0.1 | |
cvzone==1.5.6 | |
scipy==1.10.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment