This is kimo_0tak, which is the first post in a long time. You can easily make babiniku with facerig etc., but I couldn't find a way to replace only my face with another image, so I made it quickly. There is no guarantee that it will replace the face 100%, so please use it as an inner ring material.
The displayed image looks like this.
The usage environment is Windows 10, Python 3.6.5. Put the image you want to replace with this program and the xml file of the face recognition model in the same file.
Here is the site that I used as a reference.
Face detection with Haar Cascades http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_objdetect/py_face_detection/py_face_detection.html Draw another image on top of the image with OpenCV https://note.com/npaka/n/nddb33be1b782 A story about capturing webcam images with Python + OpenCV, processing them, and displaying them. https://ensekitt.hatenablog.com/entry/2017/12/19/200000
Let's make it. The completed code is at the bottom, so if you want to see that much, scroll down.
#OpenCV import
import cv2
import numpy as np
from PIL import Image
If you have any libraries that you have not installed, please install them yourself. Open a command prompt
pip install opencv-python
pip install numpy
pip install Pillow
You should be able to enter it by typing.
#Camera resolution setting
WIDTH = 1920
HEIGHT = 1080
#Specifying the image to read
img = "vtuber_may.jpg "
cv2_img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
#Designation of face recognition model
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
#You can select the camera with the argument.
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
You can create the face recognition cascade classifier yourself, but it was troublesome, so I used the classifier provided by Opencv. It is included when you install opencv with pip install opencv. In my environment it was in the C: \ Users \ {username} \ AppData \ Local \ Programs \ Python \ Python36-32 \ Lib \ site-packages \ cv2 \ data folder.
def overlayImage(src, overlay, location, size):
overlay_height, overlay_width = overlay.shape[:2]
#Convert webcam images to PIL format
src = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
pil_src = Image.fromarray(src)
pil_src = pil_src.convert('RGBA')
#Convert the image you want to combine to PIL format
overlay = cv2.cvtColor(overlay, cv2.COLOR_BGRA2RGBA)
pil_overlay = Image.fromarray(overlay)
pil_overlay = pil_overlay.convert('RGBA')
#Resize to fit the size of your face
pil_overlay = pil_overlay.resize(size)
#Combine images
pil_tmp = Image.new('RGBA', pil_src.size, (255, 255, 255, 0))
pil_tmp.paste(pil_overlay, location, pil_overlay)
result_image = Image.alpha_composite(pil_src, pil_tmp)
#Convert to OpenCV format
return cv2.cvtColor(np.asarray(result_image), cv2.COLOR_RGBA2BGRA)
Since images cannot be combined with Opencv, create a function to combine images after converting to PIL format.
I also resize it according to the height and width of my face.
def main():
#A variable that stores the coordinates of the upper left corner of face recognition
x = 0
y = 0
#A variable that stores the width and height of the face
w = 0
h = 0
while True:
#Load 1 frame from VideoCapture
ret, frame = cap.read()
#Convert frame to gray format for face recognition
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(gray, 1.3, 5)
#If the face is recognized x, y, w,update h
if face != ():
(x, y, w, h) = face[0]
#If the variable is other than the initial value, the image is combined with the frame
if w != 0:
frame = overlayImage(frame, cv2_img, (x, y), (w, h))
#Display the processed frame
cv2.imshow('Frame', frame)
#Wait 1ms for key input and break when ESC is pressed
k = cv2.waitKey(1)
if k == 27:
break
#Release the capture and close the window
cap.release()
cv2.destroyAllWindows()
One frame is read from the webcam and the image is processed.
Depending on the frame, the face may not be recognized, so if it is not recognized, it is combined with the previously recognized position.
Note that if two or more faces are recognized on the screen, the image will be combined with only one of the faces.
I thought I'd improve it, but I didn't improve it because it's unlikely that two people would appear in ZOOM at the same time.
if __name__ == '__main__':
main()
#OpenCV import
import cv2
import numpy as np
from PIL import Image
#Camera resolution setting
WIDTH = 1920
HEIGHT = 1080
#Specifying the image to read
img = "vtuber_may.jpg "
cv2_img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
#Designation of face recognition model
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
#You can select the camera with the argument.
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
def overlayImage(src, overlay, location, size):
overlay_height, overlay_width = overlay.shape[:2]
#Convert webcam images to PIL format
src = cv2.cvtColor(src, cv2.COLOR_BGR2RGB)
pil_src = Image.fromarray(src)
pil_src = pil_src.convert('RGBA')
#Convert the image you want to combine to PIL format
overlay = cv2.cvtColor(overlay, cv2.COLOR_BGRA2RGBA)
pil_overlay = Image.fromarray(overlay)
pil_overlay = pil_overlay.convert('RGBA')
#Resize to fit the size of your face
pil_overlay = pil_overlay.resize(size)
#Combine images
pil_tmp = Image.new('RGBA', pil_src.size, (255, 255, 255, 0))
pil_tmp.paste(pil_overlay, location, pil_overlay)
result_image = Image.alpha_composite(pil_src, pil_tmp)
#Convert to OpenCV format
return cv2.cvtColor(np.asarray(result_image), cv2.COLOR_RGBA2BGRA)
def main():
#A variable that stores the coordinates of the upper left corner of face recognition
x = 0
y = 0
#A variable that stores the width and height of the face
w = 0
h = 0
while True:
#Load 1 frame from VideoCapture
ret, frame = cap.read()
#Convert frame to gray format for face recognition
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(gray, 1.3, 5)
#If the face is recognized x, y, w,update h
if face != ():
(x, y, w, h) = face[0]
#If the variable is other than the initial value, the image is combined with the frame
if w != 0:
frame = overlayImage(frame, cv2_img, (x, y), (w, h))
#Display the processed frame
cv2.imshow('Frame', frame)
#Wait 1ms for key input and break when ESC is pressed
k = cv2.waitKey(1)
if k == 27:
break
#Release the capture and close the window
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
All you have to do is load the webcam image displayed by this program in OBS Studio and output it as a virtual camera with virtualcam. I will not explain it here, but please see this site etc. and set it.
How to use VirtualCam to recognize OBS as a virtual camera https://loumo.jp/archives/24912
It looks like this when used with Zoom. You can hide your nerd face with a Twitter icon and have a meeting.
I posted the code little by little with the intention of explaining the code more, but most of the explanation was written in the comments, and other than the comments, I wrote only the location of the opencv cascade classifier. Also recently, I'm addicted to Hololive's vtuber Shisaki Sion-chan , so please take a look.
Recommended Posts