-The other day, the video recognition did not go well using the fitted OpenCV, but specifically, an error occurred in cv2.imshow It was occurring and remained unsolvable. ――Suddenly, when I tried to recognize if the video was useless but the image could be recognized, the same error occurred, so I found that not only the video but also the image was in a bad state. ――By the way, previously was working with images! When I tried it, it seemed that it did not work on OpenCV3 and worked normally on OpenCV2. --If you use OpenCV2 via Homebrew or pip under Mac environment, it seems that you can only install it with Python2, so I decided to do it there this time. ――I thought it would be interesting as a story, and I thought that my child might be happy with this, so I made it with a simple idea, but it became quite complicated.
Create miscellaneous Photoshop video with Python + OpenCV ② Create still image Photoshop
Python2.7
brew install python
if [ -d $(brew --prefix)/lib/python2.7/site-packages ];then
export PYTHONPATH=$(brew --prefix)/lib/python2.7/site-packages:$PYTHONPAT
fi
OpenCV2
brew install opencv
Numpy
pip install numpy
I'm using the camera that came with my MacBook.
Have them made at the bread factory.
I haven't done that much, so I haven't added much comment.
webcam.py
#! -*- coding: utf-8 -*-
import cv2
import numpy as np
from PIL import Image
def facedetect(face_cascade, cap, image):
cnt = 0
while(True):
ret, frame = cap.read()
if ret == False:
break
else:
if (cnt % 10) == 0:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
facerect = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=1, minSize=(1, 1))
cnt += 1
if len(facerect) > 0:
for rect in facerect:
x = rect[0]
y = rect[1]
w = rect[2]
h = rect[3]
#Adjustment may be necessary depending on the shape of the face
x = x - w / 2
y = y - h / 1.8
w = w * 1.7
h = h * 1.7
x = int(round(x))
y = int(round(y))
w = int(round(w))
h = int(round(h))
#Resize the image to be composited to fit the rectangle.
image = cv2.resize(image, (w, h))
#Combines the image of the camera face.
frame = overlay(frame, image, x, y)
#Use this if you want to perform mosaic processing.
# dst = frame[y:y+h, x:x+w]
# blur = cv2.blur(dst, (50, 50))
# frame[y:y+h, x:x+w] = blur
cv2.imshow('fram', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
def overlay(frame, image, x, y):
height, width = image.shape[:2]
layer1 = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
layer2 = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA)
layer1 = Image.fromarray(layer1)
layer2 = Image.fromarray(layer2)
layer1 = layer1.convert('RGBA')
layer2 = layer2.convert('RGBA')
tmp = Image.new('RGBA', layer1.size, (255, 255, 255, 0))
tmp.paste(layer2, (x, y), layer2)
result = Image.alpha_composite(layer1, tmp)
return cv2.cvtColor(np.asarray(result), cv2.COLOR_RGBA2BGRA)
if __name__ == '__main__':
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
image = cv2.imread('anpan.png', cv2.IMREAD_UNCHANGED)
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
facedetect(face_cascade, cap, image)
――It was helpful to have unexpected information. --Slight adjustment was needed to match the frame with the degree of recognition. ――It was necessary to make adjustments to draw a rectangle on the entire head and above the shoulders, which only recognizes the face by default. ――I feel that if you recognize the torso and cover it, it will look a little more like that. ――This time you can challenge SSD. (Should) ――I would like to try it if I can make it a smartphone app.
-I tried to become an Ann Man using OpenCV -I made a multiplayer video chat using SkyWay -I tried face recognition for video chat using SkyWay
Recommended Posts